//W6 CheckPoint: InventoryPart3.java
//Inventory Program Part 3 Week 6 class Television modified with additional class and restocking fee
//import statements
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Arrays;
class Television { //class name and attributes
//declare class variables
private String itemNumber; //item # of product
private String televisionName; //product name
public int unitsinStock; //# of units in stock
public double unitPrice; //Price per unit
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
//class constructor
public Television(String itemNumber, String televisionName, int unitsinStock, double price) {
this.itemNumber = itemNumber;
this.televisionName = televisionName;
this.unitsinStock = unitsinStock;
this.unitPrice = price;
} //end constructor
//define set and get methods for class variables
//getter and setter methods for Television
//item number
public String getItemNumber() { //getter for item number
return itemNumber;
} //end getter item number
public void setItemNumber (String itemNumber) { //setter for item number
this.itemNumber = itemNumber;
} //end setter item number
//television name
public String getTelevisionName() { //getter for product name
return televisionName;
} //end getter television name
public void setTelevisionName (String product) { //setter for product name
this.televisionName = televisionName;
} //end setter television name
//available units
public double getUnitsInStock() { //getter for units in stock
return unitsinStock;
} //end getter units in stock
public void setUnitsInStock (double units) { //setter for units in stock
this.unitsinStock = unitsinStock;
} //end setter units in stock
//price
public double getUnitPrice() { //getter for unit price
return unitPrice;
} //end getter for unit price
public void setUnitPrice (double price) { //setter for unit price
this.unitPrice = unitPrice;
} //end setter unit price
//calculate the total inventory by returning the product of available units and price
public double calculateInventory(){
return unitPrice * unitsinStock;
}
//toString method that outputs the class variables
public String toString () {
return "Television Name:" + "\t" + televisionName + "\n" +
"Item Number:" + "\t" + itemNumber + "\n" +
"UnitsInStock:" + "\t \t" + unitsinStock + "\n" +
"UnitPrice:" + "\t \t" + nf.format(unitPrice) + "\n" +
"Item Total:" + "\t" + nf.format(calculateInventory());
}
}
// The is-a test the inheritance relationship between Samsung and Television
class Televisions extends Television{
//class variables
private String serialNumber;
private double restockFee;
private static final double RESTOCK_FEE_PERCENTAGE = .05;
//class constructor
public Televisions(String itemNumber, String televisionName, int unitsinStock, double price, String serialNumber){
super (itemNumber, televisionName, unitsinStock, price);
this.serialNumber = serialNumber;
//calculate restock fee as 5% of the inventory total
restockFee = super.calculateInventory() * RESTOCK_FEE_PERCENTAGE;
}
//declare get and set methods
//serial number
public String getSerialNumber(){
return serialNumber;
}
public void setSerialNumber(String serialNumber){
this.serialNumber = serialNumber;
}
//restockFee does not have a set method, to ensure that it is only set through the HP constructor
public double getRestockFee(){
return restockFee;
}
//add restock fee and serial number to the output
public String toString () {
return super.toString() + "\n" +
"Serial Number: " + "\t" + serialNumber + "\n" +
"Restock Fee:" + "\t" + nf.format(getRestockFee());
}
}
public class InventoryPart3 {
public static Television[] sortArray(Television[] televisions){
// First Step
String[] names = new String[televisions.length];
//Second Step
Television[] tempProduct = new Television [televisions.length];
//Third Step
for (int i = 0; i < televisions.length; i++){
names[i] = televisions[i].getTelevisionName();
}
//Fourth Step
Arrays.sort(names);
//Fifth Step
for (int i = 0; i < televisions.length; i++){ //Product Array
for (int j = 0; j < names.length; j++){ //Names Array
if (televisions[i].getTelevisionName().equalsIgnoreCase(names[j])) {
tempProduct[j] = televisions[i];
break;
}
}
}
return tempProduct;
}
public static double calculateInventoryTotal(Television[] televisions){
double total = 0;
for (int i = 0; i < televisions.length; i++){
total += televisions[i].calculateInventory();
}
return total;
}
public static void main (String[] args) {
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
//create several items for the inventory of type Television and LG
Television[] televisions = new Television[5];
Television samsung = new Television ("003", "Samsung UN46D6400",9,1599.99);
Television vizio = new Television ("005", "Vizio XVT553SV",6,1299.00);
Television panasonic = new Television ("002", "Panasonic Viera TC-P50VT25",2,2079.99);
Television sony = new Television ("004", "Sony Bravia KDL-55EX720",8, 1889.99);
Television lg = new Television ("001", "LG Infinia 47LX9500",2,2099.00);
//add all of the items to the inventory
televisions[2] = samsung;
televisions[4] = vizio;
televisions[1] = panasonic;
televisions[3] = sony;
televisions[0] = lg;
//sort items
televisions = sortArray(televisions);
//calculate inventory total
double inventoryTotal = calculateInventoryTotal(televisions);
//output inventory
System.out.println("Inventory Items: " + "\n");
for (int i = 0; i < televisions.length; i++){
System.out.println(televisions[i]);
System.out.println();
}
//output inventory total
System.out.println("Inventory Total: " + nf.format(inventoryTotal));
}
}