Untitled
unknown
plain_text
3 years ago
3.2 kB
7
Indexable
public class TestInventory{
public static void printCompare(InventoryItem item1, InventoryItem item2){
switch (InventoryItem.compare(item1, item2)) {
case -1:
System.out.println(item1.getItemName() + " has less value than " + item2.getItemName());
break;
case 0:
System.out.println(item1.getItemName() + " has equal value to " + item2.getItemName());
break;
case 1:
System.out.println(item1.getItemName() + " has greater value than ");
}
}
public static void printValue(InventoryItem item){
System.out.println("Total Value: $"+ String.format("%.2f",item.getTotalValue()));
}
public static void main(String[] args){
InventoryItem emptyItem = new InventoryItem();
InventoryItem staplers = new InventoryItem("Stapler, Red", 91745, 7.89);
InventoryItem pencils = new InventoryItem("Pencil, #2", 73105, 0.35, 210);
InventoryItem notebooks = new InventoryItem("Notebook, Spiral", 68332, 2.57, 38);
InventoryItem[] store = {emptyItem, staplers, pencils, notebooks};
for (InventoryItem e: store){
System.out.println(e.toString());
printValue(e);
}
printCompare(pencils, notebooks);
}
}
class InventoryItem{
private String itemName = "TBD";
private int sku = 0;
private double price = 0.0;
private int quantity = 0;
public InventoryItem(){}
public InventoryItem(String itemName, int sku, double price){
this.itemName = itemName;
this.sku = Math.abs(sku);
this.price = Math.abs(price);
}
public InventoryItem(String itemName, int sku, double price, int quantity){
this.itemName = itemName;
this.sku = Math.abs(sku);
this.price = Math.abs(price);
this.quantity = Math.abs(quantity);
}
//GETTERS
public String getItemName(){
return this.itemName;
}
public int getSku(){
return this.sku;
}
public double getPrice(){
return this.price;
}
public int getQuantity(){
return this.quantity;
}
//END OF GETTERS
//SETTERS
public void setItemName(String itemName){
this.itemName = itemName;
}
public void setSku(int sku){
this.sku = Math.abs(sku);
}
public void setPrice(double price){
this.price = Math.abs(price);
}
public void setQuantity(int quantity){
this.quantity = Math.abs(quantity);
}
//END OF SETTERS
//METHODS
public double getTotalValue(){
return this.quantity*this.price;
}
public String toString(){
return this.itemName + " [" + this.sku + "]: " + this.quantity + " at $" + String.format("%.2f",this.price) + " each.";
}
public static int compare(InventoryItem item1, InventoryItem item2){
if (item1.getTotalValue() > item2.getTotalValue()){
return 1;
}else if (item1.getTotalValue() == item2.getTotalValue()){
return 0;
}else{
return -1;
}
}
}Editor is loading...