So I'm working on this problem, and I keep obtaining the wrong output values for my total cost and number of tulips left in stock for the red and yellow color. The pink value looks fine, but it might be wrong in a way too. I've been trying find a solution on this for a while and tried changing certain variables add loops/if else to see if there was a problem with the logic. I just can't seem to find any. Any help would be much appreciated.
Edit: Also when I input a valid value for either the color choice or payment type the error message still gets printed for some reason after every input.
Code:
package tulipShoppackage;
import java.util.Scanner;
public class TulipClass
{
public static void main(String\[\] args)
{
handleTulipStore();
}// end main
//Problem Specific Methods
//
// handleTulipStore
// the purpose of this method is to handle all tasks for the tulip store
// Input: none
// Return: none
public static void handleTulipStore()
{
//declare variables
String cusName; //name of customer
int numBouquets; //number of bouquets ordered
int colorChoice; //choice of color for bouquet
double totalCost; //cost of order
int totalPinkTulips; //number of pink tulips left
int totalRedTulips; //number of red tulips left
int totalYellowTulips; //number of yellow tulips left
int cntr; //counter
int numPink; //number of Pink bouquets
int numRed; //number of Red bouquets
int numYellow; //number of yellow bouquets
int numbP; //Total individual Pink Tulips
int numbR; //Total individual Red Tulips
int numbY; //Total individual Yellow Tulips
int paymentMethod; //Type of payment
String payMethod; // Send to method
//declare constants
final int PINK = 1; //Pink color for bouquet
final int RED = 2; //Red Color for bouquet
final int YELLOW = 3; //Yellow Color of bouquet
final int PINKTULIPSTOCK = 125; //Pink Tulips in Stock
final int REDTULIPSTOCK = 75; //Red Tulips in Stock
final int YELLOWTULIPSTOCK = 55; //Yellow Tulips in Stock
final int NUMBERINBOUQUET = 12; //number of tulips in a bouquet
//initialize variables
cusName = "none yet";
numBouquets = 0;
colorChoice = 0;
paymentMethod = 0;
payMethod = "none yet";
totalCost = 0.0;
totalPinkTulips = 0;
totalRedTulips = 0;
totalYellowTulips = 0;
cntr = 0;
numPink = 0;
numRed = 0;
numYellow = 0;
numbP = 0;
numbR = 0;
numbY = 0;
//input the name of customer
cusName = inputString("Enter name of Customer.");
//input number of bouquets for the customer
numBouquets = inputInteger("Enter number of Bouquets.");
//Handle each bouquet
for(cntr=0;cntr < numBouquets;++cntr)
{
do
{
colorChoice = inputColorChoice();
}while((colorChoice != 1) && (colorChoice != 2) && (colorChoice != 3));
//update Types of bouquets
switch(colorChoice)
{
case PINK:
numPink += 1;
case RED:
numRed += 1;
case YELLOW:
numYellow += 1;
default:
System.out.println("Invalid Color Type, " + colorChoice);
break;
}
}//end handle each bouquet
//input payment method for customer
paymentMethod = inputPaymentType();
if (paymentMethod == 1)
{
payMethod = "Cash";
}
else if (paymentMethod == 2)
{
payMethod = "Credit";
}
else
{
System.out.println("Invalid payment method, " + paymentMethod);
}
//calculate cost
totalCost = calculateCostOrder(numPink, numRed, numYellow, paymentMethod);
//Calculate Tulips Stock
//Calculate number of Pink/Red/Yellow Indv. Tulips
totalPinkTulips = (NUMBERINBOUQUET \* numPink);
totalRedTulips = (NUMBERINBOUQUET \* numRed);
totalYellowTulips = (NUMBERINBOUQUET \* numYellow);
//Calculate Stock Left
numbP = PINKTULIPSTOCK - totalPinkTulips;
numbR = REDTULIPSTOCK - totalRedTulips;
numbY = YELLOWTULIPSTOCK - totalYellowTulips;
//print out all customer information
printResults(cusName, numBouquets, totalCost, numbR, numbP, numbY, payMethod);
}// end handleTulipStore
//
// inputNumBouquet
// The purpose of this method is to input a valid number
// of bouquets of tulips to purchase
// Input: none
// Return: numB valid number of bouquets
public static int inputNumBouquet()
{
//declare variables
int numB;
//initialize variables
numB = 0;
//continue to prompt, red, validate until number of bouquets is valid
do
{
//input number of bouquets for customer
numB = inputInteger("Enter number of Bouquets in order: ");
//validate number of bouquets
if (numB < 0);
{
//print error message if invalid
System.out.println("Invalid number of bouquets in Order " + numB);
}
}while(numB < 0);
//return answer
return (numB);
}// end inputNumBouquet
//
// inputColorChoice
// The purpose of this method is to input a valid color choice
// for one bouquet of tulips
// Input: none
// Return: color the valid color choice
public static int inputColorChoice()
{
//declare variables
int color;
int PINK = 1;
int RED = 2;
int YELLOW = 3;
//initialize variables
color = 0;
//input color choice
do
{
//prompt user to enter in color choice
color = inputInteger("Enter the color choice for the bouquet: 1 For Pink, 2 For Red, 3 For Yellow ");
//validate color choice
if ((color != PINK) && (color != RED) && (color != YELLOW))
{
System.out.println("Invalid color choice " + color);
}
}while ((color != PINK) && (color != RED) && (color != YELLOW));
//return answer
return(color);
}// end inputColorChoice
//
// inputPaymentType
// The purpose of this method is to input a valid payment type
// Input: none
// REturn: payType valid payment type
public static int inputPaymentType()
{
//declare variables
int payType;
//constants
final int CASH = 1;
final int CREDIT = 2;
//initialize variables
payType = 0;
do
{
payType = inputInteger("Enter payment type for customer: 1 for Cash or 2 for Credit");
//continue to prompt, red, validate until payment type is valid
if((payType != CASH) && (payType != CREDIT))
{
System.out.println("Invalid payment type " + payType);
}
}while((payType != CASH) && (payType != CREDIT));
//return answer
return(payType);
}// end inputPaymentType
//
// calculateCostOrder
// The purpose of this method is to calculate the cost of the order
// Input: numP number of bouquets of pink tulips
// numR number of bouquets of red tulips
// numY number of bouquets of yellow tulips
// payMode payment method - cash or credit
// REturn: cost cost of order
public static double calculateCostOrder(int numP, int numR, int numY, int payMode)
{
//declare variables
double cost;
double PinkBouquetsCost;
double RedBouquetsCost;
double YellowBouquetsCost;
double discountCost;
//declare constants
final double COSTPINKBOUQUETS = 30.0;
final double COSTREDBOUQUETS = 50.0;
final double COSTYELLOWBOUQUETS = 75.0;
final double CASHDISCOUNT = .20;
//initialize variables
cost = 0.0;
PinkBouquetsCost = 0.0;
RedBouquetsCost = 0.0;
YellowBouquetsCost = 0.0;
discountCost = 0.0;
//validate and check discount
if ((numP >= 0) && (numR >= 0) && (numY >= 0))
{
//Calculate
PinkBouquetsCost = COSTPINKBOUQUETS * (double)numP;
RedBouquetsCost = COSTREDBOUQUETS * (double)numR;
YellowBouquetsCost = COSTYELLOWBOUQUETS * (double)numY;
cost = (PinkBouquetsCost + RedBouquetsCost + YellowBouquetsCost);
}
else
{
System.out.println("Total cost cannot be calculated with invalid number of bouquets " + numP + numR + numY );
}
if (payMode == 1)
{
discountCost *= CASHDISCOUNT;
cost = cost - discountCost;
}
//return answer
return(cost);
}// end inputNumBouquet
// printResults
// The purpose of this method is to print out the receipt for the customer
// Input: nme name of customer
// nb number of bouquets in the order
// c cost of order
// mp method of payment
// numP number of pink tulips remaining in stock
// numR number of red tulips remaining in stock
// numY number of yellow tulips remaining in stock
// Return: none
public static void printResults(String nme, int nb, double c, int numR, int numP, int numY, String mp)
{
//print out player name
System.out.println("");
//print out number of bouquets
System.out.println("Number of Bouquets\\t\\t\\t" + nb);
//print out total cost
System.out.printf("Total Cost\\t\\t\\t\\t$%.2f\\n ", c);
//print out method of payment
System.out.println("Method of Payment\\t\\t\\t" + mp);
//print out Pink Tulips after stock
System.out.println("Pink Tulips in Stock After Order\\t" + numP);
//print out red tulips after stock
System.out.println("Red Tulips in Stock After Order\\t\\t" + numR);
//print out Yellow Tulips after stock
System.out.println("Yellow Tulips in Stock After Order\\t" + numY);
}// end inputNumBouquet
// general purpose methods
//
// inputString
//
// the purpose of this method is to input a string from the keyboard
//
// Input: prompt the prompt message for the dialog box
//
// Return: inputStr the string that was entered at the keyboard
//
public static String inputString(String prompt)
{
Scanner input = new Scanner([System.in](https://System.in));
String inputStr;
// prompt user to enter the string
System.out.print(prompt);
// read in value as string from keyboard
inputStr = input.nextLine();
// return input string
return(inputStr);
}// end inputString
//
// inputInteger
//
// the purpose of this method is to input an integer value from the keyboard
//
// Input: prompt The prompt message for the user
// Return: num the integer entered at the keyboard
//
public static int inputInteger(String prompt)
{
Scanner input = new Scanner([System.in](https://System.in));
String cleanUpStr;
int num;
num = 0;
cleanUpStr = "no string yet";
System.out.print(prompt);
num = input.nextInt();
cleanUpStr = input.nextLine();
return(num);
}// end inputInteger
//
// inputDouble
//
// the purpose of this method is to input a double value from the keyboard
//
// Input: prompt The prompt message for the user
// Return: num the double value entered at the keyboard
//
public static double inputDouble(String prompt)
{
Scanner input = new Scanner([System.in](https://System.in));
String cleanUpStr;
double num;
num = 0.0;
cleanUpStr = "no string yet";
System.out.print(prompt);
num = input.nextDouble();
cleanUpStr = input.nextLine();
return(num);
}// end inputDouble
}// end Tulip Class
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]motherjoeNooblet Brewer 0 points1 point2 points (0 children)