-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrder.java
More file actions
51 lines (42 loc) · 1.32 KB
/
Copy pathOrder.java
File metadata and controls
51 lines (42 loc) · 1.32 KB
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
package comp_equ_hash_impl_interf;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
interface Discountable {
double getDiscount();
}
public abstract class Order implements Comparable<Order> {
int id;
String customer;
double totalAmount;
public Order(int id, String customer, double totalAmount) {
this.id = id;
this.customer = customer;
this.totalAmount = totalAmount;
}
public int compareTo(Order other) {
return Double.compare(this.totalAmount, other.totalAmount);
}
public boolean equals(Object o) {
if (o == this) return true;
if (o == null || getClass() != o.getClass()) return false;
Order order = (Order) o;
return id == order.id;
}
public int hashCode() {
return Double.hashCode(totalAmount);
}
public String toString() {
return customer + " | " + totalAmount;
}
public static void main(String[] args) {
ArrayList<Order> list = new ArrayList<>(Arrays.asList(
new OrderOnline(1, "Alice", 150.00),
new OrderOffline(2, "Bob", 200.00),
new OrderOnline(3, "Charlie", 100.00),
new OrderOnline(1, "Alice", 150.00)
));
Collections.sort(list);
System.out.println(list);
}
}