一个模拟真实收银机的收银机对象 - Java

时间:2016-02-10 16:46:30

标签: java methods transactions

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

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

(2)顾客给出纳员一定数额的钞票和硬币。

(3)收银员计算确切的变化。变化是 计算,以便给予客户的账单和硬币总数最小化。

(4)出纳员向顾客提供他们的变更和剩余的 账单和硬币被添加到注册表中。 如果交易因任何原因必须取消,现金 register返回事务开始前的状态。

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

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

第一堂课包括cashregister.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 $15.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 $105.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
        // ==========================================================

    }

1 个答案:

答案 0 :(得分:1)

<强> completePurchase

  

如果尚未计算更改,则抛出IllegalStateException

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

  

如果收银员没有足够的钱以改变其计算的确切方式进行更改,交易将被取消 - 该商品未被购买,客户将获得他们的确切硬币。

迭代每种类型的硬币,并从寄存器中的金额中减去变化量。如果有任何低于零,那么给客户回钱。

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

如果你过了这里,那么寄存器中的每种类型都有足够的变化,所以继续进行交易。

  

现金登记册中的资金已更新。

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

价格,付款和更改全部变为0.交易计数增加,所有交易中的总金额都会更新。

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

Arrays.fill(array, 0);

这就是它的全部!我希望有所帮助。

顺便说一句,你的导师教你不良习惯。 其他班级不应该知道收银机的内部状态才能进行购买;他们应该能够调用方法&#39;购买&#39;并且让一切都发生在一起。