使用到的知识点:ArrayList、for循环、数组、Scanner输入、类的定义和封装、函数的定义和使用
有哪里可以改进的更好请大家指教,代码有点长要分两次
import java.util.Scanner;
import java.util.ArrayList;
public class ShoppingSystem
{
ArrayList<Fruit> fruitItem=new ArrayList<Fruit>();
Scanner sc=new Scanner(System.in);
ArrayList<Integer> totalPriceSystem=new ArrayList<Integer>();
public static void main(String[] args)
{
ShoppingSystem ss=new ShoppingSystem();
}
ShoppingSystem()
{
shoppingSystem();
}
public void shoppingSystem()
{
init();
System.out.println("欢迎使用《水果超市管理系统》");
doBusiness();
}
public void init()
{
Fruit liulian=new Fruit("泰国空运大榴莲","9056",120.0,"个");
fruitItem.add(liulian);
Fruit strawBerry=new Fruit("昌平农家有机草莓","3025",20.0,"斤");
fruitItem.add(strawBerry);
Fruit hamigua=new Fruit("新疆原产哈密瓜","2012",7.0,"斤");
fruitItem.add(hamigua);
}
public void doBusiness()
{
System.out.println("请输入您的身份:");
System.out.println("1、我是顾客 2、我是老板 3、退出系统");
int custom=enterNumber();
int[] number=new int[fruitItem.size()];
if(custom==1)
{
System.out.println(" 欢迎光临 ");
purchase(number);
}
else if(custom==2)
{
check();
}
else if(custom==3)
{
System.out.println(" ");
System.out.println(" ");
System.out.println("感谢您使用本系统");
System.exit(0);
}
else
{
System.out.println("身份选择错误,请重新选择身份:");
doBusiness();
}
}
public void purchase(int[] number)
{
System.out.println("请输入您的操作:");
System.out.println("1、购物(或继续购物) 2、付款(打印小票) 3、放弃购买");
int activity=enterNumber();
if(activity==1)
{
buy(number);
}
else if(activity==2)
{
printTicket(number);
}
else if(activity==3)
{
number=null;
System.out.println("");
System.out.println("欢迎使用《水果超市管理系统》");
doBusiness();
}
else
{
System.out.println("");
System.out.println(" 输入错误,请重新输入您的操作!");
System.out.println("");
System.out.println("欢迎使用《水果超市管理系统》");
doBusiness();
}
}
public void buy(int[] number)
{
showFruitList();
System.out.println(" 好消息!年底大促销:");
System.out.println("满300,打九五折!");
System.out.println("满600,打九折!!");
System.out.println("满1000,打八折!!!");
System.out.println("满1000且榴莲购买超过10个,立减50元!!!!!");
for(int i=0;i<fruitItem.size();i++)
{
System.out.println("请输入购买"+fruitItem.get(i).getName()+"的数量:");
number[i]+=enterNumber();
}
System.out.println("购物完成,如无异议请结算:");
purchase(number);
}
public void showFruitList()
{
System.out.println(" 水果报价单");
System.out.println("水果编号 水果名称 水果单价 计价单位 ");
for(int i=0;i<fruitItem.size();i++)
{
System.out.println(" "+fruitItem.get(i).getId()+" "+fruitItem.get(i).getName()+" "+fruitItem.get(i).getPrice()+" "+fruitItem.get(i).getUnit());
}
}
void printTicket(int[] number)
{
double totalPrice=0.0;
int totalItem=0;
int totalNumber=0;
int liulianNumber=0;
int discountPrice=0;
System.out.println(" 欢 迎 光 临 ");
System.out.println("======================================================================");
System.out.println("水果编号 水果名称 水果单价 计价单位 购买数量 金额");
for(int i=0;i<fruitItem.size();i++)
{
double price=fruitItem.get(i).getPrice()*number[i];
System.out.println(" "+fruitItem.get(i).getId()+" "+fruitItem.get(i).getName()+" "+fruitItem.get(i).getPrice()+" "+fruitItem.get(i).getUnit()+" "+number[i]+" "+price);
totalPrice+=price;
totalItem++;
totalNumber+=number[i];
if(fruitItem.get(i).getName().equals("泰国空运大榴莲"))
{
liulianNumber=number[i];
}
}
discountPrice=discount(totalPrice,liulianNumber);
System.out.println("======================================================================");
System.out.println(" "+totalItem+" 项商品 ,共计"+totalNumber+" 件");
System.out.println("总价 "+discountPrice+" 元 折扣为"+(totalPrice-discountPrice)+" 元");
totalPriceSystem.add(discountPrice);
number=null;
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println("欢迎使用《水果超市管理系统》");
doBusiness();
}
public int discount(double totalPrice,int liulianNumber)
{
if(totalPrice>=1000&&liulianNumber>10)
{
return (int)Math.round(totalPrice*0.8-50);
}
else if(totalPrice>=1000&&liulianNumber<=10)
{
return (int)Math.round(totalPrice*0.8);
}
else if(totalPrice>=600)
{
return (int)Math.round(totalPrice*0.9);
}
else if(totalPrice>=300)
{
return (int)Math.round(totalPrice*0.95);
}
else
{
return (int)Math.round(totalPrice);
}
}
public void check()
{
System.out.println("老板、请输入您的操作:");
System.out.println("1、流水查询 2、总收入查询 3、查询货物 4、添加新货物");
System.out.println("5、删除货物 6、修改货物 7、退出查询 ");
int activity=enterNumber();
if(activity==1)
{
flowSearch();
}
else if(activity==2)
{
totalSearch();
}
else if(activity==3)
{
fruitSearch();
}
else if(activity==4)
{
fruitAdd();
}
else if(activity==5)
{
fruitDelete();
}
|
|