# Thread: Average Sales

1. Member
Join Date
Jan 2015
Posts
31
Rep Power
0

## Average Sales

I am working on trying to show the average daily sales for the store. I cannot quite figure out why my calculation is not working. Everything works except my average sales is always \$0.00... lines 185-189 is where my calculation is located.

Java Code:
```import java.util.*;
import java.text.*;
import javax.swing.JOptionPane;

/*******************************************************************************
* Create a fancy class for a cash register.
*
*
*******************************************************************************/
public class fancyRegister{

/** current amount due */
private double currentAmount;

/** daily sales */
private double dailySales;

/** average sales for the day */
private double avgSales;

/** number of customers */
private int customers;

/** store name */
private String name;

/** sales tax */
private final double TAX = 0.06;

/** calculate sales tax */
private double salesTax;

/** number format */
private NumberFormat fmt;

/***************************************************************************
Constructor for objects of class Register
****************************************************************************/
public fancyRegister(){
//Define variables
currentAmount = 0.00;
dailySales = 0.00;
avgSales = 0.00;
customers = 0;
fmt = NumberFormat.getCurrencyInstance();
name = "GVSU Corner Store";
salesTax = 0.00;
System.out.println("Welcome to " + name);

}

/**************************************************************************
Constructor for objects of class Register
Alternative constructor

@param pName name of store
***************************************************************************/
public fancyRegister(String pName){
//Define Variables
currentAmount = 0.00;
dailySales = 0.00;
avgSales = 0.00;
customers = 0;
name = pName;
salesTax = 0.00;
fmt = NumberFormat.getCurrencyInstance();
System.out.println("Welcome to " + pName + "!");

}

/***************************************************************************
Scan an item's price

@param price total price of object
****************************************************************************/
public void scanPrice(double price){
//Set up formula for current amount and apply number format
currentAmount += price;
System.out.println("Price: " + fmt.format(price));

}

/***************************************************************************
Get current amount due

@return current amount due
****************************************************************************/
public double getPrice(){
//Tells customer how much is due
System.out.println("Your total is: " + fmt.format(currentAmount));
return currentAmount;
}

/***************************************************************************
Get daily sales

@return daily sales
****************************************************************************/
public double getDailySales(){
return dailySales;
}

/***************************************************************************
Get number of customers

@return number of customers
****************************************************************************/
public int getCustomers(){
return customers;
}

/***************************************************************************
Get name of store

@return name of store
****************************************************************************/
public String getName(){
return name;
}

/***************************************************************************
Tally Purchases

@param purchases amount of purchase after sales tax included
****************************************************************************/
public void tallyPurchases(){
//Gets total amount of purchases including sales tax and applies number format
System.out.println("");
salesTax = (currentAmount * TAX);
currentAmount = salesTax + currentAmount;
System.out.println("Sales Tax: " + fmt.format(salesTax));
System.out.println("Amount Due: " + fmt.format(currentAmount));

}

/***************************************************************************
Cancel Purchase

@param cancel all purchases
****************************************************************************/
public void cancelPurchases(){
//Cancels a purchase, resetting amount due to 0
currentAmount = 0.00;
System.out.println("Purchase cancelled.");

}

/***************************************************************************
Make a payment

@param payment for sale
****************************************************************************/
public void makePayment(double payment){
//Makes payment, adds payment to the total daily sales, increases customer count by 1
if (payment < 0){
System.out.println("Payment denied.");
}
else if (payment < currentAmount){
currentAmount = currentAmount - payment;
System.out.println("You owe: " + fmt.format(currentAmount));
}
else if (payment > currentAmount){
currentAmount = currentAmount;
System.out.print("Thank you. ");
System.out.println("Your change: " + fmt.format(payment - currentAmount));
dailySales += currentAmount;
customers = (customers + 1);
currentAmount = 0.00;
}
else{
System.out.println("Thank you. Have a nice day!");
dailySales += currentAmount;
customers = (customers + 1);
currentAmount = 0.00;

}

}

/***************************************************************************
Calculate average sales for the day

@param average total sales of day
****************************************************************************/
public void averageSale(double avg){
//Formula for calculating average sales of the day
avgSales = (dailySales / customers);

}

/***************************************************************************
Get avaerage sales

@return average sales for the day
****************************************************************************/
public double getAvg(){
//Returns average sales of the day
return avgSales;
}

/***************************************************************************
Reset register
****************************************************************************/
public void resetRegister(){
//Resets register totals to 0
String message = "Reset Register?";
int answer = JOptionPane.showConfirmDialog(null, message);
if (answer == JOptionPane.YES_OPTION) {
dailySales = 0.00;
avgSales = 0.00;
currentAmount = 0.00;
customers = 0;

}
}

/***************************************************************************
Daily Sales Report
****************************************************************************/
public void dailySalesReport(){
//Daily sales report consisting of store name, total customers, and daily sales
System.out.println(name);
System.out.println("Number of Customers: " + customers);
System.out.println("Total Sales for Today: " + fmt.format(dailySales));
System.out.println("Average sales for Today: " + fmt.format(avgSales));
}

}```

