基于字符界面设计。主菜单包括:展示商品列表、添加商品、修改商品、删除商品、查询商品、退出。
先放上代码段,代码段后我会放上图文注释。
PartA类
package ShangPinSystem;
public class PartA {
private String DDid;
private String DDname;
private String DDprice;
private int DDhow;
private double DDall;
private boolean DDbad;
public String getDDid() {
return DDid;
}
public void setDDid(String dDid) {
DDid = dDid;
}
public String getDDname() {
return DDname;
}
public void setDDname(String dDname) {
DDname = dDname;
}
public String getDDprice() {
return DDprice;
}
public void setDDprice(String dDprice) {
DDprice = dDprice;
}
public int getDDhow() {
return DDhow;
}
public void setDDhow(int dDhow) {
DDhow = dDhow;
}
public double getDDall() {
return DDall;
}
public void setDDall(double dDall) {
DDall = dDall;
}
public boolean isDDbad() {
return DDbad;
}
public void setDDbad(boolean dDbad) {
DDbad = dDbad;
}
public PartA(String dDid, String dDname, String dDprice, int dDhow, double dDall, boolean dDbad) {
super();
DDid = dDid;
DDname = dDname;
DDprice = dDprice;
DDhow = dDhow;
DDall = dDall;
DDbad = dDbad;
}
public PartA() {
super();
}
@Override
public String toString() {
return "商品信息:"+DDid+","+DDname+","+DDprice+","+DDhow+","+DDall+","+DDbad;
}
}
PartB类
package ShangPinSystem;
import java.util.List;
public class PartB {
public static boolean add(PartA d, List<PartA> dds) {
boolean f = true;
for (PartA dingdan : dds) {
if (d.getDDid().equals(dingdan.getDDid())) {
f = false;
break;
}
}
if (f)
dds.add(d);
return f;
}
public static boolean modify(PartA d, List<PartA> dds) {
boolean f = false;
for (PartA dingdan : dds) {
if (d.getDDid().equals(dingdan.getDDid())) {
f=true;
dingdan.setDDname(d.getDDname());
dingdan.setDDprice(d.getDDprice());
dingdan.setDDhow(d.getDDhow());
dingdan.setDDall(d.getDDall());
dingdan.setDDbad(d.isDDbad());
break;
}
}
return f;
}
public static boolean delete(String id, List<PartA> dds) {
boolean f = false;
for (PartA dingdan : dds) {
if (dingdan.getDDid().equals(id)) {
dds.remove(dingdan);
f=true;
break;
}
}
return f;
}
public static PartA select(String id, List<PartA> dds) {
PartA p = null;
for (PartA dingdan : dds) {
if (dingdan.getDDid().equals(id)) {
p = dingdan;
break;
}
}
return p;
}
public static void show(List<PartA> dds) {
for (PartA dingdan : dds) {
System.out.println(dingdan);
}
}
}
PartC类
package ShangPinSystem;
public class PartC {
private String DDsolId;
private String DDsoldName;
private int DDsoldHow;
public String getDDsolId() {
return DDsolId;
}
public void setDDsolId(String dDsolId) {
DDsolId = dDsolId;
}
public String getDDsoldName() {
return DDsoldName;
}
public void setDDsoldName(String dDsoldName) {
DDsoldName = dDsoldName;
}
public int getDDsoldHow() {
return DDsoldHow;
}
public void setDDsoldHow(int dDsoldHow) {
DDsoldHow = dDsoldHow;
}
public PartC(String dDsolId, String dDsoldName, int dDsoldHow) {
super();
DDsolId = dDsolId;
DDsoldName = dDsoldName;
DDsoldHow = dDsoldHow;
}
public PartC() {
super();
}
@Override
public String toString() {
return "已卖出:"+DDsolId+","+DDsoldName+","+DDsoldHow;
}
}
PartD类
package ShangPinSystem;
import java.util.List;
public class PartD {
public static boolean add2(PartC d2, List<PartC> dds2) {
boolean f = true;
for (PartC dingdan2 : dds2) {
if (d2.getDDsolId().equals(dingdan2.getDDsolId())) {
f = false;
break;
}
}
if (f)
dds2.add(d2);
return f;
}
public static boolean modify2(PartC d2, List<PartC> dds2) {
boolean f = false;
for (PartC dingdan2 : dds2) {
if (d2.getDDsolId().equals(dingdan2.getDDsolId())) {
f=true;
dingdan2.setDDsolId(d2.getDDsolId());
dingdan2.setDDsoldName(d2.getDDsoldName());
dingdan2.setDDsoldHow(d2.getDDsoldHow());
break;
}
}
return f;
}
public static boolean delete2(String id2, List<PartC> dds2) {
boolean f = false;
for (PartC dingdan2 : dds2) {
if (dingdan2.getDDsolId().equals(id2)) {
dds2.remove(dingdan2);
f=true;
break;
}
}
return f;
}
public static void show2(List<PartC> dds2) {
for (PartC dingdang2 : dds2) {
System.out.println(dingdang2);
}
}
}
App类
package ShangPinSystem;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class App {
static List<PartA> dds = new ArrayList<PartA>();
static List<PartC> dds2 = new ArrayList<PartC>();
static Scanner read = new Scanner(System.in);
public static void main(String[] args) {
dds.add(new PartA("01", "饼干", "13.00", 50, 650, true));
dds.add(new PartA("02", "汽水", "03.00", 50, 150, false));
dds.add(new PartA("03", "香烟", "10.00", 50, 500, false));
dds2.add(new PartC("01","饼干", 10));
dds2.add(new PartC("02","汽水",20));
dds2.add(new PartC("03","香烟",50));
int n = 0;
int how;
int soldHow;
String id, name, price;
String soldID,soldName;
double all;
boolean f;
boolean bad;
PartA p;
PartC p2;
do {
System.out.println("欢迎使用229商品管理系统");
System.out.println("-----添加商品请输入:1");
System.out.println("-----修改商品请输入:2");
System.out.println("-----删除商品请输入:3");
System.out.println("-----查询商品请输入:4");
System.out.println("展示所有商品列表请输入:5");
System.out.println("添加商品卖出状态请输入:6");
System.out.println("修改商品卖出状态请输入:7");
System.out.println("删除商品卖出状态请输入:8");
System.out.println("展示商品卖出状态请输入:9");
System.out.println("退出:0");
System.out.print("请选择:");
n = read.nextInt();
switch (n) {
case 1:
System.out.println("请输入新商品的订单号:");
id = read.next();
System.out.println("请输入新商品的商品名:");
name = read.next();
System.out.println("请输入新商品的商品价格:");
price = read.next();
System.out.println("请输入新商品的商品数量:");
how = read.nextInt();
System.out.println("请输入新商品的商品总价:");
all = read.nextDouble();
System.out.println("请输入新商品的商品是否有损坏:");
bad = read.nextBoolean();
p = new PartA(id, name, price, how, all, bad);
f = PartB.add(p, dds);
if (f)
System.out.println("添加成功");
else
System.out.println("添加失败");
break;
case 2:
System.out.println("请输入要修改的商品订单号:");
id = read.next();
System.out.println("请输入要修改的商品名:");
name = read.next();
System.out.println("请输入要修改的商品价格:");
price = read.next();
System.out.println("请输入要修改的商品数量:");
how = read.nextInt();
System.out.println("请输入要修改的商品总价:");
all = read.nextDouble();
System.out.println("请输入要修改的商品是否有损坏:");
bad = read.nextBoolean();
p = new PartA(id, name, price, how, all, bad);
f = PartB.modify(p, dds);
if (f)
System.out.println("修改成功");
else
System.out.println("修改失败");
break;
case 3:
System.out.println("请输入要删除的商品订单号:");
id = read.next();
f = PartB.delete(id, dds);
if (f)
System.out.println("删除成功");
else
System.out.println("删除失败");
break;
case 4:
System.out.println("请输入要查询的商品订单号:");
id = read.next();
p = PartB.select(id, dds);
if (p != null)
System.out.println(p);
else
System.out.println("没有此商品");
break;
case 5:
System.out.println("所有商品信息为:");
System.out.println("----订单号 商品名 价格 数量 总价 是否有损坏");
PartB.show(dds);
break;
case 6:
System.out.println("请输入新卖出商品的订单号:");
soldID = read.next();
System.out.println("请输入新卖出商品的商品名:");
soldName = read.next();
System.out.println("请输入新卖出商品的数量:");
soldHow = read.nextInt();
p2 = new PartC(soldID,soldName,soldHow);
f = PartD.add2(p2, dds2);
if (f)
System.out.println("添加成功");
else
System.out.println("添加失败");
break;
case 7:
System.out.println("请输入要修改卖出商品的订单号:");
soldID = read.next();
System.out.println("请输入要修改卖出商品的商品名:");
soldName = read.next();
System.out.println("请输要修改卖出商品的数量:");
soldHow = read.nextInt();
p2 = new PartC(soldID,soldName,soldHow);
f = PartD.modify2(p2, dds2);
if (f)
System.out.println("修改成功");
else
System.out.println("修改失败");
break;
case 8:
System.out.println("请输入要删除的卖出商品订单号:");
soldID = read.next();
f = PartD.delete2(soldID, dds2);
if (f)
System.out.println("删除成功");
else
System.out.println("删除失败");
break;
case 9:
System.out.println("已卖出:");
System.out.println("---订单号 商品名 数量");
PartD.show2(dds2);
break;
}
} while (n != 0);
}
}
因为老师要求代码段300行,所以我加上了商品卖出系列功能来凑数(其实我算过不加这个功能刚好可以到300行,但是又觉得刚好300行可能有点尴尬,所以还是加了这个功能,嘿嘿)
图示
功能展示图
因为商品卖出状态系列功能与商品系列功能代码段大致相同,所以我就不整图了嗷。