Repository navigation
Expand file tree
/
Copy pathLab02.java
More file actions
255 lines (210 loc) · 5.61 KB
/
Copy pathLab02.java
File metadata and controls
255 lines (210 loc) · 5.61 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
public class Lab02 {
public static void main(String[] args) {
Car car1 = new Car("RANGE OVER", "BLACK", 4);
car1.start();
Item item1 = new Item("Toy", 55);
Wallet wallet1 = new Wallet("Purple", 50);
Person person = new Person("Duygu", car1, wallet1);
Brain brain = new Brain(person, "medium");
System.out.println("Person is: " + person.getName());
System.out.println("Persons car is: " + car1.toString());
System.out.println("ıtem's price is " + item1.getValue());
System.out.println("Duygu's wallet filled : " + wallet1.getAmount());
System.out.println("Does Duygu buy this item ?");
person.buy(item1);
System.out.println(brain.getSize());
System.out.println(brain.remember());
}
}
class Car {
private String model;
private String color;
private int speed;
private boolean isOn;
private int acceralation;
Car(String model, int par, String color, int par1, int par2) {
this.model = model;
this.color = color;
}
Car(String model, String color, int acceralation) {
this(model, 2020, color, 15000, 300);
this.acceralation = acceralation;
}
public void setAccarelation(int acceralation) {
this.acceralation = acceralation;
}
public void acceralate() {
if (isOn) {
speed += acceralation;
}
}
public void acceralate(int acceralation) {
this.acceralation = acceralation;
acceralate();
}
public void start() {
if (!isOn) {
isOn = true;
acceralate();
System.out.println("The car started");
} else {
System.out.println("The car alderay started");
}
}
public void stop() {
if (isOn) {
speed = 0;
isOn = false;
System.out.println("Car has stopped");
} else {
System.out.println("Car has alderay stopped");
}
}
public String toString() {
if (!isOn) {
return String.format("Color: %s", "Model:%s", color, model);
} else {
return String.format("Color: %s, Model:%s, Current Speed: %d, Acceleration:%d", color, model, speed,
acceralation);
}
}
}
class Item {
private String name;
private int value;
Item(String name, int value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public int getValue() {
return value;
}
public String toString() {
return String.format("%s (%d)", name, value);
}
}
class Wallet {
private String color;
private int amount;
Wallet(String color) {
this.color = color;
}
Wallet(String color, int amount) {
this(color);
this.amount = amount;
}
public String getColor() {
return color;
}
public int getAmount() {
return amount;
}
public void addMoney(int money) {
if (money > 0) {
amount += money;
} else {
throw new ArithmeticException("The Money cannot be negative");
}
}
public void removeMoney(int money) {
if (money <= amount) {
amount -= money;
} else {
throw new IllegalArgumentException("The money cannot bigger than amount");
}
}
@Override
public String toString() {
return String.format("Wallet", color);
}
}
class Brain {
private String size;
private Person person;
Brain(Person person) {
this.person = person;
size = "Medium";
}
Brain(Person person, String size) {
Setperson(person);
Setsize(size);
}
public void Setperson(Person person) {
this.person = person;
}
public void Setsize(String size) {
this.size = size;
}
public Person getPedrson() {
return person;
}
public String getSize() {
return size;
}
public int chance() {
if (size.equalsIgnoreCase("small")) {
return 1;
} else if (size.equalsIgnoreCase("medium")) {
return 2;
} else {
return 3;
}
}
public boolean remember() {
double chance = Math.random() * 4;
if (chance < chance()) {
return true;
} else {
return false;
}
}
@Override
public String toString() {
return person.getName() + " " + size;
}
}
class Person {
private String name;
private Car car;
private Wallet wallet;
private Brain brain;
Person(String name) {
this.name = name;
this.brain = new Brain(null);
}
Person(String name, Car car, Wallet wallet) {
this.name = name;
this.car = car;
this.wallet = wallet;
this.brain = new Brain(null);
}
public boolean think() {
return brain.remember();
}
public String getName() {
return name;
}
public void drive() {
if (car != null) {
car.start();
if (think()) {
car.acceralate();
}
}
}
public void buy(Item item) {
if (wallet != null) {
if (wallet.getAmount() < item.getValue()) {
System.out.println("you don't have enough money.");
} else {
wallet.removeMoney(item.getValue());
System.out.println("You bought the item!");
}
} else {
System.out.println("You dont even have a wallet.");
}
}
}