Advertisement
Guest User

Code

a guest
Feb 20th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. package a3;
  2.  
  3. public interface Ingredient {
  4.  
  5. String getName();
  6. boolean getIsVegetarian();
  7. double getPricePerOunce();
  8. int getCaloriesPerOunce();
  9. double getCaloriesPerDollar();
  10.  
  11. }
  12.  
  13. package a3;
  14.  
  15. public class IngredientImpl implements Ingredient {
  16.  
  17. private String name;
  18. private double price;
  19. private int calories;
  20. private boolean is_vegetarian;
  21.  
  22. public IngredientImpl(String name, double price, int calories, boolean is_vegetarian) {
  23.  
  24. if (name == null) {
  25. throw new RuntimeException("Name is null");
  26. }
  27. if (price < 0) {
  28. throw new RuntimeException("Price can't be negative");
  29. }
  30. if (calories < 0) {
  31. throw new RuntimeException("Calories can't be negative");
  32. }
  33.  
  34. }
  35.  
  36. @Override
  37. public String getName() {
  38. // TODO Auto-generated method stub
  39. return name;
  40. }
  41.  
  42. @Override
  43. public boolean getIsVegetarian() {
  44. // TODO Auto-generated method stub
  45. return is_vegetarian;
  46. }
  47.  
  48. @Override
  49. public double getPricePerOunce() {
  50. // TODO Auto-generated method stub
  51. return price;
  52. }
  53.  
  54. @Override
  55. public int getCaloriesPerOunce() {
  56. // TODO Auto-generated method stub
  57. return calories;
  58. }
  59.  
  60. @Override
  61. public double getCaloriesPerDollar() {
  62. // TODO Auto-generated method stub
  63. return calories * price;
  64. }
  65.  
  66. }
  67.  
  68. package a3;
  69.  
  70. public interface IngredientPortion {
  71.  
  72. Ingredient getIngredient(); // returns a reference to the Ingredient object that this is a portion of
  73. double getAmount(); // return the weight of the portion in ounces
  74. String getName(); // returns the name of the ingredient
  75. boolean getIsVegetarian(); // vegetarian status of the ingredient
  76. double getCalories(); // number of calories -- calories per ounce -- don't round
  77. double getCost(); // how much it costs -- cost per ounce -- don't round
  78. IngredientPortion combine(IngredientPortion other);
  79. // combined weight of the current (i.e., this) object and other. If other is null, then the method should just return the current object.
  80.  
  81. }
  82.  
  83. package a3;
  84.  
  85. public class IngredientPortionImpl implements IngredientPortion {
  86.  
  87. private Ingredient ing;
  88. private double amount;
  89.  
  90. public IngredientPortionImpl(Ingredient ing, double amount) {
  91. if (ing == null) {
  92. throw new RuntimeException("ing is null");
  93. }
  94. if (amount < 0) {
  95. throw new RuntimeException("amount is negative");
  96. }
  97. }
  98.  
  99. @Override
  100. public Ingredient getIngredient() {
  101. // TODO Auto-generated method stub
  102. return ing;
  103. }
  104.  
  105. @Override
  106. public double getAmount() {
  107. // TODO Auto-generated method stub
  108. return amount;
  109. }
  110.  
  111. @Override
  112. public String getName() {
  113. // TODO Auto-generated method stub
  114. return ing.getName();
  115. }
  116.  
  117. @Override
  118. public boolean getIsVegetarian() {
  119. // TODO Auto-generated method stub
  120. return ing.getIsVegetarian();
  121. }
  122.  
  123. @Override
  124. public double getCalories() {
  125. // TODO Auto-generated method stub
  126. return ing.getCaloriesPerOunce();
  127. }
  128.  
  129. @Override
  130. public double getCost() {
  131. // TODO Auto-generated method stub
  132. return ing.getCaloriesPerDollar();
  133. }
  134.  
  135. @Override
  136. public IngredientPortion combine(IngredientPortion other) {
  137. // TODO Auto-generated method stub
  138. if (other == null) {
  139. return this;
  140. }
  141. if (!other.getName().equals(this.getName())) {
  142. throw new RuntimeException("ingredients do not match");
  143. }
  144. // Need to return an IngredientPortion object that is the combined weight of the current object and other (getAmount())
  145. IngredientPortion temp = this;
  146. double tempAmount = temp.getAmount();
  147. tempAmount = this.getAmount() * other.getAmount();
  148. return temp; // DOESNT WORK, returns temp and nothing has changed, figure out how to * amounts and return
  149. }
  150.  
  151. }
  152.  
  153. package a3;
  154.  
  155. public interface MenuItem {
  156.  
  157. String getName();
  158. IngredientPortion[] getIngredients();
  159. int getCalories();
  160. double getCost();
  161. boolean getIsVegetarian();
  162.  
  163. }
  164.  
  165. package a3;
  166.  
  167. public class MenuItemImpl implements MenuItem {
  168.  
  169. private String name;
  170. private IngredientPortion[] ingredients;
  171.  
  172. public MenuItemImpl(String name, IngredientPortion[] ingredients) {
  173.  
  174. }
  175.  
  176. @Override
  177. public String getName() {
  178. // TODO Auto-generated method stub
  179. return name;
  180. }
  181.  
  182. @Override
  183. public IngredientPortion[] getIngredients() {
  184. // TODO Auto-generated method stub
  185. return ingredients;
  186. }
  187.  
  188. @Override
  189. public int getCalories() {
  190. // TODO Auto-generated method stub
  191. int temp = 0;
  192. return 0; // Problem here
  193. }
  194.  
  195. @Override
  196. public double getCost() {
  197. // TODO Auto-generated method stub
  198. return 0;
  199. }
  200.  
  201. @Override
  202. public boolean getIsVegetarian() {
  203. // TODO Auto-generated method stub
  204. return false;
  205. }
  206.  
  207. }
  208.  
  209. INSTRUCTIONS (some, but not all):
  210. Now create an interface called MenuItem. This interface represents a menu item defined by a list of ingredient portions.
  211. getName returns the menu item's name. getIngredients returns an array of IngredientPortion objects that represent the amounts of the various ingredients that go into the menu item. getCalories returns the sum of the calorie information from the menu item's ingredients rounded to the nearest integer. Similarly getCost returns the sum of the cost information rounded to the nearest cent. getIsVegetarian should return true if all of the ingredients in the menu item are vegetarian and false otherwise.
  212. Create a class called MenuItemImpl that implements the MenuItem interface. Again, be sure to validate the parameter values. In this case, name should not be null, ingredients should not be null and have a length greater than zero. You also should check to ensure that none of the elements in ingredients are null.
  213.  
  214. A few hints (beyond the basic ones from adept and novice that I won't repeat):
  215.  
  216. Be sure to clone the array of ingredient portions passed to the constructor so that you encapsulate a copy of the array rather than the original array. If you don't, then you can't prevent the elements from being changed by whatever code provided the array to the constructor.
  217. Similarly, don't return your encapsulated ingredient portion array directly as the result of getIngredients for the same reason. Be sure to return a clone.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement