1.1  超市购物购物小票需求分析

JAVA商品超市的商品维护 java超市购物程序_数据库

用户循环进行三个操作:

1、输入购买数量,代表为所购买货物的数量赋值,从而计算每项商品金额

2、打印小票,将已有数据打印

3、退出系统(因为该程序为循环操作,无法终止,如果不想再进行操作,则退出系统)

 

1.1.1 案例代码一



public class GoodsItem { 
 
 
 

                        //成员变量 
  
                     private String name;   //名称 
  
                     private String id;     //编号 
  
                     private double price;  //单价 
  
                     private int num;       //数量 
  
                     private String unit;   //计价单位 
  
                     private double money;  //金额 
  
  
  
 //有参构造方法 
  
 public GoodsItem(String name, String id, double price, int num, String unit, double money) { 
  
            super(); 
  
            this.name = name; 
  
            this.id = id; 
  
            this.price = price; 
  
            this.num = num; 
  
            this.unit = unit; 
  
            this.money = money; 
  
 } 
  
 //无参构造方法 
  
 public GoodsItem() { 
  
  super(); 
  
  // TODO Auto-generated constructor stub 
  
 } 
  
  
  
 //取值(get)和赋值(set)方法 
  
 public String getName() { 
  
  return name; 
  
 } 
  
 public void setName(String name) { 
  
  this.name = name; 
  
 } 
  
 public String getId() { 
  
  return id; 
  
 } 
  
 public void setId(String id) { 
  
  this.id = id; 
  
 } 
  
 public double getPrice() { 
  
  return price; 
  
 } 
  
 public void setPrice(double price) { 
  
  this.price = price; 
  
 } 
  
 public int getNum() { 
  
  return num; 
  
 } 
  
 public void setNum(int num) { 
  
  this.num = num; 
  
 } 
  
 public String getUnit() { 
  
  return unit; 
  
 } 
  
 public void setUnit(String unit) { 
  
  this.unit = unit; 
  
 } 
  
 public double getMoney() { 
  
  return money; 
  
 } 
  
 public void setMoney(double money) { 
  
  this.money = money; 
  
 } 
  
 

               } 
 
 
 

     
 
 
 
1.2  打印超市购物小票
 
 

    import java.util.ArrayList; 
   
import java.util.Scanner; 
   
  

    public class supermaket { 
   
/** 
   
  

    *@Fields data :成员位置定义集合,存储所有的商品项对象 
  
 
  

    */                     
  
 
  

              static ArrayList<GoodsItem> data=new ArrayList<GoodsItem>(); 
   
                       public static void main(String[] args) { 
   
                       //为集合准备数据   
   
  

                           initData(); 
  
 
  

                           //打印欢迎语句 
   
                       System.out.println("欢迎进入超市购物系统!"); 
   
   
   
  

    while(true) { 
   
                 //使用switch给出操作选择 
   
  

                     System.out.println("请输入你要进行的操作:1输入购物数量 2打印小票 3退出"); 
   
                 //通过键盘录入得到数据 
   
                 Scanner sc=new Scanner(System.in); 
   
                 int optNumber=sc.nextInt(); 
   
   
   
  

              switch(optNumber) { 
   
          case 1:   //给所有数据量赋值,调用给所有数据的数量与金额赋值方法 
   
                        enterNumber(); 
   
                        break; 
   
          case 2:  //打印购物小票,调用打印购物小票的方法 
   
                        printReceipt(); 
   
                        break; 
   
          case 3: 
   
                      System.out.println("谢谢您的使用"); 
   
                      //退出程序          
   
  

                         System.exit(0); 
   
         default: 
   
  

                        System.out.println("请输入正确的数字!"); 
  
 
  

                        break; 
   
               } 
   
         } 
   
} 
   
  

    //定义方法,向成员位置的集合赋值 
   
 public static void initData() { 
   
            //创建多个商品项对象   
   
  

                GoodsItem sls=new GoodsItem("少林寺核桃","090115",15.5,0,"个",0); 
   
            GoodsItem shk=new GoodsItem("尚康饼干","090027",14.5,0,"袋",0); 
   
            //将多个商品项对象放入集合中   
   
  

                data.add(sls); 
   
            data.add(shk); 
   
 } 
   
  

      
  
 
  

    //为所有的数据赋值数量和金额 
   
  public static void enterNumber() { 
   
                    //集合的变遍历    
   
  

                        for(int i=0;i<data.size();i++) { 
   
                                   //依次获取到集合中每一个元素     
   
  

                                       GoodsItem thisGoods = data.get(i); 
   
                                   
   
  

                                      //提示用户,输入该商品项的数量 
  
 
  

                                      //获取该商品项名称 
   
                                  String thisGoodsName = thisGoods.getName(); 
   
                                  System.out.println("请输入"+thisGoodsName+"的购买数量"); 
   
                          
   
  

                                      //键盘录入接受商品数量   
   
                                 Scanner sc = new Scanner(System.in); 
   
                                 int thisGoodsNumber=sc.nextInt(); 
   
                                 //根据数量计算金额   金额=单价*数量         
   
  

                                     double thisGoodsMoney=thisGoods.getPrice()*thisGoodsNumber; 
   
     
   
  

                                     //为该商品的数量与金额赋值 
   
                                 thisGoods.setNum(thisGoodsNumber); 
   
                                 thisGoods.setMoney(thisGoodsMoney); 
   
                   } 
   
  } 
   
  
   
  

    //打印小票的方法 
   
 private static void printReceipt() { 
   
                       //票头 
   
  

                           System.out.println("欢迎光临"); 
   
                       System.out.println("品名\t售价\t数量\t单位\t金额"); 
   
                       System.out.println("------------------------"); 
   
                       //票体 
   
  

                           //定义变量,记录所有的商品数量   
  
 
  
 
                            int totalNumber = 0;
                       //定义变量,记录所有的商品金额  
  
 
  
 
                           double totalMoney = 0;
                       //遍历集合     
                        for(int i=0;i<data.size();i++) {
                                  //一依次获取每一个商品项    
  
 
  
 
                                    GoodsItem g=data.get(i);
                                 //打印商品项    
  
 
  
 
   System.out.println(g.getName()+g.getId()+"\t"+g.getPrice()+"\t"+g.getNum()+"\t"+g.getUnit()+"\t"+g.getMoney());
                            
  
 
  
 
                                   //累加数量和金额  
                                totalNumber+=g.getNum();
                                totalMoney+=g.getMoney();
               }
             System.out.println("------------------------");
             //票脚
             System.out.println("共"+data.size()+"项商品");
             System.out.println("共"+totalNumber+"件商品");
             System.out.println("共"+totalMoney+"元");
             System.out.println();
       }
}