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

Thread: Struggling Student seeks Guidance...

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Location
    San Antonio, TX
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Struggling Student seeks Guidance...

    I'm having trouble trying to return a value in a method I have created. Here is the code, class Cupcake. In my PurchaseQuantity() method, I'm supposed to prompt a user for the number of donuts they want and then return an integer value (given some value checking). In my test class, when I call the toString() for Cupcake I get the right int answer for PurchaseQuantity(), but when I try to call the value for CalculatePrice(), i get re-prompted for the number of donuts...should I just get returned the int value for PurchaseQuantity() and be getting prompted again? I'm probably missing something basic here...

    This class assignment isn't due for another two weeks, but I'm really scratching my head on this one, and can't wait 'til next week to get to a TA. This is a superclass, so my other subclass are depending on this one being right. So I'm stuck...Any help or guidance would be appreciated.


    import java.util.Scanner;
     
    public class Cupcake {
     
    	//Declare instance variables
    	private String flavors;
    	private double price;
    	private int quantitySold = 0; //initiate to 0
    	//Scanner input = new Scanner(System.in);
     
    	//Cupcake constructor
        public Cupcake( String ccflavors, double ccprice ) {
        	setFlavors( ccflavors );
        	setPrice( ccprice );
        }
     
        //Set-Get methods for flavors
        public void setFlavors( String ccflavors ){
        	flavors = ccflavors;
        }
     
        public String getFlavors(){
        	return flavors;
        }
     
        //Set-Get methods for price
        public void setPrice( double ccprice ){
        	price = ccprice;
        }
     
        public double getPrice(){
        	return price;
        }
     
        //Set-Get methods for quantitySold
        public void setQtySold( int qty ){
        	quantitySold = qty;
        }
     
        public int getQtySold(){
        	return quantitySold;
        }
     
        //Start PurchaseQuantity method
        public int PurchaseQuantity(){
        	int local_qty;
     
        	Scanner input = new Scanner( System.in );
        	System.out.println( "Please enter the number of cupcakes requested." ); //prompt Ms. Del, probably needs to be a Scanner
        	local_qty = input.nextInt();
     
        	//Verify that the quantity enter is valid
        	//Case if quantity is greater than 5 dozen limit
        	if(local_qty > 60){
        		System.out.println( "Sorry, Delicious Cupcake limits customers to five (5) dozen cupcakes per flavor." );
        		setQtySold( 0 );    		  		    		
        	}
        	//Case if quantity is valid
        	else if( (61 > local_qty) && (local_qty > 0) ){
        		setQtySold( local_qty );  
    		//System.out.printf( "else if local_qty = %d\n", getQtySold() );      		   		
        	}
        	//Case if quantity is 0 or negative
        	else{
        		System.out.println( "The value enter is not valid, please enter an integer (i.e. 1, 12, 36, etc.)." );
        		setQtySold( 0 );		    		
        	}
     
    	return getQtySold();		
     
        } //End PurchaseQuantity method
     
        public void CalculatePrice(){
    	int x = PurchaseQuantity(); //causes a loop into PurchaseQuantity method, doesn't call the value...why?
        	System.out.printf( "PurchaseQuantity = %d\n", x );
        }
     
        //Start CalculatePrice method
    //    public void CalculatePrice(){
    //    	System.out.printf( "PurchaseQuantity = %d\n", getQtySold() );
    //    	if ( getQtySold() == 0 )  //Need to use PurchaseQuantity once it works
    //    		System.out.println( "No price is calculated." );
    //    	else{
    //    		double total_cost = getQtySold() * getPrice(); //Need to use PurchaseQuantity once it works
    //   		System.out.printf( "Total cost = $%.2f\n", total_cost );
    //    	}   		
    //    }
     
        @Override
        public String toString(){
        	return String.format( "The Cupcake flavor is %s\nThe cost per cupcake is $%.2f\nThe quantity is %d\n", getFlavors(), getPrice(), PurchaseQuantity() );
        }
     
    } //End Cupcake class


    public class TestCupCake {
     
        public static void main(String[] args) {
     
     
            Cupcake strawberry = new Cupcake( "Strawberry", 1.50 );
            //GourmetCupcake chocolate = new GourmetCupcake( "Chocolate", 2.50 );
            //GourmetCupcake coffee = new GourmetCupcake( "Coffee", 3.20 );
     
            System.out.println( strawberry.toString() );        
            strawberry.CalculatePrice();        
     
     
        }
    }


  2. #2
    Member
    Join Date
    Dec 2011
    Posts
    50
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Struggling Student seeks Guidance...

    Hi, here is my suggestion:
    - Move the strawberry.CalculatePrice(); up, before the line System.out.println( strawberry.toString() );
    - Do not call PurchaseQuantity() in the toString() method, instead call the getQtySold() method.

  3. The Following User Says Thank You to nickyc For This Useful Post:

    gabe (February 14th, 2012)

  4. #3
    Junior Member
    Join Date
    Feb 2012
    Location
    San Antonio, TX
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Struggling Student seeks Guidance...

    Quote Originally Posted by nickyc View Post
    Hi, here is my suggestion:
    - Move the strawberry.CalculatePrice(); up, before the line System.out.println( strawberry.toString() );
    - Do not call PurchaseQuantity() in the toString() method, instead call the getQtySold() method.
    nickyc, had to do some other tweaking but I think i get your idea. i think my issue is that I'm expecting PurchaseQuantity() to return an int value once i execute it (but only once) and not ask for an input from Scanner again, but it does. think your idea will work and I need to call calculateprice() to execute purchasequantity() to get the input only once. thanks for the help!

Similar Threads

  1. Recursion Guidance
    By Mission_Control in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 2nd, 2011, 08:35 PM
  2. HIBERNATE & JPA GUIDANCE NEEDED
    By Rituparna in forum JDBC & Databases
    Replies: 0
    Last Post: September 3rd, 2011, 03:54 AM
  3. Some guidance with a program please
    By derekxec in forum Java Theory & Questions
    Replies: 3
    Last Post: June 11th, 2011, 05:52 AM
  4. Looking for guidance and instructions
    By coyboss in forum Java Theory & Questions
    Replies: 4
    Last Post: February 12th, 2011, 09:57 AM
  5. [SOLVED] Medication of ArrayDemo.java to include iterative menu
    By xyldon27 in forum Loops & Control Statements
    Replies: 8
    Last Post: June 30th, 2009, 02:10 AM