类和对象5个例题-4

2.模拟购物车

题目:
*
1、总体要求
模拟创建购物车,加入商品,删除商品,修改商品数量,清空购物车操作,期待输出如下:

2、购物车条目类CartItem
(1) 成员变量: id(商品id) ,name(商品名称) , price(商品单价) ,quantity(数量)
(2) 构造方法,4个参数,为4个成员变量赋初值
(3) 成员变量的get和set方法
(4) 返回条目信息的方法 public String toString()
返回“商品id\t商品名\t商品单价\t数量\t条目总价”
注意:条目总价 = 单价 × 数量
3、购物车类ShoppingCart,要求如下
(1) 成员变量:
a)userName:用户名
b)CartItem[] items = new CartItem[5]; //存放条目的数组,假设每个购物车最多放5个条目
c)count:购物车中共有几种商品(条目的数量)
d)totalPrice:购物车商品总价
(2) 构造方法,带一个参数,为userName赋初值。
(3) 存取4个成员变量的get,set方法。
(4) public void addItem(Item i) {//向购物车中加入一个条目
1、检查购物车中是否已经有该商品(以id号作为判别依据),如果已经存在,修改购物车中该条目数量,更新购物车总价。(例如:购物车中已经有2条中华牙膏,如果再添加1个中华牙膏,应该将购物车中的中华牙膏数量改成3)
2、如果购物车中无此商品,将该条目加入购物车列表中, count++,更新总价
}
(5) public void removeItem(int i){// 从购物车中删除一个条目(下标从0开始)
思路:从items数组中删除第i个位置的item对象,后续元素向前移动,count–,更新总
价。
}
(6) public void setQuantity(int i , int quantity){ //修改购物车中第i个条目的数量
思路:找到数组中第i个item元素,调用其setQuantity()方法修改数量,更新总价。
}
(7) 返回购物车信息的方法public String toString(){
1、如果购物车中没有商品,返回“用户名的购物车是空的”
2、如果购物车中有商品,并返回如下面格式的购物车信息:
“用户名的购物车如下:
id 商品名 单价 数量 总价
001 洗发水 20.0 4 80.0
003 酸奶 5.0 2 10.0
合计90.0“
}
(8) public void clearCart(){ //清空购物车
//删除购物车中的所有商品。count归0,总价归0
}
4、测试类CartTest,要求如下:
(1) 创建一个ShoppingCart对象myCart, 用户名为自己的姓名缩写
(2) 向myCart中依次加入下面4件商品(id,名字,单价,数量)
001,洗发水,20,1
001,洗发水,20,2
002,牙膏,10,1
003,酸奶,5,2
(3) 输出购物车信息

(4) 修改购物车里某个商品的数量(按下标修改),然后输出购物信息,下面以第0件商品洗发水改成4为例,修改后,输出购物车的结果如下:

(5) 删除购物车里某件商品(按下标删除),再次输出购物车信息。下面是删除第1件商品-牙膏后,输出购物车的结果:

(6) 清空购物车,输出购物车信息:

代码实现:

以下是ShoppingCart .java

package 购物车;

public class ShoppingCart {
private String userName;
private CartItem[]  items = new CartItem[5]; //存放条目的数组,假设每个购物车最多放5个条目
private int count;
private double totalPrice;
/************************************************************************************************/
//构造函数
ShoppingCart(String userName) {
	this.userName=userName;
}
/************************************************************************************************/
//为成员变量添加get和set方法
public String getUserName() {
	return userName;
}

public void setUserName(String userName) {
	this.userName = userName;
}

public CartItem[] getItems() {
	return items;
}

public void setItems(CartItem[] items) {
	this.items = items;
}

public int getCount() {
	return count;
}

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

public double getTotalPrice() {
	return totalPrice;
}

public void setTotalPrice(double totalPrice) {
	this.totalPrice = totalPrice;
}

/************************************************************************************************/
//增加购物车条目
public void addItem(CartItem i) {
	int countTotal=0;			//countTotal用来判断购物车内有没有同样的商品
	
	for(int j=0;j<count;j++) {
		if(i.getId().equals(items[j].getId())) {
			items[j].setQuantity(items[j].getQuantity()+i.getQuantity());		//同种商品增加数量
			totalPrice+=i.getQuantity()*i.getPrice();		//更新购物车总价
			countTotal--;
		}
		countTotal++;
	}
	

	if(countTotal==count) {
		items[countTotal]=i;
		count++;		//购物车商品数量更新
		totalPrice+=i.getQuantity()*i.getPrice();		//更新购物车总价
	}
}

/************************************************************************************************/
//删除购物车条目
public void removeItem(int i) {
	double changeMoney=items[i].getPrice()*items[i].getQuantity();
	for(int j=i;j<count;j++) {
		items[j]=items[j+1];		//前移
	}
	count--;		//条目总数修改
	totalPrice-=changeMoney;		//更新购物车总价

}

/************************************************************************************************/
//修改单个条目商品数量
public  void setQuantity(int  i , int quantity) {
	int change=quantity-items[i].getQuantity();		//单个条目商品数量变化量
	items[i].setQuantity(quantity);
	totalPrice+=change*items[i].getPrice();		//更新购物车总价

}

/************************************************************************************************/
public String toString() {
	String infor=userName+"的购物车如下:\nid\t商品名\t单价\t数量\t总价";
	if(count<=0) {
		return "\n用户名的购物车是空的!";
	}
	else {
		{
			for(int i=0;i<count;i++) {
				infor+=items[i].toString();
				if(i==count-1) {
					infor+="\n合计:"+totalPrice+"\n";
				}
			}
			/*return items[0].toString()
				  +items[1].toString()
				  +items[2].toString();
	*/
		}
	}
	return infor;
	
}
/************************************************************************************************/
public void clearCart() {
	for(int i=0;i<5;i++) {
		items[i]=null;
	}
	count=0;
	totalPrice=0;
}
}


以下是CartItem.java

package 购物车;

public class CartItem {
private String id;
private String name;
private double price;
private int quantity;

CartItem(String id,String name,double price,int quantity){
	this.id=id;
	this.name=name;
	this.price=price;
	this.quantity=quantity;
}

public String getId() {
	return id;
}

public void setId(String 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 int getQuantity() {
	return quantity;
}

public void setQuantity(int quantity) {
	this.quantity = quantity;
}

public String toString() {
	return "\n"+id+"\t"+name+"\t"+price+"\t"+quantity+"\t"+price*quantity;
}
}

以下是CartText.java

package 购物车;

public class CartText {

	public static void main(String[] args) {
		ShoppingCart myCart=new ShoppingCart("夏新澳");
		CartItem item1=new CartItem("001","洗发水",20,1 );
		CartItem item2=new CartItem("001","洗发水",20,2 );
		CartItem item3=new CartItem("002","牙膏",10,1 );
		CartItem item4=new CartItem("003","洗发水",5,2 );
		
		myCart.addItem(item1);
		myCart.addItem(item2);
		myCart.addItem(item3);
		myCart.addItem(item4);
	
		System.out.println(myCart.toString());

		myCart.setQuantity(0, 4);
		System.out.println(myCart.toString());
		
		myCart.removeItem(0);
		System.out.println(myCart.toString());
		
		myCart.clearCart();
		System.out.println(myCart.toString());
	}

}

运行结果:

夏新澳的购物车如下:
id 商品名 单价 数量 总价
001 洗发水 20.0 3 60.0
002 牙膏 10.0 1 10.0
003 洗发水 5.0 2 10.0
合计:80.0

夏新澳的购物车如下:
id 商品名 单价 数量 总价
001 洗发水 20.0 4 80.0
002 牙膏 10.0 1 10.0
003 洗发水 5.0 2 10.0
合计:100.0

夏新澳的购物车如下:
id 商品名 单价 数量 总价
002 牙膏 10.0 1 10.0
003 洗发水 5.0 2 10.0
合计:20.0

用户名的购物车是空的!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值