考试要求:
一、使用技术
javaSE
二、实现功能
超市管理系统
具体要求如下:
使用选择结构,循环结构,数组的知识实现一个超市管理系统
三、运行效果图如下:
1.货物清单
2.增加货物
3.删除货物
4.修改货物
5.退出
先创一个购物类和测试类
1.TestShop
public class TestShop {
public static void main(String[] args) {
while (true){
System.out.println("=========超市管理系统==========");
System.out.print("1:货物清单 ");
System.out.print("2:增加货物 ");
System.out.print("3:删除货物 ");
System.out.print("4:修改货物 ");
System.out.println("5:退出");
Scanner sc = new Scanner(System.in);
Shop s = new Shop();
System.out.println("输出你要操作的编号");
int a = sc.nextInt();
if(a<=5&&a>=1){
switch (a){
case 1:
s.qingdan();
break;
case 2:
s.add();
break;
case 3:
s.delete();
break;
case 4:
s.update();
break;
case 5:
return;
}
}else {
System.out.println("你输入有误,请正确输入");
}
}
}
}
然后再创一个Shop类
public class Shop {
static Object[][]arr={{1000,10.0,"笔记本"},{1001,2.0,"西红柿"},{1002,5.0,"辣条"},{0,0,0},{0,0,0},{0,0,0}};
static int o=3;
Scanner sc=new Scanner(System.in);
public void qingdan() {
System.out.println("=======商品清单=======");
System.out.println("商品编号 商品单价 商品名称");
for (int i = 0; i <arr.length ; i++) {
if((int)arr[i][0]!=0){
System.out.println(arr[i][0]+" "+arr[i][1]+" "+arr[i][2]);
}
}
}
public void add(){
System.out.println("选择的是添加商品的功能");
System.out.println("输出商品编号ID");
int a = sc.nextInt();
System.out.println("输出商品单价");
double b = sc.nextDouble();
System.out.println("输出商品名称");
String c = sc.next();
arr[o][0]=a;
arr[o][1]=b;
arr[o][2]=c;
o++;
System.out.println("添加成功");
}
public void delete(){
System.out.println("选择的是删除功能");
System.out.println("输出要删除的商品ID");
int a = sc.nextInt();
for (int i = 0; i <arr.length ; i++) {
if((int)arr[i][0]==a){
arr[i][0]=0;
System.out.println("删除成功");
}
}
}
public void update(){
System.out.println("选的是修改功能");
System.out.println("输入你要修改的商品编号ID");
int a = sc.nextInt();
for (int i = 0; i <arr.length ; i++) {
if((int)arr[i][0]==a){
System.out.println("输入新的商品编号ID");
int b = sc.nextInt();
System.out.println("输入商品单价");
double c = sc.nextDouble();
System.out.println("输入商品名称");
String d = sc.next();
arr[i][0]=b;
arr[i][1]=c;
arr[i][2]=d;
System.out.println("修改成功");
break;
}
}
}
}
运行后的截图