Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.knowledgeblackbelt.students.ideynega.sandwichdeliveryservice.beans;
- import com.knowledgeblackbelt.students.ideynega.sandwichdeliveryservice.beans.Sandwich;
- import com.knowledgeblackbelt.students.ideynega.sandwichdeliveryservice.exceptions.SandwichPurchasingFailedException;
- import com.knowledgeblackbelt.students.ideynega.sandwichdeliveryservice.utils.Utility;
- import java.util.Set;
- import java.util.HashSet;
- public class Worker {
- private String name;
- private double availableMoney;
- private Set<Sandwich> availableSandwiches;
- public Worker(String name, double availableMoney) {
- this.name = name;
- this.availableMoney = availableMoney;
- availableSandwiches = new HashSet<Sandwich>();
- }
- public String getName() {
- return this.name;
- }
- public double getAvailableMoney() {
- return this.availableMoney;
- }
- public void setAvailableMoney(double amount) {
- this.availableMoney = amount;
- }
- public Worker purchaseSandwich(Sandwich sandwich)
- throws SandwichPurchasingFailedException {
- if (sandwich.getOwner() != null) {
- throw new SandwichPurchasingFailedException(sandwich
- + " is already purchased by " + sandwich.getOwner());
- }
- if (sandwich.getPrice() * (1 + Utility.TAX / 100) > this
- .getAvailableMoney()) {
- throw new SandwichPurchasingFailedException(this
- + "has unsufficient money (" + this.getAvailableMoney()
- + ") to buy " + sandwich);
- }
- this.setAvailableMoney(this.getAvailableMoney() - sandwich.getPrice()
- * (1 + Utility.TAX / 100));
- sandwich.setOwner(this);
- availableSandwiches.add(sandwich);
- return this;
- }
- @Override
- public String toString() {
- return "Worker " + name;
- }
- }
Add Comment
Please, Sign In to add comment