forked from slgobinath/Java-Helps-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiSales.java
More file actions
55 lines (45 loc) · 1.08 KB
/
MultiSales.java
File metadata and controls
55 lines (45 loc) · 1.08 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
52
53
54
55
public class MultiSales {
public static void main(String[] args) {
doStuff(new Pen(10.00), 5);
doStuff(new Book(250.00, 5.00), 2);
}
public static void doStuff(Sellable item, int count) {
double income = item.sell(count);
System.out.println("Income: $" + income);
}
}
interface Sellable {
double sell();
default double sell(int count) {
double total = 0.0;
for(int i = 1; i <= count; i++) {
total += this.sell();
}
return total;
}
default String toString() {
return "df";
}
}
class Book implements Sellable {
private double price;
private double discount;
public Book(double price, double discount) {
this.price = price;
this.discount = discount;
}
@Override
public double sell() {
return price - (price * discount / 100.00);
}
}
class Pen implements Sellable {
private double price;
public Pen(double price) {
this.price = price;
}
@Override
public double sell() {
return price;
}
}