/*
- 库存管理
- 1.存储商品信息
-
存储商品类型变量,存储到集合中
- 2.查看库存清单
- 3.修改商品库存
-
集合遍历 ,获取集合中存储的Goods类型变量
-
变量调用Goods雷达属性count,进行修改(键盘输入)
*/
import java.util.*;
public class shopp {
public static void main(String[] args){
//
ArrayList <Goods> array = new ArrayList<Goods>();
//调用添加商品信息的方法
add(array);
//调用print方法
print(array);
//进入死循环
/*
* 不输入3 就不结束,功能为重复执行效果
*/
while(true){
//调用选择功能的方法,获取到用户输入的数字
int number = chooseFunction();
//对序号进行判断,如果=1 进入查看库存功能 ...
switch(number){
case 1:
print(array);
break;
case 2:
update(array);
break;
case 3:
return;
default:
System.out.println("无此功能");
break;
}
}
}
/*
* 方法定义,修改库存
* 键盘输入,将Goods中的属性值,修改
*/
public static void update(ArrayList<Goods> array){
//创建Scanner,类变量; 格式:类型 变量名 = new 类型();
Scanner sc = new Scanner(System.in);
//遍历集合,获取集合中的每个元素
for (int i = 0; i < array.size(); i++) {
//集合方法get获取的是集合的元素,元素类型Goods
Goods g = array.get(i);
//提示用户输入
System.out.println("请输入"+g.brand+"的库存数");
//Goods属性,count进行修改
g.count= sc.nextInt();
}
}
/*
* 定义方法,实现选择菜单,用户根据功能选择菜单
*/
public static int chooseFunction(){
//输出表头
System.out.println("-------------库存管理----------");
System.out.println("1.查看库存清单");
System.out.println("2.修改商品库存数量");
System.out.println("3.退出");
System.out.println("请输入要执行的操作编号:");
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
return number;
}
/*
定义方法 遍历集合 查看库存清单
*/
public static void print(ArrayList<Goods> array){
//输出表头
System.out.println("-------------商场库存清单----------");
System.out.println("品牌型号 尺寸 价格 库存数");
//
int totalCount = 0 ;
double totalMoney = 0 ;
//遍历集合
for (int i = 0; i < array.size(); i++) {
//使用Goods类型变量,接受get方法结果
Goods number = array.get(i);
System.out.println(number.brand+" "+number.count+" "+number.price+" "+number.size);
totalCount = totalCount + number.count;
totalMoney = totalMoney + number.price*totalCount;
}
System.out.println("总库存数"+totalCount);
System.out.println("总金额"+totalMoney);
}
/*
* 存储商品的方法
*/
public static void add(ArrayList<Goods> array){
//创建Goods类型的变量 ,四个容器
Goods sn1 = new Goods();
Goods sn2 = new Goods();
sn1.brand="西瓜";
sn1.count=5;
sn1.price=9.99;
sn1.size=13.3;
sn2.brand="菠萝";
sn2.count=15;
sn2.price=19.99;
sn2.size=15.3;
//将变量存储到集合中
array.add(sn1);
array.add(sn2);
}
}