当前位置:网站首页>多个类的设计
多个类的设计
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)
};
}
}
边栏推荐
猜你喜欢
Imx8qxp DMA resources and usage (unfinished)
ZABBIX deployment instructions (server+win client + switch (H3C))
MySQL索引详解
SPARQL基础入门练习
MATLAB小技巧(19)矩阵分析--主成分分析
100%弄明白5种IO模型
MySQL环境变量配置的教程
多网络设备存在时,如何配置其上网优先级?
Ready to migrate to the cloud? Please accept this list of migration steps
Digital ic-1.9 understands the coding routine of state machine in communication protocol
随机推荐
Oracle uses an SQL to find out which data is not in a table
Order by injection of SQL injection
AQS underlying source code of concurrent programming JUC
粗读DS-TransUNet: Dual Swin Transformer U-Net for Medical Image Segmentation
i=i++;
IO管脚配置和pinctrl驱动
I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!
Some considerations on operation / method overloading for thread to release lock resources
100%弄明白5种IO模型
[ 扩散模型(Diffusion Model) ]
Analysis of key technologies for live broadcast pain points -- second opening, clarity and fluency of the first frame
Digital ic-1.9 understands the coding routine of state machine in communication protocol
2022.06.26 (LC Luo 6101 Luo determines whether the matrix is an X matrix)
See how much volatile you know
Demand visual Engineer
並發編程JUC的AQS底層源碼
Lvgl GUI guide porting code to stm32
Understanding mvcc in MySQL transactions is super simple
JS EventListener
Creation process and memory layout of objects at JVM level