2. ## Re: Average Sales

How are you debugging the code?
Do all the amounts get added into the total?
Java Code:
`public void averageSale(double avg){`
What is avg for? It's not used.

3. Member
Join Date
Jan 2015
Posts
31
Rep Power
0

## Re: Average Sales

It is supposed to be used to get the average sales for the whole day. After a payment is made the sale goes towards the daily sales. So if two customers buy \$50 worth of stuff (sales tax included) the average sales should equal \$25.

4. ## Re: Average Sales

It is supposed to ...
Yes, but what does the code do? Does it add up the values so an average can be computed?

How are you debugging the code to see what it is doing?

5. Member
Join Date
Jan 2015
Posts
31
Rep Power
0

## Re: Average Sales

dailySales is the total of all the sales for the day. Depending on how many customers and how much is bought they all get totaled to dailySales. And I am using BlueJ as my IDE.

6. ## Re: Average Sales

Is the value of dailySales changing as the code executes?

7. Member
Join Date
Jan 2015
Posts
31
Rep Power
0

## Re: Average Sales

Yes, after a payment is made the "money" goes towards the dailySales. So if two people paid for 2 items of \$25 each, dailySales would be \$50.

8. ## Re: Average Sales

How do you execute the code for testing? It does not have a main() method.

9. Member
Join Date
Jan 2015
Posts
31
Rep Power
0

## Re: Average Sales

I have another class used for testing.

10. ## Re: Average Sales

How can someone else test the code?

11. Member
Join Date
Jan 2015
Posts
31
Rep Power
0

## Re: Average Sales

Now with adding in average sales I get an error message saying they cannot find the method for average sales.

Java Code:
```
/**
* Write a description of class FancyRegisterTest here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class FancyRegisterTest
{
/***************************************************************************
Cash register test. Tests all inputs/outputs
****************************************************************************/
public static void main (String args []){
cashRegister gMart = new cashRegister("George's Mart");
System.out.println("");
System.out.println("Items");
gMart.scanPrice(10);
gMart.scanPrice(20);
System.out.println("");
gMart.getPrice();
gMart.tallyPurchases();
System.out.println("");
gMart.makePayment();
System.out.println("");
System.out.println("Items");
gMart.scanPrice(14);
gMart.scanPrice(134.10);
System.out.println("");
gMart.getPrice();
gMart.tallyPurchases();
System.out.println("");
gMart.makePayment();
System.out.println("");
System.out.println("Total customers: " + gMart.getCustomers());
System.out.println("Sales for today: \$"+ gMart.getDailySales());
System.out.println("");
System.out.println("");
System.out.println("");

cashRegister sMart = new cashRegister("Scott's Mart");
System.out.println("");
System.out.println("Items");
sMart.scanPrice(10);
sMart.scanPrice(20);
System.out.println("");
sMart.getPrice();
sMart.cancelPurchases();
System.out.println("");
System.out.println("Items");
sMart.scanPrice(95);
sMart.scanPrice(18.50);
System.out.println("");
sMart.getPrice();
sMart.tallyPurchases();
System.out.println("");
sMart.makePayment();
System.out.println("");
System.out.println("Daily Store Report");
System.out.println("Total customers: " + sMart.getCustomers());
System.out.println("Sales for today: \$"+ sMart.getDailySales());
System.out.println("Sales for today: \$" + sMart.getAvg());
System.out.println("");
System.out.println("Reset Register");
sMart.resetRegister();
System.out.println("Daily Store Report");
System.out.println("Total customers: " + sMart.getCustomers());
System.out.println("Sales for today: \$"+ sMart.getDailySales());
System.out.println("Average sales for today: \$" + sMart.getAvg());
}
}```

