/*
管理员能够进行的操作有3项(查看、修改、退出),
我们可以采用(switch)菜单的方式来完成。
-------------库存管理------------
1.查看库存清单
2.修改商品库存数量
3.退出
请输入要执行的操作序号:
每一项功能操作,我们采用方法进行封装,这样,可使程序的可读性增强。
选择1.查看库存清单”功能,则控制台打印库存清单
选择2.修改商品库存数量”功能,则对每种商品库存数进行更新
选择3.退出”功能,则退出库存管理,程序结束
*/
import java.util.Scanner;
public class Shop{
public static void main(String[] args){
//定义数组
//用来存储品牌名字,尺寸,价格,库存数
String[] brand = {"MacBookAir" , "Thinkpad T450" , "Asus-FL5800"};
double[] size = {13.3 , 14.0 , 15.6};
double[] price = {6988.88 , 5999.99 ,4999.5};
int[] count = {5 ,10, 18 };
while(true){
int choose = chooseFunction();
switch(choose){
//查看库存清单
case 1:
printStore(brand , size , price ,count );
break;
case 2:
changeCount(brand , count);
break;
case 3:
return;
default:
System.out.println("Sorry,暂时不提供此功能");
break;
}
}
}
/*
修改商品库存数量:
定义方法,遍历数组
返回值?没有
参数?数组
*/
public static void changeCount(String [] brand,int[] count){
Scanner ran = new Scanner(System.in);
//int number_1 = ran.nextInt();
for(int i = 0;i < brand.length; i++){
System.out.println(brand[i]);
count[i] = ran.nextInt();
}
}
/*
查看库存清单:
定义方法,遍历数组
返回值? 没有
参数?数组
*/
public static void printStore(String [] brand,double[] size, double [] price, int [] count ){
int totalCount = 0;
double totalMoney = 0;
System.out.println("——————————————商城库存清单————————————————");
System.out.println("品牌型号 尺寸 价格 库存数");
for(int i = 0; i < brand.length; i++){
System.out.println(brand[i]+" "+size[i]+" "+price[i]+" "+count[i]);
totalCount += count[i];
totalMoney += price[i]*count[i];
}
System.out.println("库存总数:"+totalCount);
System.out.println("库存商品总金额:"+totalMoney);
}
/*
库存管理界面
@return返回用户选择的功能
*/
public static int chooseFunction(){
System.out.println("-------------库存管理------------");
System.out.println("1.查看库存清单");
System.out.println("2.修改商品库存数量");
System.out.println("3.退出");
System.out.println("请选择您要使用的功能:");
Scanner ran = new Scanner(System.in);
int number = ran.nextInt();
return number;
}
}
运行结果: