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(); } }