The bottom section is where it is stuffing up. When The if statements start after while, the product doesn't store the name of the product (but keeps what product it is and the cost for later - so the end result works). The issue is, regardless of what is entered, the message still comes up "That isn't a valid product" even if it is. It doesn't affect the program, but does affect the output.
import java.util. * ;
public class Shopping {
String p1Name,
p2Name,
p3Name; // variables for storing product names
double p1UP,
p2UP,
p3UP; // variables for storing product unit prices
String c1Name,
c2Name; // variables for storing customer names
double c1DRate,
c2DRate; // variables for customer discount rates
double quant; //variables for unit quantity required
double sum; // unit and quantity
String custName; // Customer name
double discount1;
double discount2;
String product;
double cost = p1UP;
Scanner scan;
public void setProductData() {
System.out.println("Initializing product data");
System.out.print("Enter name of part 1 : ");
p1Name = scan.next();
System.out.print("Enter unit price of part 1 : ");
p1UP = scan.nextDouble();
System.out.print("Enter name of part 2 : ");
p2Name = scan.next();
System.out.print("Enter unit price of part 2 : ");
p2UP = scan.nextDouble();
System.out.print("Enter name of part 3 : ");
p3Name = scan.next();
System.out.print("Enter unit price of part : ");
p3UP = scan.nextDouble();
}
public void setCustomerData() {
System.out.println("Initializing customer data");
System.out.print("Enter name of customer 1 : ");
c1Name = scan.next();
System.out.print("Enter discount Rate for customer 1 : ");
c1DRate = scan.nextDouble();
System.out.print("Enter name of customer 2 : ");
c2Name = scan.next();
System.out.print("Enter discount Rate for customer 2 : ");
c2DRate = scan.nextDouble();
}
public void computeCost() {
// prompt and read product name, qty and customer name before computing and
// printing the cost taking into consideration customer specific discounts.
boolean discount1 = false;
boolean discount2 = false;
System.out.print("Please enter your name : ");
custName = scan.next();
if (custName.equalsIgnoreCase(c1Name)) {
discount1 = true;
}
if (custName.equalsIgnoreCase(c2Name)) {
discount2 = true;
}
// Determined if customer eligible for a discount
System.out.println("Our products and prices are as follows");
System.out.println(p1Name + " is priced at " + p1UP);
System.out.println(p2Name + " is priced at " + p2UP);
System.out.println(p3Name + " is priced at " + p3UP);
System.out.println("What product would you like");
//what product did they want to buy
product = scan.next();
while (! (product.equalsIgnoreCase(p1Name) || product.equalsIgnoreCase(p2Name) || product.equalsIgnoreCase(p3Name))) {
System.out.println("That isn't a valid product.");
product = scan.next();
}
if (product.equals(p1Name)) {
cost = p1UP;
}
if (product.equals(p2Name)) {
cost = p2UP;
}
if (product.equals(p3Name)) {
cost = p3UP;
}
else {
System.out.println("You have selected an incorrect product");
System.out.println("You have been assigned " + p1Name + " instead");
}
scan.nextLine();
I think you're having a mistake here.
I suppose you're trying to print the message "That isn't a valid product." if the input does not match any of p1Name
, p2Name
and p3Name
, right?
So your while condition must be NOT EQUALS to p1Name
AND p2Name
, p3Name
too.
So you can use this:
while (!(product.equalsIgnoreCase(p1Name) && !(product.equalsIgnoreCase(p2Name)) && !
(product.equalsIgnoreCase(p3Name)))) {
System.out.println("That isn't a valid product.");
product = scan.next();
}
use String.trim() maybe when you scan you scan also the space or whitespace character.
in ur while condition:
!(product.trim().equalsIgnoreCase(p1Name.trim()) || product.trim().equalsIgnoreCase(p2Name.trim()) || product.trim().equalsIgnoreCase(p3Name.trim()))
Ok, I worked it out. I had if not else if for the later statements. my bad.
Thanks
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.