Java实现简单的电商管理系统
完成一个电商系统的商品模块功能,商品类包含以下属性:商品ID,商品名,类别名,单价,库存量,产地,计量单位等信息,要求实现商品管理功能以及管理员登录功能,
具体如下:
- 管理员登录(账号密码固定admin/admin)
- 修改管理员密码
- 商品添加
- 商品列表
- 查询指定id的商品
- 根据商品id删除商品
- 根据id修改指定商品的价格
- 根据id修改指定商品的库存
- 根据商品类别查询所有商品
- 查询指定价格区间的商品信息
首先对商品类和管理员类分别实现所需功能:
public class Goods {
int id;
String name;
String type;
double price;
int num;
String place;
String unit;
public Goods() {
}
public Goods(int id, String name, String type, double price, int num, String place, String unit) {
super();
this.id = id;
this.name = name;
this.type = type;
this.price = price;
this.num = num;
this.place = place;
this.unit = unit;
}
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 String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
@Override
public String toString() {
return id + " " + name + " " + type + " " + price + " " + num + " " + place + " " + unit;
}
}
//在管理类里实现商品的增删查改
import java.util.ArrayList;
public class GoodsManager {
//调用ArrayList简化操作
static ArrayList<Goods> list = new ArrayList<>();
public void addGoods(Goods d) {
list.add(d);
}
public ArrayList<Goods> findAll() {
return list;
}
public Goods findById(int id) {
Goods goods = null;
for (Goods e : list) {
if (e.getId() == id) {
goods = e;
}
}
return goods;
}
public boolean deleteById(int id) {
Goods goods = findById(id);
if (goods != null) {
return list.remove(goods);
}
return false;
}
public boolean modifyPriceById(int id1, double price1) {
Goods goods = findById(id1);
if (goods != null) {
goods.setPrice(price1);
return true;
}
return false;
}
public boolean modifyNumById(int id, int num) {
Goods goods = findById(id);
if (goods != null) {
goods.setNum(num);
return true;
}
return false;
}
public ArrayList<Goods> findByType(String type) {
ArrayList<Goods> goods1 = new ArrayList<>();
for (Goods e : list) {
if (e.getType().equals(type)) {
goods1.add(e);
}
}
return goods1;
}
public ArrayList<Goods> findByPriceSection(double a, double b) {
ArrayList<Goods> goods2 = new ArrayList<>();
for (Goods e : list) {
if (e.getPrice() >= a && e.getPrice() <= b) {
goods2.add(e);
}
}
return goods2;
}
}
public class User {
String name;
String password;
public User() {
}
public User(String name, String password) {
super();
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
import java.util.ArrayList;
public class UserManager {
User u = new User();
// 注册账号
static ArrayList<User> list = new ArrayList<>();
public void login(User u) {
list.add(u);
}
// 登陆账号
public void load(String name, String password) {
for (User e : list) {
if (e.getName().equals(name) && e.getPassword().equals(password)) {
System.out.println("登陆成功");
break;
}
}
}
// 修改密码
public void passwordChange(String name, String password) {
for (User e : list) {
if (e.getName().equals(name)) {
e.setPassword(password);
}
}
}
}
通过这个测试类实现用菜单的方式来完成信息的输入
import java.util.ArrayList;
import java.util.Scanner;
public class Test3 {
User u = new User();
GoodsManager gm = new GoodsManager();
UserManager um = new UserManager();
Scanner sc = new Scanner(System.in);
public void menu() {
msg("====================");
msg("=====用户注册和登陆=======");
msg("=====【1】注册=========");
msg("=====【10】登陆========");
msg("=====【11】修改密码======");
msg("=====商品管理===========");
msg("=====【2】商品添加=======");
msg("=====【3】商品列表=======");
msg("=====【4】由编号查商品=====");
msg("=====【5】由编号删商品=====");
msg("=====【6】由编号改价格======");
msg("=====【7】由编号改库存======");
msg("=====【8】由类别查商品======");
msg("=====【9】由价格区间查商品====");
msg("=====【0】退出系统=========");
msg("请输入指令");
start();
}
private void start() {
sc = new Scanner(System.in);
int i = sc.nextInt();
switch (i) {
case 10:
denglu();
break;
case 1:
zhuce();
break;
case 11:
xiugai();
break;
case 2:
add();
break;
case 3:
list();
break;
case 4:
queryById();
break;
case 5:
delById();
break;
case 6:
changeByPrice();
break;
case 7:
changeByNum();
break;
case 8:
queryByType();
break;
case 9:
queryByPriceChange();
break;
case 0:
exit();
break;
default:
msg("请输入正确指令");
break;
}
menu();
}
private void xiugai() {
sc = new Scanner(System.in);
msg("请输入您想修改的账号和新的密码");
String a = sc.nextLine();
String b = sc.nextLine();
um.passwordChange(a, b);
}
private void zhuce() {
sc = new Scanner(System.in);
msg("请输入您想要的账号密码");
String s = sc.nextLine();
String[] info = s.split("/");
if (u.getName() != null) {
msg("账号已存在,请重新输入");
add();
return;
} else {
User u = new User(info[0], info[1]);
um.login(u);
}
}
private void denglu() {
sc = new Scanner(System.in);
msg("请输入正确的账号密码");
String a = sc.nextLine();
String b = sc.nextLine();
um.load(a, b);
}
private void exit() {
sc = new Scanner(System.in);
msg("是否确定退出?(X/Y)");
String op = sc.next();
if (op.equalsIgnoreCase("y")) {
msg("再见");
System.exit(1);
}
}
private void queryByPriceChange() {
sc = new Scanner(System.in);
msg("请输入查询的价格区间");
double a = sc.nextDouble();
double b = sc.nextDouble();
ArrayList<Goods> list = gm.findByPriceSection(a, b);
msg("编号 名称 类别 价格 库存 产地 计量单位");
if (list.size() > 0) {
for (Goods e : list) {
msg(e);
}
}
}
private void queryByType() {
sc = new Scanner(System.in);
msg("请输入查询的商品类别");
String type = sc.nextLine();
ArrayList<Goods> list = gm.findByType(type);
msg("编号 名称 类别 价格 库存 产地 计量单位");
if (list.size() > 0) {
for (Goods e : list) {
msg(e);
}
}
}
private void changeByNum() {
sc = new Scanner(System.in);
msg("请输入编号");
int id = sc.nextInt();
msg("请输入需要调整的库存");
int num = sc.nextInt();
if (gm.modifyNumById(id, num)) {
msg("修改成功");
} else {
msg("修改失败");
}
}
private void changeByPrice() {
sc = new Scanner(System.in);
msg("请输入编号");
int id = sc.nextInt();
msg("请输入需要调整的价格");
double price = sc.nextDouble();
if (gm.modifyPriceById(id, price)) {
msg("修改成功");
} else {
msg("修改失败");
}
}
private void delById() {
sc = new Scanner(System.in);
int id = sc.nextInt();
if (gm.deleteById(id)) {
msg("删除成功");
} else {
msg("删除失败");
}
}
private void queryById() {
sc = new Scanner(System.in);
msg("请输入查询的编号");
int id = sc.nextInt();
Goods e = gm.findById(id);
System.out.println(e.getId() + " " + e.getName() + " " + e.getType() + " " + e.getPrice() + " " + e.getNum()
+ " " + e.getPlace() + " " + e.getUnit());
if (e == null) {
msg(id + "商品不存在");
queryById();
return;
}
}
private void list() {
msg("编号 名称 类别 价格 库存 产地 计量单位");
for (Goods e : gm.findAll()) {
msg(e);
}
}
private void add() {
msg("输入商品信息");
sc = new Scanner(System.in);
String s = sc.nextLine();
String[] info = s.split("/");
if (gm.findById(Integer.parseInt(info[0])) != null) {
msg("编号已存在,请重新输入");
add();
return;
} else {
Goods d = new Goods(Integer.parseInt(info[0]),
info[1],
info[2],
Double.parseDouble(info[3]),
Integer.parseInt(info[4]),
info[5],
info[6]);
gm.addGoods(d);
}
}
public void msg(Object ob) {
System.out.println(ob);
}
public static void main(String[] args) {
new Test3().menu();
}
}
在商品类里调用ArrayList,使得更轻松地完成信息输入,在进行增删查改方法实现时调用了当前类的方法,使代码更简洁。在测试类里通过使用switch,并通过建新方法来调用其他类的方法实现了信息的输入和其他各种操作,测试类就是把其他只进行了方法实现的类串起来了。