Question: APPLIANCE.JAVA //Abstract class capturing shared state between Fridge and ToasterOven public abstract class Appliance extends Product{ private int wattage; private String color; private String brand;
APPLIANCE.JAVA
//Abstract class capturing shared state between Fridge and ToasterOven
public abstract class Appliance extends Product{
private int wattage;
private String color;
private String brand;
public Appliance(double initPrice, int initQuantity, int initWattage, String initColor, String initBrand){
super(initPrice, initQuantity);
wattage = initWattage;
color = initColor;
brand = initBrand;
}
public String getColor(){
return color;
}
public String getBrand(){
return brand;
}
public int getWattage(){
return wattage;
}
}
--------
COMPUTER.JAVA
//Abstract class capturing shared state between Desktop and Laptop
public abstract class Computer extends Product{
private double cpuSpeed;
private int ram;
private boolean ssd;
private int storage;
public Computer(double initPrice, int initQuantity, double initCPUSpeed, int initRAM, boolean initSSD, int initStorage){
super(initPrice, initQuantity);
cpuSpeed = initCPUSpeed;
ram = initRAM;
ssd = initSSD;
storage = initStorage;
}
public double getCPUSpeed(){
return cpuSpeed;
}
public int getRAM(){
return ram;
}
public boolean getSSD(){
return ssd;
}
public int getStorage(){
return storage;
}
}
-----
ElectronicStore.JAVA
//Class representing an electronic store
//Has an array of products that represent the items the store can sell
import java.util.ArrayList;
import java.util.Scanner;
public class ElectronicStore{
public final int MAX_PRODUCTS = 10; //Maximum number of products the store can have
private int curProducts;
String name;
Product[] stock; //Array to hold all products
double revenue;
public ElectronicStore(String initName){
revenue = 0.0;
name = initName;
stock = new Product[MAX_PRODUCTS];
curProducts = 0;
}
public String getName(){
return name;
}
//Adds a product and returns true if there is space in the array
//Returns false otherwise
public boolean addProduct(Product newProduct){
if(curProducts < MAX_PRODUCTS){
stock[curProducts] = newProduct;
curProducts++;
return true;
}
return false;
}
//Sells 'amount' of the product at 'index' in the stock array
//Updates the revenue of the store
//If no sale occurs, the revenue is not modified
//If the index is invalid, the revenue is not modified
public void sellProducts(int index, int amount){
if(index >= 0 && index < curProducts){
revenue += stock[index].sellUnits(amount);
}
}
public double getRevenue(){
return revenue;
}
//Prints the stock of the store
public void printStock(){
for(int i = 0; i < curProducts; i++){
System.out.println(i + ". " + stock[i] + " (" + stock[i].getQuantity() + " in stock, " + stock[i].getPrice() + " dollars each)");
}
}
public static ElectronicStore createStore(){
ElectronicStore store1 = new ElectronicStore("Watts Up Electronics");
Desktop d1 = new Desktop(100, 10, 3.0, 16, false, 250, "Compact");
Desktop d2 = new Desktop(200, 10, 4.0, 32, true, 500, "Server");
Laptop l1 = new Laptop(150, 10, 2.5, 16, true, 250, 15);
Laptop l2 = new Laptop(250, 10, 3.5, 24, true, 500, 16);
Fridge f1 = new Fridge(500, 10, 250, "White", "Sub Zero", 15.5, false);
Fridge f2 = new Fridge(750, 10, 125, "Stainless Steel", "Sub Zero", 23, true);
ToasterOven t1 = new ToasterOven(25, 10, 50, "Black", "Danby", 8, false);
ToasterOven t2 = new ToasterOven(75, 10, 50, "Silver", "Toasty", 12, true);
store1.addProduct(d1);
store1.addProduct(d2);
store1.addProduct(l1);
store1.addProduct(l2);
store1.addProduct(f1);
store1.addProduct(f2);
store1.addProduct(t1);
store1.addProduct(t2);
return store1;
}
}
-------
DESKTOP.JAVA
//Class representing a single desktop computer
public class Desktop extends Computer{
String towerProfile;
public Desktop(double initPrice, int initQuantity, double initCPUSpeed, int initRAM, boolean initSSD, int initStorage, String initProfile){
super(initPrice, initQuantity, initCPUSpeed, initRAM, initSSD, initStorage);
towerProfile = initProfile;
}
public String toString(){
String result = towerProfile + " Desktop PC with " + getCPUSpeed() + "ghz CPU, " + getRAM() + "GB RAM, " + getStorage() + "GB ";
if(getSSD()){
result += "SSD drive.";
}else{
result += "HDD drive.";
}
return result;
}
-----
Fridge.java
//Class representing a single type of Fridge
public class Fridge extends Appliance{
double cubicFeet;
boolean hasFreezer;
public Fridge(double initPrice, int initQuantity, int initWattage, String initColor, String initBrand, double initFeet, boolean initFreezer){
super(initPrice, initQuantity, initWattage, initColor, initBrand);
cubicFeet = initFeet;
hasFreezer = initFreezer;
}
public String toString(){
String result = cubicFeet + " cu. ft. " + getBrand() + " Fridge ";
if(hasFreezer){
result += "with Freezer ";
}
result += "(" + getColor() + ", " + getWattage() +" watts)";
return result;
}
}
-----
Laptop.java
//Class representing a single laptop product
public class Laptop extends Computer{
private double screenSize;
public Laptop(double initPrice, int initQuantity, double initCPUSpeed, int initRAM, boolean initSSD, int initStorage, double initScreen){
super(initPrice, initQuantity, initCPUSpeed, initRAM, initSSD, initStorage);
screenSize = initScreen;
}
public String toString(){
String result = screenSize + " inch Laptop PC with " + getCPUSpeed() + "ghz CPU, " + getRAM() + "GB RAM, " + getStorage() + "GB ";
if(getSSD()){
result += "SSD drive.";
}else{
result += "HDD drive.";
}
return result;
}
}
---
Product.JAVA
//Base class for all products the store will sell
public class Product{
private double price;
private int quantity;
public Product(double initPrice, int initQuantity){
price = initPrice;
quantity = initQuantity;
}
public int getQuantity(){
return quantity;
}
public double getPrice(){
return price;
}
//Returns the total revenue (price * amount) if there are at least amount items in stock
//Return 0 otherwise (i.e., there is no sale completed)
public double sellUnits(int amount){
if(amount > 0 && quantity >= amount){
quantity -= amount;
return price * amount;
}
return 0.0;
}
}
----
Toasteroven.java
//Class representing a single toaster oven product
public class ToasterOven extends Appliance{
private int width;
private boolean convection;
public ToasterOven(double initPrice, int initQuantity, int initWattage, String initColor, String initBrand, int initWidth, boolean initConvection){
super(initPrice, initQuantity, initWattage, initColor, initBrand);
width = initWidth;
convection = initConvection;
}
public String toString(){
String result = width + " inch " + getBrand() + " Toaster ";
if(convection){
result += "with convection ";
}
result += "(" + getColor() + ", " + getWattage() +" watts)";
return result;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
