Java大作业——购物车

成员:詹毅斌,王昕,罗吉洋。

前期调查

系统功能结构图

系统描述

系统打印出所有商品,然后根据现有的商品的ID,可以将商品添加到购物车里,也可以进行删除,显示购物车里的商品信息,能清空购物车里的商品,最后结算购物车计算出总价。

UML类图

代码

1.Main类

mian方法,进入商品目录界面和进行选择操作。

package Shopping;

import java.awt.AWTException;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) throws AWTException {
		Scanner sc = new Scanner(System.in);
		int n,num,id,flat = 0;
		Mall mall = new Mall();
		Cart cart = new Cart();
		while(true) {
			mall.showGoods();
			Menu.menu();
			n = Integer.valueOf(sc.nextLine());
			switch (n) {
	        case 1:
	        	 System.out.println("请输入要增加的商品ID:");
		         id = Integer.valueOf(sc.nextLine());
		         System.out.println("请输入要增加的商品数量:");
		         num = Integer.valueOf(sc.nextLine());
		         if(id>5||id<=0)
		         {
		        	 System.out.println("不存在此商品");
		        	 System.out.println("请按任意键继续");
			         String s=sc.nextLine();
		        	 break;
		         }
		         for(int i = 0; i < num; i++) {
		        	 cart.add(mall.searchById(id));
		         }
		         System.out.println("请按任意键继续");
		         String s=sc.nextLine();
//		         Menu.clear_all();
		         break;
	        case 2:
	        	if(cart.Len()==0) {
	        		System.out.println("购物车为空");
	        		System.out.println("请按任意键继续");
		        	s=sc.nextLine();
//		        	Menu.clear_all();
	        		break;
	        	}
	            System.out.println("请输入要删除的商品ID:");
	            id = Integer.valueOf(sc.nextLine());
	            System.out.println("请输入要减少的商品数量:");
	            num = Integer.valueOf(sc.nextLine());
	            if(cart.find(id)==0) {
	            	System.out.println("请按任意键继续");
		        	s=sc.nextLine();
//		        	Menu.clear_all();
		        	break;
	            }
	            for(int i = 0; i < num; i++) {
	            	cart.remove(id);
	            }
	            System.out.println("删除成功");
	            System.out.println("请按任意键继续");
	        	s=sc.nextLine();
//	            Menu.clear_all();
	            break;
	        	
	        case 3:
	        	cart.diplayAll();
	        	System.out.println("请按任意键继续");
	        	s=sc.nextLine();
//	        	Menu.clear_all();
	        	break;
	        case 4:
	        	cart.clear();
	        	System.out.println("已清空");
	        	System.out.println("请按任意键继续");
	        	s=sc.nextLine();
//	        	Menu.clear_all();
	        	break;
	        case 5:
	        	cart.settleaccounts();
	        	System.out.println("请按任意键继续");
	        	s=sc.nextLine();
//	        	Menu.clear_all();
	        	break;
	        case 0:
	        	flat = 1;
	        	break;
	        default:
	            break;
	        
			}
			if(flat == 1) {
				System.out.println("成功退出!");
				break;
			}
		}
		sc.close();
	}
}

2.Commodity类

定义商品属性,商品的ID,商品名,商品价格,描述,数量

package Shopping;

public class Commodity { //商品
	private Integer id;
	private String name;
	private Double price;
	private String description;
	
	
	public Commodity() {
		
	}

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

	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;
	}

	@Override
	public String toString() {
		return "商品  [ID:" + id + ", 名称:" + name + ", 单价:" + price + ", 类型:" + description + "]";
	}
	
}

3.CartDAO类

package Shopping;

public interface CartDAO {
	public void add(Commodity e);
	public boolean remove(Integer id);
	public void diplayAll();
	public int findById(Integer id);
	public void clear();
	public void settleaccounts();
}

4.Cart类

对购物车进行添加,删除,查看,结算,清空等操作

package Shopping;

import java.util.ArrayList;
import java.util.List;


public class Cart implements CartDAO{
	
	private List<ItemEntry> itemList;
	
	public Cart(){
		itemList = new ArrayList<>();
	}
	public int Len() {//购物车商品条目数
		return itemList.size();
	}
	
	public int find(int id)//判断购物车是否存在该商品
	{
		int index = findById(id);
		if(index==-1)
		{
			System.out.println("不存在该商品");
			return 0;
		}
		return 1;
	}
	public void add(Commodity e){//添加商品
		if (e == null){
			return ;
		}
		int index = findById(e.getId());
		if (index == -1){//如果不包含该商品的条目
			itemList.add(new ItemEntry(e));
		}else{
			itemList.get(index).increase();
		}
	}
	
	
	public boolean remove(Integer id){//删除商品
		if(itemList.size()==0) {
			System.out.println("购物车为空");
			return false;
		}
		if (id==null)
			return false;
		int index = findById(id);
		if (index == -1){//未找到
			return false;
		}else{
			ItemEntry entry = itemList.get(index);
			if (entry.getQty() <= 1){//移除相关条目qty<=0,则删除条目
				itemList.remove(index);
			}else{
				entry.decrease();
			}
		}
		
		return true;
	}
	
	public void diplayAll(){
		if(itemList.size()==0)
			System.out.println("购物车为空");
		for (ItemEntry itemEntry : itemList) {
			System.out.println(itemEntry);
		}
	}
	
	
	public int findById(Integer id){//在购物车中寻找商品
		for (int i = 0; i < itemList.size(); i++) {
			if (itemList.get(i).getItem().getId().equals(id))
				return i;
		}
		return -1;
	}
	
	public void clear() {//清空购物车
		itemList.clear();
		
	}
	
	public void settleaccounts() {//结算购物车
		double sum=0;
		int num=0;
		for (int i=0;i<itemList.size();i++) {
			sum=sum+itemList.get(i).totalPrice;
			num=num+itemList.get(i).qty;
		}
		System.out.println("共"+itemList.size()+"种商品,共"+num+"件商品");
		System.out.println("总计"+sum+"元");
		this.clear();
	}
	
	private class ItemEntry{ //内部类:购物车条目类
		
		Commodity item;//商品
		
		Integer qty;//数量
		
		private double totalPrice=0.0;
		public ItemEntry(Commodity item) {
			this.item = item;
			qty = 1;
			totalPrice=item.getPrice();
		}
		//添加
		public void increase(){
			qty++;
			totalPrice=item.getPrice()*qty;
		}
		//减少
		public void decrease(){
			qty--;
			totalPrice=item.getPrice()*qty;
		}
		public Commodity getItem() {
			return item;
		}
		public Integer getQty() {
			return qty;
		}
		@Override
		public String toString() {
			return item + ", 商品数量=" + qty + ", 小计=" + totalPrice + "]";
		}
		
	}

}

5.Mall类

打印商品目录和查找商品

package Shopping;

import java.util.ArrayList;
import java.util.List;

public class Mall {
	private List<Commodity> commodities = new ArrayList<Commodity>();
	{
		commodities.add(new Commodity(1, "iphone 12", 8199.00,"手机"));
		commodities.add(new Commodity(2, "华为 Mate40 Pro", 8199.00,"手机"));
		commodities.add(new Commodity(3, "茅台飞天", 1499.00,"酒"));
		commodities.add(new Commodity(4, "华硕VG259Q", 1449.00,"显示器"));
		commodities.add(new Commodity(5, "java学习笔记", 94.10, "书"));
	}
	public void showGoods() {// 展示商品
		for (Commodity e : commodities) {
			System.out.println(e);
		}
	}
	public Commodity searchById(int id) {// 按编号搜索商品
        int i = 0;
        for (i = 0; i < commodities.size(); i++) {
            if (commodities.get(i).getId() == (id)) {
                return commodities.get(i);
            }
        }
        return null;
    }
	
}

6.Menu类

操作选项

package Shopping;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

public class Menu {
	public static void clear_all() throws AWTException {
	    Robot r = new Robot();
	    r.mousePress(InputEvent.BUTTON3_DOWN_MASK);		  //按下鼠标右键
	    r.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);	  //释放鼠标右键
	    r.keyPress(KeyEvent.VK_CONTROL);              // 按下Ctrl键
	    r.keyPress(KeyEvent.VK_R);                    // 按下R键
	    r.keyRelease(KeyEvent.VK_R);                  // 释放R键
	    r.keyRelease(KeyEvent.VK_CONTROL);            // 释放Ctrl键
	    r.delay(10);
	}
	
    public static void menu() {
    	System.out.println("请选择:");
		System.out.println("[1]:添加商品");
		System.out.println("[2]:删除商品");
		System.out.println("[3]:查看商品");
		System.out.println("[4]:清空购物车");
		System.out.println("[5]:结算购物车");
		System.out.println("[0]:退出");
    }
}

Gitee

https://gitee.com/zhan-yibin/java-code/tree/master/shopping/ShoppingCart/Shopping

posted @ 2020-11-19 23:32  awwman  阅读(291)  评论(0编辑  收藏  举报