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 2 of 2

Thread: Alright guys, I am having some serious trouble with this one.

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Alright guys, I am having some serious trouble with this one.

    Here is one part of the project that is stumping me. I don't think I understand how this stuff works. Here is what I have to try to accomplish...

    "write a program that uses the purchase class to set the following prices:

    oranges, 10 for 2.99
    eggs, 12 for 1.69
    apples, 3 for 1.00
    watermelons, 4.39 each
    bagels, 6 for 3.50

    Then calculate the total bill for each item for the following:

    2 dozen oranges
    3 dozen eggs
    20 apples
    2 watermelons
    1 dozen bagels"

    here is the purchase class that is referenced in the question...

    import java.util.Scanner;
    /**
    Class for the purchase of one kind of item, such as 3 oranges.
    Prices are set supermarket style, such as 5 for $1.25.
    */
    public class Purchase
    {
    	private String name;
    	private int groupCount; //Part of a price, like the 2 in 2 for $1.99.
    	private double groupPrice; //Part of a price, like the $1.99.
    	//in 2 for $1.99.
    	private int numberBought; //Number of items bought.
     
    	public void setName (String newName)
    	{
    		name = newName;
    	}
     
    	/**
    	Sets price to count pieces for $costForCount.
    	For example, 2 for $1.99.
    	*/
    	public void setPrice (int count, double costForCount)
    	{
    		if ((count <=0) || (costForCount <=0))
    		{
    			System.out.println ("Error: Bad parameter in setPrice.");
    			System.exit (0);
    		}
    		else
    		{
    			groupCount = count;
    			groupPrice = costForCount;
    		}
    	}
     
    	public void setNumberBought (int number)
    	{
    		if (number <= 0)
    		{
    			System.out.println ("Error: Bad parameter in setNumberBought.");
    			System.exit (0);
    		}
    		else
    		{
    			numberBought = number;
    		}
    	}
     
    		/**
    		Reads from the keyboard the price and number of a purchase.
    		*/
    		public void readInput ()
    		{
    			Scanner keyboard = new Scanner (System.in);
    			System.out.println ("Enter name of item you are purchasing:");
    			name = keyboard.nextLine ();
    			System.out.println ("Enter the price of an item as 2 numbers.");
    			System.out.println ("For example, 3 for $2.99 is entered as");
    			System.out.println ("3 2.99");
    			System.out.println ("Enter the price of an item as two numbers, now:");
    			groupCount = keyboard.nextInt ();
    			groupPrice = keyboard.nextDouble ();
    			while ((groupCount <= 0) || (groupPrice <= 0))
    			{ //Try again:
    				System.out.println ("Both numbers must be positive.  Try again.");
    				System.out.println ("Enter the price of an item as 2 numbers.");
    				System.out.println ("For example, 3 for $2.99 is entered as");
    				System.out.println ("3 2.99");
    				System.out.println ("Enter the price of an item as two numbers, now:");
    				groupCount = keyboard.nextInt ();
    				groupPrice = keyboard.nextDouble ();
    			}
    			System.out.println ("Enter the number of items purchased:");
    			numberBought = keyboard.nextInt ();
    			while (numberBought <= 0)
    			{ //Try again:
    				System.out.println ("The number must be positive.  Try again.");
    				System.out.println ("Enter the number of items purchased:");
    				numberBought = keyboard.nextInt ();
    			}
    		}
     
    		/**
    		Displays the price and number being purchased
    		*/
    		public void writeOutput ()
    		{
    			System.out.println (numberBought + " " + name);
    			System.out.println ("at " + groupCount + " for $" + groupPrice);
    		}
    		public String getName ()
    		{
    			return name;
    		}
     
    		public double getTotalCost ()
    		{
    			return (groupPrice / groupCount) * numberBought;
    		}
     
    		public double getUnitCost ()
    		{
    			return groupPrice / groupCount;
    		}
     
    		public int getNumberBought ()
    		{
    			return numberBought;
    		}
    }

    Can someone give me some insight as to what I am supposed to be doing. I don't think I understand the question.


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Alright guys, I am having some serious trouble with this one.

    Your doing to much in one class. Try separating into a different class (or several, if you'd like) that you can instantiate as just a data class. Then move all your input/output to a seperate class (MainDriver is a good name, Driver if you are sure you won't have to input any of the packages that have a class named that, such as java.sql.Driver), that way you can has-a with Product.

    An example would be
    class Product has methods getName, setName, getUnitCost, getGroupCost, getGroupSize, setUnitCost, setGroupCost, setGroupSize, getNumBought, and buy. (buy would add one to number bought)

    class MainDriver has main and any helper methods necessary.

    A google search found a decent tutorial on data classes in java.
    Java Object Oriented Programming User-Defined Classes

Similar Threads

  1. Hi Guys
    By inayat in forum Java Networking
    Replies: 2
    Last Post: October 7th, 2011, 03:31 AM
  2. Hello guys
    By geekboy in forum Member Introductions
    Replies: 1
    Last Post: July 27th, 2011, 02:51 AM
  3. hi guys
    By johnyabu@gmail.com in forum Member Introductions
    Replies: 1
    Last Post: February 2nd, 2011, 12:13 PM
  4. Hey guys
    By StarKannon in forum Member Introductions
    Replies: 2
    Last Post: February 2nd, 2011, 04:58 AM
  5. JDBCrealm FORM based problem. But BASIC working perfectly alright!
    By salman4u in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: January 27th, 2010, 09:06 PM