-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInvoiceDemo.java
79 lines (64 loc) · 2.31 KB
/
InvoiceDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
interface Payable {
double getPaymentAmount();
}
abstract class Employee implements Payable {
String name;
String surname;
String SSN;
public Employee(String fn, String ln, String soc){
name = fn;
surname = ln;
SSN = soc;
}
public String toString(){
return String.format("%s %s\nSocial SN : %s",name,surname,SSN);
}
}
class Invoice implements Payable {
String partNumber;
String partDescription;
int quantity;
double pricePerItem;
public Invoice(String pn, String pd, int quan, double ppi) {
partNumber = pn;
partDescription = pd;
quantity = quan;
pricePerItem = ppi;
}
public double getPaymentAmount(){
return quantity * pricePerItem;
}
public String toString(){
return String.format("Invoice:\nPart number : %s (%s)\nQuantity : %d\nPrice per item : $%,.2f",partNumber,partDescription,quantity,pricePerItem);
}
}
class SalariedEmployee extends Employee {
double weeklySalary;
public SalariedEmployee(String fn, String ln, String soc, double salary){
super(fn,ln,soc);
weeklySalary = salary;
}
public double getPaymentAmount(){
return weeklySalary;
}
public String toString(){
return String.format("Salaried Employee : %s\nWeekly salary %.2f",super.toString(),getPaymentAmount());
}
}
public class InvoiceDemo {
public static void main(String[] args){
//int n = 4;
Payable payabaleObjects[] = new Payable[4];
double total = 0;
payabaleObjects[0] = new Invoice("111","thingamagic",3,100.00);
payabaleObjects[1] = new Invoice("112","thingbmagic",4,100.00);
payabaleObjects[2] = new Invoice("113","thingcmagic",5,100.00);
payabaleObjects[3] = new SalariedEmployee("Russell","Saerang","123-AAA-ZZZ",10000.00);
System.out.println("Polymorphism go!:\n");
for (int i=0;i<4;i++){
System.out.printf("%s\nPayment due : $%,.2f\n\n",payabaleObjects[i].toString(),payabaleObjects[i].getPaymentAmount());
total += payabaleObjects[i].getPaymentAmount();
}
System.out.printf("Total = $%,.2f\n\n",total);
}
}