Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Sammy Samkough
- // Trio
- // Spec: Test your ability to build a class that implements an interface as well as build classes and methods around it.
- public interface MenuItem
- {
- /** @return the name of the menu item */
- String getName();
- /** @return the price of the menu item */
- double getPrice();
- }
- ------------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // Trio
- // Spec: Test your ability to build a class that implements an interface as well as build classes and methods around it.
- public class Sandwich implements MenuItem
- {
- private String name;
- private double price;
- public Sandwich(String n, double p)
- {
- name = n;
- price = p;
- }
- /** @return the name of the Sandwich */
- public String getName()
- {
- return name;
- }
- /** @return the price of the Sandwich */
- public double getPrice()
- {
- return price;
- }
- }
- ------------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // Trio
- // Spec: Test your ability to build a class that implements an interface as well as build classes and methods around it.
- public class Salad implements MenuItem
- {
- private String name;
- private double price;
- public Salad(String n, double p)
- {
- name = n;
- price = p;
- }
- /** @return the name of the Salad */
- public String getName()
- {
- return name;
- }
- /** @return the price of the Salad */
- public double getPrice()
- {
- return price;
- }
- }
- ------------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // Trio
- // Spec: Test your ability to build a class that implements an interface as well as build classes and methods around it.
- public class Drink implements MenuItem
- {
- private String name;
- private double price;
- public Drink(String n, double p)
- {
- name = n;
- price = p;
- }
- /** @return the name of the Drink */
- public String getName()
- {
- return name;
- }
- /** @return the price of the Drink */
- public double getPrice()
- {
- return price;
- }
- }
- ------------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // Trio
- // Spec: Test your ability to build a class that implements an interface as well as build classes and methods around it.
- public class Trio implements MenuItem
- {
- private Sandwich sn;
- private Salad sl;
- private Drink d;
- public Trio(Sandwich sandwich, Salad salad, Drink drink)
- {
- sn = sandwich;
- sl = salad;
- d = drink;
- }
- public String getName()
- {
- String result = "";
- result += "Combo: " + sn.getName() + " / " + sl.getName() + " / " + d.getName() + " ";
- return result;
- }
- public double getPrice()
- {
- double price = 0;
- double snPrice = sn.getPrice();
- double slPrice = sl.getPrice();
- double dPrice = d.getPrice();
- // if sandwich is greater than salad, and sandwich is greater than drink, and salad is greater than drink...
- // add sandwich and salad
- if (snPrice > slPrice && snPrice > dPrice && slPrice > dPrice)
- {
- price = snPrice + slPrice;
- }
- // if salad is greater than sandwich, and salad is greater than drink, and drink is greater than sandwich...
- // add salad and drink
- else if (slPrice > snPrice && slPrice > dPrice && dPrice > snPrice)
- {
- price = slPrice + dPrice;
- }
- // other, add drink and sandwich
- else
- {
- price = dPrice + snPrice;
- }
- return price;
- }
- public String toString()
- {
- String result = "";
- result += getName() + "\nPrice: $" + getPrice() + " \n";
- return result;
- }
- }
- ------------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // Trio
- // Spec: Test your ability to build a class that implements an interface as well as build classes and methods around it.
- public class TrioRunner
- {
- public static void main(String[] args)
- {
- Sandwich cheeseBurger = new Sandwich("Cheese Burger", 3.50);
- Sandwich club = new Sandwich("Club Sandwich", 2.75);
- Salad spinach = new Salad("Spinach Salad", 1.25);
- Salad slaw = new Salad("Coleslaw", 1.25);
- Drink orange = new Drink("Orange Soda", 1.25);
- Drink cappuccino = new Drink("Cappuccino", 3.50);
- Trio trio1 = new Trio(cheeseBurger, slaw, orange);
- Trio trio2 = new Trio(club, spinach, cappuccino);
- System.out.println(trio1);
- System.out.println(trio2);
- }
- }
- /*
- Combo: Cheese Burger / Coleslaw / Orange Soda
- Price: $4.75
- Combo: Club Sandwich / Spinach Salad / Cappuccino
- Price: $6.25
- Press any key to continue . . .
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement