I'm having difficulty with writing a Java program, in particular with the enterCustomerName method. I have used String so that I can get worded input from the user. There are other errors, but this one I don't understand. I'm new to Java programming, so any help is greatly appreciated.
It's a program for a fruit and vegetable store.
import java.util.Scanner;
public class FruitVegShop
{
private void printWelcomeMessage()
{ // print the welcome message
System.out.println("Welcome to Rocky Fruit and Vegetable Market");
}
private String enterCustomerName()
{ // prompt for, read and return a customer name
Scanner in = new Scanner (System.in);
System.out.println("Please enter the customer's name: ");
String customerName;
customerName = in.nextLine();
System.out.printf("Hello %s\n", customerName);
return "";
}
private String enterProduceName()
{ // prompt for, read and return the name of the produce
Scanner in = new Scanner(System.in);
System.out.println("Please enter the name of the produce");
String name;
name = in.nextLine();
System.out.printf("P: %s\n", name);
return "";
}
private void printPurchaseNumber(int purchaseNumber)
{ // print the number of the purchase
}
private double enterProduceWeight(String name)
{ // prompt for, read and return the weight of the produce
Scanner in = new Scanner (System.in);
System.out.println("Please enter the weight of the produce: ");
double weight;
weight = in.nextDouble();
System.out.printf("Produce Weight: %d\n", weight);
return 0;
}
private double enterProduceCost(String name)
{ // prompt for, read and return the cost of the produce
Scanner in = new Scanner (System.in);
System.out.println("Please enter the price of the produce: ");
double costPerKg;
costPerKg = in.nextDouble();
System.out.printf("Produce Price: $%d\n", costPerKg);
return 0;
}
private void printPurchase(String name, double weight, double costPerKg, double cost)
{ // print the details of one pruchase
double produceTotal;
System.out.println("Produce:");
System.out.printf("Produce: %d\n", name);
System.out.printf("Weight (kg): %d\n", weight);
System.out.printf("@ $%d.2\n /kg", costPerKg);
System.out.printf("TOTAL: $%d.2\n", produceTotal); // ERROR: variable produceTotal might not have been initialized
produceTotal = weight * costPerKg;
}
private boolean discount(String name)
{ // determine if the customer is eligle for a discount and return true or false
if(
return scannerObject.nextLine().equalsIgnoreCase(“y”);
}
private void printTotalCost(String name, int numberOfPurchases, double cost, boolean discount)
{ // print the total cost of the order and discount information if neccessary
}
private void printExitMessage(String name)
{ // print the exit message
}
private void processCustomer()
{ // process one customer's order
String customerName, // the name of the customer
produceName; // the name of the produce
double produceWeight, // the weight ofthe produce
produceCostPerKg, // the cost per kilo of the produce
purchaseCost, // the cost of the purchase
totalCost = 0; // the total cost of the order
int purchaseNumber = 0; // the number of the current purchase
boolean discount; // boolean to say if customer is eligible for a discount
printWelcomeMessage(); // print the title message
customerName = enterCustomerName(); // get the customer name
purchaseNumber++; // increment the number of the purchase
printPurchaseNumber(purchaseNumber); // print the number of the purchase
produceName = enterProduceName(); // get the name of the produce item
produceWeight = enterProduceWeight(produceName); // get the weight of the purchase
produceCostPerKg = enterProduceCost(produceName); // get the cost per kg
purchaseCost = produceWeight * produceCostPerKg; // calculate the cost of the purchase
totalCost += purchaseCost; // add to total cost
printPurchase(produceName, produceWeight, produceCostPerKg, purchaseCost); // print the purchase details
discount = discount(customerName); // determine if the customer is eligible for a discount
printTotalCost(customerName, purchaseNumber, totalCost, discount); // print the total cost
printExitMessage(customerName); // print the final exit message
}
public static void main(String [] args)
{
FruitVegShop fruitVegShop = new FruitVegShop();
fruitVegShop.processCustomer();
}
}
And this is the error I get:
Welcome to Rocky Fruit and Vegetable Market
Please enter the customer's name:
Anonymous
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at FruitVegShop.enterCustomerName(FruitVegShop.java:20)
at FruitVegShop.processCustomer(FruitVegShop.java:102)
at FruitVegShop.main(FruitVegShop.java:127)
Press any key to continue . . .
import java.util.Scanner;
public class FruitVegShop
{
private void printWelcomeMessage()
{ // print the welcome message
System.out.println(" Welcome to Rocky Fruit and Vegetable Market");
}
private String enterCustomerName()
{ // prompt for, read and return a customer name
Scanner in = new Scanner (System.in);
System.out.println("Enter the name of the customer ==> ");
String customerName;
customerName = in.nextLine();
return customerName;
}
private String enterProduceName()
{ // prompt for, read and return the name of the produce
Scanner in = new Scanner(System.in);
System.out.println("Enter the name of the produce ==> ");
String produceName;
produceName = in.nextLine();
return produceName;
}
private void printPurchaseNumber(int purchaseNumber)
{ // print the number of the purchase
int counter = 0;
while ( counter <= 3 ) // loop the order 3 times
}
private double enterProduceWeight(String name)
{ // prompt for, read and return the weight of the produce
Scanner in = new Scanner (System.in);
System.out.printf("Enter the weight (kg) of the %s\n ==> ", produceName);
double produceWeight;
produceWeight = in.nextDouble();
return produceWeight;
}
private double enterProduceCost(String name)
{ // prompt for, read and return the cost of the produce
Scanner in = new Scanner (System.in);
System.out.printf("Enter the cost per kg of the %s\n ==> ", produceName);
double produceCostPerKg;
produceCostPerKg = in.nextDouble();
return produceCostPerKg;
}
private void printPurchase(String produceName, double produceWeight, double produceCostPerKg, double cost)
{ // print the details of one pruchase
double produceCost;
System.out.println("--------------------------------------------------");
System.out.printf("%f\n of %s\n at $%.2f\n per kg is $%.2f\n", produceWeight, produceName, produceCostPerKg, produceCost);
System.out.println("--------------------------------------------------");
produceCost = produceWeight * produceCostPerKg;
}
private boolean discount(String name)
{ // determine if the customer is eligle for a discount and return true or false
Scanner in = new Scanner(System.in);
if (in.nextLine().equalsIgnoreCase("y"))
return true;
else
return false;
}
private void printTotalCost(String name, int numberOfPurchases, double cost, boolean discount)
{ // print the total cost of the order and discount information if neccessary
double totalCost;
double discountValue;
System.out.println("**********************************************************************");
System.out.printf("%s\n the total for %d\n fruit and vegetable purchases is $%.2f\n", customerName, purchaseNumber, totalCost);
System.out.printf("This includes a discount of $%.2d\n", discountValue);
System.out.println("**********************************************************************");
// make a addition counter for the order.
// set a discount value
}
private void printExitMessage(String name)
{ // print the exit message
System.out.printf("Thank you %s\n for shopping at the Rocky Fruit and Vegetable Market", customerName);
System.out.println("Written by s0270780");
}
private void processCustomer()
{ // process one customer's order
String customerName, // the name of the customer
produceName; // the name of the produce
double produceWeight, // the weight of the produce
produceCostPerKg, // the cost per kilo of the produce
purchaseCost, // the cost of the purchase
totalCost = 0; // the total cost of the order
int purchaseNumber = 0; // the number of the current purchase
boolean discount; // boolean to say if customer is eligible for a discount
printWelcomeMessage(); // print the title message
customerName = enterCustomerName(); // get the customer name
purchaseNumber++; // increment the number of the purchase
printPurchaseNumber(purchaseNumber); // print the number of the purchase
produceName = enterProduceName(); // get the name of the produce item
produceWeight = enterProduceWeight(produceName); // get the weight of the purchase
produceCostPerKg = enterProduceCost(produceName); // get the cost per kg
purchaseCost = produceWeight * produceCostPerKg; // calculate the cost of the purchase
totalCost += purchaseCost; // add to total cost
printPurchase(produceName, produceWeight, produceCostPerKg, purchaseCost); // print the purchase details
discount = discount(customerName); // determine if the customer is eligible for a discount
printTotalCost(customerName, purchaseNumber, totalCost, discount); // print the total cost
printExitMessage(customerName); // print the final exit message
}
public static void main(String [] args)
{
FruitVegShop fruitVegShop = new FruitVegShop();
fruitVegShop.processCustomer();
}
}
C:\Users\Jockman\Desktop\The Fruit & Veg Shop\FruitVegShop.java:45: error: cannot find symbol
System.out.printf("Enter the weight (kg) of the %f\n ==> ", produceName);
^
symbol: variable produceName
location: class FruitVegShop
C:\Users\Jockman\Desktop\The Fruit & Veg Shop\FruitVegShop.java:55: error: cannot find symbol
System.out.printf("Enter the cost per kg of the %s\n ==> ", produceName);
^
symbol: variable produceName
location: class FruitVegShop
C:\Users\Jockman\Desktop\The Fruit & Veg Shop\FruitVegShop.java:93: error: cannot find symbol
System.out.printf("%s\n the total for %d\n fruit and vegetable purchases is $%.2f\n", customerName, purchaseNumber, totalCost);
^
symbol: variable customerName
location: class FruitVegShop
C:\Users\Jockman\Desktop\The Fruit & Veg Shop\FruitVegShop.java:93: error: cannot find symbol
System.out.printf("%s\n the total for %d\n fruit and vegetable purchases is $%.2f\n", customerName, purchaseNumber, totalCost);
^
symbol: variable purchaseNumber
location: class FruitVegShop
C:\Users\Jockman\Desktop\The Fruit & Veg Shop\FruitVegShop.java:104: error: cannot find symbol
System.out.printf("Thank you %s\n for shopping at the Rocky Fruit and Vegetable Market", customerName);
^
symbol: variable customerName
location: class FruitVegShop
5 errors
Tool completed with exit code 1