-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIceCreamTruck.java
160 lines (117 loc) · 2.82 KB
/
IceCreamTruck.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
public class IceCreamTruck {
private int iceCreamSandwich, pushUpPop, fudgeBar;
private double sales;
private final double icsCost = 2, pupCost = 1.25, fbCost = 6;
//final is, roughly, Java for const
public IceCreamTruck() {
iceCreamSandwich = 0;
pushUpPop = 0;
fudgeBar = 0;
sales = 0;
}
public IceCreamTruck(int ics, int pup, int fb) {
if (ics > 0)
iceCreamSandwich = ics;
if (pup > 0)
pushUpPop = pup;
if (fb > 0)
fudgeBar = fb;
}
public void sellIceCreamSandwich(int ics) {
if (ics <= iceCreamSandwich) {
iceCreamSandwich -= ics;
sales += ics * icsCost;
}
else {
sales += iceCreamSandwich * icsCost;
iceCreamSandwich = 0;
}
}
public void sellIceCreamSandwich() {
if (iceCreamSandwich > 0) {
iceCreamSandwich--;
sales += icsCost;
}
}
public void sellPushUpPop(int pup) {
if (pup > pushUpPop)
pup = pushUpPop;
pushUpPop -= pup;
sales += pup * pupCost;
}
public void sellPushUpPop() {
sellPushUpPop(1);
}
public void sellFudgeBar(int fb) {
if (fb > fudgeBar)
fb = fudgeBar;
fudgeBar -= fb;
sales += fb * fbCost;
}
public void sellFudgeBar() {
sellFudgeBar(1);
}
public void transferIceCreamSandwiches(int ics) {
if (iceCreamSandwich + ics >= 0 )
iceCreamSandwich += ics;
else
iceCreamSandwich = 0;
}
public void transferPushUpPops(int pup) {
if (pushUpPop + pup >= 0)
pushUpPop += pup;
else
pushUpPop = 0;
}
public void transferFudgeBars(int fb) {
if (fudgeBar + fb >= 0)
fudgeBar += fb;
else
fudgeBar = 0;
}
public int getIceCreamSandwiches() {
return iceCreamSandwich;
}
public int getPushUpPops() {
return pushUpPop;
}
public int getFudgeBars() {
return fudgeBar;
}
public double getSales() {
return sales;
}
public void resetSales() {
sales = 0;
}
public String toString() {
String s = "Ice Cream Sandwiches: " + iceCreamSandwich + '\n';
s += "PushUp Pops: " + pushUpPop + '\n';
s += "Fudge Bars: " + fudgeBar + '\n';
s += "Current total sales: $" + sales + '\n';
return s;
}
}
class IceCreamBusiness {
public static void main(String args[]) {
IceCreamTruck ic1 = new IceCreamTruck(40, 40, 40);
IceCreamTruck ic2 = new IceCreamTruck(50, 100, 100);
IceCreamTruck ic3 = new IceCreamTruck(50, -300, 80);
outputICSs(ic1, ic2, ic3);
ic2.sellFudgeBar(5);
ic2.sellFudgeBar();
ic2.sellPushUpPop(13);
ic1.sellIceCreamSandwich(20);
ic1.sellIceCreamSandwich(15);
ic1.sellIceCreamSandwich(20);
outputICSs(ic1, ic2, ic3);
}
public static void outputICSs(IceCreamTruck ic1, IceCreamTruck ic2, IceCreamTruck ic3) {
System.out.println("Truck 1\n" + ic1.toString());
System.out.println();
System.out.println("Truck 2\n" + ic2.toString());
System.out.println();
System.out.println("Truck 3\n" + ic3.toString());
System.out.println();
}
}