Question
In Java, define class Item with five fields, name, price, tax, amountOnStock, and amountSold, a proper constructor, and a method revenue() which returns the amount
In Java, define class Item with five fields, name, price, tax, amountOnStock, and amountSold, a proper constructor, and a method revenue() which returns the amount of money for selling particular items; then define class Store which can hold in an array up to 10 items; define also a proper constructor, a method revenue() which prints an itemized list of items in the store, the money earned for selling each item one along, and the total amount of money earned. Define a method sell(String item) to sell an item and update the amount in stock and the amount sold; make sure that the item is in the store. Also, define methods toString() and equals(Object o). Here is a possible program run after the store has been initialized: 1. Print list of items; 2. Sell an Item. Your choice: 1 item1: price = $5.55, tax = 5.0%, in stock: 1, number sold: 0 item2: price = $15.55, tax = 6.0%, in stock: 1, number sold: 0 Store revenue = $0.00 1. Print list of items; 2. Sell an Item. Your choice: 2 Enter the name of an item: item1 item1 is unavailable. 1. Print list of items; 2. Sell an Item. Your choice: 1 item1: price = $5.55, tax = 5.0%, in stock: 0, number sold: 1 item2: price = $15.55, tax = 6.0%, in stock: 1, number sold: 0 Store revenue = $5.83 1. Print list of items; 2. Sell an Item. Your choice: 2 Enter the name of item: item1 item1 is not in the store. 1. Print list of items; 2. Sell an Item. Your choice: 3 This is what I have: In Item.java:
public class Item { public String name; public double price; public double tax; public int amountOnStock; public int amountSold;
public Item(String name, double price, double tax, int amountOnStock) { this.name = name; this.price = price; this.tax = tax; this.amountOnStock = amountOnStock; this.amountSold = 0; }
public double revenue() { return (price + price * tax/100) * amountSold; }
public String toString() { return name + ": price = $" + price + ", tax = " + tax + "%, in stock: " + amountOnStock + ", number sold: " + amountSold; }
public boolean equals(Object o) { if (o == this) { return true; } if (!(o instanceof Item)) { return false; } Item i = (Item) o; return i.name.equals(name) && i.price == price && i.tax == tax; } }
class Store { private Item[] items; private int numItems;
public Store() { items = new Item[10]; numItems = 0; }
public void addItem(Item item) { if (numItems < 10) { items[numItems++] = item; } }
public double revenue() { double totalRevenue = 0; for (int i = 0; i < numItems; i++) { System.out.println(items[i] + " Revenue: $" + items[i].revenue()); totalRevenue += items[i].revenue(); } return totalRevenue; }
public void sell(String itemName) { for (int i = 0; i < numItems; i++) { if (items[i].name.equals(itemName)) { if (items[i].amountOnStock > 0) { items[i].amountOnStock--; items[i].amountSold++; } else { System.out.println(itemName + " is unavailable."); } return; } } System.out.println(itemName + " is not in the store."); }
public String toString() { StringBuilder sb = new StringBuilder(); for (int i = 0; i < numItems; i++) { sb.append(items[i]).append(" "); } return sb.toString(); } }
In Driver.java:
import java.util.Scanner;
public class Driver { public static void main(String[] args) { Store store = new Store(); store.addItem(new Item("item1", 5.55, 5.0, 1)); store.addItem(new Item("item2", 15.55, 6.0, 1));
Scanner sc = new Scanner(System.in); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started