In: Computer Science
public class GroceryShopping
{
//declared variable
private String vegetableName;
private String fruitName;
private double vegetablePrice;
private double fruitPrice;
private double vegetableOrdered;
private double fruitOrdered;
//declared constants
public static final double SERVICE_RATE =0.035;
public static final double DELIVERY_FEE=5;
public GroceryShopping( String vegetableName, String
fruitName, double vegetablePrice, double fruitPrice)
{
this.vegetableName =
vegetableName;
this.fruitName = fruitName;
this.vegetablePrice =
vegetablePrice;
this.fruitPrice = fruitPrice;
this.vegetableOrdered =
vegetableOrdered;
this.fruitOrdered=fruitOrdered;
}
public String getVegetableName()
{
return vegetableName;
}
public void setVegetableName(String
vegetableName)
{
this.vegetableName =
vegetableName;
}
public String getFruitName()
{
return fruitName;
}
public void setFruitName(String fruitName)
{
this.fruitName = fruitName;
}
public double getVegetablePrice()
{
return vegetablePrice;
}
public void setVegetablePrice(double
vegetablePrice)
{
this.vegetablePrice =
vegetablePrice;
}
public double getFruitPrice()
{
return fruitPrice;
}
public void setFruitPrice(double fruitPrice)
{
this.fruitPrice = fruitPrice;
}
public double getVegetableOrdered()
{
return vegetableOrdered;
}
public void setVegetableOrdered(double
vegetableOrdered)
{
this.vegetableOrdered =
vegetableOrdered;
}
public double getFruitOrdered()
{
return fruitOrdered;
}
public void setFruitOrdered(double fruitOrdered)
{
this.fruitOrdered =
fruitOrdered;
}
public double calculateSubtotal()
{
double
calculateSubtotal=(vegetablePrice*vegetableOrdered)+(fruitPrice*fruitOrdered);
return calculateSubtotal;
}
public double calculateAdditionalFee()
{
double
calculateAdditionalFee=calculateSubtotal()* SERVICE_RATE +
DELIVERY_FEE;
return
calculateAdditionalFee;
}
public void displayOrderSummary()
{
double
totalBill=calculateSubtotal()+calculateAdditionalFee();
System.out.println("--------------------------------------");
System.out.println("Grocery
Shopping Order Summary");
System.out.println("\nName"+"\t\t\tPrice Per Pound");
System.out.println("Sub-total:"+"\t\t$"+calculateSubtotal());
System.out.println("Additional
Fee:"+"\t\t$"+calculateAdditionalFee());
System.out.println("Total
Bill:"+"\t\t$"+totalBill);
System.out.println("--------------------------------------");
}
}
public class GroceryShoppingApp
{
/**
* @param args
*/
public static void main(String[] args)
{
Scanner input=new
Scanner(System.in);
displayTable1();
System.out.println("\nPlease select
the vegetable from Table 1: ");
String
vegetableName=input.next();
System.out.println("please enter
the price of the selected vegetable: ");
double
vegetablePrice=input.nextDouble();
displayTable2();
System.out.println("\nPlease select
the fruit from Table 2");
String fruitName
=input.next();
System.out.println("Please enter
the price of the selected fruit:");
double fruitPrice
=input.nextDouble();
GroceryShopping gs =new
GroceryShopping(vegetableName, fruitName, vegetablePrice,
fruitPrice);
System.out.println("\n--------------------------------------");
System.out.println("Grocery
Shopping Menu");
System.out.println("\nName"+"\t\tPrice Per Pound");
System.out.println(gs.getVegetableName()+"\t"+gs.getVegetablePrice());
System.out.println(gs.getFruitName()+"\t\t"+gs.getFruitPrice());
System.out.println("--------------------------------------\n");
System.out.println("\nEnter the
pounds of "+ gs.getVegetableName()+" ordered: ");
double
vegetableOrdered=input.nextDouble();
System.out.println("Enter the
pounds of "+ gs.getFruitName()+" ordered: ");
double
fruitOrdered=input.nextDouble();
System.out.println("\n\n\n--------------------------------------");
System.out.println("Grocery
Shopping Order Summary");
System.out.println("\nName"+"\t\tPrice Per Pound");
System.out.println(vegetableName+"\t"+vegetablePrice);
System.out.println(fruitName+"\t\t"+fruitPrice);
gs.displayOrderSummary();
input.close();
}
private static void displayTable1()
{
System.out.println("Vegetable Name\t\t"+"Price Per
Pound");
System.out.println("Broccoli\t\t"+"$3.12");
System.out.println("Yellow Onion\t\t"+"$1.15");
System.out.println("Chill Pepper\t\t"+"$4.58");
System.out.println("Greens Bundle\t\t"+"$2.82");
System.out.println("--------------------------------------");
System.out.println("Table 1: Vegetable names with
corresponding price per pound");
}
private static void displayTable2()
{
System.out.println("\n--------------------------------------");
System.out.println("Fruit Name\t\t"+"Price Per
Pound");
System.out.println("Apple\t\t\t"+"$1.73");
System.out.println("Grape\t\t\t"+"$2.15");
System.out.println("Key Lime\t\t"+"$2.58");
System.out.println("Navel Orange\t\t"+"$1.86");
System.out.println("--------------------------------------");
System.out.println("Table 2: Fruit names with
corresponding price per pound ");
}
}
I have two question
1) My Sub-Total fee is always is zero, no matter what i input.
2)When i input the vegetable and fruit name, only the apple , Grape and Broccoli are working. The others always shows error, when i try to input them.
import java.util.Scanner;
class GroceryShopping
{
//declared variable
private String vegetableName;
private String fruitName;
private double vegetablePrice;
private double fruitPrice;
private double vegetableOrdered;
private double fruitOrdered;
//declared constants
public static final double SERVICE_RATE =0.035;
public static final double DELIVERY_FEE=5;
public GroceryShopping( String vegetableName, String fruitName,
double vegetablePrice, double fruitPrice)
{
this.vegetableName = vegetableName;
this.fruitName = fruitName;
this.vegetablePrice = vegetablePrice;
this.fruitPrice = fruitPrice;
this.vegetableOrdered = vegetableOrdered;
this.fruitOrdered=fruitOrdered;
}
public String getVegetableName()
{
return vegetableName;
}
public void setVegetableName(String vegetableName)
{
this.vegetableName = vegetableName;
}
public String getFruitName()
{
return fruitName;
}
public void setFruitName(String fruitName)
{
this.fruitName = fruitName;
}
public double getVegetablePrice()
{
return vegetablePrice;
}
public void setVegetablePrice(double vegetablePrice)
{
this.vegetablePrice = vegetablePrice;
}
public double getFruitPrice()
{
return fruitPrice;
}
public void setFruitPrice(double fruitPrice)
{
this.fruitPrice = fruitPrice;
}
public double getVegetableOrdered()
{
return vegetableOrdered;
}
public void setVegetableOrdered(double vegetableOrdered)
{
this.vegetableOrdered = vegetableOrdered;
}
public double getFruitOrdered()
{
return fruitOrdered;
}
public void setFruitOrdered(double fruitOrdered)
{
this.fruitOrdered = fruitOrdered;
}
public double calculateSubtotal()
{
double
calculateSubtotal=(vegetablePrice*vegetableOrdered)+(fruitPrice*fruitOrdered);
return calculateSubtotal;
}
public double calculateAdditionalFee()
{
double calculateAdditionalFee=calculateSubtotal()* SERVICE_RATE +
DELIVERY_FEE;
return calculateAdditionalFee;
}
public void displayOrderSummary()
{
double
totalBill=calculateSubtotal()+calculateAdditionalFee();
System.out.println("--------------------------------------");
System.out.println("Grocery Shopping Order Summary");
System.out.println("\nName"+"\t\t\tPrice Per Pound");
System.out.println("Sub-total:"+"\t\t$"+calculateSubtotal());
System.out.println("Additional
Fee:"+"\t\t$"+calculateAdditionalFee());
System.out.println("Total Bill:"+"\t\t$"+totalBill);
System.out.println("--------------------------------------");
}
}
public class GroceryShoppingApp
{
/**
* @param args
*/
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
displayTable1();
System.out.println("\nPlease select the vegetable from Table 1:
");
String vegetableName=input.nextLine();
System.out.println("please enter the price of the selected
vegetable: ");
double vegetablePrice=input.nextDouble();
displayTable2();
System.out.println("\nPlease select the fruit from Table 2");
//issue-2
String fruitName =input.nextLine();
fruitName =input.nextLine();
System.out.println("Please enter the price of the selected
fruit:");
double fruitPrice =input.nextDouble();
GroceryShopping gs =new GroceryShopping(vegetableName, fruitName,
vegetablePrice, fruitPrice);
System.out.println("\n--------------------------------------");
System.out.println("Grocery Shopping Menu");
System.out.println("\nName"+"\t\tPrice Per Pound");
System.out.println(gs.getVegetableName()+"\t"+gs.getVegetablePrice());
System.out.println(gs.getFruitName()+"\t\t"+gs.getFruitPrice());
System.out.println("--------------------------------------\n");
System.out.println("\nEnter the pounds of "+
gs.getVegetableName()+" ordered: ");
double vegetableOrdered=input.nextDouble();
System.out.println("Enter the pounds of "+ gs.getFruitName()+"
ordered: ");
double fruitOrdered=input.nextDouble();
//issue 1
gs.setVegetableOrdered(vegetableOrdered);
gs.setFruitOrdered(fruitOrdered);
System.out.println("\n\n\n--------------------------------------");
System.out.println("Grocery Shopping Order Summary");
System.out.println("\nName"+"\t\tPrice Per Pound");
System.out.println(vegetableName+"\t"+vegetablePrice);
System.out.println(fruitName+"\t\t"+fruitPrice);
gs.displayOrderSummary();
input.close();
}
private static void displayTable1()
{
System.out.println("Vegetable Name\t\t"+"Price Per Pound");
System.out.println("Broccoli\t\t"+"$3.12");
System.out.println("Yellow Onion\t\t"+"$1.15");
System.out.println("Chill Pepper\t\t"+"$4.58");
System.out.println("Greens Bundle\t\t"+"$2.82");
System.out.println("--------------------------------------");
System.out.println("Table 1: Vegetable names with corresponding
price per pound");
}
private static void displayTable2()
{
System.out.println("\n--------------------------------------");
System.out.println("Fruit Name\t\t"+"Price Per Pound");
System.out.println("Apple\t\t\t"+"$1.73");
System.out.println("Grape\t\t\t"+"$2.15");
System.out.println("Key Lime\t\t"+"$2.58");
System.out.println("Navel Orange\t\t"+"$1.86");
System.out.println("--------------------------------------");
System.out.println("Table 2: Fruit names with corresponding price
per pound ");
}
}
// Issue 1: missed setting the quantity so its always taking quantity as 0
// Issue 2: read string with spaces we need to use nextLine() not next that is the reason for you only single work names are working
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me