I have the following CashRegister class where i must implement 3 methods:
-getSalesTotal total up sales for the day
-getSalesCount total number of sales for the day
- rest a reset method that returns all instance variables to 0

Here is my code any help on mistakes i may have made is great, but my real problem is that i keep getting missing return statement when i try to compile the program. TY

/**

   A cash register totals up sales and computes change due.

*/

public class CashRegister

{

   private double purchase;

   private double payment;

   private double purchaseTotal;
   private double purchaseCount;



   /**

      Constructs a cash register with no money in it.

   */

   public CashRegister()

   {

      purchase = 0;

      payment = 0;

      purchaseTotal = 0;
      purchaseCount = 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;
      purchaseCount++;
      purchaseTotal = purchaseTotal + 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.

      @return the change due to the customer

   */

   public double giveChange()

   {   

      double change = payment - purchase;

      purchase = 0;

      payment = 0;

      return change;

   }



   /**

      Get the total amount of all sales for the day.

      @param return sale total

   */

   public double getSalesTotal(double salesTotal)
   {
      salesTotal = purchaseTotal;
   }



   /**

      Get the total number of sales for the day.

      @param return the number of sales.

   */

   public double getSalesCount(double salesCount)
   {
      salesCount = purchaseCount; 

   }

   /**

      Reset counters and totals for the next days sales.

      @param none

   */

   public void reset(double resetComplete)	
   {      
      purchaseCount = 0;
      purchaseTotal = 0;
      purchase = 0;
      payment = 0;
      resetComplete = purchase + payment + purchaseTotal + purchaseCount;
   }	
}

Dani AI

Generated

Several distinct issues are causing the compiler error and the incorrect behavior seen in the thread. Getters that are declared to return a value must actually return something and should not be written to take a parameter and try to “output” via that parameter — Java is pass‑by‑value, so assigning to a parameter does not change the caller’s variable. The reset method should be void and simply zero the fields instead of computing and returning a meaningless sum. The sales counter is conceptually an integer (use int for purchaseCount) and purchaseTotal should accumulate only the sale amounts (not the running customer total). Finally, decide whether enterPayment should set or add to payment (use payment += amount if multiple coins/payments can be entered per customer).

A concise, corrected outline (fields + key methods) that avoids the problems above:

private double purchase;       // current customer's total
private double payment;
private double purchaseTotal;  // total sales for the day
private int purchaseCount;     // number of sales for the day

public void recordPurchase(double amount) {
  purchase += amount;
  purchaseTotal += amount;
  purchaseCount++;
}

public double getSalesTotal() {
  return purchaseTotal;
}

public int getSalesCount() {
  return purchaseCount;
}

public void reset() {
  purchase = 0.0;
  payment = 0.0;
  purchaseTotal = 0.0;
  purchaseCount = 0;
}

Practical notes and troubleshooting: the compiler’s “missing return statement” means a non‑void method can reach the end without returning a value. Use parameterless getters that directly return fields. If enterPayment should accept multiple calls per sale, use payment += amount; otherwise use payment = amount. The suggestions from (return values required), (getters shouldn't take parameters) and (accessor/mutator pattern) all align with the fixes above. A quick test sequence (record a sale, enter payment, call giveChange(), then check getSalesTotal()/getSalesCount()) will confirm correct behavior.

Recommended Answers

All 4 Replies

getSalesTotal() and getSalesCount() have been declared as double so they must return some value.
Declare them void if you do not wish to return any value from these methods

public double getSalesTotal(double salesTotal) {
  salesTotal = purchaseTotal;
}

You got yourself quite confused about get methods. You don't need to pass in the value if the purpose of the method is to get it! And setting the value of a parameter like that doesn't achieve anything anyway.
A typical get method looks like this example:

class Person {
  private String name;
  public String getName() {
    return name;
  }
  ...

I have made some changes to the code, my getSalesCount method is just the name of the method and was not meant to be

get.SalesCount a I think you mayb think I meant to do. I am very new to programmning so I could be wrong. In any case I have solved the missing return value by adding a return to the bottom of both getSalesCount and getSalesTotal. I have howeve encountered a new problem where I dont believe I am using the return method correctly. Any help on how to make this program work would be really appreciated.

/**

   A cash register totals up sales and computes change due.

*/

public class CashRegister

{

   private double purchase;

   private double payment;

   private double purchaseTotal;
   private double purchaseCount;



   /**

      Constructs a cash register with no money in it.

   */

   public CashRegister()

   {

      purchase = 0;

      payment = 0;

      purchaseTotal = 0;
      purchaseCount = 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;
      purchaseCount++;
      purchaseTotal = purchaseTotal + 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.

      @return the change due to the customer

   */

   public double giveChange()

   {   

      double change = payment - purchase;

      purchase = 0;

      payment = 0;

      return change;

   }



   /**

      Get the total amount of all sales for the day.

      @param return sale total

   */

   public double getSalesTotal(double salesTotal)
   {
      salesTotal = purchaseTotal;
      return salesTotal;
   }



   /**

      Get the total number of sales for the day.

      @param return the number of sales.

   */

   public double getSalesCount(double salesCount)
   {
      salesCount = purchaseCount;
      return salesCount; 

   }

   /**

      Reset counters and totals for the next days sales.

      @param none

   */

   public double reset(double resetComplete)	
   {      
      purchaseCount = 0;
      purchaseTotal = 0;
      purchase = 0;
      payment = 0;
      resetComplete = purchase + payment + purchaseTotal + purchaseCount;
      return resetComplete;  
   }  	
}

Read up on accessor and mutator functions.

public double getSalesTotal(double salesTotal)
{
salesTotal = purchaseTotal;
return salesTotal;
}

This should be two separate functions, it will help avoid confusion.

One function should 'set' the fields and another should 'get' a value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.