In: Computer Science
JAVA program to simulate a customer’s online grocery shopping. The customer will shop fruit and vegetable each week online. The customer will select one vegetable and one fruit from the Table 1 and 2 respectively.
The program that is needed for the online ordering system will allow the customer to place a single order, calculating subtotals, additional fee, and outputting a shopping summary. When completing an order, an itemized summary is to be displayed. This bill should include a 3.5% service rate and flat fee of $5 for delivery should be added after the service fee has been applied to the bill.
Vegetable Name |
Price Per Pound |
Broccoli |
$3.12 |
Yellow Onion |
$1.15 |
Chili Pepper |
$4.58 |
Greens Bundle |
$2.82 |
Table 1: Vegetable names with corresponding price per pound
Fruit Name |
Price Per Pound |
Apple |
$1.73 |
Grape |
$2.15 |
Key Lime |
$2.58 |
Navel Orange |
$1.86 |
Table 2: Fruit names with corresponding price per pound
The main requirement of this program that is different from your earlier assignments is that you are required to create 2 different Java classes in the design of your program. Here are some other design details that may be helpful:
Design:
The classes should be placed in a package with the name edu.ilstu
The GroceryShopping class should have proper Javadoc comments
The GroceryShopping class
Keeps track of the information for 1 order. Write the class GroceryShopping that has the following fields:
Instance Variables:
Named Constants
Methods:
Additional fee = subtotal * service rate + delivery fee
GroceryShoppingApp class:
This is the starting point for the application which is the only class to contain a main method. You will use the GroceryShopping class here. For this program, the main method handles all of the input and output for the program and will perform the following tasks:
Input:
Output:
Print the title, a line with the selected vegetable and the number of pounds purchased, a line with the selected fruit and the number of pounds purchased, a subtotal of the cost of the items ordered, additional fee, and the total bill.
package edu.ilstu;
public class GroceryShopping {
//Below are instance variables
String vegetableName;
String fruitName;
double vegetablePrice;
double fruitPrice;
double vegetableOrdered;
double fruitOrdered;
//Below two variables are constants
final double serviceRate = 0.035;
final int deliveryFee = 5;
//This is the constructor
public GroceryShopping(String vegetableName, String
fruitName, double vegetablePrice, double fruitPrice) {
super();
this.vegetableName =
vegetableName;
this.fruitName = fruitName;
this.vegetablePrice =
vegetablePrice;
this.fruitPrice = fruitPrice;
this.vegetableOrdered=0;
this.fruitOrdered=0;
}
//Getter method for vegetableOrdered
public double getVegetableOrdered() {
return vegetableOrdered;
}
//Setter method for vegetableOrdered
public void setVegetableOrdered(double
vegetableOrdered) {
this.vegetableOrdered =
vegetableOrdered;
}
//Getter method for FruitOrdered
public double getFruitOrdered() {
return fruitOrdered;
}
//Setter method for FruitOrdered
public void setFruitOrdered(double fruitOrdered)
{
this.fruitOrdered =
fruitOrdered;
}
//Getter method for VegetableName
public String getVegetableName() {
return vegetableName;
}
//Getter method for FruitName
public String getFruitName() {
return fruitName;
}
//Getter method for VegetablePrice
public double getVegetablePrice() {
return vegetablePrice;
}
//Getter method for FruitPrice
public double getFruitPrice() {
return fruitPrice;
}
public double calculateSubTotal() {
return
vegetablePrice*vegetableOrdered + fruitPrice*fruitOrdered;
}
public double calculateAdditionalFee() {
double subtotal
=calculateSubTotal();
double additionalFee =
subtotal*serviceRate + deliveryFee;
return additionalFee;
}
public void display() {
System.out.println("-------------------------------------------------");
System.out.println("Groccery
Shopping Order Summary");
System.out.println("Name\t\t\tPounds Ordered: ");
System.out.println(vegetableName+"\t\t"+vegetableOrdered);
System.out.println(fruitName+"\t\t"+fruitOrdered);
System.out.println();
System.out.println("Sub-Total\t\t"+calculateSubTotal());
System.out.println("Additional
Fee:\t\t"+calculateAdditionalFee());
System.out.println("Total
Bill:\t\t"+(calculateSubTotal()+calculateAdditionalFee()));
System.out.println("-------------------------------------------------------------");
}
}
package edu.ilstu;
import java.util.Scanner;
public class GroceryShoppingApp {
public static void main(String[] args) {
// TODO Auto-generated method
stub
Scanner scan = new
Scanner(System.in);
displayTable1();
System.out.println("Please select
the vegetable from Table 1: ");
String vName = scan.next();
System.out.println("Please enter
the price of the selected vegetable : ");
double vprice = scan.nextDouble();
displayTable2();
System.out.println("Please select
the Fruit from Table 2: ");
String fName = scan.next();
System.out.println("Please enter
the price of the selected fruit : ");
double fprice = scan.nextDouble();
GroceryShopping shop = new GroceryShopping(vName,
fName, vprice, fprice);
System.out.println("----------------------------------------------");
System.out.println("Grocery Shopping Menu");
System.out.println();
System.out.println("Name \t\t Price Per Pound");
System.out.println(shop.vegetableName+"\t\t"+shop.vegetablePrice);
System.out.println(shop.fruitName+"\t\t"+shop.fruitPrice);
System.out.println("--------------------------------------------------");
System.out.println("Enter the pounds of
"+shop.vegetableName+" ordered : ");
double vOrder = scan.nextDouble();
System.out.println("Enter the pounds of
"+shop.fruitName+" ordered : ");
double fOrder = scan.nextDouble();
shop.setVegetableOrdered(vOrder);
shop.setFruitOrdered(fOrder);
shop.display();
}
private static void displayTable1() {
// TODO Auto-generated method
stub
System.out.println("Vegetable Name
Price Per Pound");
System.out.println("Broccoli $3.12
");
System.out.println("Yellow Onion
$1.15 ");
System.out.println("Chili Pepper
$4.58 ");
System.out.println("Greens Bundle
$2.82 ");
System.out.println("-----------------------------------------------------------");
System.out.println("Table 1:
Vegetable names with Corresponding price per pound");
}
private static void displayTable2() {
// TODO Auto-generated method
stub
System.out.println("Fruit Name
Price Per Pound");
System.out.println("Apple $1.73
");
System.out.println("Grape $2.15
");
System.out.println("Key Lime $2.58
");
System.out.println("GNavel Orange
$1.86 ");
System.out.println("-----------------------------------------------------------");
System.out.println("Table 2: Fruit
names with Corresponding price per pound");
}
}
Feel free to ask any doubts, if you face any difficulty in understanding.
Please upvote the answer if you find it helpful