2020-05-03

实现书籍的一个购物车

书的信息类
public class Book {
String id;
String name;
String publisher;
double price;
public Book(){ }
public Book(String id, String name, String publisher, double price) {
super();
this.id = id;
this.name = name;
this.publisher = publisher;
this.price = price;
}
public Book(String id, String name , double price) {
super();
this.id = id;
this.name = name;
this.price = price;
}
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 String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) { this.publisher = publisher;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override public String toString() {
return "Name: " + name+ " Price: "+ price ;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
项目类
public class Item {
private Book book;
private int quantity;//数量
private double price;//单价
public Item() {
}
public Item(Book book, int quantity) { this.book = book;
this.quantity = quantity;
this.price = book.getPrice();
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) { this.quantity = quantity;}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;

	public double totalPrice() {//返回这本书的总价
		return price*quantity;	//return book.price*quantity;
		}
		}

购物车功能实现
public class Cart {
private Map<String, Item> bookMap = new HashMap<String ,Item>();
public Cart() { } public void addItem(Book book, int count) {
Item bit = bookMap.get(book.getId()); if(bit == null) {
Item it = new Item(book, count); bookMap.put(book.getId(),it); }else { bit.setQuantity(bit.getQuantity()+count); }
}
public double getTotalPrice() {//返回购物车中所有商品的总价
double totalPrice =0;
for(String key:bookMap.keySet()) { Item it = bookMap.get( key); totalPrice=totalPrice+(it.getPrice())*(it.getQuantity());
}
return totalPrice;
}
public int getTotalCount() {//返回购物车中所有商品的总数
int count =0;
for(String key:bookMap.keySet()) {
Item it = bookMap.get( key); count=count+it.getQuantity();
}
return count;
}
public void printItemList() {//打印输出购物车所有商品列表 if(bookMap == null) {
System.out.println(“购物车为空 !”); return;
}else {
for(String key:bookMap.keySet()) {
Item it = bookMap.get( key);
Book bk = (Book)it.getBook();
System.out.println(“ID: “+key+”\t”+ bk.toString()+"数量为: “+it.getQuantity()+”\t总价: "+it. totalPrice()); }
}
}
public static void main(String[] args) {
Book bj = new Book(“101”, “Java”,25);
Book bp = new Book(“102”, “PHP”,25.8);
Book bc = new Book(“103”, “C++”,36);
Cart cart = new Cart();
cart.addItem(bj, 2);
cart.addItem(bp, 3);
cart.addItem(bc, 4);
cart.addItem(bj, 5);
cart. printItemList();
System.out.println(“购物车商品总数量为:”+cart.getTotalCount()); System.out.println(“购物车商品总价格为:”+cart.getTotalPrice());
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值