12. ## Re: Average Sales

I get an error message
Please copy the full text of the error message and paste it here. It has important info about the error.

What is the code in post#11 for?

BTW Java naming conventions say that class names should start with an Uppercase letter.
Last edited by Norm; 02-12-2015 at 10:38 PM.

13. Member
Join Date
Jan 2015
Posts
31
Rep Power
0

## Re: Average Sales

That is the testing method.

Error message says: "Cannot find symbol - method getAvg()"

14. ## Re: Average Sales

It looks like a testing class for some other class, not for the fancyRegister(should be FancyRegister) class.
Do you have a testing class for the fancyRegister class?

Please copy full text of the compiler's error message and paste it here.
The message should show the source with a ^ under the location of the error.
Here is a sample from the javac compiler:
Java Code:
```TestSorts.java:138: cannot find symbol
symbol  : variable var
location: class TestSorts
var = 2;
^```

15. Member
Join Date
Jan 2015
Posts
31
Rep Power
0

## Re: Average Sales

This method was all in one in the same class but I was told to make a new class for testing... do I just change name of class to FancyRegister instead of FancyRegisterTest? Still get same error.

16. ## Re: Average Sales

but I was told to make a new class for testing
That seems silly to me. I prefer to have the testing code in the class that is being tested. When done testing the special code for testing (usually the main() method) can be commented out.

The code in post#11 doesn't test the FancyRegister class.

17. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Average Sales

Imho, you never sufficiently answered Norm's questions in posts #2 and #4. If you had investigated what he was asking you might have resolved your problems.

Regards,
Jim

18. Member
Join Date
Jan 2015
Posts
31
Rep Power
0

## Re: Average Sales

Okay, everything is fixed and working properly thank you all.

