Need help with java homework, please! DataAbstraction: Implement the Comparable
ID: 3831005 • Letter: N
Question
Need help with java homework, please!
DataAbstraction:
Implement the Comparable interface in the Transaction.java super class. Sort first by participantID and then by tranID if the participantID’s are the same.
Complete the two subclasses - InvoiceItem.java (extends ‘Item’) and Invoice.java (extends ‘Transaction’)
Completion of the subclass InvoiceItem includes:
Adding an instance variable 'ouncesPerUnit' as an int
Adding a constructor. Parameters are:
o int itemID,
o String description,
o double price,
o int quantityOrdered, o int ouncesPerUnit
Adding a ‘changeInventory()’ 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 'toString()' 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 'tranID' is private to the Item class.) o int customer,
o boolean isPaid
Adding an ‘addItem()’ method. The method creates a new InvoiceItem object and adds it to the ArrayList inherited from the Transaction class. – See Transaction.java. Parameters are:
o int itemID,
o String desc, o double price, o int qty,
o int ounces
Adding an override of the toString() method. Return 'tranID' from Transaction, 'isPaid' and and a listing of InvoiceItems. See example output in output.txt.
Note the ‘tranID’ instance variable in Transaction must be kept private so you will have to access it through calls to the super class constructor and the super class toString methods.
Execution of the Tester.java should produce the output shown in the output file. NOTE: Do not attempt to modify Tester.java.
Here are the codes:
Invoice.java
1 import java.util.*;
2
3 public class Invoice extends Transaction
4 {
5 // Add a true/false variable - 'isPaid'...
6
7
8
9 // Add an Explicit Value Constructor.
10 // Parameters: int tranID, int customer, boolean paid
11 // Note 'tranID' is private to the Item class
12
13
14 // Add an 'addItem' method here.
15 // The method creates a new InvoiceItem object and adds it to the ArrayList
16 // inherited from the Transaction class.
17 // Parameters: int itemID, String desc, double price, int qty, int ounces
18
19
20
21 // Override toString here.
22 // Return value must include 'tranID' from Transaction, 'isPaid'
23 // and listing of InvoiceItems...
24 // See example output in output.txt.
25 }
InvoiceItem.java
1 public class InvoiceItem extends Item
2 {
3
4 // Add variable 'ouncesPerUnit' as an int...
5
6
7 // Add constructor.
8 // Parameters are: int itemID, String description, double price, int quantityOrdered, int ouncesPerUnit
9
10
11 // Add 'changeInventory' method...
12 // The method creates a String that reports the processing that would take place.
13 // See example output in output.txt
14
15
16 // Override 'toString' method here.
17 // Return 'toString' from Item class plus the ounces per unit...
18
19 }
Item.java
1
2 // Item class - do not change anything in this class
3
4 public abstract class Item
5 {
6 protected int itemID;
7 protected String description;
8 protected double unitPrice;
9 protected int quantity;
10
11 public Item(int itemID, String description, double unitPrice, int quantity)
12 {
13 this.itemID = itemID;
14 this.description = description;
15 this.unitPrice = unitPrice;
16 this.quantity = quantity;
17 }
18
19 protected abstract String changeInventory();
20
21 @Override
22 public String toString()
23 {
24 return " Item: " + itemID + " - " + description +
25 " Quantity: " + quantity + ", Cost: " + unitPrice * quantity;
26 }
27
28 }
Tester.java
1 // Tester class. Do not change anything in this class.
2
3 /* Tasks for the data abstraction section:
4 * - Add code to the 'Transaction' class to implement the Comparable interface
5 * - Complete the InvoiceItem and Invoice classes
6 * (See comments in the InvoiceItem.java and Invoice.java)
7 * See also the Item.java class and expected output in output.txt
8 * Note your output will vary slightly for the data stamp - hh:mm:ss
9 */
10 import java.util.*;
11 public class Tester
12 {
13 public static void main(String[] args)
14 {
15 Transaction[] trans = new Transaction[4];
16 PO po;
17 Invoice iv;
18
19 po = new PO(101, 502, "Pending");
20 po.addItem(42, "Lumia 900", 425.00, 2, 1.50);
21 trans[3] = po;
22
23 po = new PO(102, 501, "Approved");
24 po.addItem(24, "Lumia 300", 433.00, 2, 2.0);
25 po.addItem(86, "iPhone 4S", 721.50, 1, 1.75);
26 trans[2] = po;
27
28 iv = new Invoice(201, 902, false);
29 iv.addItem(1255, "Samsung Flash", 125.00, 2, 5);
30 iv.addItem(198, "HTC m7", 533.00, 1, 4);
31 trans[1] = iv;
32
33 iv = new Invoice(202, 901, true);
34 iv.addItem(681, "Lumia 822", 470.50, 3, 4);
35 iv.addItem(199, "HTC One", 389.00, 1, 5);
36 iv.addItem(1255, "Samsung Flash", 125.00, 2, 3);
37 trans[0] = iv;
38
39 // This call to Arrays.sort will require implementation of the
40 // Comparable interface in the Transaction class...
41 Arrays.sort(trans);
42
43 for (Transaction t : trans)
44 {
45 // Show formatted PO's and invoices...
46 System.out.println(t);
47
48 // Process inventory...
49 for (Item i : t.getItems())
50 {
51 System.out.println(" " + i.changeInventory());
52 }
53 }
54 }
55 }
56
Transaction.java
1 import java.util.*;
2 /* Modify this source code in the space indicated only!
3 *
4 * Implement the Comparable interface in this class (see below)...
5 */
6
7
8 public abstract class Transaction // <-- Your code goes here
9 {
10 // Note the tranID variable is private - do not change
11 private int tranID;
12 protected int participantID;
13 protected Date tranDate;
14 protected ArrayList items;
15
16 public Transaction(int tranID, int participantID)
17 {
18 this.tranID = tranID;
19 this.participantID = participantID;
20 this.tranDate = Calendar.getInstance().getTime();
21 items = new ArrayList();
22 }
23
24 protected final ArrayList getItems()
25 {
26 return this.items;
27 }
28
29 /* Implement the Comparable interface here.
30 * Sort first by participantID and if the participantID's are the
31 * same, sort by tranID.
32 */
33
34 // Your code goes here...
35
36
37 @Override
38 public String toString()
39 {
40 return tranID + ", Date: " + tranDate;
41 }
42
43 }
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
Explanation / Answer
Provide: PO.java and and POItem.java to run tester code then i will able to check test properly...