完成一个电商系统的商品模块功能,商品类包含以下属性:商品ID,商品名,类别名,单价,库存量,产地,计量单位等信息,要求实现商品管理功能以及管理员登录功能,具体如下:
管理员登录(账号密码固定admin/admin)
修改管理员密码
商品添加
商品列表
查询指定id的商品
根据商品id删除商品
根据id修改指定商品的价格
根据id修改指定商品的库存
根据商品类别查询所有商品
查询指定价格区间的商品信息
本代码分为四个部分:
public class Mannager {
private String account = "admin";
String password = "admin";
public boolean init(String account, String password) {
if (this.account.equals(account) && this.password.equals(password)) {
System.out.println("登录成功!!");
}
return false;
}
public void update(String password) {
this.password = password;
System.out.println("修改成功!!!");
}
}
package com.softeem.exp1;
public class Goods {
private int id;
private String gname;
private String lname;
private double price;
private int num;
private String field;// 产地
private String unit;// 计量单位
public Goods() {
}
public Goods(int id, String gname, String lname, double price, int num, String field, String unit) {
super();
this.id = id;
this.gname = gname;
this.lname = lname;
this.price = price;
this.num = num;
this.field = field;
this.unit = unit;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getGname() {
return gname;
}
public void setGname(String gname) {
this.gname = gname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
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 getField() {
return field;
}
public void setField(String field) {
this.field = field;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
@Override
public String toString() {
return "Goods [id=" + id + ", gname=" + gname + ", lname=" + lname + ", price=" + price + ", num=" + num
+ ", field=" + field + ", unit=" + unit + "]";
}
}
商品管理类(GoodsManage):
import java.util.ArrayList;
public class GoodsMannage {
static ArrayList list = new ArrayList<>();
public void add(Goods good) {
list.add(good);
}
public void list() {
for (Goods g : list) {
System.out.println(g);
}
}
public void findById(int id) {
for (Goods g : list) {
if (g != null && g.getId() == id) {
System.out.println(g);
}
}
}
public boolean delet(int id) {
for (Goods g : list) {
if (g != null && g.getId() == id) {
return list.remove(g);
}
}
return false;
}
public boolean updateByid1(int id, double price) {
for (Goods g : list) {
if (g != null && g.getId() == id) {
g.setPrice(price);
return true;
}
}
return false;
}
public boolean updateByid2(int id, int num) {
for (Goods g : list) {
if (g != null && g.getId() == id) {
g.setNum(num);
return true;
}
}
return false;
}
public ArrayList findByField(String field) {
ArrayList gds = new ArrayList<>();
for (Goods g : list) {
if (g != null && g.getField().equals(field)) {
gds.add(g);
return gds;
}
}
return gds;
}
public void findByPrice(double price1, double price2) {
for (Goods g : list) {
if (g != null && price1 <= g.getPrice() && g.getPrice() <= price2) {
System.out.println(g);
}
}
}
}
import java.util.Scanner;
public class Test {
private GoodsMannage gm = new GoodsMannage();
private Mannager m = new Mannager();
private Scanner sc;
public void menu() {
msg("=========================");
msg("=========电商系统==========");
msg("======【1】管理员登录=======");
msg("======【2】修改管理员密码====");
msg("======【3】商品添加=====");
msg("======【4】商品列表=====");
msg("======【5】查询指定id的商品===");
msg("======【6】根据商品id删除商品=====");
msg("======【7】根据id修改指定商品的价格=====");
msg("======【8】根据id修改指定商品的库存=====");
msg("======【9】根据商品类别查询所有商品======");
msg("======【10】查询指定价格区间的商品信息=====");
msg("======【0】退出系统=========");
msg("=========================");
msg("请输入操作指令:");
start();
}
private void start() {
sc = new Scanner(System.in);
int i = sc.nextInt();
switch (i) {
case 1:
init();
break;
case 2:
updept();
break;
case 3:
add();
break;
case 4:
list();
break;
case 5:
queryByid();
break;
case 6:
deletByid();
break;
case 7:
updatePriceById();
break;
case 8:
updateNumById();
break;
case 9:
queryByField();
break;
case 10:
queryByPrice();
break;
case 0:
exit();
break;
default:
msg("请输入正确的操作指令!!!");
break;
}
menu();
}
private void exit() {
sc = new Scanner(System.in);
msg("是否确定退出?(Y/N)");
String op = sc.next();
if (op.equalsIgnoreCase("y")) {
msg("谢谢使用,再见!");
System.exit(1);
}
}
private void queryByPrice() {
gm.findByPrice(20, 50);
}
private void queryByField() {
gm.findByField("饮料");
}
private void updateNumById() {
gm.updateByid2(3, 400);
}
private void updatePriceById() {
gm.updateByid1(1, 60);
}
private void deletByid() {
gm.delet(2);
}
private void queryByid() {
gm.findById(1);
}
private void list() {
gm.list();
}
private void add() {
Goods g1 = new Goods(1, "牛奶", "饮料", 50, 100, "蒙古", "箱");
Goods g2 = new Goods(2, "牙刷", "生活用品", 10, 200, "广东", "根");
Goods g3 = new Goods(3, "可乐", "饮料", 40, 400, "广东", "箱");
Goods g4 = new Goods(4, "纸巾", "生活用品", 30, 300, "上海", "提");
gm.add(g1);
gm.add(g2);
gm.add(g3);
gm.add(g4);
}
private void updept() {
m.update("123456");
}
private void init() {
if (m.init("admin", "admin"))
;
menu();
}
private void msg(Object object) {
System.out.println(object);
}
public static void main(String[] args) {
new Test().menu();
}
}