In: Computer Science
IN JAVA 1.Write a class Item with these following requirements
ØHas two properties: itemName (text) and MSRP (decimal)
ØHas a getTax() method that has no implementation. This method returns a decimal value
ØHas a finalPrice() method that returns the price after tax
ØThis class cannot be instantiated
ØThis class is encapsulated
2.Write a class Electronics that inherits Item with these following requirements
ØHas two properties: manufacturer (text) and tax (decimal). tax value is shared among all electronic items
ØHas a constructor that initializes all attributes
ØProvide an implementation for getTax()
ØThis class is encapsulated
3.Write a class Shop that sells electronic items with these following requirements
ØHas two properties: shopName (text) and itemSold (collection). itemSold stores all the items that the shop sold. You can choose any Java built-in collection type that you like
ØHas a constructor (you can decide which input to have)
ØHas a method sellItem() that add a sold item to itemSold
ØHas a method getSoldList() that returns the names of all items sold in an array. This method shows an error when the collection is empty
ØThis class is encapsulated
4.Write a class Test that creates a Shop, sells a few items, then test getSoldList() and sortSoldList()
ØAssuming tax rate is 7%
Item.java
// abstract class Item ( can not instantiate abstract
class)
public abstract class Item {
// instance variables
private String itemName;
private double MSRP;
// default constructor
public Item() {
super();
}
// parameterized constructor
public Item(String itemName, double mSRP) {
super();
this.itemName = itemName;
MSRP = mSRP;
}
// abstract method
public abstract double getTax();
// method to calculate and return final price of
item
public double finalPrice() {
return MSRP+this.getTax();
}
// setter and getter methods
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public double getMSRP() {
return MSRP;
}
public void setMSRP(double mSRP) {
MSRP = mSRP;
}
}
Electronics.java
public class Electronics extends Item {
// instance variable
private String manufacturer;
private static double tax;
// default constructor
public Electronics() {
super();
tax=7;
}
// parameterized constructor
public Electronics(String itemName, double mSRP,
String manufacturer) {
super(itemName, mSRP); // calling
super class constructor
this.manufacturer =
manufacturer;
tax=7; // tax rate 7%
}
@Override
public double getTax() {
return getMSRP()*tax/100;
}
// setter and getter
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer)
{
this.manufacturer =
manufacturer;
}
}
Shop.java
import java.util.ArrayList;
import java.util.List;
public class Shop {
// instance varibales
private String shopName;
private List<Item> itemSold;
// parameterized constructor
public Shop(String shopName) {
super();
this.shopName = shopName;
this.itemSold = new
ArrayList<Item>(); // Items List
}
// method to add sold item to sold items list
public void sellItem(Item item) {
itemSold.add(item);
}
// method to return sold items list
public List<Item> getSoldList() {
return itemSold;
}
}
Test.java
import java.util.List;
public class Test {
public static void main(String[] args) {
// Shop object
Shop shop=new Shop("Electronics
Store");
// creating Item objects
Item mouse=new Electronics("Mouse",
120.5, "HP");
Item hardDisk=new Electronics("Hard
Disk", 428.0, "Toshiba");
Item monitor=new
Electronics("Monitor", 520.0, "Intel");
Item keyboard=new
Electronics("Keyboard", 120.5, "Dell");
Item laptop=new
Electronics("Laptop", 1200.0, "Apple");
// sell some items
shop.sellItem(laptop);
shop.sellItem(hardDisk);
shop.sellItem(keyboard);
// get all sold items list
List<Item>
soldItemList=shop.getSoldList();
// print all sold items list with
selling price including tax
for (Item item : soldItemList)
{
System.out.println(item.getItemName()+" sold for
$"+item.finalPrice());
}
}
}
Sample output: