购物车(综合题)
Problem Description
各位亲爱的小伙伴们,大家好!
欢迎来到美美自助购物商场,首先请您帮忙建立商品信息,然后选购商品、确定数量,计算出总价格到结算中心进行结算。美美祝您购物愉快噢!
首先读取N行商品信息,每行包含:商品编号、商品名称、生产商、商品价格、库存数量;
接着输入M行购买信息:商品编号、商品数量、购买标志。
输出购物车中的商品数量T,以及T种商品的详细信息(商品编号、商品名称、生产商、商品价格、库存数量)、实际购买数量和购物车商品总价格(保留2位小数)。
美美商场友情提示:商品数量充分时,您才能购买;另外,您购物成功后,一定要记得增减商品库存数量噢!
Input
首行是商品种类的数量N;
接下来是N行的商品信息,每行商品信息为:商品编号、商品名称、生产商、商品价格、库存数量;
购物操作的次数M;
接下来是M行的商品购买操作的信息,每行购买信息为:商品编号、购买数量count(count>0)、购买标志(1–购买,购物车中该商品数量增加;2–退货,购物车中该商品数量减少,3-删除商品,删除操作时count列的值无效)。
Output
购物车中商品的数量T;
购物车中的所有商品详情,每种商品详情占一行,中间数据用1个空格符隔开。商品详情包括:商品编号、商品名称、生产商、商品价格、商品最新库存数量、已购买的商品数量。(商品价格保留2位小数),按照商品编号从小到大的顺序进行输出。若T=0,则无商品详情信息输出。
最后一行输出购物车中商品的总价格。(保留2位小数)
Sample Input
5
1 运动鞋 Adidas 300.80 10
2 蓝球服 李宁 268.00 10
3 苹果 栖霞 5.00 500
4 智能手表 苹果 4888.00 10
5 鼠标 罗技 120.00 50
9
1 5 1
3 5 1
4 1 1
3 2 2
5 2 1
1 2 1
5 0 3
3 1 1
3 5 2
Sample Output
2
1 运动鞋 Adidas 300.80 3 7
4 智能手表 苹果 4888.00 9 1
6993.60
Hint
1、购买商品的实际数量要小于或等于库存数量,退货时的数量要小于购物车中该商品的现有数量。
2、购买行为发生时,注意库存的变化。购买商品之后减库存,退货之后加库存。
3、购物车中某商品的实际数量为0时,就移除它。
package pp;
import java.text.*;
import java.util.*;
//商品
class Product{
int num;
String name;
String product;//生产商
double price;
int knum;//库存数量
public Product() {
}
public Product(int num, String name, String product, double price, int knum) {
super();
this.num = num;
this.name = name;
this.product = product;
this.price = price;
this.knum = knum;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getProduct() {
return product;
}
public void setProduct(String product) {
this.product = product;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getKnum() {
return knum;
}
public void setKnum(int knum) {
this.knum = knum;
}
public String toString() {
return num + " " + name + " " + product + " " + String.format("%.2f", price) + " " + knum;
}
}
class Cart{
HashMap<Product,Integer> goods = new HashMap<Product, Integer>();
//向购物车中添加商品
public void addToCart(Product product, int count) {
//购买商品的实际数量要小于或等于库存数量
count = (count > product.getKnum()) ? product.getKnum():count;
//1.如果购物车中存在此商品,更新其数量
//否则,在购物车中增加此条商品
if(goods.containsKey(product)) {
goods.put(product, count+goods.get(product));
}
else {
goods.put(product, count);//count为增加的数量
}
//防止出现购买数量为0件的情况
if(goods.get(product) == 0){
goods.remove(product);
}
//2.修改库存数量
product.setKnum(product.getKnum()-count);
}
//退货
public void reduceFromCart(Product product,int count){
//1.若购物车中存在此物品,则进行购物车中商品货物数量的减少
//否则忽略此操作
if(goods.containsKey(product)){
//退货时的数量要小于购物车中该商品的现有数量。
count = (count > goods.get(product)) ? goods.get(product):count;
goods.put(product, goods.get(product)-count);
//2.若购物车中该商品的数量减为0,就从购物车中移除
if(goods.get(product) == 0)
goods.remove(product);
//3.修改商品的库存数量,增加增加增加
product.setKnum(product.getKnum()+count);
}
}
//删除购物车中的此商品
public void removeFromCart(Product product,int count){
//删除操作时count列的值无效
//如果购物车中存在此商品,则删除它;否则,忽略此操作
if(goods.containsKey(product)){
//1.必须先修改库存数量,否则此product对应的映射景不存在,出现异常
product.setKnum(product.getKnum()+goods.get(product));
//2.从购物车中直接删除商品
goods.remove(product);
}
}
//输出购物车中的商品
public void showCart(){
//将HashMap中的数据按照商品编号排序
List<Product> list = new ArrayList<Product>(goods.keySet());
Collections.sort(list, new Comparator<Product>() {
@Override
public int compare(Product arg0, Product arg1) {
// TODO Auto-generated method stub
int num1 = arg0.getNum();
int num2 = arg1.getNum();
return num1 - num2;
}
});
System.out.println(goods.size());
for(Product product:list){
System.out.println(product.toString()+" "+goods.get(product));
}
System.out.println(String.format("%.2f", totalPrice()));
}
//计算总价
public double totalPrice(){
double sum = 0;
for(Product product:goods.keySet()){
sum += product.getPrice()*goods.get(product);
}
return sum;
}
}
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
List<Product> productList = new ArrayList<Product>();
for(int i = 0;i < n; i++){
Product product = new Product(input.nextInt(), input.next(), input.next(), input.nextDouble(), input.nextInt());
productList.add(product);
}
Cart cart = new Cart(); //购物车
int m = input.nextInt();
for(int i = 0;i < m;i++){
int num = input.nextInt();
int count = input.nextInt();
int op = input.nextInt();
Product product = null;
//在商品中按商品编号进行查找
for(Product p : productList){
if(p.getNum() == num){
product = p;
break;
}
}
if(op == 1){
cart.addToCart(product, count);
}else if(op == 2){
cart.reduceFromCart(product, count);
}else if(op == 3){
cart.removeFromCart(product, count);
}
}
// cart.totalPrice();
cart.showCart();
input.close();
}
}