本文共 3503 字,大约阅读时间需要 11 分钟。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | //定义商品类,包含名称,货号,单价,数量计价单位,金额 public class GoodsItem{ private String name; private String id; private int number; private String unit; private double money; //构造方法 public GoodsItem(){} GoodItem(String name,String id, double price , int number,String unit, double money){ this .name = name ; this .id= id; this .price = price; this .number = number; this .money = money; } //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 getNumber() { return number; } public void setNumber( int number) { this .number = number; } public double getMoney() { return money; } public void setMoney( double money) { this .money = money; } } //实现主干逻辑,main方法. import java.util.ArrayList; import java.util.Scanner; public class ShoppingReceipt { static ArrayList<GoodItem>data = new ArrayList<GoodItem>(); public static void main(String[] args) { // TODO Auto-generated method stub System.out.println( "欢迎使用超市管理系统" ); initData(); } private static void initData() { // TODO Auto-generated method stub GoodItem sls = new GoodItem( "少林寺核桃" , "090115" , 15.5 , 0 , "个" , 0 ); GoodItem shk = new GoodItem( "尚康饼干" , "090027" , 14.5 , 0 , "个" , 0 ); data.add(sls); data.add(shk); while ( true ){ 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(); case 3 : System.out.println( "欢迎下次光临" ); System.exit( 0 ); default : System.out.println( "请输入正确的数字!" ); break ; } } } private static void printReceipt() { // TODO Auto-generated method stub System.out.println( "欢迎光临" ); System.out.println( "品名 售价 数量 单位 金额" ); System.out.println( "-------------------" ); int totalNumber = 0 ; double totalMoney = 0 ; for ( int i = 0 ; i < data.size(); i++) { //依次获取每一个商品项 GoodItem g = data.get(i); //打印商品项 System.out.println( "" +g.getName()+g.getId()+ " " +g.getPrice()+ " " +g.getNumber()+ " + " +g.getMoney()); //累加数量与金额 totalNumber += g.getNumber(); totalMoney += g.getMoney(); } System.out.println( "-------------------------------------------" ); //票脚 System.out.println( "共" +data.size()+ "项商品" ); System.out.println( "共" +totalNumber+ "件商品" ); System.out.println( "共" +totalMoney+ "元" ); System.out.println(); } private static void enterNumber() { // TODO Auto-generated method stub for ( int i = 0 ;i<data.size();i++){ GoodItem 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.setNumber(thisGoodsNumber); thisGoods.setMoney(thisGoodsMoney); } } } |
本文转自xinsz08の平行时空博客51CTO博客,原文链接http://blog.51cto.com/xinsz08/1940339如需转载请自行联系原作者
维度2018 |