超市管理系統


//商品類
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 String getCode() {
return code;
}
public void setCode(String code) {
this.code = 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;
}
}
//--------------購物車類
public class ShoppingCart extends Goods{

int count;//數量
double totalprice; //小計

public ShoppingCart() {
}

public ShoppingCart(String code, String name, double price, int count, double totalprice) {
super(code, name, price);
this.count = count;
this.totalprice = totalprice;
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}

public double getTotalprice() {
return totalprice;
}

public void setTotalprice(int totalprice) {
this.totalprice = totalprice;
}

}
----------------購物項類
public class Item {
private Goods goods ;//商品
private int amount;
private double subtotal;//當前購物項小計

public Goods getGoods() {
return goods;
}

public void setGoods(Goods goods) {
this.goods = goods;
}

public int getAmount() {
return amount;
}

public void setAmount(int amount) {
this.amount = amount;
}

public double getSubtotal() {
return subtotal;
}

public void setSubtotal(double subtotal) {
this.subtotal = subtotal;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Item item = (Item) o;
return amount == item.amount && Double.compare(item.subtotal, subtotal) == 0 && goods.equals(item.goods);
}

@Override
public int hashCode() {
return Objects.hash(goods, amount, subtotal);
}
}

-------------------------測試類
public class Supermarket {
static ArrayList<Goods> goods = new ArrayList<>();
static ArrayList<User> users = new ArrayList<>();
static ArrayList<ShoppingCart> car = new ArrayList<>();
static boolean loginstatus = false;

public static void main(String[] args) {
goods.add(new Goods("9001", "核桃1", 11));
goods.add(new Goods("9002", "核桃2", 22));
goods.add(new Goods("9003", "核桃3", 33));
System.out.println("=======================歡迎光臨=======================");
System.out.println("1:貨物清單 2:添加商品 3:刪除商品 4:修改商品 5:用戶注冊 6.顯示用戶 7.用戶登錄 8.添加購物車 10.顯示購物車 9:退出系統");
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("請輸入您要操作的功能序號");
int num = sc.nextInt();
/* if(loginstatus==false){
login();
}*/
if (!loginstatus && (num == 1 || num == 2 || num == 3 || num == 4 || num == 6 || num == 9 || num == 8 || num == 10)) {
System.out.println("進行此操作需要先登錄");
login();
}
if (loginstatus) {
if (num == 1) {
show();
} else if (num == 2) {
addGoods();
} else if (num == 3) {
delete();
} else if (num == 4) {
update();
} else if (num == 6) {
showUser();
} else if (num == 9) {
exit();
} else if (num == 8) {
addCar();
} else if (num == 10) {
showcar();
}
} else {
if (num == 7) {
login();
} else if (num == 5) {
register();
}
}


}

}

public static void show() {
System.out.println("=======================商品庫存清單=======================");
System.out.println("商品編號 商品名稱 商品單價 ");
for (int i = 0; i < goods.size(); i++) {
Goods g = goods.get(i);
if (g != null) {
System.out.println(g.getCode() + " " + g.getName() + " " + g.getPrice());
}
}
}

public static void addGoods() {
System.out.println("=======================添加新貨物=======================");
Scanner sc = new Scanner(System.in);
System.out.println("請輸入添加的貨品編號");
String code = sc.next();
System.out.println("請輸入添加的貨品名稱");
String name = sc.next();
System.out.println("請輸入添加的貨品價格");
Double price = sc.nextDouble();
Goods g = new Goods(code, name, price);
goods.add(g);
}

public static void delete() {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入需要刪除的商品編號");
String code = sc.next();
//找到要刪除的商品 方法:通過循環
//循環出來的是一個一個的Goods類型的數據
//通過循環出來的對象,獲得當前該商品的編號
//判斷該編號是否和需要刪除的商品編號相同 相等則說明找到了
for (int i = 0; i < goods.size(); i++) {
boolean flag = false;
Goods g = goods.get(i);
if (g != null && g.getCode().equals(code)) {

flag = true;

}
if (g != null && flag) {
goods.remove(i);
}
}

}

public static void update() {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入要修改的商品編號");
String code = sc.next();
boolean flag = true;
for (int i = 0; i < goods.size(); i++) {
Goods g = goods.get(i);
if (g.getCode().equals(code)) {
System.out.println("請輸入要修改的商品名稱");
String name = sc.next();
System.out.println("請輸入要修改的商品單價");
double price = sc.nextDouble();
g.setName(name);
g.setPrice(price);
flag = true;
break;
}
if (flag) {
System.out.println("修改成功");
} else {
System.out.println("修改失敗");
}
}


}

public static void exit() {
System.exit(0);
}

public static void register() {
System.out.println("====================用戶信息注冊===================");
System.out.println("請輸入用戶名");
Scanner sc = new Scanner(System.in);
String uname = sc.next();
System.out.println("請輸入密碼");
String password = sc.next();
User user = new User();
user.setUname(uname);
user.setPassword(password);
users.add(user);
System.out.println("注冊成功");
}

public static void showUser() {
System.out.println("=======================用戶信息展示=================");
System.out.println("用戶名 密碼");
for (int i = 0; i < users.size(); i++) {
User u = users.get(i);

System.out.println(u.getUname() + " " + u.getPassword());
}
}


public static void login() {
boolean flag = false;
System.out.println("=======================用戶登錄=================");
Scanner sc = new Scanner(System.in);
System.out.println("請輸入用戶名");
String uname = sc.next();
System.out.println("請輸入密碼");
String password = sc.next();
for (int i = 0; i < users.size(); i++) {
User u = users.get(i);
if (u != null && u.getPassword().equals(password) && u.getUname().equals(uname)) {
flag = true;
break;
}
}
if (flag) {
System.err.println("登錄成功");
} else {
System.err.println("登錄失敗");
}
loginstatus = flag;
}

public static ShoppingCart shengcheng(String code, int count) {
ShoppingCart a = null;
for (int i = 0; i < goods.size(); i++) {
if (code.equals(goods.get(i).getCode())) {
Goods g = goods.get(i);
a = new ShoppingCart(g.getCode(), g.getName(), g.getPrice(), count, 5 * g.getPrice());

}
}
return a;
}


public static void addCar() {
// public ShoppingCart(String code, String name, double price, int count, int totalprice)
Scanner sc = new Scanner(System.in);
System.out.println("請輸入您要買的貨品編號");
String code = sc.next();
System.out.println("請輸入您要買的貨品數量");
int count = sc.nextInt();
for (int i = 0; i < goods.size(); i++) {
if (code.equals(goods.get(i).getCode())) {
car.add(shengcheng(code, count));
System.out.println("添加成功");
break;
} else {
System.out.println("找不到此編號的商品,添加失敗");
break;
}

}
}

public static void showcar() {
for (int i = 0; i < car.size(); i++) {
System.out.println("編號\t" + "名稱\t" + "單價\t" + "數量\t" + "小計");
System.out.println(car.get(i).getCode() + "\t" + car.get(i).getName() + "\t" + car.get(i).getPrice() + "\t" + car.get(i).getCount() + "\t" + car.get(i).getTotalprice());
}
}
}



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM