Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.36 KB | None | 0 0
  1. package test;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. public class Dough {
  7. private String doughType;
  8. private String doughProperty;
  9. private Double doughWeight;
  10.  
  11. private static Map<String, Double> doughTypes = new HashMap<>();
  12. private static Map<String, Double> doughProperties = new HashMap<>();
  13.  
  14. public static void mapOfDoughTypes() {
  15. doughTypes.put("White", 1.5);
  16. doughTypes.put("Wholegrain", 1.0);
  17. }
  18.  
  19. public static void mapOfDoughProperties() {
  20. doughProperties.put("Crispy", 0.9);
  21. doughProperties.put("Chewy", 1.1);
  22. doughProperties.put("Homemade", 1.0);
  23. }
  24.  
  25. private Map<String, Double> getDoughTypes() {
  26. return doughTypes;
  27. }
  28.  
  29. public Dough() {
  30. this.doughType = "";
  31. this.doughProperty = "";
  32. this.doughWeight = -1.0;
  33. }
  34.  
  35. public Dough(String doughType, String doughProperty, Double doughWeight) {
  36. if (!doughTypes.containsKey(doughType) || !doughProperties.containsKey(doughProperty)){
  37. throw new IllegalArgumentException("Invalid type of dough.");
  38. }
  39.  
  40. // DOT HERE
  41. if (doughWeight < 1 || doughWeight > 200) {
  42. throw new IllegalArgumentException("Dough weight should be in the range [1..200].");
  43. }
  44.  
  45. this.doughType = doughType;
  46. this.doughProperty = doughProperty;
  47. this.doughWeight = doughWeight;
  48. }
  49.  
  50. public void setDoughType(String doughType) {
  51. if (!doughTypes.containsKey(doughType)){
  52. throw new IllegalArgumentException("Invalid type of dough.");
  53. }
  54.  
  55. this.doughType = doughType;
  56. }
  57.  
  58. public void setDoughProperty(String doughProperty) {
  59. if (!doughProperties.containsKey(doughProperty)){
  60. throw new IllegalArgumentException("Invalid type of dough.");
  61. }
  62.  
  63. this.doughProperty = doughProperty;
  64. }
  65.  
  66. public String getDoughType() {
  67. return doughType;
  68. }
  69.  
  70. public String getDoughProperty() {
  71. return doughProperty;
  72. }
  73.  
  74. private Double getDoughTypeModiffier(){
  75. return doughTypes.get(this.doughType);
  76. }
  77.  
  78. private Double getDoughPropertyModiffier(){
  79. return doughProperties.get(this.doughProperty);
  80. }
  81.  
  82. public double doughCalories(){
  83. return (2*this.doughWeight)*this.getDoughTypeModiffier()*this.getDoughPropertyModiffier();
  84. }
  85. }
  86.  
  87. package test;
  88.  
  89. import java.io.BufferedReader;
  90. import java.io.InputStreamReader;
  91.  
  92. public class Main {
  93. public static void main(String[] args) throws Exception {
  94. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  95.  
  96. try {
  97.  
  98. String input = bufferedReader.readLine();
  99. int toppingNumber = Integer.parseInt(input.split(" ")[2]);
  100. Pizza pizza = new Pizza(input.split(" ")[1], toppingNumber);
  101.  
  102. Dough.mapOfDoughProperties();
  103. Dough.mapOfDoughTypes();
  104. Topping.mapOfToppingTypes();
  105.  
  106. while (true) {
  107.  
  108. input = bufferedReader.readLine();
  109.  
  110. if (input.equals("END")) {
  111. break;
  112. }
  113.  
  114. switch (input.split(" ")[0]) {
  115. case "Dough":
  116. Dough dough = new Dough(input.split(" ")[1], input.split(" ")[2]
  117. , Double.parseDouble(input.split(" ")[3]));
  118. pizza.setDough(dough);
  119. break;
  120. case "Topping":
  121. Topping topping = new Topping(input.split(" ")[1],
  122. Double.parseDouble(input.split(" ")[2]));
  123. pizza.setToppings(topping);
  124. break;
  125. }
  126.  
  127.  
  128. }
  129.  
  130. System.out.printf("%s - %.2f%n", pizza.getName(), pizza.calculateCalories());
  131.  
  132.  
  133. } catch (IllegalArgumentException iae) {
  134. System.out.println(iae.getMessage());
  135. }
  136.  
  137. }
  138. }
  139.  
  140. package test;
  141.  
  142. import java.util.LinkedList;
  143. import java.util.List;
  144.  
  145. // Toppings from 0 to 10
  146. // Name trim
  147. public class Pizza {
  148. private String name;
  149. private Dough dough;
  150. private int numberOfTopping;
  151. private List<Topping> toppings;
  152.  
  153. public Pizza(String name, int numberOfTopping) {
  154. if(name.trim().length() < 1 || name.trim().length() > 15) {
  155. throw new IllegalArgumentException("Pizza name should be between 1 and 15 symbols.");
  156. }
  157.  
  158. if(numberOfTopping < 0 || numberOfTopping > 10) {
  159. throw new IllegalArgumentException("Number of toppings should be in range [0..10].");
  160. }
  161.  
  162. this.name = name;
  163. this.dough = new Dough();
  164. this.toppings = new LinkedList<>();
  165. this.numberOfTopping = numberOfTopping;
  166. }
  167.  
  168. public void setDough(Dough dough) {
  169. this.dough = dough;
  170. }
  171.  
  172.  
  173.  
  174. public void setToppings(Topping topping) {
  175. this.toppings.add(topping);
  176. }
  177.  
  178. public String getName() {
  179. return name;
  180. }
  181.  
  182. public Dough getDough() {
  183. return dough;
  184. }
  185.  
  186. public List<Topping> getToppings() {
  187. return toppings;
  188. }
  189.  
  190. public double calculateCalories() {
  191. Double doughCalories = this.dough.doughCalories();
  192. Double toppingCalories = 0.0;
  193.  
  194.  
  195. for (Topping topping : toppings) {
  196. toppingCalories = toppingCalories + topping.caloriesTopping();
  197. }
  198.  
  199. return doughCalories + toppingCalories;
  200. }
  201.  
  202.  
  203. }
  204.  
  205.  
  206. package test;
  207.  
  208. import java.util.HashMap;
  209. import java.util.Map;
  210.  
  211. // Dot in toppings weight
  212. public class Topping {
  213. private String toppingName;
  214. private Double weightTopping;
  215.  
  216. private static Map<String, Double> toppingTypes = new HashMap<>();
  217.  
  218. public static void mapOfToppingTypes() {
  219. toppingTypes.put("Meat", 1.2);
  220. toppingTypes.put("Veggies", 0.8);
  221. toppingTypes.put("Cheese", 1.1);
  222. toppingTypes.put("Sauce", 0.9);
  223.  
  224. }
  225.  
  226. public Topping() {
  227. this.toppingName = "";
  228. }
  229.  
  230. public Topping(String toppingName, double weightTopping) {
  231. if (!toppingTypes.containsKey(toppingName)) {
  232. throw new IllegalArgumentException("Cannot place " + toppingName + " on top of your pizza.");
  233. }
  234.  
  235. if (weightTopping < 1 || weightTopping > 50) {
  236. throw new IllegalArgumentException(toppingName + " weight should be in the range [1..50].");
  237. }
  238.  
  239. this.weightTopping = weightTopping;
  240. this.toppingName = toppingName;
  241. }
  242.  
  243. public void setToppingName(String toppingName) {
  244. if (!toppingTypes.containsKey(toppingName)) {
  245. throw new IllegalArgumentException("Cannot place " + toppingName + " on top of your pizza.");
  246. }
  247.  
  248. this.toppingName = toppingName;
  249. }
  250.  
  251. public void setWeightTopping(double weightTopping) {
  252. if (weightTopping < 1 || weightTopping > 50) {
  253. throw new IllegalArgumentException(toppingName + " weight should be in the range [1..50].");
  254. }
  255.  
  256. this.weightTopping = weightTopping;
  257. }
  258.  
  259. private double getToppingModifier() {
  260. return toppingTypes.get(this.toppingName);
  261. }
  262.  
  263. public double caloriesTopping(){
  264. return (2*this.weightTopping)*this.getToppingModifier();
  265. }
  266.  
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement