Sort of new to Java and was given this homework. Gettin some errors which at this given time I have no Idea on how to fix. Any suggestions on that and on the implementation of the methods themselves is appreciated.
Instructions:
Errors: CashRegisterTester
CashRegisterTester
CashRegister cannot be resolved to a type
CashRegister cannot be resolved to a type
CashRegister.java done so far:
/** * @author Javier * */ public class CashRegister{ /** * total value of purchase */ private double purchaseTotal=0.0; /** * cash given as payment for purchase */ private Cash payment = new Cash(); /** * total cash in cash register */ private Cash registerTotal = null; /** * creates a cash register with 100 of every American currency */ public CashRegister(){ this(new Cash(100,100,100,100,100)); } /** creates a cash register with given amount of cash * @param registerTotal = given amount of cash for register */ public CashRegister(Cash registerTotal) { this.registerTotal = registerTotal; } /** adds the price of an item to a purchase price sum * @param amount * @return */ public double purchase (double amount){ purchaseTotal += amount; return amount; } /** if the amount isnt 0 cash, it adds up the cash to * calculate payment given, * not sure if this is the right way for this * @param amount */ public void payment (Cash amount){ while(amount != new Cash()){ payment.add(amount); } } /** if the value of the payment is more * than the purchase it calculates the change * not sure if this is the right way for this * * @return */ public Cash giveChange(){ Cash change = new Cash(); if(payment.getValue()>purchaseTotal){ change = payment.remove(purchaseTotal); } purchaseTotal = 0; payment = null; return change; } /** * @return */ public double getPurchaseTotal(){ return purchaseTotal; } /** * @return */ public Cash getPayment(){ return payment; } /** * @return */ public Cash getRegisterTotal(){ return registerTotal; } }
CashRegisterTest:
import static org.junit.Assert.assertTrue; import org.junit.Test; /** * */ /** * @author juansuris * */ public class CashRegisterTester { /** * Test method for {@link CashRegister#CashRegister(Cash)}. */ @Test public void testCashRegisterCash() { CashRegister cr = new CashRegister(new Cash(10, 10, 10, 10, 10)); assertTrue( Math.abs(cr.getPayment().getValue()) < 1e-8 && Math.abs(cr.getPurchaseTotal()) < 1e-8 && Math.abs(cr.getRegisterTotal().getValue() - 14.10) < 1e-8 ); } /** * Test method for {@link CashRegister#CashRegister()}. */ @Test public void testCashRegister() { CashRegister cr = new CashRegister(); assertTrue( Math.abs(cr.getPayment().getValue()) < 1e-8 && Math.abs(cr.getPurchaseTotal()) < 1e-8 && Math.abs(cr.getRegisterTotal().getValue() - 141.0) < 1e-8 ); } /** * Test method for {@link CashRegister#purchase(java.lang.String, double)}. */ @Test public void testPurchase() { CashRegister cr = new CashRegister(); cr.purchase(2.50); cr.purchase(2.75); assertTrue( Math.abs(cr.getPayment().getValue()) < 1e-8 && Math.abs(cr.getPurchaseTotal() - 5.25) < 1e-8 && Math.abs(cr.getRegisterTotal().getValue() - 141.0) < 1e-8 ); } /** * Test method for {@link CashRegister#payment(Cash)}. */ @Test public void testPayment() { CashRegister cr = new CashRegister(); cr.purchase(2.50); cr.purchase(2.75); cr.payment(new Cash(4, 0, 0, 0, 0)); cr.payment(new Cash(0, 4, 9, 2, 0)); assertTrue( Math.abs(cr.getPayment().getValue() - 6.0) < 1e-8 && Math.abs(cr.getPurchaseTotal() - 5.25) < 1e-8 && Math.abs(cr.getRegisterTotal().getValue() - 141.0) < 1e-8 ); } /** * Test method for {@link CashRegister#giveChange()}. */ @Test public void testGiveChange() { CashRegister cr = new CashRegister(); cr.purchase(2.50); cr.purchase(2.75); cr.payment(new Cash(4, 0, 0, 0, 0)); cr.payment(new Cash(0, 4, 9, 2, 0)); Cash c = cr.giveChange(); Cash r = cr.getRegisterTotal(); assertTrue( Math.abs(cr.getPayment().getValue()) < 1e-8 && Math.abs(cr.getPurchaseTotal()) < 1e-8 && Math.abs(r.getValue() - 146.25) < 1e-8 && Math.abs(c.getValue() - 0.75) < 1e-8 ); } }
premade Cash.java:
/** * */ /** * @author Javier * */ public class Cash extends CashRegister{ // Static Fields /** * value of one dollar bill */ public static final int ONE_BILL_VALUE = 100; /** * value of a quarter */ public static final int QUARTER_VALUE = 25; /** * value of a dime */ public static final int DIME_VALUE = 10; /** * value of a nickel */ public static final int NICKEL_VALUE = 5; /** * value of a penny */ public static final int PENNY_VALUE = 1; // Instance Fields /** * amount of bills */ private int bills = 0; /** * amount of quarters */ private int quarters = 0; /** * amount of dimes */ private int dimes = 0; /** * amount of nickels */ private int nickels = 0; /** * amount of pennies */ private int pennies = 0; /** * value of cash collection */ private int value = 0; /** * constructor for object without given the amount of currency */ public Cash() { } /** constructor for object with given amount of currency * @param bills contained * @param quarters contained * @param dimes contained * @param nickels contained * @param pennies contained */ public Cash(int bills, int quarters, int dimes, int nickels, int pennies) { addBills(bills); addQuarters(quarters); addDimes(dimes); addNickels(nickels); addPennies(pennies); } /**Adds bills to the collection * @param amount of bills * @return value of collection */ public double addBills(int amount){ if(amount>0){ bills +=amount; value +=amount * ONE_BILL_VALUE; } return value /100.0; } /** Adds quarters to the collection * @param amount of quarters * @return value of collection */ public double addQuarters(int amount){ if(amount>0){ quarters +=amount; value +=amount * QUARTER_VALUE; } return value /100.0; } /** Add dimes to the collection * @param amount of dimes * @return value of collection */ public double addDimes(int amount){ if(amount>0){ dimes +=amount; value +=amount * DIME_VALUE; } return value /100.0; } /**Adds nickels to collection * @param amount of nickels * @return value of collection */ public double addNickels(int amount){ if(amount>0){ nickels +=amount; value +=amount * NICKEL_VALUE; } return value /100.0; } /** Adds pennies to collection * @param amount of pennies * @return value of collection */ public double addPennies(int amount){ if(amount>0){ pennies +=amount; value +=amount * PENNY_VALUE; } return value /100.0; } /** Removes bills from collection * @param amount of bills * @return amount of removed bills */ public int removeBills(int amount){ int removed = 0; if (amount>0){ removed = Math.min(amount, bills); bills -= removed; value -= removed * ONE_BILL_VALUE; } return removed; } /** Removes quarters from collection * @param amount of quarters * @return amount of quarters removed */ public int removeQuarters(int amount){ int removed = 0; if (amount>0){ removed = Math.min(amount, quarters); quarters -= removed; value -= removed * QUARTER_VALUE; } return removed; } /**Removes dimes from collection * @param amount of dimes * @return amount of dimes removed */ public int removeDimes(int amount){ int removed = 0; if (amount>0){ removed = Math.min(amount, dimes); dimes -= removed; value -= removed * DIME_VALUE; } return removed; } /** Removes nickels from collection * @param amount of nickels * @return amount of nickels removed */ public int removeNickels(int amount){ int removed = 0; if (amount>0){ removed = Math.min(amount, nickels); nickels -= removed; value -= removed * NICKEL_VALUE; } return removed; } /** Removes pennies from collection * @param amount of pennies * @return amount of pennies removed */ public int removePennies(int amount){ int removed = 0; if (amount>0){ removed = Math.min(amount, pennies); pennies -= removed; value -= removed * PENNY_VALUE; } return removed; } /** Removes cash from an existing cash collection * @param amount of cash to be retired * @return new amount of currency of collection */ public Cash remove(double amount){ Cash c = null; int toRemove = 0; int amountInCents = (int)(amount * 100.0); if(amountInCents > 0 && amountInCents <= value){ c = new Cash(); toRemove = amountInCents / ONE_BILL_VALUE; c.addBills(removeBills(toRemove)); amountInCents -= c.getBills() * ONE_BILL_VALUE; toRemove = amountInCents / QUARTER_VALUE; c.addQuarters(removeQuarters(toRemove)); amountInCents -= c.getQuarters() * QUARTER_VALUE; toRemove = amountInCents / DIME_VALUE; c.addDimes(removeDimes(toRemove)); amountInCents -= c.getDimes() * DIME_VALUE; toRemove = amountInCents / NICKEL_VALUE; c.addNickels(removeNickels(toRemove)); amountInCents -= c.getNickels() * NICKEL_VALUE; toRemove = amountInCents / PENNY_VALUE; c.addPennies(removePennies(toRemove)); amountInCents -= c.getPennies() * PENNY_VALUE; } return c; } /** Adds cash to an existing cash collection * @param c = cash to be added * @return new value of cash collection */ public double add(Cash c){ this.addBills(c.getBills()); this.addQuarters(c.getQuarters()); this.addDimes(c.getDimes()); this.addNickels(c.getNickels()); this.addPennies(c.getPennies()); return value/100.0; } /** Gets amount of bills of the cash collection * @return the bills within cash collection */ public int getBills() { return bills; } /** Gets amount of quarters of the cash collection * @return the quarters within cash collection */ public int getQuarters() { return quarters; } /** Gets amount of dimes of the cash collection * @return the dimes within cash collection */ public int getDimes() { return dimes; } /** Gets amount of nickels of the cash collection * @return the nickels within cash collection */ public int getNickels() { return nickels; } /** Gets amount of pennies of the cash collection * @return the pennies within cash collection */ public int getPennies() { return pennies; } /** Gets value of the cash collection * @return the value of the cash collection */ public double getValue() { return value/100.0; } }