一、项目名称
基于控制台显示的电子商城项目
二、功能要求
该项目能实现的功能有注册、登录、浏览商城、查看购买的商品、管理员登录,其中注册用户名有格式要求且不可重复,注册时有密码确认功能。浏览商城与查看购买的商品时会检测是否登录,并给与提示。管理员登录可对商品进行增,删,查,改功能。
三、需求分析
普通用户需要注册登录后进行书籍的浏览和购买,购买后可以查看自己的书籍列表管理员用户可以对商城的书籍进行维护(增加,删除,修改)。
四、设计思路
1.使用JAVA语言
2.在控制台下运行
3.使用集合进行数据的存储和管理
4.使用文件进行数据的存储和管理
五、相关代码
- Shop.class:
package com.qrst.shop;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Shop {
//private static final String UserFile = null;
static List<Good> goodList = new ArrayList();
static List<User> userList = new ArrayList();
static List<Good> myGoodList = new ArrayList();
static File userFile = new File("sunyumeng\\eclipse\\Shop\\src\\com\\qrst\\shop\\UserFile");
User user = new User();
static Scanner sc = new Scanner(System.in);
public static void saveUserListToFile() {
try {
FileOutputStream fos = new FileOutputStream(userFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(userList);
oos.close();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public void readListFromFile() {
try {
FileInputStream fis = new FileInputStream(userFile);
ObjectInputStream ois = new ObjectInputStream(fis);
Object object = ois.readObject();
userList = (List) object;
ois.close();
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
Shop shop = new Shop();
shop.run();
}
Admin admin = new Admin();
private void run() {
boolean go_on = true;
while (go_on) {
int choice = this.showMainMenu();
go_on = this.chooseMenu(choice);
}
}
private void initGoodList() {
Good good1 = new Good(1, "海尔冰箱", new BigDecimal("1999"), 100);
Good good2 = new Good(2, "海信电视", new BigDecimal("1499"), 100);
Good good3 = new Good(3, "小米手机", new BigDecimal("999"), 100);
goodList.add(good1);
goodList.add(good2);
goodList.add(good3);
}
private int showMainMenu() {
System.out.println("*****欢迎进入电子商城*****");
System.out.println("\t1.注册");
System.out.println("\t2.登录");
System.out.println("\t3.查看商城");
System.out.println("\t4.查看我购买的商品");
System.out.println("\t5.管理员登录");
System.out.println("\t6.退出系统");
System.out.println("*************************");
System.out.print("请选择菜单:");
int choice = sc.nextInt();
return choice;
}
private boolean chooseMenu(int choice) {
boolean result = true;
System.out.println("您输入的是:" + choice);
switch (choice) {
case 1:
System.out.println("您选择的功能是:注册");
user.reg();
break;
case 2:
System.out.println("您选择的功能是:登录");
user.login();
break;
case 3:
System.out.println("您选择的功能是:查看商城");
user.showGood();
if (user.isLogin() == true) {
user.buy();
} else {
System.out.println("您还未登陆成功,请先登录!");
}
break;
case 4:
System.out.println("您选择的功能是:查看我购买的商品");
if (user.isLogin() == true) {
user.showMyGoodList();
} else {
System.out.println("您还未登陆成功,请先登录!");
}
break;
case 5:
System.out.println("您选择的功能是:管理员登录");
admin.adminlogin();
break;
case 6:
System.out.println("您选择的功能是:退出系统,欢迎下次光临!");
result = false;
break;
default:
System.out.println("[您的输入有误!]");
break;
}
return result;
}
}
- Admin.class
package com.qrst.shop;
import java.math.BigDecimal;
public class Admin extends User {
public void adminlogin() {
System.out.println("欢迎管理员登录!");
System.out.println("请输入管理员用户名!");
String login_username = Shop.sc.next();
boolean loginResult = false;
while (true) {
System.out.println("请输入管理员密码:");
String login_password = Shop.sc.next();
if (login_username.equals("admin") && login_password.equals("admin")) {
System.out.println("****管理员登陆成功!****");
loginResult = true;
while (true) {
if (showAdminMenu() == 6)
break;
this.showAdminMenu();
}
break;
}
if (loginResult == true)
break;
else
System.out.println("登陆失败!请重新登录!");
}
}
public int showAdminMenu() {
System.out.println("*****管理员菜单*****");
System.out.println("\t1.添加商品");
System.out.println("\t2.修改商品");
System.out.println("\t3.删除商品");
System.out.println("\t4.查看商品列表");
System.out.println("\t5.查询商品");
System.out.println("\t6.退出管理员菜单");
System.out.println("*************************");
System.out.print("请选择菜单:");
int choice = Shop.sc.nextInt();
this.chooseAdminMenu(choice);
return choice;
}
private String isGo_on() {
System.out.println("您是否继续?Y/N");
String go_on = Shop.sc.next();
return go_on.toUpperCase();
}
private boolean chooseAdminMenu(int choice) {
boolean result = true;
String go_on = "Y";
switch (choice) {
case 1:
System.out.println("您选择的菜单是:添加商品");
while (go_on.equals("Y")) {
this.addGood();
go_on = this.isGo_on();
}
break;
case 2:
System.out.println("您选择的菜单是:修改商品");
while (go_on.equals("Y")) {
this.updateGood();
go_on = this.isGo_on();
}
break;
case 3:
System.out.println("您选择的菜单是:删除商品");
while (go_on.equals("Y")) {
this.deleteGood();
go_on = this.isGo_on();
}
break;
case 4:
System.out.println("您选择的菜单是:查看商品列表");
super.showGood();
break;
case 5:
System.out.println("您选择的菜单是:查询商品");
while (go_on.equals("Y")) {
this.queryGood();
go_on = this.isGo_on();
}
break;
}
return result;
}
private void queryGood() {
System.out.println("*****查询商品******");
System.out.print("请输入需要查询的商品编号:");
int id = Shop.sc.nextInt();
Good good = this.findGoodById(id);
if (good == null) {
System.out.println("未找到该商品");
} else {
System.out.println("该商品信息如下:");
System.out.println("商品id\t商品名称\t商品价格\t商品数量");
System.out.println(good.getId() + "\t" + good.getName() + "\t" + good.getPrice() + "\t" + good.getNum());
}
}
private void deleteGood() {
System.out.print("*****开始删除商品******");
super.showGood();
System.out.print("请输入需要删除的商品编号");
int id = Shop.sc.nextInt();
Good good = this.findGoodById(id);
Shop.goodList.remove(good);
System.out.print("*****商品删除成功*****");
}
private void addGood() {
System.out.println("*****开始添加商品******");
System.out.print("请输入商品编号:");
int id = Shop.sc.nextInt();
System.out.print("请输入商品名称:");
String name = Shop.sc.next();
System.out.print("请输入商品价格:");
String price = Shop.sc.next();
System.out.print("请输入商品数量:");
int num = Shop.sc.nextInt();
Good good = new Good();
good.setId(id);
good.setName(name);
good.setPrice(new BigDecimal(price));
good.setNum(num);
Shop.goodList.add(good);
}
private void updateGood() {
System.out.println("*****开始修改商品******");
super.showGood();
System.out.print("请输入需要修改的商品编号:");
int id = Shop.sc.nextInt();
Good good = this.findGoodById(id);
if (good == null) {
System.out.println("未找到该商品");
} else {
System.out.println("该商品信息如下:");
System.out.println("商品id\t商品名称\t商品价格\t商品数量");
System.out.println(good.getId() + "\t" + good.getName() + "\t" + good.getPrice() + "\t" + good.getNum());
}
System.out.println("请输入修改后的商品名称:");
String name = Shop.sc.next();
good.setName(name);
System.out.println("请输入修改后的商品价格:");
Double price = Shop.sc.nextDouble();
good.setPrice(new BigDecimal(price));
System.out.println("请输入修改后的商品数量:");
int num = Shop.sc.nextInt();
good.setNum(num);
}
}
- User.class
package com.qrst.shop;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.Scanner;
public class User implements Serializable {
private String username;
private String password;
private boolean isLogin;
public boolean isLogin() {
return isLogin;
}
public void setLogin(boolean isLogin) {
this.isLogin = isLogin;
}
public void reg() {
char ch;
String username;
System.out.println("欢迎注册!");
while (true) {
boolean[] legal_name = new boolean[3];
boolean alrReg = false;
System.out.println("请输入用户名:");
username = Shop.sc.next();
for (User user : Shop.userList) {
if (username.equals(user.getUsername())) {
System.out.println("该用户名已注册!");
alrReg = true;
break;
} else alrReg = false;
}
if (!alrReg)
break;
}
while (true) {
System.out.println("请输入密码:");
String password = Shop.sc.next();
System.out.println("请再次确认密码:");
String repassword = Shop.sc.next();
if (password.equals(repassword)) {
// System.out.println("注册成功!");
User user = new User();
user.setUsername(username);
user.setPassword(password);
Shop.userList.add(user);
System.out.println("注册成功!");
break;
} else
System.out.println("两次密码输入不一致!");
}
}
public void login() {
System.out.println("欢迎登录!");
System.out.println("请输入用户名!");
String login_username = Shop.sc.next();
boolean loginResult = false;
while (true) {
System.out.println("请输入密码:");
String login_password = Shop.sc.next();
for (User user : Shop.userList) {
if (login_username.equals(user.getUsername()) && login_password.equals(user.getPassword())) {
System.out.println("登陆成功!");
this.setLogin(true);
loginResult = true;
break;
}
}
if (loginResult == true)
break;
else
System.out.println("登陆失败!请重新登录!");
}
}
public void showGood() {
System.out.println("*****商品按价格排序列表如下*****");
Collections.sort(Shop.goodList);
for (Good good : Shop.goodList) {
System.out.println(good);
}
}
public void buy() {
while (true) {
System.out.println("请选择你要购买的商品编号:");
int id = Shop.sc.nextInt();
System.out.println("您将要购买的商品信息如下:");
Good shopGood = this.findGoodById(id);
System.out.println(shopGood);
System.out.println("请输入要购买的数量:");
int num = Shop.sc.nextInt();
Good myGood = new Good();
/*myGood.setId(shopGood.getId());myGood.setName(shopGood.getName());myGood.setPrice(shopGood.getPrice());*/
myGood = shopGood.clone();
myGood.setNum(num);
Shop.myGoodList.add(myGood);
System.out.println("是否继续Y/N");
String choice = Shop.sc.next();
choice = choice.toUpperCase();
if (choice.equals("N")) {
break;
}
}
this.showMyGoodList();
}
void showMyGoodList() {
System.out.println("***您购买的商品列表如下:***");
BigDecimal total = new BigDecimal("0");
for (Good good : Shop.myGoodList) {
System.out.println(good);
BigDecimal price = good.getPrice();
int num = good.getNum();
total = total.add(price.multiply(new BigDecimal(num)));
}
System.out.println("总价格为:" + total);
}
public Good findGoodById(int id) {
Good returnGood = null;
for (Good good : Shop.goodList) {
if (good.getId() == id) {
returnGood = good;
break;
}
}
return returnGood;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
- Good.class
package com.qrst.shop;
import java.io.Serializable;
import java.math.BigDecimal;
public class Good implements Comparable, Cloneable, Serializable {
private int id;
private String name;
private BigDecimal price;
private int num;
private Object o;
public Good() {
}
public Good(int id, String name, BigDecimal price, int num) {
this.id = id;
this.name = name;
this.price = price;
this.num = num;
}
@Override
public Good clone() {
Good g1 = new Good();
try {
Object o = new Object();
g1 = (Good) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return g1;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String toString() {
return "id=" + id + ",name=" + name + ",price=" + price + ",num=" + num;
}
/*按价格排*/
@Override
public int compareTo(Object o) {
Good good = (Good) o;
return this.price.compareTo(good.price);
}
}
六.运行结果:
- 写在最后:
这是学校上课时老师们要求搞得作业项目,很水。
留个痕迹喽~~