Browse files

price and money from long to double

purchase() method added to worker
  • Loading branch information...
1 parent 91413a2 commit 27f9a3b3390e00c84727a424ae2b0ee7a8111a0d @jcarlosgarcia committed Jul 28, 2012
View
8 ...eliveryServiceBBFWorkshop/src/com/knowledgeblackbelt/sandwichdelivery/model/Sandwich.java
@@ -6,12 +6,12 @@
public class Sandwich {
private String name;
- private long price;
+ private double price;
private Worker owner;
private List<Supplement> supplements = new ArrayList<Supplement>();
- public Sandwich(String name, long price) {
+ public Sandwich(String name, double price) {
this.name = name;
this.price = price;
}
@@ -24,11 +24,11 @@ public void setName(String name) {
this.name = name;
}
- public long getPrice() {
+ public double getPrice() {
return price;
}
- public void setPrice(long price) {
+ public void setPrice(double price) {
this.price = price;
}
View
25 ...hDeliveryServiceBBFWorkshop/src/com/knowledgeblackbelt/sandwichdelivery/model/Worker.java
@@ -5,26 +5,43 @@
public class Worker {
private String name;
- private long money;
+ private double money;
private List<Sandwich> sandwiches = new ArrayList<Sandwich>();
- public Worker(String name, long money) {
+ public Worker(String name, double money) {
this.name = name;
this.money = money;
}
- public long getMoney() {
+ public double getMoney() {
return money;
}
- public void setMoney(long 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 + ")";
}

0 comments on commit 27f9a3b

Please sign in to comment.