Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
模拟现实生活中收银机的收银机对象-Java_Java_Methods_Transactions - Fatal编程技术网

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

模拟现实生活中收银机的收银机对象-Java,java,methods,transactions,Java,Methods,Transactions,我需要编程一个可以执行交易的收银机 事务具有以下步骤: (1) 当客户决定以给定的价格购买商品时,交易开始 (2) 顾客给出纳员一定数量的钞票和硬币 (3) 出纳计算出准确的零钱。变化是 计算的目的是尽量减少给客户的纸币和硬币的总数 (4) 出纳把找零和剩下的钱给顾客 纸币和硬币被添加到登记簿中。 如果由于任何原因必须取消交易,现金 寄存器返回到事务开始前的状态 根据WebCAT,我85%的编码是正确的。我正在为代码的最后一部分而挣扎,其中包括public void completetpurch

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

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

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

(3) 出纳计算出准确的零钱。变化是 计算的目的是尽量减少给客户的纸币和硬币的总数

(4) 出纳把找零和剩下的钱给顾客 纸币和硬币被添加到登记簿中。 如果由于任何原因必须取消交易,现金 寄存器返回到事务开始前的状态

根据WebCAT,我85%的编码是正确的。我正在为代码的最后一部分而挣扎,其中包括
public void completetpurchase()
public static String prettyPrint(int-amount){}
。我的导师在所有方法中都有一些小的描述

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

第一个类包括cash register.class。第二节课是初中生

> 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
        // ==========================================================

    }
>公共类收银机
{
// ==========================================================
//常数
// ==========================================================
/**一美元的价值(100美分)*/
公共静态最终整数美元=100;
/**四分之一的价值(25美分)*/
公共静态最终整数季度=25;
/**一角硬币的价值(10美分)*/
公共静态最终整数DIME=10;
/**一枚镍币的价值(5美分)*/
公共静态最终int镍=5;
/**一便士的价值(1美分)*/
公共静态最终整数=1;
// ==========================================================
//田地
// ==========================================================
私人国际采购价格;
私有int[]注册货币;
私有int[]paymentMoney={0,0,0,0,0};
私有int[]changeMoney={0,0,0,0,0};
private int transactionCount;//数量
//交易
private int transactionTotal;//分数
//我从交易中得到的
// ==========================================================
//建设者
// ==========================================================
/**
*使用指定的货币创建一个新的CashRegister对象。如果
*指定的货币数组无效,存在IllegalArgumentException
*抛出。如果:-数组的长度为,则货币数组无效
*除5以外的任何内容-数组中的任何单元格都包含负数
*-数组包含其所有单元格中的0/**
* 
*@param money
*将要存入新收银机的钱
*/
公共收银机(整数[]货币)
{
如果(该货币为负(货币))
{
抛出新的IllegalArgumentException();
}
transactionCount=0;
transactionTotal=0;
购买价格=0;
registerMoney=Arrays.copyOf(money,money.length);
}
/**
*@param money
*货币有一个负整数。
*/
私有布尔值moneyHasNegative(int[]money)
{
如果(money.length!=5)
{
返回true;
}
如果(钱[0]<0 |钱[1]<0 |钱[2]<0 |钱[3]<0
||货币[4]<0)
{
返回true;
}
如果(货币[0]==0&&money[1]==0&&money[2]==0&&money[3]==0
&&货币[4]==0)
{
返回true;
}
返回false;
}
// ==========================================================
//公共访问器方法
// ==========================================================
/**
*返回购买价格。如果尚未开始购买,则返回0。
* 
*@返回购买价格。
*/
public int getPrice()
{
退货价格;
}
/**
*@返回注册表挂载
*/
public int getRegisterAmount()
{
返回registerMoney[0]*美元+registerMoney[1]*季度
+注册货币[2]*一角+注册货币[3]*镍
+注册货币[4]*便士;
}
/**
*付款金额的值乘以常数。
*/
私有int paymentAmountValue()
{
退货付款金额[0]*美元+付款金额[1]*季度
+付款货币[2]*一角硬币+付款货币[3]*五分硬币
+paymentMoney[4]*便士;
}
// ----------------------------------------------------------
/**
*@返回付款金额。
*/
public int getPaymentAmount()
{
返回此.paymentAmountValue();
}
/**
*更改量的值乘以常数。
*/
私有int changeAmountValue()
{
返回兑换货币[0]*美元+兑换货币[1]*季度
+零钱[2]*一角硬币+零钱[3]*五分硬币
+兑换货币[4]*便士;
}
/**
*@返回找零金额。
*/
public int getChangeAmount()
{
返回此.changeAmountValue();
}
/**
*@以字符串形式返回注册货币。
*/
公共字符串getRegisterMoney()
{
返回“美元:”+registerMoney[0]+“\n”+“季度:”
+registerMoney[1]+“\n”+“Dimes:”+registerMoney[2]+“\n”
+镍币:“+registerMoney[3]+”\n“+”便士:
+registerMoney[4]+“\n”;
}
/**
*把付款的钱作为一种奖励
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];
Arrays.fill(array, 0);