团队介绍
成员姓名 | 负责任务 |
文才学 | 代码(商品类,商城类),博客,前期调查 |
刘新臻 | 代码(购物车类),UML类图,代码规范 |
前期调查
调查总结:
代码中仅实现部分功能。
UML类图
系统功能描述
用户进入商城,浏览商城内所有商品,选择所需商品查看该商品具体信息,随后选择是否将该商品加入购物车。在购物车内可对在其中的商品进行结算或者删除。
实现截图
本次实现关键代码
商品类:
package ShoppingCartSystem;
public class Commodity {// 商品类
private String name;// 商品名称
private double price;// 商品价格
int inventory;// 商品库存计数器
private String Specification;// 商品规格(颜色,大小...)
private int id;// 商品序号
static int idCount = 0;// 商品序号计数器
int amount = 1;
public Commodity() {// 静态初始化模块
this.id = ++idCount;
}
public String getName() {
return name;
}
public Commodity(String name, double price, int inventory, String specification, int id) {
super();
this.name = name;
this.price = price;
this.inventory = inventory;
Specification = specification;
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getSpecification() {
return Specification;
}
public void setSpecification(String specification) {
Specification = specification;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public static int getIdCount() {
return idCount;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public static void setIdCount(int idCount) {
Commodity.idCount = idCount;
}
@Override
public String toString() {
return "Commodity [name=" + name + ", price=" + price + "元" + ", inventory=" + inventory + ", Specification="
+ Specification + ", id=" + id + "]";
}
}
商城类:
package ShoppingCartSystem;
import java.util.ArrayList;
import java.util.Scanner;
public class Mall {// 商城类
static {
System.out.println("欢迎来到集美商城");
}
public static void showMall(ArrayList<Commodity> commodities) {// 商城主页显示
System.out.println("商城内的商品有:");
System.out.println("===============");
for (int i = 0; i < commodities.size(); i++) {
Commodity commodity = new Commodity();
commodity = commodities.get(i);
// System.out.println(commodity.toString());
System.out.println(commodity.getName() + " " + commodity.getPrice() + "元" + " " + "序号:" + commodity.getId());
}
System.out.println("===============");
System.out.println("请选择你的操作");
System.out.println("1.查找商品");
System.out.println("2.查看购物车");
System.out.println("3.退出商城");
}
public static void SearchCommody(Cart cart, int index, ArrayList<Commodity> commodities) {// 显示选择查看的商品信息
if (index >= commodities.size()) {
System.out.println("输入错误");
showMall(commodities);
jiemian(cart, commodities);
} else {
System.out.println(commodities.get(index));
}
}
public static void jiemian(Cart cart, ArrayList<Commodity> commodities) {
Scanner sc1 = new Scanner(System.in);
int n = sc1.nextInt();// 输入操作选择
if (n == 1) {
System.out.println("请选择你要查看第几个商品(0号开始):");
int index = sc1.nextInt();// 输入需要查看的商品索引值
SearchCommody(cart, index, commodities);// 查找需要查看的商品
System.out.println("是否需要加入购物车?(Yes/No)");
String answer = sc1.next();// 输入是否加入购物车
if (answer.equals("Yes")) {
Commodity commodity = new Commodity();
commodity = commodities.get(index);
cart.addToCart(commodity);
System.out.println("添加购物车成功");
System.out.println();
if (commodity.getId() == index) {// 库存减一
commodity.inventory--;
}
showMall(commodities);
jiemian(cart, commodities);
} else if (answer.equals("No")) {
showMall(commodities);
jiemian(cart, commodities);
} else {
System.out.println("输入错误");
showMall(commodities);
jiemian(cart, commodities);
}
} else if (n == 2) {// 打开购物车,显示购物车清单
cart.print();
System.out.println("请选择你的操作");
System.out.println("1.返回商城");
System.out.println("2.继续操作购物车");
int n1 = sc1.nextInt();
if (n1 == 1) {
showMall(commodities);
jiemian(cart, commodities);
} else if (n1 == 2) {
System.out.println("请选择你对购物车的操作");
System.out.println("1.删除购物车商品");
System.out.println("2.结算");
int n2 = sc1.nextInt();
if (n2 == 1) {
System.out.println("请输入需要删除的商品名称");
String n3 = sc1.next();
for (Commodity e1 : commodities) {
if (e1.getName().equalsIgnoreCase(n3)) {
e1.inventory++;
cart.delete(e1);
System.out.println("删除成功");
break;
}
}
showMall(commodities);
jiemian(cart, commodities);
} else if (n2 == 2) {
double sum = cart.sum();
System.out.println("总计:" + sum);
System.exit(0);
}
}
} else if (n == 3) {
System.exit(0);
} else {
System.out.println("输入错误");
showMall(commodities);
jiemian(cart, commodities);
}
sc1.close();
}
}
购物车类:
package ShoppingCartSystem;
import java.util.ArrayList;
public class Cart {// 购物车类
ArrayList<Commodity> shopList = new ArrayList<>();// 购物车清单
public Cart() {
super();
}
public void print() {
if (shopList.isEmpty()) {
System.out.println("购物车为空");
}
else {
for (Commodity e : shopList) {
System.out.println(e.getName() + " " + e.getPrice() + "元" + " " + "序号:" + e.getId() + " " + "数量" + e.amount);
}
}
}
public void addToCart(Commodity c)// 添加商品
{
int flag = 0;
if (shopList.isEmpty()) {
shopList.add(c);
} else {
for (Commodity e2 : shopList) {
if (e2.getName().contains(c.getName()))//
{
e2.amount++;
flag++;
break;
}
}
if (flag == 0) {
shopList.add(c);
}
}
}
public void delete(Commodity a)// 删除商品
{
int flag = 0;
if (shopList.isEmpty()) {
System.out.println("购物车为空");
}
else {
if (a.amount == 1) {
shopList.remove(a);
flag++;
}
}
if (flag == 0) {
a.amount--;
}
}
public double sum()// 总计
{
double num = 0;
for (Commodity e : shopList) {
num += (double) e.getPrice() * e.amount;
}
return num;
}
}