Lab 6 (). Requirements: Create a program that serves as a storefront The followi
ID: 3696532 • Letter: L
Question
Lab 6 ().
Requirements:
Create a program that serves as a storefront
The following items will be for sale
Ice Cream – $1.25
Burger – $3.50
Hot Dog – $4.00
Fries – $2.50
Slice of Cake – $6.75
The program will present a menu with the following options:
List items in cart
Display all items here along with their price.
Display the total cost
Add item to cart
they select it, display a confirmation of purchase text
Checkout
Print all the items and their price in the order as well as the total price, ask user to pay
If the payment is too small, reject and ask again
If payment is enough, inform the user what their change is(if any) and return to the main menu.
Each option will be a numeric value starting at 1
1. List Items in Cart
2. Add Item to Cart
3. Checkout
The main function must be inside of a class called Main.
If your program can be crashed, you will receive a 25 point deduction. This is to say, you should not trust user input. If a user enters an incorrect input, you should notify them of the error and ask them to enter the input again
Supply a scanned UML Class Diagram that details your design.
Include the class that will contain main
Grade Breakdown:
Correct Price and Change Calculation = 15 Points
Correct Input Handling = 15 Points
Correct Menus = 15 Points
Correct Class Diagram = 30 Points
Quality of Design = 25 Points
Total = 100 Points
ADDITIONALLY DO ALSO THIS.....
Requirements:
Create a program that satisfies all the requirements of Lab 6 and the following:
The following additional items will be for sale
Burger and Fries Combo
The price should be equal to the combined process of Burger and Fries.
This shouldn’t be a hardcoded thing…If the price of burger and fries changes, this code shouldn’t have to be rewritten.
Hot Dog and Fries Combo
The price should be equal to the combined process of Hot Dog and Fries. This shouldn’t be a hardcoded thing…If the price of burger and fries changes, this code shouldn’t have to be rewritten.
Incorporate the Composite Pattern
Supply a scanned UML Class Diagram that details your design.
Include the class that will contain main
Grade Breakdown:
Correct Implementation of Composite Pattern = 45 Points
Correct Class Diagram = 30 Points
Quality of Design = 25 Points
Explanation / Answer
Solution: See the code below
1. StoreFront class: StoreFront.java
------------------------------------------------------------------
package storefront;
import java.util.Scanner;
/**
* StoreFront class
*
*/
public class StoreFront {
class Item {
private int itemId; // Id of item
private String itemName; // Item name
private double itemCost; // Item cost in $
/**
* @param itemName
* @param itemCost
*/
public Item(int itemId, String itemName, double itemCost) {
super();
this.itemId = itemId;
this.itemName = itemName;
this.itemCost = itemCost;
}
/**
* @return the itemId
*/
int getItemId() {
return itemId;
}
/**
* @return the itemName
*/
String getItemName() {
return itemName;
}
/**
* @return the itemCost
*/
double getItemCost() {
return itemCost;
}
}
private Item itemsInStore[]; // items in store
private static int MAX_ITEMS = 10; // maximum number of items in store
private int numberOfItemsInStore; // total number of items in store
private Item cart[]; // shopping cart
private int numberOfItemsInCart; // total number of items in cart
private double totalCost; // total cost of items in cart
private Scanner input; // for reading customer input
/**
* Default constructor
*/
public StoreFront() {
itemsInStore = new Item[MAX_ITEMS];
numberOfItemsInStore = 0;
cart = new Item[MAX_ITEMS];
totalCost = 0.0;
input = new Scanner(System.in);
}
/**
* Initialize store
*/
public void initializeStore() {
Item item1 = new Item(1, "Ice Cream", 1.25);
itemsInStore[numberOfItemsInStore] = item1;
numberOfItemsInStore++;
Item item2 = new Item(2, "Burger", 3.50);
itemsInStore[numberOfItemsInStore] = item2;
numberOfItemsInStore++;
Item item3 = new Item(3, "Hot Dog", 4.00);
itemsInStore[numberOfItemsInStore] = item3;
numberOfItemsInStore++;
Item item4 = new Item(4, "Fires", 2.50);
itemsInStore[numberOfItemsInStore] = item4;
numberOfItemsInStore++;
Item item5 = new Item(5, "Slice of Cake", 6.75);
itemsInStore[numberOfItemsInStore] = item5;
numberOfItemsInStore++;
}
/**
* Lists items available in store
*/
void listItemsInStore() {
System.out.println("Items available in store:");
System.out.println("Item Id Item Name Item Cost");
System.out.println("------- --------- ---------");
for (int i = 0; i < numberOfItemsInStore; i++) {
Item item = itemsInStore[i];
System.out.println(item.getItemId() + " " + item.getItemName() + " " + item.getItemCost());
}
}
/**
* Lists items available in cart
*/
public void listItemsInCart() {
if (numberOfItemsInCart == 0)
System.out.println("No items in cart.");
else {
System.out.println("Items in cart:");
for (int i = 0; i < numberOfItemsInCart; i++) {
Item item = cart[i];
System.out.println(item.getItemId() + " " + item.getItemName() + " " + item.getItemCost());
totalCost += item.getItemCost();
}
}
System.out.println("Total Cost:" + totalCost);
}
/**
* Adds an item to cart
*/
public void addItemToCart() {
System.out.println("Select an item from the list below:");
listItemsInStore();
// Scanner input=new Scanner(System.in);
System.out.println("Enter Item Id to add:");
int itemChoice = input.nextInt();
cart[numberOfItemsInCart] = itemsInStore[itemChoice - 1];
numberOfItemsInCart++;
System.out.println("Item added to cart!");
// input.close();
}
/**
* Checks out
*/
public void checkout() {
System.out.println("Are you ready to checkout? (1/0)");
int answer = input.nextInt();
if (answer == 1) {
System.out.println("Preparing to checkout...");
listItemsInCart();
System.out.println("Please make your payment as mentioned against the total cost:");
double payment = input.nextDouble();
while (payment < totalCost) {
System.out.println("Wrong payment! Please pay again.");
payment = input.nextDouble();
}
System.out.println("Payment made:" + payment);
if (payment >= totalCost) {
System.out.println("Payment complete!");
System.out.println("Change to tender, if any:" + (payment - totalCost));
System.out.println("Thank you very much for shopping with us!");
}
System.out.println("Now checking out...");
input.close();
System.out.println("Checked out!");
} else if (answer == 0) {
System.out.println("Ok, continue and enjoy your shopping...");
}
}
/**
* Prints menu
*/
private void printMenu() {
System.out.println("Options:");
System.out.println("1. List Items in Cart");
System.out.println("2. Add Item to Cart");
System.out.println("3. Checkout");
}
/**
* Utility function to interact with customer
*/
public void interactWithCustomer() {
// Scanner input=new Scanner(System.in);
while (true) {
printMenu();
System.out.print("Choice:");
int choice = input.nextInt();
// input.next();
switch (choice) {
case 1:
listItemsInCart();
break;
case 2:
addItemToCart();
break;
case 3:
checkout();
System.exit(0);
default:
System.out.println("Wrong choice. Re-enter.");
}
}
}
}
----------------------------------------------
2. Main class containing main() function: Main.java
-----------------------------------------------
/**
*
*/
package storefront;
/**
* Main class containing main() function
*/
class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
StoreFront storeFront = new StoreFront();
storeFront.initializeStore();
storeFront.listItemsInStore();
storeFront.interactWithCustomer();
}
}
------------------------------------------------------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.