A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

超市类
  1. /*
  2. *
  3. */
  4. import java.util.ArrayList;
  5. import java.util.Scanner;

  6. public class FruitStore {
  7.        
  8.         //定义fruitItem集合用来存放所有水果项对象
  9.         static ArrayList<FruitItem> list=new ArrayList<FruitItem>();
  10.         //定义Double集合用来存放流水
  11.         static ArrayList<Double> flowData=new ArrayList<Double>();       
  12.        
  13.         public static void main(String[] args) {
  14.                
  15.                 //初始化数据
  16.                 init();
  17.                
  18.                 while(true){
  19.                         //提示用户输入需要的选择
  20.                         System.out.println("欢迎光临");
  21.                         System.out.println("请选择您需要的操作:1:购买水果 2.打印小票 3:流水查询 4:总收入查询 5:查看所有水果 6:增加水果 7:删除水果 8:修改水果信息 9:退出");
  22.                        
  23.                         //接收用户输入的数据
  24.                         int choose=enterNumber();
  25.                        
  26.                         switch(choose){
  27.                                 //调用buy方法实现买的操作
  28.                                 case 1:
  29.                                         buy();
  30.                                         break;
  31.                                 //调用printReceipt方法实现打印小票结账
  32.                                 case 2:
  33.                                         printReceipt();
  34.                                         resetNumber();
  35.                                         break;
  36.                                 //调用showFlow方法显示当天流水
  37.                                 case 3:
  38.                                         showFlow();
  39.                                         break;
  40.                                 //调用dailyIncome方法查询当天总收入
  41.                                 case 4:
  42.                                         dailyIncome();
  43.                                         break;
  44.                                 case 5:
  45.                                         //展示水果列表
  46.                                         showFruitList();
  47.                                         break;
  48.                                 case 6:
  49.                                         addFruitItem();
  50.                                         break;
  51.                                 case 7:
  52.                                         delFruitItem();
  53.                                         break;       
  54.                                 case 8:
  55.                                         updateFruitItem();
  56.                                         break;
  57.                                 case 9:
  58.                                         System.out.println("欢迎下次光临!");
  59.                                         resetNumber();
  60.                                         break;
  61.                                 default:
  62.                                         System.out.println("您输入的操作有误,请重新选择!");
  63.                                         break;                                       
  64.                                        
  65.                         }
  66.                 }
  67.         }
  68.        
  69.        

  70.         //定义输入数字方法
  71.         public static int enterNumber(){
  72.                 Scanner sc=new Scanner(System.in);
  73.                 return sc.nextInt();
  74.         }
  75.        
  76.         //定义输入小数方法
  77.         public static double enterDouble(){
  78.                 Scanner sc=new Scanner(System.in);
  79.                 return sc.nextDouble();
  80.         }
  81.        
  82.         //定义输入字符串方法
  83.         public static String enterString(){
  84.                 Scanner sc=new Scanner(System.in);
  85.                 return sc.nextLine();
  86.         }
  87.        
  88.         //定义数据初始化方法
  89.         public static void init(){
  90.                 FruitItem item = new FruitItem();
  91.                 item.name = "泰国空运大榴莲";
  92.                 item.ID = 9056;
  93.                 item.price = 120;
  94.                 item.unit = "个";
  95.                
  96.                 FruitItem item2 = new FruitItem();
  97.                 item2.name = "昌平农家有机草莓";
  98.                 item2.ID = 9047;
  99.                 item2.price = 18;
  100.                 item2.unit = "斤";
  101.                
  102.                 FruitItem item3 = new FruitItem();
  103.                 item3.name = "山东大枣";
  104.                 item3.ID = 9032;
  105.                 item3.price = 50;
  106.                 item3.unit = "斤";

  107.                 list.add(item);
  108.                 list.add(item2);
  109.                 list.add(item3);
  110.         }
  111.        
  112.         //展示商品方法
  113.         public static void showFruitList(){
  114.                 System.out.println("水果编号        水果名称        水果单价        计价单位");
  115.                 for(int i=0;i<list.size();i++){                       
  116.                         System.out.println(list.get(i).ID+"     "+list.get(i).name+"      "+list.get(i).price+"      "+list.get(i).unit);
  117.                 }
  118.         }
  119.         //购买水果方法
  120.        
  121.         public static void buy(){
  122.                 //调用展示水果方法
  123.                 showFruitList();
  124.                
  125.                 //显示打折信息
  126.                 System.out.println();
  127.                 System.out.println("好消息!年底大促销:");
  128.                 System.out.println("满300,打95折");
  129.                 System.out.println("满600,打9折");
  130.                 System.out.println("满1000,打8折");
  131.                 System.out.println("满1000且榴莲购买的数量超过10个,立减50");
  132.                 System.out.println();
  133.                
  134.                 //让用户循环选择要买的水果,按0退出
  135.                 while(true){
  136.                        
  137.                         //选择水果编号
  138.                         System.out.println("请选择您要购买的水果编号,选择结束请输入0");
  139.                        
  140.                         //接收用户输入水果编号
  141.                         int fruitID = enterNumber();
  142.                         if(fruitID==0){
  143.                                 return;
  144.                         }
  145.                         //定义一个标志,查看用户是否购买水果
  146.                         boolean flag=false;
  147.                         for(int i = 0;i < list.size();i++){
  148.                                 if(list.get(i).ID == fruitID){
  149.                                         System.out.println("请选择您要购买的水果的数量:");                               
  150.                                         list.get(i).number = enterNumber();
  151.                                         list.get(i).money = list.get(i).price * list.get(i).number;
  152.                                         flag=true;
  153.                                 }
  154.                         }
  155.                         if(!flag){
  156.                                 System.out.println("您输入的编号有误,请重新输入!");
  157.                         }
  158.                 }
  159.         }
  160.        
  161.         //定义打印小票方法
  162.         public static void printReceipt() {
  163.                 //判断是否购物,若购物可以打印小票,否则提示不能打印
  164.                 //票头
  165.                 System.out.println("这是您这次的购物小票:");
  166.                 System.out.println("==============================================");
  167.                 System.out.println("欢    迎    观    临");
  168.                 System.out.println("品名        数量        单价        金额");
  169.                 System.out.println("-----------------------------------------------");
  170.                 //票体
  171.                 int count = 0;
  172.                 double totalMoney=0;
  173.                 for(int i = 0;i < list.size();i++){
  174.                         FruitItem thisItem = list.get(i);                       
  175.                         if(thisItem.number != 0){                               
  176.                                 System.out.println(thisItem.name+"("+thisItem.ID+")      "+thisItem.price+"  *  "+thisItem.number+"  =  "+thisItem.money);
  177.                                 count++;
  178.                                 totalMoney+=thisItem.money;
  179.                         }
  180.                 }
  181.                 System.out.println("-----------------------------------------------");
  182.                 //票脚
  183.                 //调用打折方法
  184.                 double afterMoney=doDiscount(totalMoney);
  185.                 //输出票脚信息
  186.                 System.out.println(count+"项商品");
  187.                 System.out.println("总计:"+afterMoney+"元");
  188.                 System.out.println("优惠活动让您节省了:"+(totalMoney-afterMoney)+"元");
  189.                 System.out.println("===============================================");
  190.                 flowData.add(afterMoney);
  191.                 resetNumber();
  192.         }
  193.         public static double doDiscount(double totalMoney) {
  194.                 //判断总金额和榴莲两种数据是否满足打折条件,满足就打折
  195.                 /*
  196.                         如果商品总价超过300元,打95折
  197.                         如果商品总价超过600元,打9折
  198.                         如果商品总价超过1000元,总价打8折
  199.                         如果商品总价超过1000元,并且榴莲购买超过10个, 则总价打8折后,立减50
  200.                 */
  201.                 double afterTotalMoney = totalMoney;
  202.                 //满300打95折
  203.                 if(totalMoney>=300&&totalMoney<600) {
  204.                         afterTotalMoney = totalMoney*0.95;
  205.                 }else if(totalMoney>=600&&totalMoney<1000) {//满600打9折
  206.                         afterTotalMoney = totalMoney*0.9;
  207.                 }else if(totalMoney>=1000) {//满1000打8折
  208.                         afterTotalMoney = totalMoney*0.8;
  209.                 }
  210.                 //满1000且榴莲超过10,打8折再立减50
  211.                 if(totalMoney>=1000&&list.get(0).number>=10) {
  212.                         afterTotalMoney = totalMoney*0.8-50;
  213.                 }
  214.                 return afterTotalMoney;
  215.         }
  216.        
  217.         //流水查询方法
  218.         public static void showFlow() {
  219.                 //遍历流水集合,输出流水数据
  220.                 for(int i=0;i<flowData.size();i++){
  221.                         System.out.println("第"+(i+1)+"笔交易:"+flowData.get(i));
  222.                 }
  223.         }
  224.        
  225.         //当天总收入查询方法
  226.         public static void dailyIncome(){
  227.                 double sum=0;
  228.                 for(int i=0;i<flowData.size();i++){
  229.                         double thisMoney = flowData.get(i);
  230.                         sum+=thisMoney;
  231.                         System.out.println("今天到目前为止已销售金额为:"+sum);
  232.                 }
  233.         }
  234.        
  235.         //添加新水果的方法
  236.         public static void addFruitItem(){
  237.                
  238.                 System.out.println("请输入您要添加的水果编号");
  239.                 int fruitID=enterNumber();
  240.                 for(int i=0;i<list.size();i++){
  241.                         FruitItem thisItem = list.get(i);
  242.                         if(thisItem.ID==fruitID){
  243.                                 System.out.println("您输入的水果编号有误,添加水果失败!");
  244.                                 return;
  245.                         }
  246.                 }
  247.                
  248.                 FruitItem fruitItem=new FruitItem();
  249.                
  250.                 fruitItem.ID=fruitID;
  251.                
  252.                 System.out.println("请您输入要添加的水果名称:");
  253.                 fruitItem.name=enterString();
  254.                 System.out.println("请您输入要添加的水果单价:");
  255.                 fruitItem.price=enterDouble();
  256.                 System.out.println("请您输入要添加的水果计价单位:");
  257.                 fruitItem.unit=enterString();
  258.                
  259.                 list.add(fruitItem);
  260.                 System.out.println("添加水果信息成功");
  261.         }
  262.        
  263.         //定义删除水果方法,用来删除List中的水果项对象
  264.         public static void delFruitItem(){
  265.                
  266.                 System.out.println("请输入您要删除的水果编号");
  267.                
  268.                 int fruitID=enterNumber();
  269.                
  270.                 boolean flag=false;
  271.                
  272.                 for(int i=0;i<list.size();i++){
  273.                         FruitItem thisItem = list.get(i);
  274.                         if(thisItem.ID==fruitID){
  275.                                
  276.                                 list.remove(i);
  277.                                 System.out.println("删除水果信息成功");
  278.                                 flag=true;                               
  279.                                 return;
  280.                         }
  281.                 }
  282.                 if(!flag){
  283.                         System.out.println("您输入的水果编号有误,删除水果失败!");
  284.                 }               
  285.         }
  286.        
  287.         //定义修改水果项方法
  288.         public static void updateFruitItem(){
  289.                
  290.                 System.out.println("请输入您要修改的水果编号");
  291.                
  292.                 int fruitID=enterNumber();
  293.                
  294.                 boolean flag=false;
  295.                
  296.                 for(int i=0;i<list.size();i++){
  297.                        
  298.                         FruitItem thisItem = list.get(i);
  299.                         if(thisItem.ID==fruitID){                       
  300.                                                
  301.                                 System.out.println("请输入您要修改的水果名称:");
  302.                                 thisItem.name=enterString();
  303.                                 System.out.println("请输入您要修改的水果单价:");
  304.                                 thisItem.price=enterDouble();
  305.                                 System.out.println("请输入您要修改的水果计价单位:");
  306.                                 thisItem.unit=enterString();
  307.                                
  308.                                 System.out.println("修改水果信息成功");
  309.                                 flag=true;
  310.                                 return;
  311.                         }
  312.                 }
  313.                 if(!flag){
  314.                         System.out.println("您输入的水果编号有误,修改水果失败!");                       
  315.                 }
  316.         }
  317.        
  318.         //重置购买水果的金额和数量
  319.         public static void resetNumber(){
  320.                 for(int i=0;i<list.size();i++){
  321.                         FruitItem thisItem=list.get(i);
  322.                         thisItem.number=0;
  323.                         thisItem.money=0;
  324.                 }
  325.         }
  326. }
复制代码

水果类
public class FruitItem {
        String  name;                //商品名称
        int  ID;                        //商品编号
        double  price;                //商品单价
        String  unit;                //商品计价单位
        int  number;                //商品数量
        double  money;                //商品金额
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马