当前位置:网站首页>多个类的设计
多个类的设计
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)
};
}
}
边栏推荐
- Static code block vs construction code block
- 快捷键 bug,可复现(貌似 bug 才是需要的功能 [滑稽.gif])
- June 26, 2022 (LC 6100 counts the number of ways to place houses)
- Oracle uses an SQL to find out which data is not in a table
- Read datasets iteratively with xgboost
- webrtc入门:12.Kurento下的RtpEndpoint和WebrtcEndpoint
- The most direct manifestation of memory leak
- Win10 add right-click menu for any file
- March into machine learning -- Preface
- Redis configuration file details
猜你喜欢

Five basic types of redis

The markdown plug-in of the browser cannot display the picture

June 26, 2022 (LC 6100 counts the number of ways to place houses)

【云原生】2.3 Kubernetes 核心实战(上)

Digital ic-1.9 understands the coding routine of state machine in communication protocol

我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!

MySQL environment variable configuration tutorial

DV scroll board width of datav rotation table component

Redis installation under Linux

IMX8QXP DMA资源和使用(未完结)
随机推荐
The markdown plug-in of the browser cannot display the picture
Associated GIS: all roads lead to ue5 City
Static code block vs construction code block
Matlab tips (19) matrix analysis -- principal component analysis
win10为任意文件添加右键菜单
ThreadLocal digs its knowledge points again
经典的一道面试题,涵盖4个热点知识
i=i++;
粗读DS-TransUNet: Dual Swin Transformer U-Net for Medical Image Segmentation
快捷键 bug,可复现(貌似 bug 才是需要的功能 [滑稽.gif])
C # solve the relative path problem using SQLite
Lvgl usage demo and instructions 2
Tips for using Jupiter notebook
Rockermq message sending mode
MATLAB小技巧(18)矩阵分析--熵权法
使线程释放锁资源的操作/方法重载一点注意事项
Game asset reuse: a new way to find required game assets faster
2022.06.26 (LC Luo 6101 Luo determines whether the matrix is an X matrix)
Win10 add right-click menu for any file
力扣84柱状图中最大的矩形