当前位置:网站首页>多个类的设计
多个类的设计
2022-06-27 08:45:00 【万伏小太阳】
//package ListTest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//创建饮料屋,传入本饮料屋有什么饮料
BeverageHouse house = new BeverageHouse(ExamTools.data());
System.out.print("输入:");
//接收点单数据,并分解若干对整数,格式看题目说明
List<int[]> codeAndAmounts = ExamTools.inputCodesAndAmounts();
//系统依次处理点饮料的请求
for (int[] codeAndAmount : codeAndAmounts)
house.order(codeAndAmount[0], codeAndAmount[1]);
//结算
house.settle();
}
}
/** * 饮料屋 * 1 1,2 8,6 3,6 -2,1 1, 2 -100 */
class BeverageHouse {
private final Cart cart = new Cart();
private final BeverageList beverageList;
public BeverageHouse(Beverage[] beverages) {
beverageList = new BeverageList(beverages);
}
/** * 点一种饮料,如果所点的饮料不存在,程序什么也不做 * @param beverageCode 饮料代码 * @param amount 点amount杯饮料,可以是负数 */
public void order(int beverageCode, int amount) {
//TODO 补全代码
Beverage beverage = beverageList.getBeverageByCode(beverageCode);
if (beverage != null) {
cart.adjust(beverage, amount);
}
}
/** * 模拟结算,输出所购饮料的数量和总价 */
public void settle() {
System.out.printf("总金额:%.2f 总杯数:%d",cart.getTotalPrice(),cart.getTotalAmount());
}
}
/** * 购物车 */
class Cart {
/** * 增减某种饮料的数量,如果将导致该种饮料点的杯数<=0,表示不要该饮料 */
private final HashMap<Beverage,Integer> cart = new HashMap<>();
public void adjust(Beverage beverage, int amount) {
//TODO 补全代码
if(cart.containsKey(beverage)){
cart.put(beverage,cart.get(beverage)+amount);
}else{
cart.put(beverage,amount);
}
}
/** * 返回购物车中的饮料总件数 */
public int getTotalAmount() {
//TODO 补全代码
int sum = 0;
for (Map.Entry<Beverage, Integer> entry : cart.entrySet()) {
if(entry.getValue()>0) sum += entry.getValue();
}
return sum;
}
/** * 返回购物车的饮料总价格 */
public float getTotalPrice() {
//TODO 补全代码
float sum = 0;
for (Map.Entry<Beverage, Integer> entry : cart.entrySet()) {
if(entry.getValue()>0&&entry.getKey().getPrice()>0) sum += entry.getKey().getPrice() * entry.getValue();
}
return sum;
}
}
/** * 饮料表类 */
class BeverageList {
private final Map<Integer, Beverage> beverageMap;
public BeverageList(Beverage[] beverages) {
this.beverageMap = new HashMap<>();
for (Beverage beverage : beverages) {
this.beverageMap.put(beverage.getCode(), beverage);
}
}
/** * 根据饮料代码返回饮料 * @param beverageCode */
public Beverage getBeverageByCode(int beverageCode) {
return beverageMap.get(beverageCode);
}
}
/** * 饮料类 */
class Beverage implements Comparable<Beverage>{
private final int code;//代码(编号)
private final String name;//名字
private final float price;//价格
public Beverage(int code, String name, float price) {
this.code = code;
this.name = name;
this.price = price;
}
public Integer getCode() {
return code;
}
public float getPrice() {
return price;
}
/** * code相同的饮料是相同的 */
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Beverage beverage = (Beverage) o;
return code == beverage.code;
}
@Override
public int hashCode() {
return Objects.hash(code);
}
/** * 各种排序用 */
@Override
public int compareTo(Beverage o) {
return this.code-o.getCode();
}
@Override
public String toString() {
return "["+ code +
"," + name +String.format(",%.2f",price) +
']';
}
}
class ExamTools{
/** * 从键盘接收输入,并按c a,c a ...(c 是饮料代码,a 是数量)的格式解析<br/> * 返回的List中每个元素都是一个数组,数组的元素0是饮料代码,元素1是该饮料点几杯<br/> */
static List<int[]> inputCodesAndAmounts() {
ArrayList<int[]> list = new ArrayList<>();
String line;
try (Scanner scanner = new Scanner(System.in)) {
line = scanner.nextLine();
}
String[] codesAndAmounts;
if (line.matches("^\\+?\\d+\\s+[+-]?\\d+(\\s*,*\\s*\\+?\\d+\\s+[+-]?\\d+)*$")) {
codesAndAmounts = line.split("[^\\d+-]+");
} else {
throw new IllegalArgumentException("error input");
}
for (int i = 0; i < codesAndAmounts.length - 1; i += 2) {
list.add(new int[]{
Integer.parseInt(codesAndAmounts[i]),
Integer.parseInt(codesAndAmounts[i + 1])});
}
return list;
}
/** * 考试用的测试数据 */
static Beverage[] data() {
return new Beverage[]{
new Beverage(1, "红茶", 5.55f),
new Beverage(2, "咖啡", 6.66f),
new Beverage(3, "奶茶", 8.18f),
new Beverage(4, "绿茶", 5.32f),
new Beverage(5, "参茶", 66.6f),
new Beverage(6, "燕窝", 22.2f),
new Beverage(7, "梨汁", 7.77f),
new Beverage(8, "开水", 0.55f),
new Beverage(9, "橙汁", 4.44f),
new Beverage(10, "豆浆", 3.43f)
};
}
}
边栏推荐
- UE5神通--POI解决方案
- Analysis log log
- ZABBIX deployment instructions (server+win client + switch (H3C))
- See how much volatile you know
- Brief introduction to SSL encryption process
- The largest rectangle in the bar graph of force buckle 84
- [daily practice] realization of product card animation effect
- Ready to migrate to the cloud? Please accept this list of migration steps
- How much do you know about the cause of amplifier distortion?
- JS EventListener
猜你喜欢

Ue5 magic power - POI solution

Eight misunderstandings, broken one by one (final): the cloud is difficult to expand, the customization is poor, and the administrator will lose control?

One week's experience of using Obsidian (configuration, theme and plug-in)

Mysql事务中MVCC理解超简单

MySQL environment variable configuration tutorial

How Oracle converts strings to multiple lines

I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!

并发编程JUC的AQS底层源码

0号进程,1号进程,2号进程

DV scroll board width of datav rotation table component
随机推荐
SPARQL基础入门练习
Obsidian 一周使用心得(配置、主题和插件)
2022.06.26 (LC Luo 6101 Luo determines whether the matrix is an X matrix)
浏览器的markdown插件显示不了图片
一种太阳能电荷泵供电电路的方案设计
正确的理解MySQL的MVCC
Process 0, process 1, process 2
针对直播痛点的关键技术解析——首帧秒开、清晰度、流畅度
How Oracle converts strings to multiple lines
Digital ic-1.9 understands the coding routine of state machine in communication protocol
我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!
Modify the contents of /etc/crontab file directly, and the scheduled task will not take effect
RockerMQ消息发送与消费模式
[ 扩散模型(Diffusion Model) ]
0号进程,1号进程,2号进程
Redis installation under Linux
March into machine learning -- Preface
This, constructor, static, and inter call must be understood!
RockerMQ消息发送模式
看看volatile你深知多少