Help with Java program Online Store. Thank you! 1 Objective After completion of
ID: 3671658 • Letter: H
Question
Help with Java program Online Store. Thank you!
1 Objective After completion of this lab, you will be able to: . Define and use several classes for an application. 2 Lab Description In the lab, you will develop three classes called OnlineStore, Product, and Order to keep product information and order information of a tiny online shopping mall. 3 Online Store Class OnlineStore name: String products: Product [] orders: Order I If needed, you can add more variables OnlineStore(String) + addProduct): void + productInfo0: void + makeOrder): void If needed, you can add more methods In the lab, assume that the OnlineStore class can have maximum ten products and ten orders. 4 Product Class Product productNum: int name: String unitPrice: double If needed, you can add more variables. Add methods.Explanation / Answer
OnlineStoreDemo.java
import java.util.Scanner;
public class OnlineStoreDemo
{
public static void main(String[] args)
{
Scanner sc = new Scanner (System.in);
OnlineStore csumbOnlineStore = new OnlineStore("ABC");
int option;
System.out.println("Welcome to ABC OnlineStore");
do
{
System.out.println("Select your choice:");
System.out.println(" 1. Add Product");
System.out.println(" 2. Product Info");
System.out.println(" 3. Make Order");
System.out.println(" 4. Exit");
option = sc.nextInt();
if (option == 1)
{
csumbOnlineStore.addProduct();
}
else if (option == 2)
{
csumbOnlineStore.productInfo();
}
else if (option == 3)
{
csumbOnlineStore.makeOrder();
}
else if (option == 4)
{
System.out.println("Bye");
return;
}
else
{
System.out.println("Incorrect option");
}
System.out.println(" ");
} while (true);
}
}
OnlineStore.java
import java.text.DecimalFormat;
import java.util.Scanner;
public class OnlineStore {
private String name;
private Product [] products = new Product[10];
private Order [] orders = new Order [10];
private int numOfProducts = 0;
private int numOfOrders = 0;
private Scanner keyboard = new Scanner(System.in);
OnlineStore(String name)
{
this.name = name;
}
void addProduct()
{
if (numOfProducts < 10){
boolean fail = false;
Scanner keyboard = new Scanner(System.in);
int productNum;
String name;
double unitPrice;
System.out.print("Product Numer: ");
productNum = keyboard.nextInt();
System.out.print("Product Name: ");
keyboard.nextLine();
name = keyboard.nextLine();
System.out.print("Product Price: ");
unitPrice = keyboard.nextDouble();
keyboard.nextLine();
for (int i = 0; i < numOfProducts; i++){
if(products[i].getProductNum() == productNum){
System.out.println("Sorry, " + productNum +
" already exists!");
fail = true;
break;
}
}
if (!fail){
DecimalFormat df = new DecimalFormat("0.00");
products[numOfProducts] = new Product(productNum, name, unitPrice);
System.out.println( "Product Added - " + products[numOfProducts].getname() + ", No: "
+ products[numOfProducts].getProductNum() + ", Price: " +
df.format(products[numOfProducts].getUnitPrice()));
numOfProducts++;
}
}
else{
System.out.println("You already have 10 products");
}
}
void productInfo()
{
int counter = 1;
DecimalFormat df = new DecimalFormat("0.00");
if(numOfProducts == 0)
{
System.out.println("There is no products. ");
}
for(int i = 0; i < numOfProducts; i ++)
{
System.out.println(counter + ". " + products[i].getname() + " (No. " +
products[i].getProductNum() + "): $" +
df.format(products[i].getUnitPrice()));
counter++;
}
}
void makeOrder()
{
int orderNumber;
int productNum;
int counter = 1;
if(numOfProducts == 0)
{
System.out.println("No items to be ordered.");
}
System.out.print("Order NUmber: ");
orderNumber = keyboard.nextInt();
for(int i = 0; i < numOfProducts; i++)
{
System.out.print("Type Product Number (0 to finish) : ");
productNum = keyboard.nextInt();
if(products[i].getProductNum() == productNum)
{
orders[numOfOrders] = new Order(orderNumber, products[i]);
numOfOrders++;
}
else
System.out.println("Error: Product " + productNum +
" does not exist.");
}
System.out.println("Order Info - Order Number: " + orderNumber);
for(int i = 0; i < numOfOrders; i++)
{
System.out.println("Item " + counter + ": " + orders[i].getItems());
counter++;
}
System.out.println("Total Price: " + orders[numOfOrders].getTotalPrice());
}
}
Order.java
public class Order {
private int orderNum;
private double totalPrice = 0;
private Product [] items = new Product [10];
private int numOfOrders;
Order( int orderNum, Product items)
{
this.orderNum = orderNum;
this.items[numOfOrders] = items;
}
public int getOrderNum()
{
return orderNum;
}
public double getTotalPrice()
{
if(numOfOrders == 0)
{
totalPrice = items[numOfOrders].getUnitPrice();
}
return totalPrice;
}
public String getItems()
{
return items[numOfOrders].getname() + " (No: " +
items[numOfOrders].getProductNum() + "): $"
+ items[numOfOrders].getUnitPrice();
}
}
Product.java
public class Product {
private int productNum;
private String name;
private double unitPrice;
Product(int productNum, String name, double unitPrice)
{
this.productNum = productNum;
this.name =name;
this.unitPrice = unitPrice;
}
public int getProductNum()
{
return productNum;
}
public String getname()
{
return name;
}
public double getUnitPrice()
{
return unitPrice;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.