Advertisement
desislava_topuzakova

Untitled

Dec 12th, 2022
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. package christmasPastryShop.core;
  2.  
  3. import christmasPastryShop.common.ExceptionMessages;
  4. import christmasPastryShop.common.OutputMessages;
  5. import christmasPastryShop.common.enums.DelicacyType;
  6. import christmasPastryShop.common.enums.CocktailType;
  7. import christmasPastryShop.common.enums.BoothType;
  8. import christmasPastryShop.core.interfaces.Controller;
  9. import christmasPastryShop.entities.delicacies.Gingerbread;
  10. import christmasPastryShop.entities.delicacies.Stolen;
  11. import christmasPastryShop.entities.delicacies.interfaces.Delicacy;
  12. import christmasPastryShop.entities.drinks.MulledWine;
  13. import christmasPastryShop.entities.drinks.Hibernation;
  14. import christmasPastryShop.entities.drinks.interfaces.Cocktail;
  15. import christmasPastryShop.entities.booths.OpenBooth;
  16. import christmasPastryShop.entities.booths.PrivateBooth;
  17. import christmasPastryShop.entities.booths.interfaces.Booth;
  18. import christmasPastryShop.repositories.interfaces.*;
  19.  
  20. public class ControllerImpl implements Controller {
  21. private final DelicacyRepository<Delicacy> delicacyRepository;
  22. private final CocktailRepository<Cocktail> cocktailRepository;
  23. private final BoothRepository<Booth> boothRepository;
  24. private double totalIncome;
  25.  
  26. public ControllerImpl(DelicacyRepository<Delicacy> delicacyRepository, CocktailRepository<Cocktail> cocktailRepository, BoothRepository<Booth> boothRepository) {
  27. this.delicacyRepository = delicacyRepository;
  28. this.cocktailRepository = cocktailRepository;
  29. this.boothRepository = boothRepository;
  30. this.totalIncome = 0;
  31. }
  32.  
  33. @Override
  34. public String addDelicacy(String type, String name, double price) {
  35. Delicacy delicacy = delicacyRepository.getByName(name);
  36. if (delicacy != null) {
  37. throw new IllegalArgumentException(String.format(ExceptionMessages.FOOD_OR_DRINK_EXIST, delicacy.getClass().getSimpleName(), name));
  38. }
  39. DelicacyType foodType = DelicacyType.valueOf(type);
  40. delicacy = foodType == DelicacyType.Gingerbread ? new Gingerbread(name, price) : new Stolen(name, price);
  41. delicacyRepository.add(delicacy);
  42. return String.format(OutputMessages.DELICACY_ADDED, name, type);
  43. }
  44.  
  45. @Override
  46. public String addCocktail(String type, String name, int size, String brand) {
  47. Cocktail cocktail = cocktailRepository.getByName(name);
  48. if (cocktail != null) {
  49. throw new IllegalArgumentException(String.format(ExceptionMessages.FOOD_OR_DRINK_EXIST, cocktail.getClass().getSimpleName(), cocktail.getName()));
  50. }
  51. cocktail = CocktailType.valueOf(type) == CocktailType.MulledWine ? new MulledWine(name, size, brand) : new Hibernation(name, size, brand);
  52. cocktailRepository.add(cocktail);
  53. return String.format(OutputMessages.COCKTAIL_ADDED, name, brand);
  54. }
  55.  
  56. @Override
  57. public String addBooth(String type, int boothNumber, int capacity) {
  58. Booth booth = boothRepository.getByNumber(boothNumber);
  59. if (booth != null) {
  60. throw new IllegalArgumentException(String.format(ExceptionMessages.BOOTH_EXIST, boothNumber));
  61. }
  62. BoothType tableType = BoothType.valueOf(type);
  63. booth = tableType == BoothType.OpenBooth ? new OpenBooth(boothNumber, capacity) : new PrivateBooth(boothNumber, capacity);
  64. boothRepository.add(booth);
  65. return String.format(OutputMessages.BOOTH_ADDED, boothNumber);
  66. }
  67.  
  68. @Override
  69. public String reserveBooth(int numberOfPeople) {
  70. for (Booth booth : boothRepository.getAll()) {
  71. if (!booth.isReserved() && booth.getCapacity() >= numberOfPeople) {
  72. booth.reserve(numberOfPeople);
  73. return String.format(OutputMessages.BOOTH_RESERVED, booth.getBoothNumber(), numberOfPeople);
  74. }
  75. }
  76. return String.format(OutputMessages.RESERVATION_NOT_POSSIBLE, numberOfPeople);
  77. }
  78.  
  79. @Override
  80. public String leaveBooth(int boothNumber) {
  81. Booth booth = boothRepository.getByNumber(boothNumber);
  82. double bill = booth.getBill();
  83. booth.clear();
  84. this.totalIncome += bill;
  85. return String.format(OutputMessages.BILL, boothNumber, bill);
  86. }
  87.  
  88. @Override
  89. public String getIncome() {
  90. return String.format(OutputMessages.TOTAL_INCOME, this.totalIncome);
  91. }
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement