小组成员

成员姓名 任务分配
郑梦露 功能设计
李悦 代码规范
孙玮羚 UML类实现
农艳洁 前期调查及制作博客

前期调查

UML类图

Git代码

https://gitee.com/xianer-bian/dashboard/projects?scope=public&&sort=

代码展示

ShoppingCartDao接口

  • 包含对购物车中商品处理的各种方法,同时还有购物车操作的方法,方便购物车的操作同时为代码的可扩展性做了提升。
package ShoppingDao;

import java.util.ArrayList;

import Shopping.Commodity;
import Shopping.ItemEntry;

public interface ShoppingCartDao {
	public void addCommodity(Commodity e,int count); //将商品加入购物车
	public boolean removeCommodity(Integer id,int count);//将商品从购物车
	public void diplayAll();               //输出购物车中的所有商品
	public boolean isEmpty();                        //判断购物车是否为空
	public ItemEntry findById(Integer id);             //通过商品编号在购物车中查找
	public void clear() ;                            //清空购物车
	public double getTotalPrice();                       //计算购物车总额 
	public ArrayList<ItemEntry> getOrders();      //将购物车中的每条商品都生成订单
	
}


购物车类

  • 添加商品到购物车
  • 删除购物车中的商品
  • 在购物车中查找商品
  • 展示购物车所有商品
  • 结算购物车
  • 清空购物车
  • 生成订单

package Shopping;
import java.util.*;

import ShoppingDao.ShoppingCartDao;
/**
 * 购物车类
 * @author 86182
 *
 */
public class ShoppingCartDaoSetImpl implements ShoppingCartDao{
	private Set<ItemEntry> itemList; //商品条目

	public ShoppingCartDaoSetImpl(){   //创建集合保存商品条目
		itemList = new HashSet<>();
	}
	public void addCommodity(Commodity e,int count){ //将商品加入购物车
		if (e == null){
			return ;
		}
		if(e.getStock()<count) {                    //商品库存不足
			System.out.println("库存不足,添加失败!");
			return;
		}
		ItemEntry x = findById(e.getId());         
		if (x == null){                            //如果不包含该商品的条目
			itemList.add(new ItemEntry(e,count));  
		}
		else{                                       //如果包含该商品条目
			if(x.getCount()+count <= x.getCommodity().getStock())  //若添加后购物车商品总数量小于商场中商品数量
			{
				x.increase(count);                
				System.out.println("添加成功!");
			}
			else {                                      //若添加后购物车商品总数量大于商场中商品数量
				System.out.println("添加后的商品数量不可以超过其库存!!");
			}
		}
		
	}
	
	public boolean removeCommodity(Integer id,int count){  //删除购物车中的商品
		if (id==null)
			return false;
		ItemEntry e = findById(id);
		
		if (e == null){       //未找到
			System.out.println("删除失败,没有找到该商品!");
			return false;
		}
		else{
			if (e.getCount() <= count){ //若商品数量少于删除数量,直接删除条目
				itemList.remove(e);
			}
			else{
				e.decrease(count);
			}
		}
		
		return true;
	}
	 
	public double getTotalPrice() {                       //计算购物车总额             
		this.diplayAll();
		double totalPrice = 0.0;
		Iterator<ItemEntry> it = itemList.iterator();
		while(it.hasNext()) {
			ItemEntry x = it.next();
			totalPrice += x.getCommodity().getPrice()*x.getCount();
		}
		this.clear();
		return totalPrice;
	}
	
	public ArrayList<ItemEntry> getOrders(){       //将购物车中的每条商品都生成订单
		String order = null;
		ArrayList<ItemEntry> orderlist=new ArrayList<>();
		Iterator<ItemEntry> its = itemList.iterator();
		while (its.hasNext()) {
			ItemEntry x = its.next();
			Commodity e = x.getCommodity();
			e.setStock(e.getStock()-x.getCount());
			orderlist.add(x);
		}
		
		return orderlist;
	}
	
	public void diplayAll(){                //输出购物车中的所有商品
		if(this.isEmpty()==true)
		{
			System.out.println("购物车为空!!");
			return;
		}
		Iterator<ItemEntry> it = itemList.iterator();
		while(it.hasNext()) {
			ItemEntry x = it.next();
			System.out.println(x);
		}
		return;
	}
	
	public boolean isEmpty() {                         //判断购物车是否为空
		if(this.itemList.size()==0)
			return true;
		else
			return false;
	}
	
	public ItemEntry findById(Integer id){             //通过商品编号在购物车中查找
		Iterator <ItemEntry>it = itemList.iterator();
		while(it.hasNext()) {
			ItemEntry e = it.next();
			if(e.getCommodity().getId().equals(id)==true)
				return e;
		}
		return null;
	}
	
	public void clear() {                            //清空购物车
		itemList.clear();
	}
	
	
}

MarketDao接口

package ShoppingDao;

import Shopping.Commodity;

public interface MarketDao {
	public boolean addCommodity(Commodity c);  //上架商品
	public boolean removeCommodity(Integer id);//下架商品
	public void diplayAll();                   //输出所有商品
	public Commodity findById(Integer id) ;     //通过编号寻找商品
	public void clear() ;                      //清空所有商品
	public boolean isEmpty();                  
}

商场类

  • 实现商品的上架与下架
  • 展示所有商品条目
  • 搜索商品

package Shopping;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import ShoppingDao.MarketDao;

/**
 * 商场类
 *
 */
public class MarketDaoSetImpl implements MarketDao {
	private Set<Commodity> commodityList;
	
	public MarketDaoSetImpl() {
		commodityList = new HashSet<Commodity>();
	}
	
	public boolean addCommodity(Commodity c){ //将商品e加入商场
		if(commodityList.add(c)){
			return true;
		}
		else 
			return false;
	}
	
	public boolean removeCommodity(Integer id){  //下架商场中的商品
		if (id==null)
			return false;
		Commodity e = findById(id);
		if (e == null){                 //未找到
			return false;
		}
		else{
			commodityList.remove(e);//删除商品
		}
		return true;
	}
	
	public void diplayAll(){ //输出的所有商品
		if(commodityList.isEmpty() == true)
			System.out.println("商城是空的!");
		Iterator<Commodity>it = commodityList.iterator();
		while(it.hasNext()) {
			Commodity e = it.next();
			System.out.println(e);
		}
	}
	
	public Commodity findById(Integer id) { //查找商场中的商品
		Iterator<Commodity>it = commodityList.iterator();
		while(it.hasNext()) {
			Commodity e = it.next();
			if(e.getId().equals(id)==true)
				return e;
		}
		return null;
	}
	public boolean isEmpty() {
		return commodityList.isEmpty();
	}
	public void clear() {    //下架所有商品
		commodityList.clear();
		System.out.println("所有商品下架成功!");
	}

}


CommodityOrdersDao接口

package ShoppingDao;

public interface CommodityOrdersDao {
	public void addOrder(String order) ;      //添加订单
	public void diplayAll();                     //输出所有订单
	public boolean isEmpty() ;  //判断订单列表是否为空
}

订单类

  • 获取订单列表
  • 展示所有订单

package Shopping;
import java.util.*;
/**
 * 商品订单
 * @author 86182
 *
 */

public class CommodityOrdersDaoArray {
	private List<String> orderList;
	private static int nextId=0;
    	
	public CommodityOrdersDaoArray() {
		orderList = new ArrayList<>();
	}
	
	public void addOrder(String order) {      //添加订单
		orderList.add("订单编号"+":"+nextId+" 订单内容: "+order);
		nextId++;
		return;
	}
	
	public void diplayAll(){                      //输出所有订单
		if(isEmpty())
			System.out.println("订单列表为空!");
		Iterator<String> it = orderList.iterator();
		while (it.hasNext()) {
			String y = it.next();
			System.out.println(y);
		}
	}
	public boolean isEmpty() {  //判断订单列表是否为空
		if(orderList.size()==0)
			return true;
		else
			return false;
	}
	public List<String> getOrderList() {         
		return orderList;
	}
	public void setOrderList(List<String> orderList) {   
		this.orderList = orderList;
	}
	
}

  • 三个接口的存在,都是为了方便我们在实现方法时因为对于对象存储方法不同时更加简便而存在,在存储使用类型不同时无需重新写方法。

菜单类

  • 实现登录界面
  • 展示用户操作选项
package Shopping;

import java.util.Scanner;

import ShoppingDao.MarketDao;

/***
 * 菜单类
 * @author 86182
 *
 */
public class Menu {
	public static void loginOrRegister() {
		System.out.println("--------------------------------------------------------------");
    	System.out.println("xx                       1. 登 入 账 号                                              xx");
    	System.out.println("xx                       2. 注 册 账 号                                              xx");
    	System.out.println("-------------------------------------------------------------");
	}
    public static void buyerOptionMenu() {//买家操作选项菜单
    	System.out.println("--------------------------------------------------------------");
    	System.out.println("xx                       1. 添 加 购 物 车 商 品                                xx");
    	System.out.println("xx                       2. 删 除 购 物 车 商 品                                xx");
    	System.out.println("xx                       3. 展 示 购 物 车 商 品                                xx");
    	System.out.println("xx                       4. 结 算 购 物 车                                         xx");
    	System.out.println("xx                       5. 查看所有订单                                          xx");
    	System.out.println("xx                       6. 修 改 账 户 密 码                                      xx");
    	System.out.println("xx                       7. 退出                                                        xx");
    	System.out.println("-------------------------------------------------------------");
    	System.out.println("请输入你的选择:");
    }
    
    public static void sellerOptionMenu() {//卖家操作选项菜单
    	System.out.println("---------------------------------------------------------------");
    	System.out.println("xx                       1. 添 加 商 城 商 品                                       xx");
    	System.out.println("xx                       2. 删 除 商城 商 品                                        xx");
    	System.out.println("xx                       3. 展 示 商 城 商 品                                       xx");
    	System.out.println("xx                       4. 查看订单                                                    xx");
    	System.out.println("xx                       5. 查找商品                                                    xx");
    	System.out.println("xx                       6. 修 改 账 户 密 码                                        xx");
    	System.out.println("xx                       7. 退 出                                                          xx");
    	System.out.println("---------------------------------------------------------------");
    	System.out.println("请输入你的选择:");
    }
    public static void getBuyerOptionChoice(int choice,Buyer buyer,MarketDao market) { //获取买家选项,进入相应操作
    	
    	Scanner sc = new Scanner(System.in);
		switch (choice) {
		case 1:buyer.addCommodity(market);break;        //添加购物车商品
    	case 2:buyer.removeCommodity();break;                                //删除购物车商品
    	case 3:buyer.getCart().diplayAll();break;                          //展示购物车所有商品
    	case 4:buyer.checkoutCart();break;                                 //结算购物车
    	case 5:buyer.getOrderList().diplayAll();break;                      //查看所有订单
    	case 6:buyer.modifyPassword();break;                               //修改账户密码
		default: 
			System.out.println("无效选项");
			break;
		}
    }
    public static void getSellerOptionChoice(int choice,Seller seller,MarketDao market) {//获取卖家选项,进入相应操作
    	int id;
    	Scanner sc = new Scanner(System.in);
		switch(choice) {
		case 1:seller.addCommodity(market);break;     //上架商品
		case 2:seller.removeCommodity(market);break;  //下架商品
		case 3:market.diplayAll();break;              //展示商品
		case 4:seller.getOrderlist().diplayAll();break; //查看订单
		case 5:                                       //输入编号查找商品
			System.out.print("请输入查找的商品编号:");
			id = Integer.parseInt(sc.nextLine());
			System.out.println(market.findById(id));
			break;
		case 6:seller.modifyPassword();break;         //修改账户密码
		default:System.out.println("无效选项");break;    //无效选项
		}
    }
}

用户类

  • 卖家类与买家类继承用户类
  • 实现买家和卖家登录注册
  • 用户修改密码
package Shopping;

import java.util.*;
import java.io.*;

public abstract class User {
	private String userName;
	private String password;
	
	public User(String userName,String password) {  
		this.userName = userName;
		this.password = password;
	}
	public void modifyPassword() {//修改密码
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入你要修改的密码:");
		String newpassword=sc.nextLine();
        this.password=newpassword;
        System.out.println("修改密码成功!");
	}
	public static User register(Map<String,User>userList) throws IOException {  //注册
		Scanner sc = new Scanner(System.in);
		User user = null;
		File filename=new File("UserList.txt");
		BufferedWriter fw = new BufferedWriter(new FileWriter(filename,true));
		String registStr;

		while(true) {
			System.out.println("请输入要创建的账号:");
			String UserName = sc.nextLine();
			if(userList.containsKey(UserName)==true) {//已经存在账户则不能注册该账户
				System.out.println("该账户已经存在!");
			}
			else              //创建新账户
			{
				System.out.println("请输入密码:");
				String password = sc.nextLine();
				System.out.println("请选择是创建商家账户或者是买家账户(商家请输入S,买家请输入B):");
				switch(sc.nextLine()) {
				case "S":
					    registStr="\n"+UserName+" "+password+" "+"Seller";
						fw.write(registStr);fw.close();
					    user = new Seller(UserName,password);
						userList.put(UserName,user);	 return user;
				case "B":
				        registStr="\n"+UserName+" "+password+" "+"Buyer";
						fw.write(registStr);fw.close();
					    user = new Buyer(UserName,password);
						userList.put(UserName,user);	 return user;
				default:System.out.println("无效输入");
				}
				//break;
			}

		}
	}
	public static User login(Map<String,User>userList) {  //用户登陆
	    Scanner sc = new Scanner(System.in);

		while(true) {			//登陆
			System.out.println("请输入你的账号:");
			String useName = sc.nextLine();
			
			if(userList.containsKey(useName)==false) { //不存在用户 
				System.out.println("不存在该用户!!");
				continue;
			}
			System.out.println("请输入你的密码:");
			String password = sc.nextLine();
			if(userList.get(useName).getPassword().equals(password)==true) {   //匹配成功
				System.out.println("登陆成功!");
				return userList.get(useName);
			}
			else {                              //匹配不成功
				System.out.println("密码错误!");
			}
		}
	}
	
	public String getUserName() {
		return this.userName;
	}
	public void setUserName(String accountNumber) {
		this.userName = accountNumber;
	}
	public String getPassword() {
		return this.password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	
}

卖家类

  • 实现上架商品和下架商品操作

package Shopping;
import java.util.*;

public class Seller extends User {
	private CommodityOrdersDaoArray  Orderlist;
	
	public Seller(String accountNumber, String password) {
		super(accountNumber, password);
		Orderlist = new CommodityOrdersDaoArray();
	}

	public void addCommodity(MarketDao market) {//上架商品
		Scanner sc = new Scanner(System.in);
		System.out.println("请按‘商品编号,名称,类别,价格,库存’按顺序输入");
		
		/*输入商品信息*/
		String []info = sc.nextLine().split(",");   
		int id = Integer.parseInt(info[0]);
		String name = info[1];
		String description = info[2];
		Double price = Double.parseDouble(info[3]);
		int count = Integer.parseInt(info[4]);
		Commodity e = new Commodity(id,name,description,price,count);
		if(market.addCommodity(e)) //上架商品
		{
			e.setSeller(this);
			System.out.println("上架成功!");
		}
		else
			System.out.println("上架失败");
	}
	
	public void removeCommodity(MarketDao market) {           //下架商品
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入需要删除的商品编号:");
		int id = Integer.parseInt(sc.nextLine());
		market.removeCommodity(id);
	}

	public CommodityOrdersDaoArray getOrderlist() {
		return Orderlist;
	}

	public void setOrderlist(CommodityOrdersDaoArray orderlist) {
		Orderlist = orderlist;
	}
	@Override
	public String toString() {
		return "Seller [accountNumber=" + this.getUseName() + "]";
	}


}

买家类

  • 删除购物车中的商品
  • 添加商品到购物车
  • 结算购物车

package Shopping;

import java.util.*;

public class Buyer extends User{
	private ShoppingCartDaoSet cart ;//购物车
	private CommodityOrdersDaoArray orderList = new CommodityOrdersDaoArray();//订单列表
	
