Advertisement
Crenox

Trio Java Program

Mar 11th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. // Sammy Samkough
  2. // Trio
  3. // Spec: Test your ability to build a class that implements an interface as well as build classes and methods around it.
  4.  
  5. public interface MenuItem
  6. {
  7. /** @return the name of the menu item */
  8. String getName();
  9.  
  10. /** @return the price of the menu item */
  11. double getPrice();
  12. }
  13. ------------------------------------------------------------------------------------------------------------------------------
  14. // Sammy Samkough
  15. // Trio
  16. // Spec: Test your ability to build a class that implements an interface as well as build classes and methods around it.
  17.  
  18. public class Sandwich implements MenuItem
  19. {
  20. private String name;
  21. private double price;
  22.  
  23. public Sandwich(String n, double p)
  24. {
  25. name = n;
  26. price = p;
  27. }
  28.  
  29. /** @return the name of the Sandwich */
  30. public String getName()
  31. {
  32. return name;
  33. }
  34.  
  35. /** @return the price of the Sandwich */
  36. public double getPrice()
  37. {
  38. return price;
  39. }
  40. }
  41. ------------------------------------------------------------------------------------------------------------------------------
  42. // Sammy Samkough
  43. // Trio
  44. // Spec: Test your ability to build a class that implements an interface as well as build classes and methods around it.
  45.  
  46. public class Salad implements MenuItem
  47. {
  48. private String name;
  49. private double price;
  50.  
  51. public Salad(String n, double p)
  52. {
  53. name = n;
  54. price = p;
  55. }
  56.  
  57. /** @return the name of the Salad */
  58. public String getName()
  59. {
  60. return name;
  61. }
  62.  
  63. /** @return the price of the Salad */
  64. public double getPrice()
  65. {
  66. return price;
  67. }
  68. }
  69. ------------------------------------------------------------------------------------------------------------------------------
  70. // Sammy Samkough
  71. // Trio
  72. // Spec: Test your ability to build a class that implements an interface as well as build classes and methods around it.
  73.  
  74. public class Drink implements MenuItem
  75. {
  76. private String name;
  77. private double price;
  78.  
  79. public Drink(String n, double p)
  80. {
  81. name = n;
  82. price = p;
  83. }
  84.  
  85. /** @return the name of the Drink */
  86. public String getName()
  87. {
  88. return name;
  89. }
  90.  
  91. /** @return the price of the Drink */
  92. public double getPrice()
  93. {
  94. return price;
  95. }
  96. }
  97. ------------------------------------------------------------------------------------------------------------------------------
  98. // Sammy Samkough
  99. // Trio
  100. // Spec: Test your ability to build a class that implements an interface as well as build classes and methods around it.
  101.  
  102. public class Trio implements MenuItem
  103. {
  104. private Sandwich sn;
  105. private Salad sl;
  106. private Drink d;
  107.  
  108. public Trio(Sandwich sandwich, Salad salad, Drink drink)
  109. {
  110. sn = sandwich;
  111. sl = salad;
  112. d = drink;
  113. }
  114.  
  115. public String getName()
  116. {
  117. String result = "";
  118.  
  119. result += "Combo: " + sn.getName() + " / " + sl.getName() + " / " + d.getName() + " ";
  120.  
  121. return result;
  122. }
  123.  
  124. public double getPrice()
  125. {
  126. double price = 0;
  127. double snPrice = sn.getPrice();
  128. double slPrice = sl.getPrice();
  129. double dPrice = d.getPrice();
  130.  
  131. // if sandwich is greater than salad, and sandwich is greater than drink, and salad is greater than drink...
  132. // add sandwich and salad
  133. if (snPrice > slPrice && snPrice > dPrice && slPrice > dPrice)
  134. {
  135. price = snPrice + slPrice;
  136. }
  137. // if salad is greater than sandwich, and salad is greater than drink, and drink is greater than sandwich...
  138. // add salad and drink
  139. else if (slPrice > snPrice && slPrice > dPrice && dPrice > snPrice)
  140. {
  141. price = slPrice + dPrice;
  142. }
  143. // other, add drink and sandwich
  144. else
  145. {
  146. price = dPrice + snPrice;
  147. }
  148.  
  149. return price;
  150. }
  151.  
  152. public String toString()
  153. {
  154. String result = "";
  155.  
  156. result += getName() + "\nPrice: $" + getPrice() + " \n";
  157.  
  158. return result;
  159. }
  160. }
  161. ------------------------------------------------------------------------------------------------------------------------------
  162. // Sammy Samkough
  163. // Trio
  164. // Spec: Test your ability to build a class that implements an interface as well as build classes and methods around it.
  165.  
  166. public class TrioRunner
  167. {
  168. public static void main(String[] args)
  169. {
  170. Sandwich cheeseBurger = new Sandwich("Cheese Burger", 3.50);
  171. Sandwich club = new Sandwich("Club Sandwich", 2.75);
  172. Salad spinach = new Salad("Spinach Salad", 1.25);
  173. Salad slaw = new Salad("Coleslaw", 1.25);
  174. Drink orange = new Drink("Orange Soda", 1.25);
  175. Drink cappuccino = new Drink("Cappuccino", 3.50);
  176.  
  177. Trio trio1 = new Trio(cheeseBurger, slaw, orange);
  178. Trio trio2 = new Trio(club, spinach, cappuccino);
  179.  
  180. System.out.println(trio1);
  181. System.out.println(trio2);
  182. }
  183. }
  184. /*
  185. Combo: Cheese Burger / Coleslaw / Orange Soda
  186. Price: $4.75
  187.  
  188. Combo: Club Sandwich / Spinach Salad / Cappuccino
  189. Price: $6.25
  190.  
  191. Press any key to continue . . .
  192. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement