import java
.util
.ArrayList
;
import java
.util
.Scanner
;
public class Shop {
public static void main(String
[] args
) {
ArrayList
<FruitItem> array
=new ArrayList<FruitItem>();
init(array
);
System
.out
.print(array
);
while(true) {
mainMenu();
int choose
= chooseFuction();
switch (choose
) {
case 1:
showFruitList(array
);
break;
case 2:
addFruit(array
);
break;
case 3:
deleteFruitItem(array
);
break;
case 4:
updateFruitItem(array
);
break;
case 5:
return;
default:
System
.out
.println("输入的序号没有");
break;
}
}
}
public static void showFruitList(ArrayList
<FruitItem>array
) {
System
.out
.println();
System
.out
.println("============商品库存清单============");
System
.out
.println("商品编号 商品名称 商品单价");
for(int i
=0;i
<array
.size();i
++) {
FruitItem item
=array
.get(i
);
System
.out
.println(item
.ID
+" "+item
.name
+" "+item
.price
);
}
}
public static void addFruit(ArrayList
<FruitItem> array
) {
System
.out
.println("选择的是添加商品属性");
Scanner sc
=new Scanner(System
.in
);
System
.out
.println("请输入商品编号");
int ID
=sc
.nextInt();
System
.out
.println("请输入商品名字");
String name
=sc
.next();
System
.out
.println("请输入商品单价");
double price
=sc
.nextDouble();
FruitItem item
=new FruitItem();
item
.ID
=ID
;
item
.name
=name
;
item
.price
=price
;
array
.add(item
);
}
public static void deleteFruitItem(ArrayList
<FruitItem> array
) {
System
.out
.println("您选择的是删除功能");
System
.out
.println("请输入商品的编号");
Scanner sc
=new Scanner(System
.in
);
int ID
=sc
.nextInt();
for(int i
=0;i
<array
.size();i
++) {
FruitItem item
=array
.get(i
);
if(item
.ID
==ID
) {
array
.remove(i
);
System
.out
.println("删除成功");
return;
}
}
System
.out
.println("您输入的编号不存在");
}
public static void updateFruitItem(ArrayList
<FruitItem> array
) {
System
.out
.println("您选择的是修改功能");
System
.out
.println("请输入要修改的商品编号");
Scanner sc
=new Scanner(System
.in
);
int ID
= sc
.nextInt();
for(int i
=0;i
<array
.size();i
++) {
FruitItem item
= array
.get(i
);
if(item
.ID
==ID
) {
System
.out
.println("请输入新的商品编号");
item
.ID
=sc
.nextInt();
System
.out
.println("请输入新的商品名字");
item
.name
=sc
.next();
System
.out
.println("请输入新的商品价格");
item
.price
=sc
.nextDouble();
}
}
}
public static int chooseFuction() {
Scanner sc
=new Scanner(System
.in
);
return sc
.nextInt();
}
public static void mainMenu() {
System
.out
.println();
System
.out
.println("==========欢迎光临Itcast超市=========");
System
.out
.println("1.货物清单 2.添加新货物 3.删除货物 4.修改货物 5.退出系统");
System
.out
.println("请您输入要操作的功能序号");
}
public static void init(ArrayList
<FruitItem>array
) {
FruitItem f1
=new FruitItem();
f1
.ID
=9527;
f1
.name
="少林寺烧饼";
f1
.price
=12.7;
FruitItem f2
=new FruitItem();
f2
.ID
=9000;
f2
.name
="贵州牡丹饼";
f2
.price
=5.6;
FruitItem f3
=new FruitItem();
f3
.ID
=9002;
f3
.name
="大新疆切糕";
f3
.price
=599;
array
.add(f1
);
array
.add(f2
);
array
.add(f3
);
}
}
转载请注明原文地址:https://tech.qufami.com/read-7725.html