Java-超市購物小票案例-詳細介紹


  

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

用戶循環進行三個操作:

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();
       }
}

 學習更多基礎Java教程可以在這個網站學習:http://how2j.cn/p/2099


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM