class 创建问题特定于以下问题

Problem with class creation specific to the below problem

我在 Java 编码问题中遇到了问题,我可以使用一点帮助来理解问题中提到的条件的思考过程(在下面突出显示)。

本题的目标是设计一个收银机程序。您的寄存器当前包含以下 notes/coins:

一便士:.01 两便士:0.02 五便士:0.05 十便士:.10 二十便士:.20 五十便士:.50 一磅:1 两磅:2 五磅:5 十磅:10 二十磅:20 五十磅: 50

该程序的目的是计算必须退还给客户的找零数量最少 coins/notes。请注意,期望是有一个面向对象的解决方案 - 考虑创建 类 以获得更好的可重用性

现在这个平台上有类似post的这个问题,但是我想知道如何在这个问题中使用类而不是硬编码面额?如果有人能对此有所了解,我将不胜感激!我为此编写了以下代码,如果您能根据上述条件更改此代码,我们将不胜感激:

    import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.io.*;

public class Main {
  /**
   * Iterate through each line of input.
   */
  public static void main(String[] args) throws IOException {
    InputStreamReader reader = new InputStreamReader(System.in);
    BufferedReader in = new BufferedReader(reader);

    try {
        double purchasePrice = Double.parseDouble(in.readLine());
        double cash = Double.parseDouble(in.readLine());
        Main.calculateChange(purchasePrice, cash);
    } catch (Exception e) {
        System.out.println(e);
    }
  }

  public static void calculateChange(double purchasePrice, double cash) {
    
    float cashBack = (float) (cash - purchasePrice);
    
    if (cashBack < 0) {
      System.out.println("ERROR");
      return ;
    }
    else if (cashBack == 0){
      System.out.println(0);
    }
    StringBuilder change = new StringBuilder();

        while (cashBack > 0.01) {
            if (cashBack >= 50.0) {
                change.append("Fifty Pounds");
                cashBack -= 50.0;
            } else if (cashBack >= 20.0) {
                change.append("Twenty Pounds");
                cashBack -= 20.0; 
            } else if (cashBack >= 10.0) {
                change.append("Ten Pounds");
                cashBack -= 10.0;
            } else if (cashBack >= 5.0) {
                change.append("Five Pounds");
                cashBack -= 5.0;
            } else if (cashBack >= 2.0) {
                change.append("Two Pounds");
                cashBack -= 2.0;
            } else if (cashBack >= 1.0) {
                change.append("One Pound");
                cashBack -= 1.0;
            } else if (cashBack >= 0.5) {
                change.append("Fifty Pence");
                cashBack -= 0.5;
            } else if (cashBack >= 0.20) {
                change.append("Twenty Pence");
                cashBack -= 0.20;
            } else if (cashBack >= 0.1) {
                change.append("Ten Pence");
                cashBack -= 0.1;
            } else if (cashBack >= 0.05) {
                change.append("Five Pence");
                cashBack -= 0.05;
            } else if (cashBack >= 0.02) {
                change.append("Two Pence");
                cashBack -= 0.02;
            } else {
                change.append("One Pence");
                cashBack -= 0.01;
            }
            change.append(", ");
        }
        change.setLength(change.length() - 2);

        System.out.println(change.toString());
    // Access your code here. Feel free to create other classes as required
  }

}

完整的解决方案在这里:

硬币class:持有硬币价值及其名称。

public class Coin {

    double value;
    String name;

    public Coin(double value, String name) {
        this.value = value;
        this.name = name;
    }

    public double getValue() {
        return value;
    }

    public String getName() {
        return name;
    }
}

收银机class:按降序存放不同类型的硬币。

public class CashRegister {

    LinkedHashSet<Coin> coins = new LinkedHashSet<>();

    public String calculateChange(double purchasePrice, double cash) {

        double cashBack = cash - purchasePrice;

        if (cashBack < 0) {
            System.out.println("ERROR");
            return "";
        }

        StringBuilder change = new StringBuilder();

        while (cashBack > 0) {
            Iterator<Coin> iterator = coins.iterator();
            while (iterator.hasNext()) {
                Coin coin = iterator.next();
                int count = 0;
                while (cashBack >= coin.getValue()) {
                    count++;
                    cashBack -= coin.getValue();
                }
                if (count > 0) {
                    change.append(count).append(" " + coin.getName());
                    change.append("\n");
                }
            }
        }
        return change.toString();
    }

    public LinkedHashSet<Coin> init() {
        coins.add(new Coin(50, "Fifty Pound"));
        coins.add(new Coin(20, "Twenty Pound"));
        coins.add(new Coin(10, "Ten Pound"));
        coins.add(new Coin(5, "Five Pound"));
        coins.add(new Coin(2, "Two Pound"));
        coins.add(new Coin(1, "One Pound"));
        coins.add(new Coin(0.5, "Fifty Pence"));
        coins.add(new Coin(0.2, "Twenty Pence"));
        coins.add(new Coin(0.1, "Ten Pence"));
        coins.add(new Coin(0.05, "Five Pence"));
        coins.add(new Coin(0.02, "Two Pence"));
        coins.add(new Coin(0.01, "One Pence"));
        return coins;
    }
}

收银机的实例化和调用以获取零钱:

public static void main(String[] args) throws IOException {
        InputStreamReader reader = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(reader);
        try {
            // Initialise cash register.
            CashRegister cashRegister = new CashRegister();
            cashRegister.init();

            double purchasePrice = Double.parseDouble(in.readLine());
            double cash = Double.parseDouble(in.readLine());

            // Calculate change.
            String result = cashRegister.calculateChange(purchasePrice, cash);
            System.out.println(result);

        } catch (Exception e) {
            System.out.println(e);
        }
    }

任何面额的硬币都可以通过init()方法添加到收银机中。您还可以向收银机添加许多其他功能,例如

  • 保持平衡。
  • 支持动态添加新面额。
  • 支持动态移除面额