小组成员
班级 | 成员1 | 成员2 | 成员3 |
网络1912 | 黄雨冰 | 陈璧君 | 甘梨梨 |
GITEE仓
地址:https://gitee.com/huang-yubing/javacode
- 小组分工:
- 黄雨冰:前期调查,代码实现
- 陈璧君:代码规范,代码实现
- 甘梨梨:UML类图,代码实现
前期调查
系统功能结构图
流程图
UML类图
运行效果
- 商城
- 加购商品
- 进入购物车结算
- 删除商品
- 删除一个
结算验证 少了5元 成功删除一个 - 删除全部
结算验证 总价为0 全部删除选择的商品
DAO
- 购物车DAO
public interface CartDao {
public boolean add(Commodity e,int count);//加入购物车
public boolean remove(Integer id);//将商品移除
public void diplayAll();//输出购物车内商品条目
public double checkout();//输出总金额
public boolean delete(Integer id);//删除条目
}
- 货架DAO
public interface ShelfDao {
public void addCommodity(Commodity e);//添加商品
public boolean deleteCommodity(Commodity e);//删除商品
public void showCommodity();//展示货架上的商品
public Commodity getItemById(Integer id);//根据id找商品
}
关键代码
商品类
- 以商品Commodity作为父类,其子类分别有食品类,书籍类以及数码类。
/**
* 商品类
* @author
*
*/
public class Commodity {
private Integer id;//编号
private String name;//名字
private Double price;//价格
private String description;//类别
private int inventory;//库存
public Commodity() {
}
public Commodity(Integer id, String name,Double price) {
this.id = id;
this.name = name;
this.price=price;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getInventory() {
return inventory;
}
public void setInventory(int inventory) {
this.inventory = inventory;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) { //只比较id,id相同则说明两个商品相同
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Commodity other = (Commodity) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
@Override
public String toString() {
return "Commodity [id=" + id + ", name=" + name + ", price=" + price + ", description=" + description + "]";
}
}
/**
* 食品类
* @author 98295
*
*/
class Food extends Commodity{
private String shelfLife;//保质期
private String date="生产日期见包装";//生产日期见包装
//private String information;//其他信息
/**
* 构造函数
*/
public Food() {
}
public Food(Integer id, String name,Double price,String shelfLife,int inventory) {
this.setId(id);
this.setName(name);
this.setPrice(price);
this.setDescription("Food");
this.setInventory(inventory);
this.shelfLife=shelfLife;
}
public String getShelfLife() {
return shelfLife;
}
public void setShelfLife(String shelfLife) {
this.shelfLife = shelfLife;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
@Override
public String toString() {
return "Food [id: " + this.getId() + ", name: " + this.getName() + ", price: " + this.getPrice() + ", shelfLife: " + shelfLife + ", date: " + date+", inventory: "+this.getInventory();
}
}
/**
* 书籍类
* @author 98295
*
*/
class Book extends Commodity{
private String author;//作者
private String press;//出版社
public Book() {
}
public Book(Integer id, String name,Double price,String author,String press,int inventory) {
this.setId(id);
this.setName(name);
this.setPrice(price);
this.setDescription("Book");
this.setInventory(inventory);
this.author=author;
this.press=press;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPress() {
return press;
}
public void setPress(String press) {
this.press = press;
}
@Override
public String toString() {
return "Book [id: " + this.getId() + ", name: " + this.getName() + ", price: " + this.getPrice() +", author: " + author + ", press: " + press+", inventory: "+this.getInventory() ;
}
}
/**
* 电子产品类
* @author 98295
*
*/
class Electronics extends Commodity{
private String manufacturer;//厂家
private String productData;//产品数据
public Electronics() {
}
public Electronics(Integer id, String name,Double price,String manufacturer,String productData,int inventory) {
this.setId(id);
this.setName(name);
this.setPrice(price);
this.setDescription("Electronics");
this.setInventory(inventory);
this.manufacturer=manufacturer;
this.productData=productData;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public String getProductData() {
return productData;
}
public void setProductData(String productData) {
this.productData = productData;
}
@Override
public String toString() {
return "Electronics [id: " + this.getId() + ", name: " + this.getName() + ", price: " + this.getPrice() +", manufacturer: " + manufacturer + ", productData: " + productData+", inventory: "+this.getInventory();
}
}
购物车类
- 对购物车内的商品进行增加,输出,移除,结算。
package shopping;
import java.util.ArrayList;
import java.util.List;
/**
* 购物车接口的实现
*
* @author
*
*/
public class CartDaoImpI implements CartDao {
private ArrayList<ItemEntry> itemList;
public CartDaoImpI() {
itemList = new ArrayList<>();
}
/**
* 结算
*
* @return 返回购物车中所有商品的总价
*/
public double checkout() {
double x = 0;
for (ItemEntry item : itemList) {
x = x + item.getItem().getPrice() * item.getQty();// 价格*数量
}
return x;
}
/**
* 将商品加入购物车
* @param e
* @param count
* @return
*/
public boolean add(Commodity e,int count) {
if (e == null) {
return false;
}
int index = findById(e.getId());
if (index == -1) {// 如果不包含该商品的条目
itemList.add(new ItemEntry(e,count));
} else {
itemList.get(index).increase();
}
return true;
}
/**
* 从购物车删除1个该商品
*
* @param id
* @return 如果id不存在,返回false;
*/
public boolean remove(Integer id) {
if (id == null) {
System.out.println("商品不存在!");
return false;
}
int index = findById(id);
if (index == -1) {// 未找到
System.out.println("商品不存在!");
return false;
} else {
ItemEntry entry = itemList.get(index);
if (entry.getQty() <= 1) {// 移除相关条目qty<=0,则删除条目
itemList.remove(index);
} else {
entry.decrease();
}
}
System.out.println("商品删除成功!!");
return true;
}
/**
* 删除条目
* @param id
* @return
*/
public boolean delete(Integer id) {
if (id == null) {
System.out.println("商品不存在!");
return false;
}
int index = findById(id);
if (index == -1) {// 未找到
System.out.println("商品不存在!");
return false;
} else {
// 删除条目
itemList.remove(index);
}
System.out.println("商品删除成功!!");
return true;
}
/**
* 展示购物车
*/
public void diplayAll() {
for (ItemEntry itemEntry : itemList) {
System.out.println(itemEntry);
}
}
/**
* 通过id在购物车中查找是否已存在相关条目。 声明为private,因为只在本类中使用。
*
* @param id
* @return 如果包含返回相关条目所在下标,否则返回-1
*/
private int findById(Integer id) {
for (int i = 0; i < itemList.size(); i++) {
if (itemList.get(i).getItem().getId().equals(id))
return i;
}
return -1;
}
private class ItemEntry { // 内部类:购物车条目类
/**
* 商品
*/
Commodity item;
/**
* 商品数量
*/
Integer qty;
public ItemEntry(Commodity item,int qty) {
this.item = item;
this.qty=qty;
}
// 添加
public void increase() {
qty++;
}
// 减少
public void decrease() {
qty--;
}
public Commodity getItem() {
return item;
}
public Integer getQty() {
return qty;
}
@Override
public String toString() {
return item + ", qty=" + qty + "]";
}
}
}
货架商城类
- 对商城卖家进行商品的添加,删除以及展示。
/**
* 实现货架接口(上架商品)
*
* @author 98295
*
*/
public class ShelfDaoImpI implements ShelfDao {
private ArrayList<Commodity> shelfList;// 货架
public ShelfDaoImpI() {
shelfList=new ArrayList<>();
}
public void addCommodity(Commodity e) {
// 添加商品
if (e == null) {
return;
}
int index = findById(e.getId());
if (index == -1) {// 如果该商品编号不存在,则需创建新的商品
shelfList.add(e);
} else {// 存在则增加库存
shelfList.get(index).setInventory(shelfList.get(index).getInventory() + e.getInventory());
}
}
/**
* 根据id找下标
* @param id
* @return 商品的下标
*/
private int findById(Integer id) {
for (int i = 0; i < shelfList.size(); i++) {
if (shelfList.get(i).getId().equals(id))
return i;
}
return -1;
}
public boolean deleteCommodity(Commodity e) {
// 删除商品,下架
if (e.getId() == null)
return false;
int index = findById(e.getId());
if (index == -1) {// 未找到
return false;
} else {
shelfList.remove(index);
}
return true;
}
public void showCommodity() {
// 展示货架上的商品
System.out.println(" 商城");
System.out.println("Food:");
for(Commodity x:shelfList) {
if(x.getDescription()=="Food")
System.out.println(x);
}
System.out.println("Book:");
for(Commodity x:shelfList) {
if(x.getDescription()=="Book")
System.out.println(x);
}
System.out.println("Electronics:");
for(Commodity x:shelfList) {
if(x.getDescription()=="Electronics")
System.out.println(x);
}
}
/**
* 根据id找商品
*
* @param id
* @return 商品
*/
public Commodity getItemById(Integer id) {
if (id == null)
return null;
int index = findById(id);
if (index == -1) {// 未找到
return null;
} else {
return shelfList.get(index);
}
}
}
菜单
package shopping;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
/**
* 读入数据:商品编号/商品名称/价格/具体数据/库存
*/
Commodity x1 = new Food(1, "apple", 13.0, "30days", 20);
Commodity x2 = new Book(3, "LittlePrince", 130.0, "Unknown", "Xinhua", 10);
Commodity x3 = new Electronics(21, "Iphone", 4999.0, "USA", "128G 8G", 3);
Commodity x4 = new Food(1, "apple", 13.0, "30days", 20);//累加
Commodity x5 = new Food(34, "Milk tea", 15.0, "2h", 20);
Commodity x6 = new Book(311, "Anna Karenina", 100.0, "Leo Tolstoy", "Xinhua", 150);
Commodity x7 = new Food(433, "ice cream", 5.0, "20min", 100);
CartDao cart = new CartDaoImpI();// 根据需求选择
ShelfDao shelf = new ShelfDaoImpI();
// 加入货架
shelf.addCommodity(x1);
shelf.addCommodity(x2);
shelf.addCommodity(x3);
shelf.addCommodity(x4);
shelf.addCommodity(x5);
shelf.addCommodity(x6);
shelf.addCommodity(x7);
int choice;
while (true) {
shelf.showCommodity();// 展示货架内的商品
System.out.println("请选择:");
System.out.println("1.加购商品 / 2.进入购物车 / 其他按键:退出");
choice = sc.nextInt();
if (choice == 1) {// 添加商品到购物车
System.out.println("请选择你要加入购物车的商品id:");
int input = sc.nextInt();
if(shelf.getItemById(input)==null) {
System.out.println("商品不存在!");
continue;
}
System.out.println("请输入你要加购的数量:");
int n = sc.nextInt();
if(n>shelf.getItemById(input).getInventory())
{
System.out.println("库存不足!!!");
}else {
cart.add(shelf.getItemById(input),n);
}
} else if (choice == 2) {// 进入购物车
while (true) {
cart.diplayAll();// 展示购物车
System.out.println("请选择:");
System.out.println("1.删除一个商品 / 2.删除一整个商品的条目 / 3.结算 / 其他按键:退出购物车");
int nextChoice = sc.nextInt();
switch (nextChoice) {
case 1:
/* 删除-数量减少 */
System.out.println("请输入需要减少一个数量的商品的id:");
int in = sc.nextInt();
cart.remove(in);
break;
case 2:
/* 删除-移除一个条目 */
System.out.println("请输入需要移除的商品的id");
int put = sc.nextInt();
cart.delete(put);
break;
case 3:
System.out.println("总金额为:" + cart.checkout());
break;
default:
break;
}
if(nextChoice!=1&&nextChoice!=2&&nextChoice!=3) {
break;
}
}
} else {
break;
}
}
}
}
GUI界面
系统运行总界面
登陆界面
加购商品界面
删除商品并结算界面
具体代码详见git