/**
*
*/
package solutions.finalexam;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import javax.swing.text.NumberFormatter;
/**
* @author lord_pretzel
*
*/
public class Menu {
private List<MenuItem> items;
public enum Category {
Appetizer,
Dessert,
MainCourse,
Beverage
}
public static class MenuItem {
private static NumberFormat formatter = NumberFormat.getCurrencyInstance();
private String name;
private double price;
private Category cat;
/**
* @param name
* @param price
* @param cat
* @throws Exception
*/
public MenuItem(String name, double price, Category cat) throws Exception {
super();
this.name = name;
setPrice(price);
this.cat = cat;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) throws Exception {
if (price <= 0.0)
throw new Exception("price of items has to be positive");
this.price = price;
}
public Category getCat() {
return cat;
}
public void setCat(Category cat) {
this.cat = cat;
}
public String toString() {
return cat.toString() + ": " + name + "\t" + formatter.format(price);
}
}
public Menu() {
items = new ArrayList<> ();
}
public void addItem (MenuItem i) {
items.add(i);
}
public List<MenuItem> getItems () {
return items;
}
public String toString() {
StringBuilder b = new StringBuilder();
for(MenuItem i: items) {
b.append(i.toString() + "\n");
}
return b.toString();
}
}
/**
*
*/
package solutions.finalexam;
/**
* @author lord_pretzel
*
*/
public class Restaurant {
private String name;
private String cuisine;
private String owner;
private String address;
private Menu menu;
public Restaurant (String name, String cuisine, String owner, String address) {
this.name = name;
this.cuisine = cuisine;
this.owner = owner;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCuisine() {
return cuisine;
}
public void setCuisine(String cuisine) {
this.cuisine = cuisine;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Menu getMenu() {
return menu;
}
public void setMenu(Menu menu) {
this.menu = menu;
}
public boolean equals (Object o) {
Restaurant r;
if (o == null || !(o instanceof Restaurant))
return false;
r = (Restaurant) o;
return this.name.equals(r.name) && this.address.equals(r.address);
}
public int hashCode () {
return name.hashCode() ^ address.hashCode();
}
public String toString() {
return name + " (" + cuisine + ") owned by " + owner + " located at " + address + " serves: \n\n" + menu.toString();
}
}
package solutions.finalexam;
import solutions.finalexam.Restaurant;
/**
* @author lord_pretzel
*
*/
public class Cafeteria extends Restaurant {
private String sponsor;
/**
* @param name
* @param cuisine
* @param owner
* @param address
*/
public Cafeteria(String name, String cuisine, String owner,
String address) {
super(name, cuisine, owner, address);
}
public Cafeteria(String name, String cuisine, String owner,
String address, String sponsor) {
super(name, cuisine, owner, address);
this.sponsor = sponsor;
}
public String getSponsor() {
return sponsor;
}
public void setSponsor(String sponsor) {
this.sponsor = sponsor;
}
public String toString() {
return super.toString() + " sponsored by " + sponsor;
}
}
/**
*
*/
package solutions.finalexam;
import solutions.finalexam.Restaurant;
/**
* @author lord_pretzel
*
*/
public class FastFoodRestaunt extends Restaurant {
private String franchise;
/**
* @param name
* @param cuisine
* @param owner
* @param address
*/
public FastFoodRestaunt(String name, String cuisine, String owner,
String address) {
super(name, cuisine, owner, address);
}
public FastFoodRestaunt(String name, String cuisine, String owner,
String address, String franchise) {
super(name, cuisine, owner, address);
this.setFranchise(franchise);
}
public String getFranchise() {
return franchise;
}
public void setFranchise(String franchise) {
this.franchise = franchise;
}
public String toString() {
return super.toString() + " from franchise " + franchise;
}
}
/**
*
*/
package solutions.finalexam;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import solutions.finalexam.Menu.MenuItem;
/**
* @author lord_pretzel
*
*/
public class RestaurantGuide {
private Set<Restaurant> restaurants;
private HashMap<Restaurant, Float> rating;
public RestaurantGuide() {
restaurants = new HashSet<> ();
rating = new HashMap<>();
}
public void addRestaurant(Restaurant r) {
restaurants.add(r);
}
public List<Restaurant> getAllRestaurants() {
return new ArrayList<> (restaurants);
}
public List<Restaurant> search(String item) {
List<Restaurant> res = new ArrayList<> ();
for(Restaurant r: restaurants) {
Menu m = r.getMenu();
for(MenuItem i: m.getItems()) {
if (i.getName().equals(item))
res.add(r);
}
}
return res;
}
public void addRating(Restaurant r, float rat) {
rating.put(r, rat);
}
public float getRating(Restaurant r) {
return rating.get(r);
}
public Restaurant getHighestRatedOne () {
Restaurant best;
if (restaurants.size() == 0)
return null;
best = restaurants.iterator().next();
for(Restaurant r: restaurants) {
if (rating.get(r) > rating.get(best))
best = r;
}
return best;
}
}
/**
*
*/
package solutions.finalexam;
import solutions.finalexam.Menu.Category;
import solutions.finalexam.Menu.MenuItem;
/**
* @author lord_pretzel
*
*/
public class RestaurantTest {
public static void main(String[] args) throws Exception {
RestaurantGuide g = new RestaurantGuide();
Restaurant r1, r2;
Menu m;
r1 = new FastFoodRestaunt("Mac", "junkfood", "Peter", "10 W 33 st, Chicago, IL", "MacDonalds");
m = new Menu();
r1.setMenu(m);
m.addItem(new MenuItem("Fries", 2.50, Category.MainCourse));
m.addItem(new MenuItem("Burger", 3.99, Category.MainCourse));
r2 = new Cafeteria("MTCC commons", "junkfood", "Alice", "3555 S State, Chicago, IL", "IIT");
m = new Menu();
r2.setMenu(m);
m.addItem(new MenuItem("Fries", 3.50, Category.MainCourse));
m.addItem(new MenuItem("Burrito", 5.99, Category.MainCourse));
g.addRestaurant(r1);
g.addRestaurant(r2);
System.out.println("Restaurants serving burritos:\n\n" + g.search("Burrito").toString());
g.addRating(r1, 3);
g.addRating(r2, 4);
System.out.println("\n\nHighest rated one is:\n\n" + g.getHighestRatedOne());
}
}
import solutions.finalexam.RestaurantTest;
RestaurantTest.main(null);
return "";