Guest User

Untitled

a guest
Jan 23rd, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.51 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. public class Worker {
  10.     private String name;
  11.     private double availableMoney;
  12.  
  13.     public Worker(String name, double availableMoney) {
  14.         this.name = name;
  15.         this.availableMoney = availableMoney;
  16.     }
  17.  
  18.     public String getName() {
  19.         return this.name;
  20.     }
  21.  
  22.     public double getAvailableMoney() {
  23.         return this.availableMoney;
  24.     }
  25.  
  26.     public void setAvailableMoney(double amount) {
  27.         this.availableMoney = amount;
  28.     }
  29.  
  30.     public Worker purchaseSandwich(Sandwich sandwich)
  31.             throws SandwichPurchasingFailedException {
  32.         if (sandwich.getOwner() != null) {
  33.             throw new SandwichPurchasingFailedException(sandwich
  34.                     + " is already purchased by " + sandwich.getOwner());
  35.         }
  36.         if (sandwich.getPrice() * (1 + Utility.TAX / 100) > this
  37.                 .getAvailableMoney()) {
  38.             throw new SandwichPurchasingFailedException(this
  39.                     + "has unsufficient money (" + this.getAvailableMoney()
  40.                     + ") to buy " + sandwich);
  41.         }
  42.         this.setAvailableMoney(this.getAvailableMoney() - sandwich.getPrice()
  43.                 * (1 + Utility.TAX / 100));
  44.         sandwich.setOwner(this);
  45.         return this;
  46.     }
  47.  
  48.     @Override
  49.     public String toString() {
  50.         return "Worker " + name;
  51.     }
  52.  
  53. }
Add Comment
Please, Sign In to add comment