集合的使用——超市购物小票案例
1.定义GoodsItem类(名称、货号、单价、数量、计价单位、金额)
public class GoodsItem {
private String name;
private String id;
private double price;
private int number;
private String unit;
private double money;
public GoodsItem() {
super();
}
public GoodsItem(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.unit = unit;
this.money = money;
}
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 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;
}
}
2.具体功能实现
public class ShoppingReceipt {
public static ArrayList<GoodsItem> items = new ArrayList<GoodsItem>();
public static ArrayList<GoodsItem> shoppingCar = new ArrayList<GoodsItem>();
public static void main(String[] args) {
initGoodsItems();
System.out.println("\t欢迎使用超市购物系统");
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("请输入你要进行的操作:\n1:购买商品\t 2:结算并打印小票\t3:退出系统");
int optNumber = sc.nextInt();
switch (optNumber) {
case 1:
buyGoods();
break;
case 2:
printTicket();
break;
case 3:
System.out.println("感谢您使用超市购物系统,欢迎下次光临,拜拜");
System.exit(0);
break;
default:
System.out.println("你输入的数字有误,请重新输入");
break;
}
}
}
public static void printTicket(){
if(shoppingCar.size() == 0) {
System.out.println("您还没购买任何商品,无法结算,请先购买!!");
return;
}
System.out.println("------------------------------");
System.out.println("\t欢迎光临");
System.out.println("名称\t售价\t数量\t金额");
System.out.println("------------------------------");
int totalNumber = 0;
double totalMoney = 0;
for (int index = 0; index < shoppingCar.size(); index++) {
GoodsItem goodsItem = shoppingCar.get(index);
totalNumber += goodsItem.getNumber();
totalMoney += goodsItem.getMoney();
System.out.println(goodsItem.getName()+"\t"+goodsItem.getPrice()+
"\t"+goodsItem.getNumber()+"\t"+goodsItem.getMoney());
}
System.out.println("------------------------------");
System.out.println(shoppingCar.size()+"项商品");
System.out.println("共计:"+totalNumber+"件");
System.out.println("共:"+totalMoney+"元");
System.out.println("------------------------------");
shoppingCar.clear();
}
public static void buyGoods() {
System.out.println("------------------------------");
System.out.println("\t商品列表");
System.out.println("商品id\t名称\t单价\t计价单位");
for (int index = 0; index < items.size(); index++) {
GoodsItem goodsItem = items.get(index);
System.out.println(goodsItem.getId()+"\t"+goodsItem.getName()+"\t"
+goodsItem.getPrice()+"\t"+goodsItem.getUnit());
}
System.out.println("------------------------------");
System.out.println("请输入您要购买的商品项(输入格式:商品id-购买数量),输入end表示购买结束。");
Scanner sc = new Scanner(System.in);
while(true){
String str = sc.nextLine();
if(str.equals("end")){
System.out.println("购买结束");
break;
} else {
String[] strs = str.split("-");
if(strs.length != 2) {
System.out.println("你输入的购买姿势不对,请换个姿势再来一次(格式:商品id-购买数量)");
continue;
}
String id = strs[0];
int number = Integer.parseInt(strs[1]);
GoodsItem item = findGoodsItemById(id);
if(item != null) {
item.setNumber(number);
item.setMoney(number * item.getPrice());
addItemToShoppingCar(item);
} else {
System.out.println("您输入的商品id不存在,请重新输入!!");
}
}
}
}
public static void addItemToShoppingCar(GoodsItem item){
for (int index = 0; index < shoppingCar.size(); index++) {
GoodsItem goodsItem = shoppingCar.get(index);
if(goodsItem.getId().equals(item.getId())){
goodsItem.setNumber(goodsItem.getNumber() + item.getNumber());
goodsItem.setMoney(goodsItem.getPrice() * goodsItem.getNumber());
return;
}
}
shoppingCar.add(item);
}
public static GoodsItem findGoodsItemById(String id){
for (int index = 0; index < items.size(); index++) {
GoodsItem goodsItem = items.get(index);
if(goodsItem.getId().equals(id)){
return new GoodsItem(goodsItem.getName(), id, goodsItem.getPrice(), 0, goodsItem.getUnit(), 0);
}
}
return null;
}
public static void initGoodsItems() {
GoodsItem item1 = new GoodsItem("少林核桃", "001", 15.5, 0, "斤", 0);
GoodsItem item2 = new GoodsItem("尚康饼干", "002", 14.5, 0, "包", 0);
GoodsItem item3 = new GoodsItem("移动硬盘", "003", 345, 0, "个", 0);
GoodsItem item4 = new GoodsItem("高清无码", "004", 199, 0, "G", 0);
items.add(item1);
items.add(item2);
items.add(item3);
items.add(item4);
}
}
运行结果:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)