超市购物小票案例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了超市购物小票案例相关的知识,希望对你有一定的参考价值。
//定义商品类,包含名称,货号,单价,数量计价单位,金额 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の平行时空” 博客,请务必保留此出处http://xinsz08.blog.51cto.com/10565212/1940339
以上是关于超市购物小票案例的主要内容,如果未能解决你的问题,请参考以下文章
java票据打印,类似于超市的小票那样的,怎么控制打印的纸张大小啊?