模拟现实生活收银机的收银机对象 - Java

a cash register object that simulates a real life cash register - Java

我需要编写一个可以执行交易的收银机。

一笔交易有以下步骤: (1) 当客户决定以给定价格购买商品时,交易开始。

(2)顾客给收银员一定数量的纸币和硬币。

(3) 收银员计算出准确的零钱。变化是 进行计算,以使提供给客户的纸币和硬币的总数最少。

(4) 收银员给顾客零钱,剩下的 钞票和硬币被添加到寄存器中。 如果出于任何原因必须取消交易,现金 将 returns 注册到事务开始之前的状态。

根据 Web-Cat,我的编码有 85% 是正确的。我真的很难处理我的代码的最后一部分,其中包括 public void completePurchase()public static String prettyPrint(int amount) { }。我的导师在所有方法的上方都包含了一些小的描述。

我真的希望你能帮我解决这两个方法。当然,我不是在寻求解决方案,而是帮助解决问题。

第一个 class 包括 cashregister.class。第二个 class 是 Junittester。

> public class CashRegister
{

    // ==========================================================
    // Constants
    // ==========================================================

    /** The value of a dollar (100 cents) */
    public static final int DOLLAR       = 100;
    /** The value of a quarter (25 cents) */
    public static final int QUARTER      = 25;
    /** The value of a dime (10 cents) */
    public static final int DIME         = 10;
    /** The value of a nickel (5 cents) */
    public static final int NICKEL       = 5;
    /** The value of a penny (1 cent) */
    public static final int PENNY        = 1;

    // ==========================================================
    // Fields
    // ==========================================================

    private int             purchasePrice;

    private int[]           registerMoney;
    private int[]           paymentMoney = { 0, 0, 0, 0, 0 };
    private int[]           changeMoney  = { 0, 0, 0, 0, 0 };

    private int             transactionCount;                // number of
// transactions
    private int             transactionTotal;                // number of cents
// that I have made from transactions


    // ==========================================================
    // Constructors
    // ==========================================================
    /**
     * Creates a new CashRegister object with the specified money in it. If the
     * specified money array is not valid, an IllegalArgumentException is
     * thrown. The money array is not valid if: - the length of the array is
     * anything but 5 - the array contains a negative number in any of the cells
     * - the array contains a 0 in _all_ of its cells /**
     * 
     * @param money
     *            the money that will go into the new cash register
     */
    public CashRegister(int[] money)

    {
        if (this.moneyHasNegative(money))
        {
            throw new IllegalArgumentException();

        }

        transactionCount = 0;
        transactionTotal = 0;
        purchasePrice = 0;
        registerMoney = Arrays.copyOf(money, money.length);

    }


    /**
     * @param money
     *            money has a negative integer.
     */
    private boolean moneyHasNegative(int[] money)
    {

        if (money.length != 5)
        {
            return true;
        }

        if (money[0] < 0 || money[1] < 0 || money[2] < 0 || money[3] < 0
                || money[4] < 0)
        {
            return true;
        }

        if (money[0] == 0 && money[1] == 0 && money[2] == 0 && money[3] == 0
                && money[4] == 0)
        {
            return true;
        }
        return false;
    }


    // ==========================================================
    // Public Accessor Methods
    // ==========================================================
    /**
     * Returns the purchase price. Returns 0 if a purchase has not begun yet.
     * 
     * @return purchase price.
     */

    public int getPrice()
    {
        return purchasePrice;
    }


    /**
     * @return the registerAmount
     */
    public int getRegisterAmount()
    {
        return registerMoney[0] * DOLLAR + registerMoney[1] * QUARTER
                + registerMoney[2] * DIME + registerMoney[3] * NICKEL
                + registerMoney[4] * PENNY;

    }


    /**
     * The value of payment amount multiplied by constants.
     */
    private int paymentAmountValue()
    {
        return paymentMoney[0] * DOLLAR + paymentMoney[1] * QUARTER
                + paymentMoney[2] * DIME + paymentMoney[3] * NICKEL
                + paymentMoney[4] * PENNY;
    }


    // ----------------------------------------------------------
    /**
     * @return the amount of payment.
     */
    public int getPaymentAmount()
    {
        return this.paymentAmountValue();

    }


    /**
     * The value of change amount multiplied by constants.
     */
    private int changeAmountValue()
    {
        return changeMoney[0] * DOLLAR + changeMoney[1] * QUARTER
                + changeMoney[2] * DIME + changeMoney[3] * NICKEL
                + changeMoney[4] * PENNY;
    }


    /**
     * @return the change amount.
     */
    public int getChangeAmount()
    {
        return this.changeAmountValue();
    }


    /**
     * @return the register money as string.
     */
    public String getRegisterMoney()
    {
        return "Dollars: " + registerMoney[0] + "\n" + "Quarters: "
                + registerMoney[1] + "\n" + "Dimes: " + registerMoney[2] + "\n"
                + "Nickels: " + registerMoney[3] + "\n" + "Pennies: "
                + registerMoney[4] + "\n";

    }


    /**
     * get the payment money as a string.
     */
    private String paymentMoneyString()
    {
        return "Dollars: " + paymentMoney[0] + "\n" + "Quarters: "
                + paymentMoney[1] + "\n" + "Dimes: " + paymentMoney[2] + "\n"
                + "Nickels: " + paymentMoney[3] + "\n" + "Pennies: "
                + paymentMoney[4] + "\n";
    }


    /**
     * @return the payment money as a string.
     */
    public String getPaymentMoney()
    {
        return this.paymentMoneyString();

    }


    /**
     * The value of payment amount multiplied by constants.
     */
    private String changeMoneyString()
    {
        return "Dollars: " + changeMoney[0] + "\n" + "Quarters: "
                + changeMoney[1] + "\n" + "Dimes: " + changeMoney[2] + "\n"
                + "Nickels: " + changeMoney[3] + "\n" + "Pennies: "
                + changeMoney[4] + "\n";
    }


    /**
     * @return the change money as a string.
     */

    public String getChangeMoney()
    {
        return this.changeMoneyString();

    }


    /**
     * @return the transaction count.
     */
    public int getTransactionCount()
    {
        return transactionCount;

    }


    /**
     * @return the transaction in total.
     */
    public int getTransactionTotal()
    {
        return transactionTotal;

    }


    // ==========================================================
    // Public Mutator Methods
    // ==========================================================

    // Begins a transaction using the specified price.
    // If a transaction is already in progress, throws an IllegalStateException
    // If the specified price is not positive, throws an
    // IllegalArgumentException
    // ----------------------------------------------------------
    /**
     * Begins a transaction using the specified price.
     * 
     * @param price
     *            the price of the item
     */
    public void beginPurchase(int price)
    {
        if (transactionAlreadyInProgress())
        {
            throw new IllegalStateException();
        }

        if (price <= 0)
        {
            throw new IllegalArgumentException();
        }
        purchasePrice = price;
    }


    /**
     * shows that the transaction is already in progress.
     */
    private boolean transactionAlreadyInProgress()
    {

        return false;
    }


    // Records the specified payment.
    // If the purchase has not begun yet, throws IllegalStateException
    // If the specified money array is not valid (see the constructor),
    // throws IllegalArgumentException
    // If the amount of money given is not sufficient to cover the
    // purchase price, the transaction is canceled.

    // ----------------------------------------------------------
    /**
     * Records the specified payment.
     * 
     * @param money
     *            payment money
     */
    public void enterPayment(int[] money)
    {

        if (purchaseHasNotBegunYet())
        {
            throw new IllegalStateException();
        }

        if (this.notValidMoneyArray(money))
        {
            throw new IllegalArgumentException();

        }

        while (true)
        {
            if (money[0] != purchasePrice || money[1] != purchasePrice
                    || money[2] != purchasePrice || money[3] != purchasePrice
                    || money[4] != purchasePrice)
                break;
        }

        paymentMoney = Arrays.copyOf(money, money.length);
    }


    /**
     * purchase has not begun. Purchase will be canceled if true.
     */
    private boolean purchaseHasNotBegunYet()
    {
        return false;
    }


    /**
     * If money array or length is not valid, it will return false.
     */

    private boolean notValidMoneyArray(int[] money)

    {
        if (money.length != 5)
        {
            return true;
        }

        if (money[0] < 0 || money[1] < 0 || money[2] < 0 || money[3] < 0
                || money[4] < 0)
        {
            return true;
        }

        if (money[0] == 0 && money[1] == 0 && money[2] == 0 && money[3] == 0
                && money[4] == 0)
        {
            return true;
        }
        {
            return false;
        }

    }


    // Calculates the change due to the customer using the largest bill
    // and coin denominations first. Thus, the change given will use the
    // maximum amount of dollars, then the maximum amount of quarters,
    // then dimes, then nickels, then pennies. For example, if the change
    // required is .84, the change will be 15 dollars, 3 quarters,
    // 1 nickel, and 4 pennies. It will NOT be 12 dollars, 8 quarters,
    // 10 dimes, 12 nickels and 24 pennies. If payment has not been entered
    // yet, throws IllegalStateException.

    // ----------------------------------------------------------
    /**
     * throws out the calculated change.
     * 
     * @param money
     *            changeMoney
     */
    public void calculateChange()
    {
        int changeToGiveBack = 1299;

        int dollarsToGiveBack = 0;

        int quartersToGiveBack = 0;

        int dimesToGiveBack = 0;

        int nickelsToGiveBack = 0;

        int penniesToGiveBack = 0;

        changeMoney[0] = dollarsToGiveBack;
        changeMoney[1] = quartersToGiveBack;
        changeMoney[2] = dimesToGiveBack;
        changeMoney[3] = nickelsToGiveBack;
        changeMoney[4] = penniesToGiveBack;

        while (changeToGiveBack >= DOLLAR)
        { // check if >= works better or worse than >
            changeMoney[0] = changeMoney[0] + 1;
            changeToGiveBack = changeToGiveBack - DOLLAR;
        }
        while (changeToGiveBack >= QUARTER)
        {
            changeMoney[1] = changeMoney[1] + 1;
            changeToGiveBack = changeToGiveBack - QUARTER;
        }

        while (changeToGiveBack >= DIME)
        {
            changeMoney[2] = changeMoney[2] + 1;
            changeToGiveBack = changeToGiveBack - DIME;
        }

        while (changeToGiveBack >= NICKEL)
        {
            changeMoney[3] = changeMoney[3] + 1;
            changeToGiveBack = changeToGiveBack - NICKEL;
        }
        while (changeToGiveBack >= PENNY)
        {
            changeMoney[4] = changeMoney[4] + 1;
            changeToGiveBack = changeToGiveBack - PENNY;
        }

        if (paymentNotBeenEntered())
        {
            throw new IllegalStateException();
        }

    }


    // Completes the transaction. Money in cash register is updated.
    // The price, payment, and change all become 0. The transaction count
    // is incremented and the total amount of money made in all transactions
    // is updated. If the cashier does not have enough money to give change
    // in the EXACT way it was calculated, the transaction is cancelled -
    // the item is not purchased, and the customer gets their exact coins back.
    // The amount and type of money in the register is unchanged from before
    // the transaction began. If change has not been calculated yet,
    // throws IllegalStateException

    private boolean paymentNotBeenEntered()
    {
        return false;
    }


    /**
     * Completes the transaction.
     * 
     * @param money
     *            complete purchase money
     */
    public void completePurchase()
    {


    }




    // ==========================================================
    // Public Static Methods
    // ==========================================================

    // Returns a string for the specified amount with a dollar sign
    // at the beginning and a decimal two places from the end.
    // For example, the amount 10538 cents becomes the string 5.38.
    // No commas are printed in the string.
    // public static String prettyPrint(int amount) { }


    // ==========================================================
    // Private Methods
    // ==========================================================

    // In this project you WILL have private methods. If you do not,
    // it means you have redundant code in your class and the grader
    // will take off points for it.
}





    This is the part where CashregisterTest begins.


     > public class CashRegisterTest
    {

        private CashRegister cr;


        // ----------------------------------------------------------
        /**
         * throws an Exception
         * 
         * @throws Exception
         */
        @Before
        public void setUp()
                throws Exception
        {
            cr = new CashRegister(new int[] { 20, 20, 20, 20, 20 });
        }


        // ----------------------------------------------------------
        /**
         * Testing the Constructors
         */
        // ==========================================================
        // Constructor Tests
        // ==========================================================

        @Test
        public void testConstructor()
        {

            assertEquals(2820, cr.getRegisterAmount());

            cr.beginPurchase(1299);
            assertEquals(1299, cr.getPrice());

            int[] paymentMoney = { 30, 0, 0, 0, 0 };
            cr.enterPayment(paymentMoney);
            assertEquals(paymentMoney, cr.getPaymentAmount());

            cr.calculateChange();
            assertEquals(1288, cr.getChangeAmount());

            assertEquals(0, cr.getChangeAmount());

            assertEquals(0, cr.getTransactionCount());

            assertEquals(0, cr.getTransactionTotal());
        }


        // ----------------------------------------------------------
        /**
         * Checks if constructor throws an illegal argument exception when array !=
         * 5.
         */
        // This test method checks that the constructor
        // correctly throws an illegal argument exception when
        // the array has a length other than five

        @Test(
                expected = IllegalArgumentException.class)
        public void testConstructorArrayBadLength()
        {
            int[] money = { 20, 20, 20, 20 }; // bad parameter; only has length four
            cr = new CashRegister(money); // this should throw an exception
            fail(); // if we reach here, our constructor is wrong
        }


        // ----------------------------------------------------------
        /**
         * Test if the constructor throws illegal argument exception if array
         * contains a negative integer.
         */
        // Write a test method that checks if the constructor
        // correctly throws an illegal argument exception when
        // the array argument contains a negative integer

        @Test(
                expected = IllegalArgumentException.class)
        public void testConstructorArrayNegativeLength()
        {

            int[] money = { -20, 20, 20, 20, 20 }; // bad parameter; only has
    // length
    // four
            cr = new CashRegister(money); // this should throw an exception
            fail(); // if we reach here, our constructor is wrong
        }


        // Write a test method that checks if the constructor
        // correctly throws an illegal argument exception when
        // the array argument contains all zeros

        // ----------------------------------------------------------
        /**
         * Tests if the constructor correctly throws an illegal argument Exception
         * when array = 0.
         * 
         * @Test IllegalArgumentException
         */
        @Test(
                expected = IllegalArgumentException.class)
        public void testConstructorArrayAllZeros()
        {

            int[] money = { 0, 0, 0, 0, 0 }; // bad parameter; only has length four
            cr = new CashRegister(money); // this should throw an exception
            fail(); // if we reach here, our constructor is wrong
        }


        // ==========================================================
        // Accessor Tests
        // ==========================================================

        // ----------------------------------------------------------
        /**
         * Dont know yet
         */
        @Test
        public void testGetMoney()
        {
            // getRegisterMoney
            // getPaymentMoney
            // getChangeMoney
            String registerMoney =
                    "Dollars: 20\n" + "Quarters: 20\n" + "Dimes: 20\n"
                            + "Nickels: 20\n" + "Pennies: 20\n";
            String zeroMoney =
                    "Dollars: 0\n" + "Quarters: 0\n" + "Dimes: 0\n"
                            + "Nickels: 0\n" + "Pennies: 0\n";
            assertEquals(registerMoney, cr.getRegisterMoney());
            assertEquals(zeroMoney, cr.getPaymentMoney());
            assertEquals(zeroMoney, cr.getChangeMoney());
        }


        // ==========================================================
        // Mutator Tests
        // ==========================================================

        // ----------------------------------------------------------
        /**
         * Place a description of your method here.
         */

        // ----------------------------------------------------------
        /**
         * Sunny day not yet
         */
        @Test
        public void testSunnyDay()
        {
            System.out.println(cr.getRegisterMoney());

            cr.beginPurchase(1800);
            assertEquals(1800, cr.getPrice());

            int[] paymentMoney = { 30, 0, 0, 0, 0 };
            cr.enterPayment(paymentMoney);
            System.out.println(cr.getPaymentMoney());
            cr.calculateChange();
            System.out.println(cr.getChangeMoney());

            // cr.completePurchase();

            System.out.println(cr.getRegisterMoney());

            String registerMoney =
                    "Dollars: 20\n" + "Quarters: 20\n" + "Dimes: 20\n"
                            + "Nickels: 20\n" + "Pennies: 20\n";
            assertEquals(registerMoney, cr.getRegisterMoney());
        }

        // ==========================================================
        // Static Method Tests
        // ==========================================================

    }

完成购买:

If change has not been calculated yet throws IllegalStateException

这很容易检查,我会把它留给你。

If the cashier does not have enough money to give change in the EXACT way it was calculated, the transaction is cancelled - the item is not purchased, and the customer gets their exact coins back.

遍历每种类型的硬币,并从寄存器中的数量中减去找零的数量。如果低于零,则将钱还给客户。

for (int x = 0; x < registerMoney.length; x++)
    if (registerMoney[x] - changeMoney[x] < 0)
        // GIVE ME BACK MY MONEY!

如果你能从这里过去,那么收银机里的每种零钱都足够了,所以继续交易吧。

Money in cash register is updated.

for (int x = 0; x < registerMoney.length; x++)
    registerMoney[x] -= changeMoney[x];

The price, payment, and change all become 0. The transaction count is incremented and the total amount of money made in all transactions is updated.

这是相当简单的 Java 东西。您可以像这样用零填充 paymentMoney 和 changeMoney 数组:

Arrays.fill(array, 0);

仅此而已!希望对您有所帮助。

顺便说一下,你的导师正在教你坏习惯。 其他 类 不必知道收银机的内部状态就可以进行购买;他们应该能够调用方法 'Purchase' 并一次性完成所有事情。