超市管理系统v1.0
使用Java初级知识编写的系统
v1.0:简单的增删改查功能
初学的小伙伴可以一起交流
超市类代码:
package com.zhou.supermarket;
import java.util.Scanner;
public class SuperMarket {
//很多商品
private Goods[] goodsList = new Goods[10];
//超市名字
private String name;
//记录商品个数
private int count;
public void initizlazation() {
Goods g1 = new Goods();
g1.setCode("9527");
g1.setName("苹果");
g1.setPrice(9.9);
goodsList[0] = g1;
Goods g2 = new Goods();
g2.setCode("9528");
g2.setName("柠檬");
g2.setPrice(5.0);
goodsList[1] = g2;
Goods g3 = new Goods();
g3.setCode("9529");
g3.setName("蓝莓");
g3.setPrice(9.2);
goodsList[2] = g3;
}
public SuperMarket() {
}
public SuperMarket(String name) {
goodsList = new Goods[10];
this.name = name;
initizlazation();
count = 3;
}
public void menu() {
System.out.println("==================== 欢迎光临【" + name + "超市】 ====================");
System.out.print("1:商品列表 ");
System.out.print("2:添加商品 ");
System.out.print("3:删除商品 ");
System.out.println("4:修改商品");
}
//展示商品列表
public void show() {
System.out.println("货物清单");
System.out.println("==================== 商品库存清单 ====================");
System.out.println("商品编号 商品名称 商品单价");
for (int i = 0; i < goodsList.length; i++) {
Goods g = goodsList[i];
if (g != null) {
System.out.println(g.getCode() + " " + g.getName() + " " + g.getPrice());
}
}
}
//添加商品
public void addGoods() {
if (count > 9) {
System.out.println("仓库已满,sorry~~");
return;
}
Scanner scanner = new Scanner(System.in);
System.out.println("请输入商品编号:");
String code = scanner.next();
System.out.println("请输入商品名称:");
String name = scanner.next();
System.out.println("请输入商品价格:");
double price = scanner.nextDouble();
Goods g = new Goods(code, name, price);
goodsList[count++] = g;
/*g.setCode(code);
g.setName(name);
g.setPrice(price);*/
}
//删除商品
public void delGoods() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的商品编号;");
String code = scanner.next();
boolean flag = false;
for (int i = 0; i < count; i++) {
Goods g = goodsList[i];
if (code.equals(g.getCode())) {
System.out.println("您输入的商品编号为:" + g.getCode() + " 名称:" + g.getName() + " 价格:" + g.getPrice());
System.out.println("您确认删除该商品吗? true或false");
boolean judge = scanner.nextBoolean();
if (judge == false) {
System.out.println("那就算了叭");
} else if (judge) {
goodsList[i] = null;
flag = true;
if (i != count - 1) {
goodsList[i] = goodsList[i + 1];
} else {
goodsList[i] = null;
}
}
}
}
if (flag) {
count--;
System.out.println("删除成功!");
} else {
System.out.println("您输入的序号有误,删除失败!");
}
}
//修改商品
public void updateGoods() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要修改商品信息的编号:");
String code = scanner.next();
boolean flag = false;
Goods goods = null;
for (int i = 0; i < count; i++) {
Goods g = goodsList[i];
if (code.equals(g.getCode())) {
flag = true;
goods = g;
break;
}
}
if (flag) {
System.out.println("您输入的商品编号为:" + goods.getCode() + " 名称:" + goods.getName() + " 价格:" + goods.getPrice());
System.out.println("您确认修改该商品的信息吗? true或false");
boolean judge = scanner.nextBoolean();
if (judge == false) {
System.out.println("那就算了吧");
} else if (judge) {
System.out.println("请输入要修改的商品名称:");
String name = scanner.next();
System.out.println("请输入要修改的商品价格:");
double price = scanner.nextDouble();
goods.setName(name);
goods.setPrice(price);
System.out.println("恭喜你!修改成功");
}
} else {
System.out.println("铺gai啦哩,修改失败!");
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
商品类代码:
package com.zhou.supermarket;
public class Goods {
private String code;
private String name;
private double price;
public Goods() {
}
public Goods(String code, String name, double price) {
this.code = code;
this.name = name;
this.price = price;
}
public void setCode(String code) {
this.code = code;
}
public String getCode() {
return code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
测试类代码:
package com.zhou.supermarket;
import java.util.Scanner;
public class SuperMarkerTest {
public static void main(String[] args) {
SuperMarket superMarket = new SuperMarket("白昼");
Scanner scanner = new Scanner(System.in);
while (true) {
superMarket.menu();
System.out.print("请输入对应的功能编号:");
int num = scanner.nextInt();
System.out.println("---------------------");
if (num == 1) {
superMarket.show();
} else if (num == 2) {
superMarket.addGoods();
} else if (num == 3) {
superMarket.delGoods();
} else if (num == 4) {
superMarket.updateGoods();
} else {
System.out.println("您输入的功能编号有误");
}
}
}
}