We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,007 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Missing return statement!

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;
   }	
}
4
Contributors
4
Replies
2 Hours
Discussion Span
1 Year Ago
Last Updated
5
Views
nick100555
Newbie Poster
2 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

stephen84s
Nearly a Posting Virtuoso
1,444 posts since Jul 2007
Reputation Points: 668
Solved Threads: 156
Skill Endorsements: 10
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;
  }
  ...
JamesCherrill
... trying to help
Moderator
8,516 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

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;  
   }  	
}
nick100555
Newbie Poster
2 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

Syrne
Junior Poster
104 posts since Sep 2010
Reputation Points: 30
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1063 seconds using 2.72MB