In Class Lab Advancement Programming Exam Preparation Data Abstraction Implement
ID: 3830825 • Letter: I
Question
In Class Lab Advancement Programming Exam Preparation Data Abstraction Implement the Comparable interface in the Transaction java super class. Sort first by 1. participantID and then by tranID if the participantID's are the same. 2. Complete the two subclasses (extends 'Item and invoice java (extends Transaction) Completion of the subclass Invoiceltem includes: Adding an instance variable "ouncesPerUnit' as an int Adding a constructor. Parameters are: int itemID, o String description, o double price. o int quantityOrdered int ouncesPerunit Adding a "changelnventory()' method to override the abstract method in 'Item' This method creates a String that reports the processing that would take place. See example output in output.txt Adding an override of the inherited "toString0' method. Return toString from Item class plus the ounces per unit. See example output in output.txt. Completion of the subclass Invoice includes: Adding an instance variable "isPaid 'as type boolean Adding a constructor. Parameters are: o int tranID (note 'tranlD' is private to the Item class int customer, o boolean isPaid Adding an 'addltem (0' method. The method creates a new Invoiceltem object and adds it to the ArrayList inherited from the Transaction class. See Transaction.java. Parameters are: o nt itemID, o String desc, o double price, o nt qty, o nt ounces Adding an override of the toString() method. Return "tranID' from Transaction isPaid' and and a listing of Invoiceltems. See example output in output.txt.Explanation / Answer
Tester.java
import java.util.*;
public class Tester
{
public static void main(String[] args)
{
Transaction[] trans = new Transaction[4];
PO po;
Invoice iv;
po = new PO(101, 502, "Pending");
po.addItem(42, "Lumia 900", 425.00, 2, 1.50);
trans[3] = po;
po = new PO(102, 501, "Approved");
po.addItem(24, "Lumia 300", 433.00, 2, 2.0);
po.addItem(86, "iPhone 4S", 721.50, 1, 1.75);
trans[2] = po;
iv = new Invoice(201, 902, false);
iv.addItem(1255, "Samsung Flash", 125.00, 2, 5);
iv.addItem(198, "HTC m7", 533.00, 1, 4);
trans[1] = iv;
iv = new Invoice(202, 901, true);
iv.addItem(681, "Lumia 822", 470.50, 3, 4);
iv.addItem(199, "HTC One", 389.00, 1, 5);
iv.addItem(1255, "Samsung Flash", 125.00, 2, 3);
trans[0] = iv;
// This call to Arrays.sort will require implementation of the
// Comparable interface in the Transaction class...
Arrays.sort(trans);
for (Transaction t : trans)
{
// Show formatted PO's and invoices...
System.out.println(t);
// Process inventory...
for (Item i : t.getItems())
{
System.out.println(" " + i.changeInventory());
}
}
}
}
Invoice.java
import java.util.*;
public class Invoice extends Transaction
{
// Add a true/false variable - 'isPaid'...
protected boolean isPaid;
protected String items = "";
protected String inventory = "";
public Invoice(int tranID, int customer, boolean paid) {
super(tranID,customer);
isPaid = paid;
}
protected InvoiceItem addItem(int itemID, String desc, double price, int qty, int ounces) {
InvoiceItem temp = new InvoiceItem(itemID, desc, price, qty, ounces);
items += " Item: " + itemID + " - " + desc + " Quantity: " + qty + ", Cost: + "
+ price * qty + " Ounces: " + ounces;
inventory += temp.changeInventory();
return temp;
}
See example output in output.txt.
@Override
public String toString() {
return " Invoice#: " + super.toString() +
" Customer: " + participantID + " Paid: " + isPaid +
items +
" " + inventory;
}
}
InvoiceItem.java
public class InvoiceItem extends Item
{
// Add variable 'ouncesPerUnit' as an int...
protected int ouncesPerUnit;
// Add constructor.
// Parameters are: int itemID, String description, double price, int quantityOrdered, int ouncesPerUnit
public InvoiceItem(int itemID, String description, double price, int quantityOrdered, int ouncesPerUnit) {
super(itemID, description, price, quantityOrdered);
this.ouncesPerUnit = ouncesPerUnit;
}
// Add 'changeInventory' method...
// The method creates a String that reports the processing that would take place.
// See example output in output.txt
protected String changeInventory() {
return " Inventory has been relieved for " + description + " by " + quantity;
}
// Override 'toString' method here.
// Return 'toString' from Item class plus the ounces per unit...
@Override
public String toString() {
return " Item: " + itemID + " - " + description +
" Quantity: " + quantity + ", Cost: " + unitPrice * quantity + "Ounces: " + ouncesPerUnit;
}
}
Item.java
// Item class - do not change anything in this class
public abstract class Item
{
protected int itemID;
protected String description;
protected double unitPrice;
protected int quantity;
public Item(int itemID, String description, double unitPrice, int quantity)
{
this.itemID = itemID;
this.description = description;
this.unitPrice = unitPrice;
this.quantity = quantity;
}
protected abstract String changeInventory();
@Override
public String toString()
{
return " Item: " + itemID + " - " + description +
" Quantity: " + quantity + ", Cost: " + unitPrice * quantity;
}
}
Transaction.java
import java.util.*;
/* Modify this source code in the space indicated only!
*
* Implement the Comparable interface in this class (see below)...
*/
public abstract class Transaction implements Comparable <Transaction> // <-- Your code goes here
{
// Note the tranID variable is private - do not change
private int tranID;
protected int participantID;
protected Date tranDate;
protected ArrayList<Item> items;
public Transaction(int tranID, int participantID)
{
this.tranID = tranID;
this.participantID = participantID;
this.tranDate = Calendar.getInstance().getTime();
items = new ArrayList<Item>();
}
protected final ArrayList<Item> getItems()
{
return this.items;
}
@Override
public int compareTo(Transaction tran)
{
if (this.participantID - tran.participantID == 0) {
return this.tranID - tran.tranID;
}
return this.participantID - tran.participantID;
}
@Override
public String toString()
{
return tranID + ", Date: " + tranDate;
}
}
output.txt
Purchase order#: 102, Date: Sat Apr 27 hh:mm:ss PDT YYYY
Vendor: 501 Status: Approved
Item: 24 - Lumia 300 Quantity: 2, Cost: 866.0 Markup: 2.0
Item: 86 - iPhone 4S Quantity: 1, Cost: 721.5 Markup: 1.75
Quantity on hand has been increased for Lumia 300 by 2
Quantity on hand has been increased for iPhone 4S by 1
Purchase order#: 101, Date: Sat Apr 27 hh:mm:ss PDT YYYY
Vendor: 502 Status: Pending
Item: 42 - Lumia 900 Quantity: 2, Cost: 850.0 Markup: 1.5
Quantity on hand has been increased for Lumia 900 by 2
Invoice#: 202, Date: Sat Apr 27 hh:mm:ss PDT YYYY
Customer: 901 Paid: true
Item: 681 - Lumia 822 Quantity: 3, Cost: 1411.5 Ounces: 4
Item: 199 - HTC One Quantity: 1, Cost: 389.0 Ounces: 5
Item: 1255 - Samsung Flash Quantity: 2, Cost: 250.0 Ounces: 3
Inventory has been relieved for Lumia 822 by 3
Inventory has been relieved for HTC One by 1
Inventory has been relieved for Samsung Flash by 2
Invoice#: 201, Date: Sat Apr 27 hh:mm:ss PDT YYYY
Customer: 902 Paid: false
Item: 1255 - Samsung Flash Quantity: 2, Cost: 250.0 Ounces: 5
Item: 198 - HTC m7 Quantity: 1, Cost: 533.0 Ounces: 4
Inventory has been relieved for Samsung Flash by 2
Inventory has been relieved for HTC m7 by 1