Reputation: 371
I am not able to calculate the price a customer pays based on the number of bottles they have bought. It keeps returning a value of $14 for any amount of bottles purchased. Please see Java code below:
// PriceCalculator class that calculates the prices for customers
// counter controlled repetition
import java.util.Scanner; // program uses class Scanner
public class PriceCalculator
{
private String beginSale; // name of shop this price is calculated
// constructor initializes beginSale
public PriceCalculator( String name )
{
beginSale = name; // initializes shopName
} // end constructor
// method to set the shop name
public void setBeginSale (String name )
{
beginSale = name; // set the welcome address
} // end method setBeginSale
// method to retrieve the welcome address
public String getBeginSale()
{
return beginSale;
} // end method getBeginSale
// display a welcome message to the shopper
public void displayMessage ()
{
// getBeginSale gets the thank you address
System.out.printf( "Welcome to Price Calculator for Purchasing Wines at %s!\n",
getBeginSale() );
} // end method displayMessage
// determine charges based on 5 customers who shopped
public void calculatePrice()
{
// create scanner to obtain input from command window
Scanner input = new Scanner( System.in );
int basePrice; // base price of a bottle of wine
String customer; // name of customer
int gradeCounter; // number of customers to be entered next
int bottles; // number of bottles purchased
double discount; // discount value on the number of bottles
double rate;
double total; // total costs of wine purchased
// initialization phase
basePrice = 10; // initialize base price
gradeCounter = 1; // initialize loop counter
bottles = 1; // initialize bottles
discount = (basePrice * bottles); // initialize discount
rate = 0;
customer = null; // initialize customer name
// processing phase uses counter-controlled repetition
while ( gradeCounter <= 3 ) // loop 5 times
{
System.out.print( "Enter the name for customer " + gradeCounter + " : " ); // prompt
customer = input.next(); // input next name
System.out.print( "Enter the number of bottles for customer " + gradeCounter + " : "); // prompt
bottles = input.nextInt(); // input next bottles sold
gradeCounter = gradeCounter + 1; // increment counter by 1
discount = (basePrice * bottles) - (rate * bottles);
System.out.printf( "\nThe Price for Customer - %s is : $%.2f\n\n",
customer, discount );
} // end while loop
// calculate purchase based on number of bottles sold
if ( bottles <= 6 )
{
discount = (basePrice * bottles) - (rate * bottles); // calculates discount
}
else if
( bottles > 6 && bottles<= 12 )
{
discount = (basePrice * bottles) - (rate * bottles); // calculates discount
}
else if ( bottles > 48 )
System.out.println ( "It is forbidden to sell above the maximum range");
} // end method calculatePrice
} // end class PriceCalculator
Upvotes: 0
Views: 3099
Reputation: 13785
Change || to &&
if (bottles > 6 || bottles <= 12) // Your code
if (bottles > 6 && bottles <= 12) // changes
Upvotes: 2
Reputation: 14363
It's simple; you are printing the price in the loop where you input data so the price doesn't change and the same statement is printed again and again.
Just move following statements:
System.out.printf( "\nThe Price for Customer - %s is : $%.2f\n", customer, discount );
} // end while loop
post this line:
System.out.println( "It is forbidden to sell above the maximum range" );
The output will be:
Welcome to Price Calculator for Purchasing Wines at Red Rocky!
Enter the name for customer 1: asdfsa
Enter the number of bottles for customer 1: 3
The Price for Customer - asdfsa is : $42.00
Enter the name for customer 1: gasdfs
Enter the number of bottles for customer 1: 4
The Price for Customer - gasdfs is : $56.00
Enter the name for customer 1: asfasdf
Enter the number of bottles for customer 1: 5
The Price for Customer - asfasdf is : $70.00
Also you can replace this statement
System.out.print( "Enter the name for customer 1: " );
with
System.out.print( "Enter the name for customer " + gradeCounter + ":");
Final Code would look like:
while (gradeCounter <= 3) // loop 3 times
{
System.out.print("Enter the name for customer 1: "); // prompt
customer = input.next(); // input next name
System.out.print("Enter the number of bottles for customer 1: "); // prompt
bottles = input.nextInt(); // input next bottles sold
gradeCounter = gradeCounter + 1; // increment counter by 1
// calculate purchase based on number of bottles sold
if (bottles <= 6) {
discount = (basePrice * bottles); // calculates discount
} else if (bottles > 6 || bottles <= 12) {
discount = discount * 0.05 * bottles * basePrice; // calculates discount
} else if (bottles > 48)
System.out.println("It is forbidden to sell above the maximum range");
System.out.printf("\nThe Price for Customer - %s is : $%.2f\n",customer, discount);
} // end while loop
Upvotes: 1
Reputation: 6778
You want to change your
if (bottles > 6 || bottles <= 12)
to
if (bottles > 6 && bottles <= 12)
Upvotes: 1
Reputation: 171
You are not changing any values... First, you set the value of discount:
discount = (basePrice * bottles); // initialize discount
Then in the while loop, it never is updated based on the values given:
System.out.printf( "\nThe Price for Customer - %s is : $%.2f\n",
customer, discount );
Shouldn't the discount value be set inside the while loop before the printf? :)
Or to be more clear:
// processing phase uses counter-controlled repetition
while ( gradeCounter <= 3 ) // loop 3 times
{
System.out.print( "Enter the name for customer 1: " ); // prompt
customer = input.next(); // input next name
System.out.print( "Enter the number of bottles for customer 1: "); // prompt
bottles = input.nextInt(); // input next bottles sold
gradeCounter = gradeCounter + 1; // increment counter by 1
discount = (basePrice * bottles);
System.out.printf( "\nThe Price for Customer - %s is : $%.2f\n",
customer, discount );
} // end while loop
Upvotes: 1