//商場類
public class Goods {
int no; //編號 String name; //商品名稱 double price; //商品價格 int number; //商品數量
//初始化數據方法 public void setData(int iNo, String sName, double dPrice, int iNumber) { no = iNo; name = sName; price = dPrice; number = iNumber; } // 按格式輸出
public void showGoods() { System.out.println(no + "\t\t" + name + (name.getBytes().length > 10 ? "\t" : "\t\t") + price + "\t\t" + number); } }
//用戶類
public class User {
String username; //用戶名
String password; //密碼
String nickName; //昵稱// 購物車
Goods[] cart = new Goods[10];
//顯示用戶信息方法 public void showInfo() {
System.out.println("用戶名:\t" + username);
System.out.println("密碼:\t" + password);
System.out.println("昵稱:\t" + nickName);
}
//購買商品 --> 傳入商品參數 public void buyGoods(Goods goods) {
// 1.購物車中沒有該商品
// 2.購物車中有了該商品
int index = searchGoodsByName(goods.no);
if (index != -1) {
// 購物車已經有了該商品,直接增加數量
cart[index].number += goods.number;
} else {
// 沒有呢,就把他放入 第一個為null的位置
for (int i = 0; i < cart.length; i++) {
if (cart[i] == null) {
cart[i] = goods;
return;
}
}
System.out.println("商品已滿,請清空購物車后,在來購買");
}
}
//查找購物車是否有相同的商品 public int searchGoodsByName(int goodsNo) {
for (int i = 0; i < cart.length; i++) {
if (cart[i] == null)
return -1;
else if (goodsNo == cart[i].no) {
return i;
}
}
return -1;
}
//顯示商品 public void showGoods() {
System.out.println("商品編號\t\t商品名稱\t\t商品價格\t\t庫存");
for (int i = 0; i < cart.length; i++) {
if (cart[i] == null)
return;
cart[i].showGoods(); //調用商場的顯示方法 顯示商品
}
}
// 返回購物車中需要的金額
public double getGoodsMoney() {
double sum = 0;
for (int i = 0; i < cart.length; i++) {
if (cart[i] == null)
break;
double value = cart[i].price * cart[i].number;
sum += value;
}
return sum;
}
// 清空購物車
public void clearCart() {
for (int i = 0; i < cart.length; i++) {
cart[i] = null;
}
}
}
import java.util.Scanner;
//商場系統
public class ShoppingManager {
//用戶數組
User[] users = new User[10];
//商品數組
Goods[] goods = new Goods[10];
Scanner input = new Scanner(System.in);
//當前登錄用戶
User loginUser;
// 顯示menu之前,先初始化數據
public void init() {
goods[0] = new Goods();
goods[0].setData(10101, "海爾冰箱", 3999.9, 50);
goods[1] = new Goods();
goods[1].setData(10102, "格力冰箱", 2888.8, 50);
goods[2] = new Goods();
goods[2].setData(10103, "TCL冰箱", 3333.0, 100);
goods[3] = new Goods();
goods[3].setData(10104, "美的冰箱", 5100.5, 80);
goods[4] = new Goods();
goods[4].setData(10105, "海爾空調", 4622, 50);
goods[5] = new Goods();
goods[5].setData(10106, "格力空調", 1555, 70);
goods[6] = new Goods();
goods[6].setData(10107, "TCL空調", 2100.5, 30);
goods[7] = new Goods();
goods[7].setData(10108, "美的空調", 3355.5, 150);
goods[8] = new Goods();
goods[8].setData(10109, "小天鵝洗衣機", 2500, 30);
goods[9] = new Goods();
goods[9].setData(10110, "西門子冰箱", 3800, 80);
// 預存用戶 測試用的
User user = new User();
user.nickName = "李四";
user.username = "lisi";
user.password = "123456";
users[0] = user;
}
//顯示商場菜單
public void showMenu() {
// 初始化商品
init();
String choose = "";
do {
// 打印菜單系統
System.out.println("*****************國美電器*****************");
System.out.println("\t\t1.用戶登錄");
System.out.println("\t\t2.用戶注冊");
System.out.println("\t\t3.退出系統");
System.out.println("*****************國美電器*****************");
choose = input.next();
switch (choose) {
case "1":
userLogin(); //登錄
break;
case "2":
userRegister(); //注冊
break;
case "3":
System.out.println("退出系統成功!");
break;
default:
System.out.println("輸入錯誤,請重新輸入");
break;
}
} while (!choose.equals("3"));
// 退出系統
input.close();
}
//用戶注冊
private void userRegister() {
System.out.print("請輸入用戶名:");
String name = input.next();
if (isDuplicationName(name)) {
System.out.println("用戶名已存在!注冊失敗");
return;
}
// 沒有重名
System.out.print("請輸入密碼:");
String password = input.next();
if (password.length() < 6) {
System.out.println("密碼輸入錯誤!注冊失敗");
return;
}
System.out.print("請輸入昵稱:");
String nickName = input.next();
if (addUser(name, password, nickName)) {
System.out.println("注冊成功");
} else {
System.out.println("注冊失敗,用戶已滿!");
}
}
//添加用戶方法,判斷用戶數組中是否存在該用戶
public boolean addUser(String username, String password, String nickname) {
User user = new User();
user.username = username;
user.nickName = nickname;
user.password = password;
for (int i = 0; i < users.length; i++) {
if (users[i] == null) {
users[i] = user;
return true;
}
}
return false;
}
//判斷用戶名是否存在,返回ture或false
private boolean isDuplicationName(String name) {
for (int i = 0; i < users.length; i++) {
// 為空就跳出循環
if (users[i] == null)
break;
// 有重名
else if (users[i].username.equals(name))
return true;
}
return false;
}
private void userLogin() {
System.out.print("請輸入用戶名:");
String username = input.next();
System.out.print("請輸入密碼:");
String password = input.next();
User user = login(username, password);
if (user == null) {
System.out.println("登錄失敗!");
} else {
// 下一級菜單
loginUser = user;
System.out.println("登錄成功,你好:" + loginUser.nickName);
userMenu(); //用戶菜單
}
}
public User login(String username, String password) {
for (int i = 0; i < users.length; i++) {
if (users[i] == null)
return null;
else if (users[i].username.equals(username)
&& users[i].password.equals(password)) {
return users[i];
}
}
return null;
}
// 用戶登錄后的菜單
public void userMenu() {
String choose = "";
do {
System.out.println("1.商品購買");
System.out.println("2.我的購物車");
System.out.println("3.個人信息");
System.out.println("4.注銷");
choose = input.next();
switch (choose) {
case "1":
buyGoods(); //購買商品
break;
case "2":
showMyGoods(); //顯示購物車
break;
case "3":
showUserInfo(); //顯示用戶信息
break;
case "4":
System.out.println("注銷成功");
break;
default:
System.out.println("輸入錯誤,請重新輸入!");
break;
}
} while (!choose.equals("4"));
}
private void showUserInfo() {
loginUser.showInfo();
}
//顯示購物車
private void showMyGoods() {
System.out.println("********************購物車********************");
loginUser.showGoods();
// 開始付款
System.out.println("\t\t你購買的商品總價格為:" + loginUser.getGoodsMoney());
// 是否付款
if (loginUser.getGoodsMoney() == 0)
return;
System.out.println("是否付款?Y/N");
String choose = input.next();
if (choose.equalsIgnoreCase("y")) {
// 付款
System.out.println("輸入實付金額:");
double money = input.nextDouble();
if (money < loginUser.getGoodsMoney()) {
System.out.println("輸入的金額不夠啊!");
} else {
double zero = money - loginUser.getGoodsMoney();
System.out.println("恭喜你付款成功,找您" + zero + "元");
loginUser.clearCart();
}
}
}
private void buyGoods() {
System.out.println("********************商品購買*********************");
showGoodsList();
String choose = "";
while (!choose.equalsIgnoreCase("exit")) {
System.out.print("請輸入需要購買的商品編號,輸入exit后退出");
choose = input.next();
if (!choose.equalsIgnoreCase("exit")) {
// 購買商品
int index = getGoodsIndex(choose);
if (index == -1) {
System.out.println("沒有該商品,請重新輸入");
} else {
System.out.print("請輸入商品數量:");
int number = input.nextInt();
// 判斷由沒有這么多庫存
if (number <= goods[index].number) {
// 1.商城需要減少數量
// 2.購物車是增加
goods[index].number -= number;
// 用戶買的商品
Goods good = new Goods();
good.setData(goods[index].no, goods[index].name,
goods[index].price, number);
loginUser.buyGoods(good);
System.out.println("購買成功");
} else {
System.out.println("商品不夠了。。。");
}
}
}
}
// 顯示購物車
showMyGoods();
}
//顯示商品列表
public void showGoodsList() {
System.out.println("商品編號\t\t商品名稱\t\t商品價格\t\t庫存");
for (int i = 0; i < goods.length; i++) {
Goods good = goods[i];
if (good == null)
return;
good.showGoods();
}
}
// 判斷購買的商品在數組中的位置
/**
*
* @param no
* 輸入的商品編號
* @return 找到 返回索引號,沒有找到返回-1
*/
public int getGoodsIndex(String no) {
// 將編號轉成int
// int iNo = Integer.parseInt(sNo);
for (int i = 0; i < goods.length; i++) {
if ((goods[i].no + "").equals(no)) {
return i;
}
}
return -1;
}
}
//測試類
public class Test {
public static void main(String[] args) {
ShoppingManager manager = new ShoppingManager();
manager.showMenu();
}
}