8:12 am online.uts.edu.au ..ooo vodafone AU 100% A store sells a number of produ
ID: 3741961 • Letter: 8
Question
8:12 am online.uts.edu.au ..ooo vodafone AU 100% A store sells a number of products. The initial stock is shown below:: Stock Whiteboard Eraser Black Pen ed Pen lue Pen The store has a cash register which stores all cash from sales of the store's product. The store clerk uses the following menu: 1. sell 2. restock 3. view stock 4. view cash 5. prune products 6. exit The "prune products" option removes all products with zero stock from the store. ample VO: n addition to the basic functionality above, your program should also satisfy the e The "sell option prints an error if the selected product is not found. The "sell" option ignores the case of the product name (e.g. bLaCk pEn). e The "sell" option allows the product name to be partially entered. e.g. Entering "red will sell a "Red Pen". Entering "pen" will instead show a list of matching products (Black Pen, Red Pen, Blue Pen). The "restock" option also allows a partial product name to be entered. All matching products will be restocked. The "restock" option, if given a product name that doesn't exist, will automatically create and add a new product with that nameExplanation / Answer
-----------------------------------------------------Product.java----------------------------------------------------------------------
package Store;
public class Product {
String name;
int quantity;
double price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Product() {
this.name="";
}
public Product(String name, int quantity, double price) {
super();
this.name = name;
this.quantity = quantity;
this.price = price;
}
}
-------------------------------------------------------Stock.java-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
package Store;
import java.util.ArrayList;
import java.util.Scanner;
public class Stock {
Product[] s = new Product[20];
ArrayList<Product> stock = new ArrayList<Product>();
public void feelInitialStock() {
stock.add(new Product("Whiteboard Marker", 85 , 1.5));
stock.add(new Product("Whiteboard Eraser",45,5));
stock.add(new Product("Black Pen",100,1.5));
stock.add(new Product("Red Pen",100,1.5));
stock.add(new Product("Blue Pen",100,1.5));
}
public void viewStock() {
for(Product st : stock) {
System.out.println(st.getName()+" - "+st.getQuantity()+" at $"+st.getPrice());
}
}
public double viewCash() {
double cash=0.0;
for(Product st : stock) {
cash = cash + (st.getQuantity()*st.getPrice());
}
return cash;
}
public void sellProduct() {
String name="";
String nameInStock="";
int quantity;
double price;
ArrayList<String> nameToBeSearched = new ArrayList<String>();
int count = 0;
System.out.println("Name : ");
Scanner sc = new Scanner(System.in);
name = sc.nextLine();
for(Product p : stock) {
nameInStock = p.getName();
if(nameInStock.toLowerCase().contains(name)) {
nameToBeSearched.add(nameInStock);
}
}
if(nameToBeSearched.size()==0) {
System.out.println("Selected Product is not Found");
}
else if(nameToBeSearched.size()==1) {
System.out.println("Selling "+nameToBeSearched.get(0));
for(Product p:stock) {
if(p.getName().equalsIgnoreCase(nameToBeSearched.get(0))) {
System.out.println("Number ");
quantity = Integer.parseInt(sc.next());
p.setQuantity(p.getQuantity()-quantity);
}
}
}
else{
System.out.println("Multiple Products Match");
for(String nam : nameToBeSearched) {
for(Product p:stock) {
if(p.getName().equalsIgnoreCase(nam)) {
System.out.println(p.getName()+" - "+p.getQuantity()+" at $"+p.getPrice());
}
}
}
sellProduct();
}
}
public void restockProduct() {
String name="";
String nameInStock="";
int quantity;
double price;
ArrayList<String> nameToBeSearched = new ArrayList<String>();
int count = 0;
System.out.println("Name : ");
Scanner sc = new Scanner(System.in);
name = sc.nextLine();
for(Product p : stock) {
nameInStock = p.getName();
if(nameInStock.toLowerCase().contains(name)) {
nameToBeSearched.add(nameInStock);
}
}
for(String nam : nameToBeSearched) {
for(Product p:stock) {
if(p.getName().equalsIgnoreCase(nam)) {
System.out.println("Restocking "+p.getName());
System.out.println("Number ");
quantity = Integer.parseInt(sc.next());
p.setQuantity(p.getQuantity()+quantity);
}
}
}
if(nameToBeSearched.isEmpty()) {
System.out.println("Adding new Product");
System.out.println("Number ");
quantity = Integer.parseInt(sc.next());
System.out.println("Price ");
price = Double.parseDouble(sc.next());
stock.add(new Product(name,quantity,price));
}
}
public void pruneProducts() {
for(Product p : stock) {
if(p.getQuantity()==0) {
stock.remove(p);
}
}
}
public void displayMenu() {
System.out.println("Menu");
System.out.println("1 Sell");
System.out.println("2 Restock");
System.out.println("3 View Stock");
System.out.println("4 View Cash");
System.out.println("5 Prune Products");
System.out.println("6 Exit");
}
}
-------------------------------------------------------Driver.java-------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------
package Store;
import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
Stock stock = new Stock();
char choice='x';
stock.displayMenu();
stock.feelInitialStock();
do{
System.out.println("Choice (s/r/v/c/p/x) : ");
Scanner sc = new Scanner(System.in);
choice = sc.next().charAt(0);
switch(choice) {
case 's' : stock.sellProduct(); break;
case 'r' : stock.restockProduct(); break;
case 'v' : stock.viewStock(); break;
case 'c' : stock.viewCash(); break;
case 'p' : stock.pruneProducts(); break;
}
}while(choice =='s' || choice=='r' || choice=='v' || choice=='c' || choice=='p' || choice=='x');
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.