Results 1 to 3 of 3
Thread: Average Sales... cont'd
- 02-16-2015, 12:10 AM #1
Member
- Join Date
- Jan 2015
- Posts
- 31
- Rep Power
- 0
Average Sales... cont'd
Okay, my avgSales method works when used in my test method, but when I call my getDailySales method my avgSales amounts to $0.00 no matter what the amount and number of customers is. Any tips on how to solve this? And when the resetRegister method is called and the user answers "yes" my amounts are not formatted to the currency format and the avgSales amounts to $NaN..
Here is my code
Java Code:import java.util.*; import java.text.*; import javax.swing.JOptionPane; /******************************************************************************* * Create a fancy class for a cash register. * * @author * @version February 17, 2015 * *******************************************************************************/ 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("Amount paid: " + fmt.format(payment)); System.out.println("Payment denied."); } else if (payment < currentAmount){ currentAmount = currentAmount - payment; System.out.println("Amount paid: " + fmt.format(payment)); System.out.println("Amount due: " + fmt.format(currentAmount)); } else if (payment > currentAmount){ currentAmount = currentAmount; System.out.println("Amount paid: " + fmt.format(payment)); System.out.println("Change: " + fmt.format(payment - currentAmount)); System.out.println("Thank you. "); dailySales += currentAmount; customers = (customers + 1); currentAmount = 0.00; } else{ System.out.println("Amount paid: " + fmt.format(payment)); 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)); } }
And here is my test method
Java Code:/** * Write a description of class FancyRegisterTest here. * * @author * @version February 17, 2014 */ public class fancyRegisterTest { /*************************************************************************** Fancy Cash register test. Tests all inputs/outputs ****************************************************************************/ 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(""); gMart.makePayment(-40); System.out.println(""); 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(""); gMart.makePayment(200); System.out.println(""); System.out.println("Total customers: " + gMart.getCustomers()); System.out.println("Sales for today: $"+ gMart.getDailySales()); System.out.println("Average sales: $" + gMart.getAvg()); 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(""); sMart.makePayment(18); 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("Average sales: $" + 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()); } }
- 02-16-2015, 12:33 AM #2
Member
- Join Date
- Jan 2015
- Posts
- 31
- Rep Power
- 0
Re: Average Sales... cont'd
Updated avgSales code
Java Code:public double getAvg(){ //Returns average sales of the day if (customers == 0){ avgSales = 0.00; } else{ avgSales = (dailySales / customers); } return avgSales; }
- 02-16-2015, 01:03 AM #3
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Average Sales... cont'd
I am not certain what the problem is. Can you show your output and what it is supposed to be. Here are a couple observations.
1. Class names should start with uppercase letters.
2. You getAvg routine should return an average. You are both returning an average and setting an instance field.
All you should do is return the dailySales / customers.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads
-
Average Sales
By glockner in forum New To JavaReplies: 18Last Post: 02-13-2015, 10:15 AM -
Re: Simple Calculator cont'd
By JavaHater in forum New To JavaReplies: 3Last Post: 01-07-2011, 03:00 AM
Bookmarks