Java Code:
```import java.util.*;
import java.text.*;
import javax.swing.JOptionPane;

/*******************************************************************************
* Create a fancy class for a cash register.
*
*
*
*******************************************************************************/
public class fancyRegister{

/** current amount due */
private double currentAmount;

/** daily sales */
private double dailySales;

/** average sales for the day */
private double avgSales;

/** number of customers */
private int customers;

/** store name */
private String name;

/** sales tax */
private final double TAX = 0.06;

/** calculate sales tax */
private double salesTax;

/** number format */
private NumberFormat fmt;

/***************************************************************************
Constructor for objects of class Register
****************************************************************************/
public fancyRegister(){
//Define variables
currentAmount = 0.00;
dailySales = 0.00;
avgSales = 0.00;
customers = 0;
fmt = NumberFormat.getCurrencyInstance();
name = "GVSU Corner Store";
salesTax = 0.00;
System.out.println("Welcome to " + name);

}

/**************************************************************************
Constructor for objects of class Register
Alternative constructor

@param pName name of store
***************************************************************************/
public fancyRegister(String pName){
//Define Variables
currentAmount = 0.00;
dailySales = 0.00;
avgSales = 0.00;
customers = 0;
name = pName;
salesTax = 0.00;
fmt = NumberFormat.getCurrencyInstance();
System.out.println("Welcome to " + pName + "!");

}

/***************************************************************************
Scan an item's price

@param price total price of object
****************************************************************************/
public void scanPrice(double price){
//Set up formula for current amount and apply number format
currentAmount += price;
System.out.println("Price: " + fmt.format(price));

}

/***************************************************************************
Get current amount due

@return current amount due
****************************************************************************/
public double getPrice(){
//Tells customer how much is due
System.out.println("Your total is: " + fmt.format(currentAmount));
return currentAmount;
}

/***************************************************************************
Get daily sales

@return daily sales
****************************************************************************/
public double getDailySales(){
return dailySales;
}

/***************************************************************************
Get number of customers

@return number of customers
****************************************************************************/
public int getCustomers(){
return customers;
}

/***************************************************************************
Get name of store

@return name of store
****************************************************************************/
public String getName(){
return name;
}

/***************************************************************************
Tally Purchases

@param purchases amount of purchase after sales tax included
****************************************************************************/
public void tallyPurchases(){
//Gets total amount of purchases including sales tax and applies number format
System.out.println("");
salesTax = (currentAmount * TAX);
currentAmount = salesTax + currentAmount;
System.out.println("Sales Tax: " + fmt.format(salesTax));
System.out.println("Amount Due: " + fmt.format(currentAmount));

}

/***************************************************************************
Cancel Purchase

@param cancel all purchases
****************************************************************************/
public void cancelPurchases(){
//Cancels a purchase, resetting amount due to 0
currentAmount = 0.00;
System.out.println("Purchase cancelled.");

}

/***************************************************************************
Make a payment

@param payment for sale
****************************************************************************/
public void makePayment(double payment){
//Makes payment, adds payment to the total daily sales, increases customer count by 1
if (payment < 0){
System.out.println("Payment denied.");
}
else if (payment < currentAmount){
currentAmount = currentAmount - payment;
System.out.println("You owe: " + fmt.format(currentAmount));
}
else if (payment > currentAmount){
currentAmount = currentAmount;
System.out.println("Your change: " + fmt.format(payment - currentAmount));
System.out.println("Thank you. ");
dailySales += currentAmount;
customers = (customers + 1);
currentAmount = 0.00;
}
else{
System.out.println("Thank you. Have a nice day!");
dailySales += currentAmount;
customers = (customers + 1);
currentAmount = 0.00;

}

}

/***************************************************************************
Get avaerage sales

@return average sales for the day
****************************************************************************/
public double getAvg(){
//Returns average sales of the day
avgSales = dailySales / customers;
return avgSales;
}

/***************************************************************************
Reset register
****************************************************************************/
public void resetRegister(){
//Resets register totals to 0
String message = "Reset Register?";
int answer = JOptionPane.showConfirmDialog(null, message);
if (answer == JOptionPane.YES_OPTION) {
dailySales = 0.00;
avgSales = 0.00;
currentAmount = 0.00;
customers = 0;

}
}

/***************************************************************************
Daily Sales Report
****************************************************************************/
public void dailySalesReport(){
//Daily sales report consisting of store name, total customers, and daily sales
System.out.println(name);
System.out.println("Number of Customers: " + customers);
System.out.println("Total Sales for Today: " + fmt.format(dailySales));
System.out.println("Average sales for Today: " + fmt.format(avgSales));
}

public static void main (String args []){
fancyRegister gMart = new fancyRegister("George's Mart");
System.out.println("");
System.out.println("Items");
gMart.scanPrice(10);
gMart.scanPrice(20);
System.out.println("");
gMart.getPrice();
gMart.tallyPurchases();
System.out.println("");
System.out.println("Amount Paid: \$40.00 ");
gMart.makePayment(40);
System.out.println("");
System.out.println("Items");
gMart.scanPrice(14);
gMart.scanPrice(134.10);
System.out.println("");
gMart.getPrice();
gMart.tallyPurchases();
System.out.println("");
System.out.println("Amount Paid: \$200.00");
gMart.makePayment(200);
System.out.println("");
System.out.println("Total customers: " + gMart.getCustomers());
System.out.println("Sales for today: \$"+ gMart.getDailySales());
System.out.println("");
System.out.println("");
System.out.println("");

fancyRegister sMart = new fancyRegister("Scott's Mart");
System.out.println("");
System.out.println("Items");
sMart.scanPrice(10);
sMart.scanPrice(20);
System.out.println("");
sMart.getPrice();
sMart.cancelPurchases();
System.out.println("");
System.out.println("Items");
sMart.scanPrice(95);
sMart.scanPrice(18.50);
System.out.println("");
sMart.getPrice();
sMart.tallyPurchases();
System.out.println("");
System.out.println("Amount Paid: \$18.00 ");
sMart.makePayment(18);
System.out.println("Amount Paid: \$1,000.000");
sMart.makePayment(1000);
System.out.println("");
System.out.println("Daily Store Report");
System.out.println("Total customers: " + sMart.getCustomers());
System.out.println("Sales for today: \$"+ sMart.getDailySales());
System.out.println("Sales for today: \$" + sMart.getAvg());
System.out.println("");
System.out.println("Reset Register");
sMart.resetRegister();
System.out.println("Daily Store Report");
System.out.println("Total customers: " + sMart.getCustomers());
System.out.println("Sales for today: \$"+ sMart.getDailySales());
System.out.println("Average sales for today: \$" + sMart.getAvg());
}

}```

19. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
12

## Re: Average Sales

Originally Posted by Norm
That seems silly to me. I prefer to have the testing code in the class that is being tested. When done testing the special code for testing (usually the main() method) can be commented out.
(sounds to me the idea was to create a unit test)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•