I am working on a Object Oriented Program that gives change for a certain Purchase. You give it the purchase amount and the coins and amount of coins that you are paying with.
The problem is that even though I'm not getting any errors, I'm also not getting any output.
Tester:
/** This program tests the CashRegister class. */ package application; import classes.*; public class CashRegisterCoinTester { public static void main(String[] args) { //define constants for PENNY_VALUE,NICKEL_VALUE,DIME_VALUE,QUARTER_VALUE final double QUARTER_VALUE = 0.25; final double DIME_VALUE = 0.1; final double NICKEL_VALUE = 0.05; final double PENNY_VALUE = 0.01; //create new cash register CashRegister register = new CashRegister(); //do some purcahse register.recordPurchase(20.00); //enter some payment usign pennies, Coin penny=new Coin(PENNY_VALUE,"penny"); Coin quarter=new Coin(QUARTER_VALUE,"quarter"); Coin dime=new Coin(DIME_VALUE,"dime"); Coin nickel=new Coin(NICKEL_VALUE,"nickel"); register.enterPayment(5,penny ); register.enterPayment(1,dime ); register.enterPayment(5,quarter ); register.enterPayment(5,nickel ); // new Coin(QUARTER_VALUE,"quarter"); //enter some payments using other coins too //get change register.giveChange(); //display change } }
Cash Register:
/** A cash register totals up sales and computes change due. */ package classes; public class CashRegister { // add two attributes (variables) purchase and payment private double purchase; private double payment; /** Constructor that constructs a cash register with no money in it. */ public CashRegister() { //initialize the purchase and payment to 0.0 double purchase = 0.0; double payment = 0.0; } /** Records the sale of an item. @param amount the price of the item */ public void recordPurchase(double amount) { //add the amount of purchase to purchase purchase = purchase + amount; } /** Enters the payment received from the customer; should be called once for each coin type. @param coinCount the number of coins @param coinType the type of the coins in the payment */ public void enterPayment(int coinCount, Coin coinType) { //calculate the payment payment = payment+ coinCount * coinType.getValue(); } /** Computes the change due and resets the machine for the next customer. @return the change due to the customer */ public double giveChange() { double change = payment - purchase; purchase = 0; payment = 0; return change; //calculate change,change is the difference between payment and purchase, return change } }
Coin:
/** A coin with a monetary value. */ package classes; public class Coin { //declare coin attributes/data/variable value & name double value; String name; /** Constructs a coin. @param aValue the monetary value of the coin. @param aName the name of the coin */ public Coin(double aValue, String aName) { //initialize the coin value and name to the arguments passed value = aValue; name = aName; } /** Gets the coin value. @return the value */ public double getValue() { return value; //return value } /** Gets the coin name. @return the name */ public String getName() { //return name, return name; } }