Skip to content

Instantly share code, notes, and snippets.

@Eckankar
Created June 2, 2011 17:31
Show Gist options
  • Save Eckankar/1004864 to your computer and use it in GitHub Desktop.
Save Eckankar/1004864 to your computer and use it in GitHub Desktop.
Introductory Programming, Spring 2010, Problem 5
package jøptest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
List<Restaurant> restaurants = new ArrayList<Restaurant>(10);
for (int i = 0; i < 10; i++) {
Restaurant r = new Restaurant(getName(i), getCity(i),
getOwner(i), getCuisine(i));
System.out.println("We now have " + Restaurant.getNumRestaurants() +
" restaurants.");
System.out.println(r.displayRestaurant());
restaurants.add(r);
}
Scanner sIn = new Scanner(System.in);
while (true) {
System.out.println("\n");
System.out.println("What type of cuisine are you looking for?");
System.out.print("Type of cuisine (write 'quit' to quit): ");
String query = sIn.nextLine();
if (query.equalsIgnoreCase("quit")) {
break;
}
System.out.println("");
Collection<Restaurant> matches =
Restaurant.findCuisine(restaurants, query);
if (matches.isEmpty()) {
System.out.println("Sorry, no matches were found.");
} else {
System.out.println("Found " + matches.size() +
" matching restaurants:");
for (Restaurant r : matches) {
System.out.println("\n" + r.displayRestaurant());
}
}
}
}
// <editor-fold defaultstate="collapsed" desc="fjollede ting">
private static Random random = new Random();
private static String[] names = {
"Alfonso", "Benedetto", "Cecco", "Damiano",
"Eduardo", "Felicia", "Gilberto",
"Hans-Kristian", "Ian", "Jakob"
};
private static String[] cuisine = {
"Italian", "Italian", "Italian", "Italian",
"Spanish", "Spanish", "Spanish",
"Danish", "Danish", "Danish"
};
private static String[] suffix = {
"Diner", "Take-Away", "Pølsevogn", "BBQ-Hut",
"Delights", "Restaurant"
};
private static String getName(int i) {
return names[i] + "'s " + suffix[random.nextInt(suffix.length)];
}
private static String getOwner(int i) {
return names[i];
}
private static String getCuisine(int i) {
return cuisine[i];
}
private static String getCity(int i) {
String city = "Karlslunde";
for (int j = 0; j < i; j++) {
city = "New " + city;
}
return city;
}
// </editor-fold>
}
package jøptest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class Restaurant {
// Name of the restaurant
private String name;
// Location of the restaurant
private String location;
// Name of restaurant owner
private String owner;
// Type of cuisine served
private String cuisine;
// How many restaurants exist?
private static int numRestaurants = 0;
public Restaurant(String name, String location,
String owner, String cuisine) {
this.name = name;
this.location = location;
this.owner = owner;
this.cuisine = cuisine;
numRestaurants++;
}
// <editor-fold defaultstate="collapsed" desc="getters/setters">
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the location
*/
public String getLocation() {
return location;
}
/**
* @param location the location to set
*/
public void setLocation(String location) {
this.location = location;
}
/**
* @return the owner
*/
public String getOwner() {
return owner;
}
/**
* @param owner the owner to set
*/
public void setOwner(String owner) {
this.owner = owner;
}
/**
* @return the cuisine
*/
public String getCuisine() {
return cuisine;
}
/**
* @param cuisine the cuisine to set
*/
public void setCuisine(String cuisine) {
this.cuisine = cuisine;
}
/**
* @return the number of restaurants
*/
public static int getNumRestaurants() {
return numRestaurants;
}
// </editor-fold>
/**
* Generates a pretty-printed version of the restaurant's info.
* @return
*/
public String displayRestaurant() {
return "Name: " + name + "\n" +
"Location: " + location + "\n" +
"Owner: " + owner + "\n" +
"Cuisine: " + cuisine;
}
public static Collection<Restaurant> findCuisine(
Collection<Restaurant> restaurants,
String cuisine) {
List<Restaurant> result = new ArrayList<Restaurant>();
for (Restaurant r : restaurants) {
if (r.cuisine.equalsIgnoreCase(cuisine)) {
result.add(r);
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment