JAVA大作业——购物车

团队介绍

成员 负责任务
王林涛 代码编写和类的设计
郭其松 代码编写和类的设计
黄鸿森 博客编写
洪礼强 前期调查、命名规范

前期调查


顾客先从商城挑选商品,然后选择想要的商品的种类,在选择数量增添进购物车内,在购物车内可以对商品进行增加、减少、计算总价钱和结算商品等功能。

系统功能结构图

UML类图

运行截图

商城:

添加商品进购物车:


将商品移除购物车:

关键代码

Main:

package team.shoppingcart;

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Store store = new Store();
		userCart myCart = new userCart();
		
		Scanner sc = new Scanner(System.in);
		
		while(true) {
			System.out.println("1:进入商城");
			System.out.println("2:查看购物车");
			System.out.println("3:退出系统");
			int choice = sc.nextInt();
			switch(choice) {
			case 1:{
				System.out.println(store.toString());
				System.out.println("输入商品的Id可将商品添加至购物车");
				if(sc.hasNextInt()) {
					int id = sc.nextInt();
					System.out.println("确认添加该商品至购物车(1:yes;2:no)");
					int isAdd = sc.nextInt();
					System.out.print("添加的数量:");
					int number;
					number = sc.nextInt();
					if(isAdd == 1) {
						myCart.addGoods(store.IDSearch(id),number);
					}else if(isAdd == 2) {
						break;
					}
				}
				break;
			}
			case 2:{
				myCart.CartMenu();
				System.out.println("输入商品的Id可将商品移除出购物车");
				if(sc.hasNextInt()) {
					int id = sc.nextInt();
					System.out.println("确认移除该商品车(1:yes;2:no)");
					int isDelete = sc.nextInt();
					if(isDelete == 1) {
						myCart.deleteGoods(store.IDSearch(id));
					}else if(isDelete == 2) {
						break;
					}
				}
				break;
			}
			default:{
				sc.close();
				return;
			}
			}
		}
	}
}

购物车增添删除功能userCart:

package team.shoppingcart;

import java.io.Serializable;
import java.util.ArrayList;

public class userCart implements Serializable{
	private static final long serialVersionUID = 1L;
	
	//购物车内商品包括同种类商品的总数量
	private int TotalAmount;
	//购物车内总价
	private double TotalPrice;
	
	public int getTotalAmount() {
		return TotalAmount;
	}
	public double getTotalPrice() {
		return TotalPrice;
	}
	public userCart() {
		this.TotalAmount = 0;
		this.TotalPrice = 0.0;
	}
	//购物车总览通过数组储存
	private ArrayList<userItem> myCart = new ArrayList<userItem>();
	
	public void addGoods(Goods good, int number) {
		int found = 0;
		for(int index = 0;index < myCart.size(); index++)
			if((myCart.get(index).getMyGoods().getId() == good.getId())) {
				myCart.get(index).addgoods(number);
				found = 1;
			}
		if(found == 0)
			myCart.add(new userItem(good,number,good.getPrice()));
		TotalAmount += number;
		TotalPrice += good.getPrice() * number;
		System.out.println("添加成功!");
	}
	public void deleteGoods(Goods good) {
		for(int index = 0;index < myCart.size(); index++)
			if((myCart.get(index).getMyGoods().getId() == good.getId())) {
				myCart.get(index).deletegoods(1);
			}
		TotalAmount --;
		TotalPrice -= good.getPrice();
		System.out.println("删除成功!");
	}
	public void CartMenu() {
		if(getTotalAmount() == 0) {
			System.out.println("购物车为空!");
			return;
		}
		System.out.println("购物车清单:");
		for(userItem item : myCart) {
			System.out.println(item.toString());
		}
		System.out.println("共"+getTotalAmount()+"件 ; "+"总价为"+getTotalPrice());
	}
}

商品类Goods:

package team.shoppingcart;

class Goods {
	private int Id;
	private String name;
	private double price;
	public int getId() {
		return Id;
	}
	public void setId(int id) {
		Id = id;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Goods(int Id, String name, double price) {
		this.Id = Id;
		this.name = name;
		this.price = price;
	}
	@Override
	public String toString() {
		return "Goods [Id=" + Id + ", name=" + name + ", price=" + price + "]";
	}
	
	
}
posted @ 2020-12-25 13:45  hhs(黄鸿森)  阅读(95)  评论(0编辑  收藏  举报