【发布时间】:2016-02-10 16:46:29
【问题描述】:
我需要编写一个可以执行交易的收银机。
一笔交易有以下步骤: (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
// ==========================================================
}
【问题讨论】:
-
对于漂亮打印方法,查看Java浮点数,因此您可以除以100并获得小数点数量,以及System.out.printf()。它具有指定打印时显示多少小数位的方法。
-
您能说明一下您需要什么帮助吗?
-
@AndrewRegan 我不知道如何处理 completePurchase() 方法。我试图用 paymentMoney 替换 purchasePrice 但它不起作用,因为 paymentMoney 是一个数组, purchasePrice 是一个整数。此外,讲师希望我更新所有交易中的总金额。我该怎么做?
-
@AndrewWilliamson 谢谢。我会试试的。
-
@AndrewRegan 基本上这就是他要我做的:更新收银机中的钱。价格、付款和零钱都变成 0。交易计数增加,所有交易中的总金额被更新。如果收银员没有足够的钱以准确计算的方式提供零钱,则交易被取消 - 物品没有被购买,客户会拿回他们确切的硬币。收银机中的金额和类型不变从交易开始之前。如果尚未计算更改,则抛出 IllegalStateException
标签: java methods transactions