Guest User

Untitled

a guest
Jan 24th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.69 KB | None | 0 0
  1. package com.knowledgeblackbelt.students.ideynega.sandwichdeliveryservice.beans;
  2.  
  3. import com.knowledgeblackbelt.students.ideynega.sandwichdeliveryservice.beans.Sandwich;
  4.  
  5. import com.knowledgeblackbelt.students.ideynega.sandwichdeliveryservice.exceptions.SandwichPurchasingFailedException;
  6.  
  7. import com.knowledgeblackbelt.students.ideynega.sandwichdeliveryservice.utils.Utility;
  8.  
  9. import java.util.Set;
  10. import java.util.HashSet;
  11.  
  12. public class Worker {
  13.     private String name;
  14.     private double availableMoney;
  15.     private Set<Sandwich> availableSandwiches;
  16.  
  17.     public Worker(String name, double availableMoney) {
  18.         this.name = name;
  19.         this.availableMoney = availableMoney;
  20.         availableSandwiches = new HashSet<Sandwich>();
  21.     }
  22.  
  23.     public String getName() {
  24.         return this.name;
  25.     }
  26.  
  27.     public double getAvailableMoney() {
  28.         return this.availableMoney;
  29.     }
  30.  
  31.     public void setAvailableMoney(double amount) {
  32.         this.availableMoney = amount;
  33.     }
  34.  
  35.     public Worker purchaseSandwich(Sandwich sandwich)
  36.             throws SandwichPurchasingFailedException {
  37.         if (sandwich.getOwner() != null) {
  38.             throw new SandwichPurchasingFailedException(sandwich
  39.                     + " is already purchased by " + sandwich.getOwner());
  40.         }
  41.         if (sandwich.getPrice() * (1 + Utility.TAX / 100) > this
  42.                 .getAvailableMoney()) {
  43.             throw new SandwichPurchasingFailedException(this
  44.                     + "has unsufficient money (" + this.getAvailableMoney()
  45.                     + ") to buy " + sandwich);
  46.         }
  47.         this.setAvailableMoney(this.getAvailableMoney() - sandwich.getPrice()
  48.                 * (1 + Utility.TAX / 100));
  49.         sandwich.setOwner(this);
  50.         availableSandwiches.add(sandwich);
  51.         return this;
  52.     }
  53.  
  54.     @Override
  55.     public String toString() {
  56.         return "Worker " + name;
  57.     }
  58.  
  59. }
Add Comment
Please, Sign In to add comment