Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: SoftwareSales Program

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    29
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default SoftwareSales Program

    Hello, I'm having trouble with my SoftwareSales.java program that involves another demo part. I have it running but my output seems off, something tells me its a simple fix but I'm having trouble again. Thanks for any tips/suggestions.

    SoftwareSales.java

    package chapter4;
     
    public class SoftwareSales 
    {
    	private double retailCostPerUnit = 99.00;
    	private double costBeforeDiscount;
    	private double costAfterDiscount;
    	private double discount;
    	private double unitsSold;
    	private double cost;
     
    	public SoftwareSales(int units) 
    	{
    		unitsSold = units;
    	}
     
    	public void setUnitsSold(int units)
    	{
    		unitsSold = units;
    	}
     
    	public double getDiscount()
    	{
    		costBeforeDiscount = retailCostPerUnit * unitsSold;
     
    		if (unitsSold >= 100)
    		{
    			discount = costBeforeDiscount * 0.5;
    		}
     
    		else if(unitsSold >= 50)
    		{
    			discount = costBeforeDiscount * 0.4;
    		}
     
    		else if(unitsSold >= 20)
    		{
    			discount = costBeforeDiscount * 0.3;
    		}
     
    		else if (unitsSold >= 10)
    		{
    			discount = costBeforeDiscount * 0.2;
    		}
     
    		else
    		{
    			discount = 0;
    		}
     
    		costAfterDiscount = costBeforeDiscount - discount;
     
    		return discount;
    	}
     
    	public double getUnitsSold() 
    	{
    		return unitsSold;
    	}
     
    	public double getCost()
    	{
    		cost = retailCostPerUnit - discount;
     
    		return cost;
    	}
    }

    SoftwareSalesDemo.java

    package chapter4;
     
    import java.text.DecimalFormat;
    import java.util.Scanner;
     
    public class SoftwareSalesDemo 
    {
    	public static void main(String[] args) 
    	{
    		int units; 
     
    		// Create a Scanner object for keyboard input.
    		Scanner keyboard = new Scanner(System.in);
     
    		// Get the units sold.
    		System.out.print("Enter the units sold: ");
    		units = keyboard.nextInt();
     
    		// Create a SoftwareSales object.
    		SoftwareSales sales = new SoftwareSales(units);
     
    		// Display purchase info.
    		DecimalFormat dollar = new DecimalFormat("#, ## 0.00");
    		System.out.println("Units sold: " + sales.getUnitsSold());
    		System.out.println("Discount: $" + dollar.format(sales.getDiscount()));
    		System.out.println("Cost: $" + dollar.format(sales.getCost()));
     
    		keyboard.close();
    	}
    }


  2. #2
    Junior Member
    Join Date
    Jun 2014
    Posts
    22
    My Mood
    Starving
    Thanks
    1
    Thanked 8 Times in 6 Posts

    Default Re: SoftwareSales Program

    So what exactly is the problem? I am not sure exactly what it is supposed to do so I can't tell what is wrong.

    --- Update ---

    I noticed your getDiscount method returns the variable discount, but I think you wanted to return costAfterDiscount.

    you have this statement : costAfterDiscount = costBeforeDiscount - discount;

    and then you just return discount anyway, so I'm assuming you want to return costAfterDiscount.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    29
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: SoftwareSales Program

    I'm just suspecting my output is funny and/or wrong. This is what I get after making that slight change:

    Enter the units sold: 100
    Units sold: 100.0
    Discount: $4,950.00  
    Cost: $4,851.00

    Which is the same output as I got before.

  4. #4
    Junior Member
    Join Date
    Jun 2014
    Posts
    22
    My Mood
    Starving
    Thanks
    1
    Thanked 8 Times in 6 Posts

    Default Re: SoftwareSales Program

    And it seems that your getCost method should look more like:



    cost = costBeforeDiscount - discount;
    return cost;

  5. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    29
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: SoftwareSales Program

    Making that change, now my output displays:

    Enter the units sold: 100
    Units sold: 100.0
    Discount: $4,950.00  
    Cost: $4,950.00

    Update:

    Entering 100 seems to calculate it right, but doing other numbers it doesn't seem to.
    Last edited by Caffeine; June 8th, 2014 at 11:03 PM.

  6. #6
    Junior Member
    Join Date
    Jun 2014
    Posts
    22
    My Mood
    Starving
    Thanks
    1
    Thanked 8 Times in 6 Posts

    Default Re: SoftwareSales Program

    Actually, at 100 it should be 50% off right? which would make that correct..

    --- Update ---

    But for others it is wrong, so you are right, you do want to go back and return discount for getDiscount.

    Still I believe getCost should be changed to:
    	public double getCost()
    	{
                    cost = costBeforeDiscount - discount;                
    		return cost;
    	}


    I am getting an output like this
    Enter the units sold: 40
    Units sold: 40.0
    Discount: $1,188.00  
    Cost: $2,772.00

    Is that correct output?

  7. #7
    Junior Member
    Join Date
    Jun 2014
    Posts
    29
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: SoftwareSales Program

    40 * 99 = $3,960

    $3,960 - $1,188.00 (discount) = $2,772.00

    So yes that is correct. And that seemed to of fixed it all. Thank you greatly for your help Koder, much appreciated.

  8. #8
    Junior Member
    Join Date
    Jun 2014
    Posts
    22
    My Mood
    Starving
    Thanks
    1
    Thanked 8 Times in 6 Posts

    Default Re: SoftwareSales Program

    No problem. Glad I could help.


    Just a tip: This was a little difficult to understand because calculations are happening in different places (which is why I was confused about the first part)

    I would recommend doing your calculations once you gather the number of units. This will make your code easier to read and easier to tweak.


    So your code could look something like this:


     
    {
    	private double retailCostPerUnit = 99.00;
    	private double costBeforeDiscount;
    	private double costAfterDiscount;
    	private double discount;
    	private double unitsSold;
    	private double cost;
     
    	public Million(int units) 
    	{
    		unitsSold = units;
    	}
     
    	public void setUnitsSold(int units)
    	{
    		unitsSold = units;
                    setDiscount();
                    costAfterDiscount = costBeforeDiscount - discount;
                    cost = costBeforeDiscount - discount;                
     
     
     
    	}
     
    	public void setDiscount()
    	{
    		costBeforeDiscount = retailCostPerUnit * unitsSold;
     
    		if (unitsSold >= 100)
    		{
    			discount = costBeforeDiscount * 0.5;
    		}
     
    		else if(unitsSold >= 50)
    		{
    			discount = costBeforeDiscount * 0.4;
    		}
     
    		else if(unitsSold >= 20)
    		{
    			discount = costBeforeDiscount * 0.3;
    		}
     
    		else if (unitsSold >= 10)
    		{
    			discount = costBeforeDiscount * 0.2;
    		}
     
    		else
    		{
    			discount = 0;
    		}
     
     
    	}
     
            public double getDiscount(){
                return discount;
            }
     
    	public double getUnitsSold() 
    	{
    		return unitsSold;
    	}
     
    	public double getCost()
    	{
    		return cost;
    	}
    }

    	public static void main(String[] args) 
    	{
    		int units; 
     
    		// Create a Scanner object for keyboard input.
    		Scanner keyboard = new Scanner(System.in);
     
    		// Get the units sold.
    		System.out.print("Enter the units sold: ");
    		units = keyboard.nextInt();
     
    		// Create a SoftwareSales object.
    		Million sales = new Million(units);
     
    		// Display purchase info.
    		DecimalFormat dollar = new DecimalFormat("#, ## 0.00");
    		System.out.println("Units sold: " + sales.getUnitsSold());
                    sales.setUnitsSold(units); //added this
    		System.out.println("Discount: $" + dollar.format(sales.getDiscount()));
    		System.out.println("Cost: $" + dollar.format(sales.getCost()));
     
    		keyboard.close();
    	}
    }


    I know mine isn't perfect either, but you can see that all the calculations are done in one place so it makes it easier to read without needing to jump around. Also makes it easier to debug since its more compact. Once you get the units you can find all of your other variables, so why not do it in the setUnitsSold method like that?

  9. #9
    Junior Member
    Join Date
    Jun 2014
    Posts
    29
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: SoftwareSales Program

    Looks good, yeah I apologize for the confusion, I myself was because of the calculations. But I'll def consider the revised code you provided, right now going to take a break from all this coding. Been at it for a few hours now.

  10. #10
    Junior Member
    Join Date
    Jun 2014
    Posts
    22
    My Mood
    Starving
    Thanks
    1
    Thanked 8 Times in 6 Posts

    Default Re: SoftwareSales Program

    Quote Originally Posted by Caffeine View Post
    Looks good, yeah I apologize for the confusion, I myself was because of the calculations. But I'll def consider the revised code you provided, right now going to take a break from all this coding. Been at it for a few hours now.


    Sometimes the best thing to do is take a break, but it can be difficult to do when you know the answer is going to be something so simple!

Similar Threads

  1. How to restart the math in the program without geting out of the program ?
    By Dragon3002 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 15th, 2014, 06:10 AM
  2. Replies: 1
    Last Post: February 6th, 2014, 01:11 PM
  3. Invoke a Java program with windows program
    By jackhard in forum Object Oriented Programming
    Replies: 1
    Last Post: February 21st, 2013, 07:16 AM
  4. Program goes into infinite compilation. University project - Library Program.
    By clarky2006 in forum What's Wrong With My Code?
    Replies: 35
    Last Post: November 10th, 2012, 03:56 PM
  5. Replies: 1
    Last Post: July 8th, 2012, 11:23 AM