package demo1;

import java.util.ArrayList;
import java.util.Scanner; public class Demo1 {
public static void main(String[] args) {
ArrayList<Goods> array = new ArrayList<Goods>();
while(true) { //功能块的选择 一直循环之道用户选择退出
int num = chooseFunction(); //选择功能
switch(num) {
case 1:
printGoods(array); //功能1:打印所有商品
break;
case 2: //功能2:添加商品
addGoods(array);
break;
case 3: //功能3:修改商品
updateGoods(array);
break;
case 4: //功能4:删除商品
deleteGoods(array);
break;
case 5: //退出
return;
default: //其他情况
System.out.println("无此功能");
}
}
}
//选择功能
public static int chooseFunction() {
System.out.println("=========商品库存管理=========");
System.out.println("1.查看商品");
System.out.println("2.添加商品");
System.out.println("3.修改商品");
System.out.println("4.删除商品");
System.out.println("5.退出");
System.out.println("请选择序号:");
Scanner sc = new Scanner(System.in);
int index = sc.nextInt();
return index;
}
//功能1:打印所有商品
public static void printGoods(ArrayList<Goods> array) {
System.out.println("编号\t品牌\t尺寸\t价格\t数量");
for (int i = 0; i < array.size(); i++) {
Goods g = array.get(i);
System.out.println((i+1)+"\t"+g.brand+"\t"+g.size+"\t"+g.price+"\t"+g.count);
}
}
//功能2:添加商品
public static void addGoods(ArrayList<Goods> array) {
Scanner sc = new Scanner(System.in);
while(true) {
System.out.println("请输入品牌:");
String brand = sc.next();
System.out.println("请输入尺寸:");
double size = sc.nextDouble();
System.out.println("请输入价格:");
double price = sc.nextDouble();
System.out.println("请输入数量:");
int count = sc.nextInt(); Goods g = new Goods();
g.brand = brand;
g.size = size;
g.price = price;
g.count = count;
array.add(g);
System.out.println("添加成功");
System.out.println("是否继续输入(1为继续输入0为结束输入)");
int number = sc.nextInt();
if(number==0)
return;
}
}
//功能3:修改商品
public static void updateGoods(ArrayList<Goods> array) {
Scanner sc = new Scanner(System.in);
while(true) {
System.out.println("请输入要修改的商品的序号:");
int index= sc.nextInt()-1;
Goods g = array.get(index);
System.out.println("请输入品牌:");
String brand = sc.next();
g.brand = brand; System.out.println("请输入尺寸:");
double size = sc.nextDouble();
g.size = size; System.out.println("请输入价格:");
double price = sc.nextDouble();
g.price = price; System.out.println("请输入价格:");
int count = sc.nextInt();
g.count = count;
System.out.println("修改成功");
System.out.println("是否继续输入(1为继续输入0为结束输入)");
int number = sc.nextInt();
if(number==0)
return;
}
}
//功能4:删除商品
public static void deleteGoods(ArrayList<Goods> array) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除的序号:");
int num = sc.nextInt()-1;
array.remove(num);
System.out.println("删除成功");
}
}
05-08 15:38