Нижняя секция там, где набивается. Когда операторы if начинаются через некоторое время, продукт не сохраняет название продукта (но сохраняет то, что это за продукт, и стоимость на потом, поэтому конечный результат работает). Проблема в том, что независимо от того, что введено, все равно появляется сообщение «Это недопустимый продукт», даже если это так. Это не влияет на программу, но влияет на вывод.
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();