	public Buyer(String accountNumber,String password) {
		super(accountNumber, password); 
		cart = new ShoppingCartDaoSet();
	}
	public void addCommodity(MarketDao market) {          //添加商品到购物车中
		Scanner sc = new Scanner(System.in);
		System.out.println("输入添加的商品编号:");
		int id=Integer.parseInt(sc.nextLine());
		
		
		Commodity e = market.findById(id);
		if(e == null) {
			System.out.println("商城中不存在该商品!");
		}
		else {
			System.out.println("输入添加的数量:");
			int count = Integer.parseInt(sc.nextLine());
			cart.addCommodity(e,count);
		}
	}
	
	public void removeCommodity() {                   //从购物车中删除商品
		if(cart.isEmpty()) {
			System.out.println("购物车为空!");
			return;
		}
		cart.diplayAll();
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入要删除的商品编号:");
		int id=Integer.parseInt(sc.nextLine());
		System.out.println("输入删除数量:");
		int count = Integer.parseInt(sc.nextLine());
		if(cart.removeCommodity(id,count)==false)
			System.out.println("不存在该商品!");
		else
			System.out.println("删除成功!");
	}

	public void checkoutCart() {                           //结算购物车生成订单保存到订单列表中
		ArrayList<ItemEntry> orders = this.cart.getOrders();
		for(int i=0;i<orders.size();i++) {
			ItemEntry order = orders.get(i);
			String orderContent = orders.get(i)+" "+this.toString();
			this.orderList.addOrder(orderContent);
			order.getCommodity().getSeller().getOrderlist().addOrder(orderContent);
		}
		System.out.println("应付金额为:"+this.cart.getTotalPrice());
		
	}
	
	public ShoppingCartDaoSet getCart() {
		return cart;
	}

	public void setCart(ShoppingCartDaoSet cart) {
		this.cart = cart;
	}

	public CommodityOrdersDaoArray getOrderList() {
		return orderList;
	}

	public void setOrderlist(CommodityOrdersDaoArray orderlist) {
		this.orderList = orderlist;
	}
	@Override
	public String toString() {
		return "Buyer [accountNumber=" + this.getUseName() + "]";
	}
}


商品类

  • 描述商品属性(id,名称,类型,价格,库存,所属卖家)

package Shopping;

/**
 * 商品类
 * @author 86182
 *
 */
public class Commodity {
	private Integer id;//商品id
	private String name;//商品名称
	private Double price;//商品价格
	private String description;//商品类型
	private Integer stock;//商品库存
	private Seller seller;//卖家
	
	
	public Commodity() {
		
	}

	public Commodity(Integer id, String name,String description,Double price,int stock) {
		this.id = id;
		this.name = name;
		this.price = price;
		this.description = description;
		this.stock = stock;
	}

	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 getStock() {
		return stock;
	}

	public void setStock(int count) {
		this.stock = count;
	}

	

	public Seller getSeller() {
		return seller;
	}

	public void setSeller(Seller seller) {
		this.seller = seller;
	}

	public void setStock(Integer stock) {
		this.stock = stock;
	}

	@Override
	public int hashCode() {  //计算id的hashcode
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) { //只比较商品编号来保证商品编号唯一
		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 [" +seller +", id="+ id + ", name=" + name + ", price=" + price + ", description=" + description
				+ ", stock=" + stock + "]";
	}

}

商品条目类

package Shopping;

public class ItemEntry {
	private Commodity commodity;
	private int count;//数量/库存
	
	public ItemEntry() {
		
	}
	public  ItemEntry(Commodity commodity, int count) {
		this.commodity = commodity;
		this.count = count;
	}
	public void increase(int count){        //添加部分数量的商品
		this.count+=count;
	}
	
	public void decrease(int count){         //删除部分数量的商品
		this.count-=count;
	}
	
	public Commodity getCommodity() {
		return commodity;
	}
	public Integer getCount() {
		return count;
	}
	@Override
	public String toString() {
		return "ItemEntry ["+commodity + ", count=" + count + "]";
	}		
}

运行结果

用户登陆

用户修改密码


商家上架,下架商品


用户添加商品进入购物车


买家结算购物车并生成订单




删除购物车商品

posted on 2020-11-17 23:26  uhoh  阅读(145)  评论(0编辑  收藏  举报