I'm trying to figure out how to transfer several methods in a cash register into one void method. Here is my code.

/**
 *  A cash register totals up sales and computes change due.
 */
public class CashRegister
{
   private double purchase;
   private double payment;
   /**
    *  Constructs a cash register with no money in it.
       */
   public CashRegister()
   {
      purchase = 0;
      payment = 0;
   }

   /**
    *  Records the sale of an item.
    *  @param amount the price of the item
    */
   public void recordPurchase(double amount)
   {
      double total = purchase + amount;
      purchase = total;
   }

   /**
    *  Enters the payment received from the customer.
    *  @param amount the amount of the payment
    */
   public void enterPayment(double amount)
   {
      payment = amount;
   }

   /**
    * Computes the change due and resets the machine for the next customer, and
    * Print a receipt.
    */
  public void printReceipt()
  {
     double change = payment - purchase;
     System.out.printf("   Total Sales: $ %.2f\n", purchase);
     System.out.printf("       Payment: $ %.2f\n", payment);
     System.out.printf("==========================\n");
     if (payment < purchase) {
         System.out.printf("Not Enough Payment: $ %.2f\n\n", change);
     }
     else {
         purchase = 0;
         payment = 0;
         System.out.printf("    Change Due: $ %.2f\n\n", change);
     }
   }

}

This is the class with the separate method. I want the methods in that to run in the code below.

public class RunCashRegister
{  
   public void run()
   {

   }
}

Help would be most appreciated as I am very new to java. Thank you.

Edited 3 Years Ago by Dani: Formatting fixed

You have to instantiate a new CashRegister and then call the methods, with <variable name>.<method name>(parameters)

For example, if I made a new CashRegister c, then I could call printReceipt() with c.printReceipt().

This article has been dead for over six months. Start a new discussion instead.