-
Notifications
You must be signed in to change notification settings - Fork 0
/
Worker.java
48 lines (36 loc) · 1.01 KB
/
Worker.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
package com.knowledgeblackbelt.sandwichdelivery.model;
import java.util.ArrayList;
import java.util.List;
public class Worker {
private String name;
private double money;
private List<Sandwich> sandwiches = new ArrayList<Sandwich>();
public Worker(String name, double money) {
this.name = name;
this.money = money;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public List<Sandwich> getSandwiches() {
return sandwiches;
}
public void purchase(Sandwich sandwich) {
if (sandwich.getOwner() != null) {
System.out.println("Cannot buy such sandwich. It has already an owner");
}
if (sandwich.getPrice() > this.getMoney()) {
System.out.println("Cannot buy such sandwich. Worker hasn't got enough money: " + this);
} else {
this.setMoney(this.getMoney() - sandwich.getPrice());
this.getSandwiches().add(sandwich);
sandwich.setOwner(this);
}
}
public String toString() {
return name + " (" + money + ")";
}
}