Question

In: Computer Science

JAVA program to simulate a customer’s online grocery shopping. The customer will shop fruit and vegetable...

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:

  • Vegetable name: the vegetable name field is a String object that holds the name of the vegetable selected from the Table 1.
  • Fruit name: the fruit name field is a String object that holds the name of the fruit selected from the Table 2.
  • Vegetable price: this vegetable price field is a double that holds the per pound price of the selected vegetable.
  • Fruit price: this fruit price field is a double that holds the per pound price of the selected fruit.
  • Vegetable ordered: the vegetable ordered field is a double that holds the pounds of the selected vegetable ordered
  • Fruit ordered: the fruit ordered field is a double that holds the pounds of the selected fruit ordered

Named Constants

  • service rate: a double type constant for 3.5% service rate on the total money spent on the vegetable and fruit ordered (not include delivery fee)
  • Delivery fee: an integer type constant for 5 dollars delivery fee.

Methods:

  • Constructor: the constructor should accept the selected vegetable and fruit names along with the corresponding price per pound as arguments (please use Table 1 and 2 to choose the vegetable and fruit name and the corresponding price per pound). The constructor should also assign 0 to the fields of vegetable ordered and fruit ordered.
  • Getters for each instance variable
  • Setters for the pounds of the vegetable and fruit ordered
  • A method, calculateSubtotal, calculates the subtotal based on the selected vegetable and fruit, their price per pound and how many pounds ordered.
  • A method, calculateAdditionalFee that will calculate the total cost of delivery and service fee of the total vegetable and fruit ordered:

Additional fee = subtotal * service rate + delivery fee

  • A method, displayOrderSummary, that will display the title of “Grocery Shopping Order Summary”, names of the selected vegetable and fruit and the corresponding pounds ordered, as well as the subtotal, additional fee and the total bill with formatting (Please use the sample run as a reference).

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:

  • Display the Table 1 and 2
  • Create a GroceryShopping object using the values of the selected vegetable and fruit name and corresponding price per pound as input arguments (using Table 1 and 2).
  • Display the grocery shopping menu of vegetable and fruit items and their prices
  • Utilize methods in the GroceryShopping object to perform calculations and display the grocery shopping order summary
  • Follow the sample interaction on what to display for prompts, labels, messages and the flow for screen input and outputs.
    • Ask for how many pounds the customer wants to order for the selected vegetable and fruit
    • Update the GroceryShopping instance with the order numbers entered.
    • Display a summary of the grocery order, see sample run.
  • Review the sample run of the program to determine how the interaction with the user should flow.

Input:

  • Your program prompts the user to enter the selected vegetable name, then the price per pound
  • Next, the program prompts the user to enter the selected fruit name, then the price per pound
  • Then, it displays a Menu with the selected vegetable and fruit with their price per pound.
  • It should then prompt the user for the pounds of the selected vegetable he/she would like to order. After the number of pounds of the selected vegetable has been entered (which may be 0), the program should continue displaying a prompt and reading the number of pounds to order for the selected fruit.

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.

Solutions

Expert Solution

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


Related Solutions

Python: Write a program to simulate the purchases in a grocery store. A customer may buy...
Python: Write a program to simulate the purchases in a grocery store. A customer may buy any number of items. So, you should use a while loop. Your program should read an item first and then read the price until you enter a terminal value (‘done’) to end the loop. You should add all the prices inside the loop. Add then a sales tax of 8.75% to the total price. Print a receipt to indicate all the details of the...
draw traceability matrix for online shopping system ( online grocery store)
draw traceability matrix for online shopping system ( online grocery store)
5.9 Online shopping cart (Java) Create a program using classes that does the following in the...
5.9 Online shopping cart (Java) Create a program using classes that does the following in the zyLabs developer below. For this lab, you will be working with two different class files. To switch files, look for where it says "Current File" at the top of the developer window. Click the current file name, then select the file you need. (1) Create two files to submit: ItemToPurchase.java - Class definition ShoppingCartPrinter.java - Contains main() method Build the ItemToPurchase class with the...
7.7 Ch 7 Program: Online shopping cart (continued) (C) This program extends the earlier "Online shopping...
7.7 Ch 7 Program: Online shopping cart (continued) (C) This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase struct to contain a new data member. (2 pt) char itemDescription[ ] - set to "none" in MakeItemBlank() Implement the following related functions for the ItemToPurchase struct. PrintItemDescription() Has an ItemToPurchase parameter. Ex. of PrintItemDescription() output: Bottled Water: Deer Park, 12 oz. (2) Create three new files: ShoppingCart.h - struct definition and...
Java 176 Lottery Program in Word. Using ArrayLists to Simulate a Lottery Program Simulate a Lottery...
Java 176 Lottery Program in Word. Using ArrayLists to Simulate a Lottery Program Simulate a Lottery Drawing by choosing 7 numbers at random from a pool containing 30 numbers Each time a number is selected, it is recorded and removed from the pool The pool values are 00 to 29 inclusive Your program must output each selected number from the drawing using a two-digit format. For Example, the number 2 would be represented by 02 on the “Picked” line. The...
IN C++ 7.22 LAB*: Program: Online shopping cart (Part 2) This program extends the earlier "Online...
IN C++ 7.22 LAB*: Program: Online shopping cart (Part 2) This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class per the following specifications: Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt) Public member functions SetDescription() mutator & GetDescription() accessor (2 pts) PrintItemCost() - Outputs the item name followed by the quantity, price, and subtotal PrintItemDescription() - Outputs the...
Java: Using ArrayLists to Simulate a Lottery Program Simulate a Lottery Drawing by choosing 7 numbers...
Java: Using ArrayLists to Simulate a Lottery Program Simulate a Lottery Drawing by choosing 7 numbers at random from a pool containing 30 numbers Each time a number is selected, it is recorded and removed from the pool The pool values are 00 to 29 inclusive Your program must output each selected number from the drawing using a two-digit format. For Example, the number 2 would be represented by 02 on the “Picked” line.    The numbers drawn cannot repeat Sort...
In Java, Write a Java program to simulate an ecosystem containing two types of creatures, bears...
In Java, Write a Java program to simulate an ecosystem containing two types of creatures, bears and fish. The ecosystem consists of a river, which is modeled as a relatively large array. Each cell of the array should contain an Animal object, which can be a Bear object, a Fish object, or null. In each time step, based on a random process, each animal either attempts to move into an adjacent array cell or stay where it is. If two...
This week you will write a program that mimics an online shopping cart . You will...
This week you will write a program that mimics an online shopping cart . You will use all the programming techniques we have learned thus far this semester, including if statements, loops, lists and functions. It should include all of the following: The basics - Add items to a cart until the user says "done" and then print out a list of items in the cart and a total price. - Ensure the user types in numbers for quantities -...
Bluemart Limited ("BL") provides online grocery shopping and delivery in Singapore. The company has been gaining...
Bluemart Limited ("BL") provides online grocery shopping and delivery in Singapore. The company has been gaining market share with its fresh produce, friendly user-interface and on-time delivery services. Delivery service is currently outsourced to an external provider, and is expected to cost $250,000 for the coming year. BL wants to have better control over delivery operations, and hence, is considering investing in its own fleet of delivery vehicles. The useful life of the vehicle fleet is expected to be five...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT