完成一个电商系统的商品模块功能,商品类包含以下属性:商品ID,商品名,类别名,单价,库存量,产地,计量单位等信息,要求实现商品管理功能以及管理员登录功能,具体如下:
package com.softeem.homework1;
public class Test {
public static void main(String[] args) {
new UserClient().menu();
}
}
package com.softeem.homework1;
import java.util.Scanner;
public class UserClient {
private CommodityManage co = new CommodityManage();
private User user = new User("admin", "admin");
private Scanner sc;
public void menu() {
msg("===================================");
msg("=========SOFTEEM管理员登录系统==========");
msg("=========【1】管理员登陆================");
msg("=========【2】管理员修改密码=============");
msg("=========【0】退出系统=================");
msg("请输入指令:");
start();
}
private void start() {
sc = new Scanner(System.in);
int i = sc.nextInt();
switch (i) {
case 1:
load();
break;
case 2:
modifyPwd();
break;
case 0:
exit();
break;
default:
msg("请输入正确的操作!");
}
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 modifyPwd() {
msg("请输入修改后的密码:");
sc = new Scanner(System.in);
String s = sc.next();
user.setPwd(s);
msg("请重新登陆:");
load();
}
private void load() {
msg("请输入管理员信息(按以下格式:账号/密码)");
sc = new Scanner(System.in);
String s = sc.nextLine();
String[] info = s.split("/");
if (info[0].equals(user.getSno())) {
if (info[1].equals(user.getPwd())) {
enterManager();
}
msg("密码输入错误!");
}
// msg("请输入有效账号!");
menu();
}
private void enterManager() {
msg("===================================");
msg("=========SOFTEEM管理员操作界面==========");
msg("=========【1】商品添加=================");
msg("=========【2】商品列表=================");
msg("=========【3】查询指定id的商品===========");
msg("=========【4】根据商品id删除商品==========");
msg("=========【5】根据id修改指定商品的价格======");
msg("=========【6】根据id修改指定商品的库存======");
msg("=========【7】根据商品类别查询所有商品=======");
msg("=========【8】根据指定价格区间查询商品信息=======");
msg("=========【0】退出登陆==================");
msg("请输入指令:");
manager();
}
private void manager() {
sc = new Scanner(System.in);
int i = sc.nextInt();
switch (i) {
case 1:
enter1();
break;
case 2:
co.list();
break;
case 3:
enter3();
break;
case 4:
enter4();
break;
case 5:
enter5();
break;
case 6:
enter6();
break;
case 7:
enter7();
break;
case 8:
enter8();
break;
case 0:
menu();
break;
default:
msg("请输入正确的操作!");
}
enterManager();
}
private void enter8() {
msg("请输入需要查询商品的价格区间");
int ss2 = sc.nextInt();
int ss3 = sc.nextInt();
if (co.findByPriceRange(ss2, ss3) != null) {
co.print(co.findByPriceRange(ss2, ss3));
}
}
private void enter7() {
msg("请输入需要查询的商品类别");
String ss1 = sc.next();
if (co.findByTypeName(ss1) != null) {
co.print(co.findByTypeName(ss1));
} else {
msg("未查询到该类商品");
}
}
private void enter6() {
msg("请输入需要修改库存的商品id和商品的库存");
int id4 = sc.nextInt();
int storeNum = sc.nextInt();
if (co.modify(id4, storeNum)) {
msg("修改成功!");
} else {
System.out.println("未查询到该商品!");
}
}
private void enter5() {
msg("请输入需要修改价格的商品id和商品的价格");
int id3 = sc.nextInt();
double price = sc.nextDouble();
if (co.modify(id3, price)) {
msg("修改成功!");
} else {
System.out.println("未查询到该商品!");
}
}
private void enter4() {
msg("请输入需要删除的商品id");
int id2 = sc.nextInt();
if (co.delete(id2)) {
msg("删除成功!");
} else {
System.out.println("未查询到该商品!");
}
}
private void enter3() {
msg("请输入需要查询的id");
int id1 = sc.nextInt();
if (co.findById(id1) != null) {
System.out.println(co.findById(id1));
} else {
System.out.println("未查询到该商品!");
}
}
private void enter1() {
msg("请输入需要添加的商品信息,按以下格式:\n(商品id/商品名/类别名/单价/库存量/产地/计量单位)");
sc = new Scanner(System.in);
String x = sc.nextLine();
String[] ss = x.split("/");
int s1 = Integer.parseInt(ss[0]);
String s2 = ss[1];
String s3 = ss[2];
Double s4 = Double.parseDouble(ss[3]);
int s5 = Integer.parseInt(ss[4]);
String s6 = ss[5];
String s7 = ss[6];
Commodity c = new Commodity(s1, s2, s3, s4, s5, s6, s7);
if (co.add(c)) {
msg("添加成功");
}
}
private void msg(Object o) {
System.out.println(o);
}
}
package com.softeem.homework1;
/**
* 商品类
*
* @author Administrator
*
*/
public class Commodity {
private int id;
private String name;
private String typeName;
private double unitPrice;
private int storeNum;
private String place;
private String unit;
public Commodity() {
super();
}
public Commodity(int id, String name, String typeName, double unitPrice, int storeNum, String place, String unit) {
super();
this.id = id;
this.name = name;
this.typeName = typeName;
this.unitPrice = unitPrice;
this.storeNum = storeNum;
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 getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
public double getUnitPrice() {
return unitPrice;
}
public void setUnitPrice(double unitPrice) {
this.unitPrice = unitPrice;
}
public int getStoreNum() {
return storeNum;
}
public void setStoreNum(int storeNum) {
this.storeNum = storeNum;
}
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 "Commodity [id=" + id + ", name=" + name + ", typeName=" + typeName + ", unitPrice=" + unitPrice
+ ", storeNum=" + storeNum + ", place=" + place + ", unit=" + unit + "]";
}
}
package com.softeem.homework1;
import java.util.ArrayList;
public class CommodityManage {
static ArrayList list = new ArrayList();
// 商品添加
public boolean add(Commodity c) {
return list.add(c);
}
// 商品列表
public void list() {
for (Commodity c : list) {
System.out.println(c);
}
}
// 查询指定id的商品
public Commodity findById(int id) {
for (Commodity c : list) {
if (c.getId() == id) {
return c;
}
}
return null;
}
// 根据商品id删除商品
public boolean delete(int id) {
for (Commodity c : list) {
if (c.getId() == id) {
list.remove(c);
return true;
}
}
return false;
}
// 根据商品id修改指定商品的价格
public boolean modify(int id, double price) {
for (Commodity c : list) {
if (findById(id) != null) {
c.setUnitPrice(price);
return true;
}
}
return false;
}
// 根据商品id修改指定商品的库存
public boolean modify(int id, int storeNum) {
for (Commodity c : list) {
if (findById(id) != null) {
c.setStoreNum(storeNum);
return true;
}
}
return false;
}
// 根据商品类别查询所有商品
public ArrayList findByTypeName(String name) {
ArrayList arr = new ArrayList();
for (Commodity c : list) {
if (c.getTypeName().equals(name)) {
arr.add(c);
}
}
return arr;
}
// 查询指定价格区间的商品信息
public ArrayList findByPriceRange(double a, double b) {
ArrayList array = new ArrayList();
for (Commodity c : list) {
if (a < b) {
if (c.getUnitPrice() >= a && c.getUnitPrice() <= b) {
array.add(c);
}
} else if (a == b) {
if (c.getUnitPrice() == a) {
array.add(c);
}
} else {
if (c.getUnitPrice() <= a && c.getUnitPrice() >= b) {
array.add(c);
}
}
}
return array;
}
public void print(ArrayList a) {
for (Commodity c : a) {
System.out.println(c);
}
}
}
package com.softeem.homework1;
/**
* 管理员类
*
* @author Administrator
*
*/
public class User {
private String sno;
private String pwd;
public User() {
super();
}
public User(String sno, String pwd) {
super();
this.sno = sno;
this.pwd = pwd;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User [sno=" + sno + ", pwd=" + pwd + "]";
}
}