生活随笔
收集整理的這篇文章主要介紹了
购物车_JAVA
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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
Output
2
1 運動鞋 Adidas 300.80 3 7
4 智能手表 蘋果 4888.00 9 1
6993.60
Hint
1、購買商品的實際數量要小于或等于庫存數量,退貨時的數量要小于購物車中該商品的現有數量。
2、購買行為發生時,注意庫存的變化。購買商品之后減庫存,退貨之后加庫存。
3、購物車中某商品的實際數量為0時,就移除它。
import java
.text
.DecimalFormat
;
import java
.util
.*
;class Product
{int id
;String name
;String produce
;double price
;int sum
;int count
;DecimalFormat dlf
=new DecimalFormat("0.00");public Product(int id
,String name
,String produce
,double price
,int sum
,int count
){this.id
=id
;this.name
=name
;this.produce
=produce
;this.price
=price
;this.sum
=sum
;this.count
=count
;}public String
toString(){return id
+" "+name
+" "+produce
+" "+dlf
.format(price
)+" "+sum
+" "+count
;}
}public class Main {public static void main(String
[] args
) {Scanner reader
= new Scanner(System
.in
);Map
<Integer,Product> map
=new TreeMap<Integer,Product>();DecimalFormat dlf
=new DecimalFormat("0.00");int n
=reader
.nextInt();reader
.nextLine();for(int i
=0;i
<n
;i
++){int id
=reader
.nextInt();Product p
=new Product(id
,reader
.next(),reader
.next(),reader
.nextDouble(),reader
.nextInt(),0);map
.put(id
,p
);}int m
=reader
.nextInt();double total
=0;while(m
-->0){int id
=reader
.nextInt();int count
=reader
.nextInt();int op
=reader
.nextInt();Product p
=map
.get(id
);if(op
==1){if(p
.sum
>=count
){p
.sum
=p
.sum
-count
;p
.count
=p
.count
+count
;map
.put(id
,p
);}else {p
.count
=p
.count
+p
.sum
;p
.sum
=0;map
.put(id
,p
);}}else if(op
==2){if(p
.count
>count
){p
.sum
=p
.sum
+count
;p
.count
=p
.count
-count
;map
.put(id
,p
);}else {p
.sum
=p
.sum
+p
.count
;p
.count
=0;map
.put(id
,p
);}}else if(op
==3){p
.sum
=p
.sum
+p
.count
;p
.count
=0;map
.put(id
,p
);}}int k
=0;for(Product p
:map
.values()){if(p
.count
>0){k
++;}}System
.out
.println(k
);for(Product p
:map
.values()){if(p
.count
>0){System
.out
.println(p
);total
=total
+p
.count
*p
.price
;}}System
.out
.format("%.2f",total
);reader
.close();}
}
總結
以上是生活随笔為你收集整理的购物车_JAVA的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。