Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mohibmir_CSCI201L_Assignment1;
- import com.google.gson.*;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.lang.reflect.Array;
- import java.math.RoundingMode;
- import java.text.DecimalFormat;
- import java.text.NumberFormat;
- import java.text.ParseException;
- import java.util.ArrayList;
- import java.util.LinkedList;
- import java.util.Locale;
- import java.util.Queue;
- import java.util.Scanner;
- import java.util.Stack;
- public class Main {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- RestaurantList rList = null;
- Gson gson = new GsonBuilder().setPrettyPrinting().create();
- String fileName = null;
- while(rList == null){
- System.out.print("Please enter the name of enter the name of the JSON file: ");
- fileName = input.nextLine();
- try {
- rList = gson.fromJson(new FileReader(fileName), RestaurantList.class);
- } catch (JsonSyntaxException e) {
- // e.printStackTrace();
- System.out.println("Errors found in JSON file.");
- } catch (JsonIOException e) {
- // e.printStackTrace();
- System.out.println("Errors found in JSON file.");
- } catch (FileNotFoundException e) {
- // e.printStackTrace();
- System.out.println("File not found. Please try again.");
- }
- }
- System.out.print("\nWhat is your latitude? ");
- double latitude = getDouble("What is your latitude?");
- System.out.print("\nWhat is your longitude? ");
- double longitude = getDouble("\nWhat is your longitude? ");
- boolean exited = false;
- while(!exited){
- printMenu();
- int option = getInt("\nWhat would you like to do? ");
- if(option == 1){
- rList.print(latitude, longitude);
- }else if(option == 2){
- String name = searchRestaurant(rList);
- rList.search(name, latitude, longitude);
- }else if(option == 3){
- searchItem(rList);
- }else if(option == 4){
- addRestaurant(rList, latitude, longitude);
- }else if(option == 5){
- removeRestaurant(rList);
- }else if(option == 6){
- sortRestaurants(rList, latitude, longitude);
- }else if(option == 7){
- System.out.println("\nWould you like to save your edits? \n\t1) Yes \n\t2) No");
- int save = getInt("\nWould you like to save your edits? \n\t1) Yes \n\t2) No");
- if(save == 1){
- String json = gson.toJson(rList);
- boolean saved = true;
- try {
- FileWriter writer = new FileWriter(fileName);
- writer.write(json);
- writer.close();
- }catch (IOException e){
- System.out.println("Error: File couldn't be saved");
- saved = false;
- }
- if(saved) System.out.println("Your edits have been saved to " + fileName);
- }
- exited = true;
- System.out.println("Thank you for using my program! :D");
- }else{
- System.out.println("\nThat's not a valid option.");
- }
- }
- }
- private static void sortRestaurants(RestaurantList rList, double latitude, double longitude) {
- System.out.print("\n\t1) A to Z \n\t2) Z to A \n\t3) Closest to farthest \n\t4) Farthest to closest"
- + "\nHow would you like to sort by? ");
- int choice = getInt("\n\t1) A to Z \n\t2) Z to A \n\t3) Closest to farthest \n\t4) Farthest to closest"
- + "\nHow would you like to sort by? ");
- while(choice < 0 || choice > 4){
- System.out.println("\nInvalid choice, try again.");
- System.out.print("\n\t1) A to Z \n\t2) Z to A \n\t3) Closest to farthest \n\t4) Farthest to closest"
- + "\nHow would you like to sort by? ");
- choice = getInt("\n\t1) A to Z \n\t2) Z to A \n\t3) Closest to farthest \n\t4) Farthest to closest"
- + "\nHow would you like to sort by? ");
- }
- rList.sort(choice, latitude, longitude);
- }
- private static void removeRestaurant(RestaurantList rList) {
- if(rList.size() == 0){
- System.out.println("No restaurants to remove. ");
- return;
- }
- rList.displayRestaurants();
- System.out.print("\nWhich restaurant would you like to remove? ");
- int choice = getInt("\nWhich restaurant would you like to remove? ");
- while(choice < 1 || choice > rList.size()){
- System.out.println("\nInvalid choice.");
- rList.displayRestaurants();
- System.out.print("\nWhich restaurant would you like to remove? ");
- choice = getInt("\nWhich restaurant would you like to remove? ");
- }
- String name = rList.get(choice-1).getName();
- rList.remove(name);
- System.out.println("\n" + name + " is now removed.");
- }
- private static void printMenu() {
- System.out.print("\n\t1) Display all restaurants \n\t2) Search for a restaurant \n\t3) Search for a menu item");
- System.out.print("\n\t4) Add a new restaurant \n\t5) Remove a restaurant \n\t6) Sort restaurants \n\t7) Exit");
- System.out.print("\nWhat would you like to do? ");
- }
- private static boolean searchItem(RestaurantList rList){
- Scanner input = new Scanner(System.in);
- System.out.print("\nWhat menu item would you like to search for? ");
- String item = input.nextLine();
- // if item not found must ask again.
- while(rList.searchMenu(item) == false){
- System.out.println("\nNo restaurant nearby serves " + item + ".\n");
- System.out.print("What menu item would you like to search for? ");
- item = input.nextLine();
- }
- return false;
- }
- private static String searchRestaurant(RestaurantList rList){
- if(rList.size() == 0){
- System.out.println("No restaurants to search for.");
- return null;
- }
- Scanner input = new Scanner(System.in);
- String name = null;
- while(!rList.exists(name)){
- System.out.print("\nWhat is the name of the restaurant you would like to search for? ");
- name = input.nextLine();
- }
- return name;
- }
- private static Restaurant addRestaurant(RestaurantList rList, double lati, double longi) {
- Scanner input = new Scanner(System.in);
- System.out.print("\nWhat is the name of the restaurant you would like to add? ");
- String name = input.nextLine();
- System.out.print("\nWhat is the address for " + name + "? ");
- String address = input.nextLine();
- System.out.print("\nWhat is the latitude for " + name + "? ");
- double latitude = getDouble("\nWhat is the latitude for " + name + "? ");
- System.out.print("\nWhat is the longitude for " + name + "? ");
- double longitude = getDouble("\nWhat is the longitude for " + name + "? ");
- System.out.print("\nWhat does " + name + " serve? ");
- ArrayList<String> menu = new ArrayList<String>();
- menu.add(input.nextLine());
- boolean running = true;
- while(running){
- System.out.println("\n\t1) Yes\n\t2) No\nDoes " + name + " serve anything else? ");
- int choice = getInt("\n\t1) Yes\n\t2) No\nDoes " + name + " serve anything else? ");
- if(choice == 1){
- System.out.print("What does " + name + " serve? ");
- String item = input.nextLine();
- menu.add(item);
- }else if(choice == 2){
- running = false;
- }else{
- System.out.println("That's not a choice!");
- }
- }
- Restaurant r = new Restaurant(name, address, latitude, longitude, menu);
- rList.add(r);
- String items = "";
- for(int i = 0; i < menu.size(); i++){
- items += menu.get(i);
- if(i != menu.size()-1){
- items += ", ";
- }else{
- items += ".";
- }
- }
- System.out.println("There is now an entry for " + name + ", located " + r.calculateDistance(lati, longi) + " miles away at " + address);
- System.out.println(name + " serves " + items);
- return null;
- }
- public static int getInt(String msg){
- Scanner input = new Scanner(System.in);
- boolean valid = false;
- int i = 0;
- while(!valid){
- valid = true;
- try{
- String in = input.nextLine();
- i = Integer.parseInt(in.trim());
- }catch(NumberFormatException nf){
- System.out.println("\nNot an integer.");
- valid = false;
- if(msg != null) System.out.print(msg);
- }
- }
- return i;
- }
- public static double getDouble(String msg){
- Scanner input = new Scanner(System.in);
- boolean valid = false;
- double i = 0.0;
- while(!valid){
- valid = true;
- try{
- String in = input.nextLine();
- NumberFormat nf = NumberFormat.getInstance(Locale.US);
- i = nf.parse(in).doubleValue();
- i = Double.parseDouble(in.trim());
- }catch(NumberFormatException nf){
- System.out.println("\nNot a Double.");
- valid = false;
- if(msg != null) System.out.println(msg);
- } catch (ParseException e) {
- System.out.println("\nNot a Double.");
- valid = false;
- if(msg != null) System.out.print(msg); }
- }
- return i;
- }
- }
- class RestaurantList {
- private ArrayList<Restaurant> data;
- public RestaurantList(){
- }
- public RestaurantList(ArrayList<Restaurant> data){
- this.data = data;
- }
- public void sort(int choice, double latitude, double longitude) {
- Restaurant[] temp = new Restaurant[size()];
- if(choice == 1){
- for(int i = 0; i < size(); i++){
- int index = i;
- for(int j = i-1; j >= 0; j--){
- if(get(index).getName().compareToIgnoreCase(get(j).getName()) < 0){
- swap(index, j);
- index = j;
- }
- }
- }
- }else if(choice == 2){
- for(int i = 0; i < size(); i++){
- int index = i;
- for(int j = i-1; j >= 0; j--){
- if(get(index).getName().compareToIgnoreCase(get(j).getName()) > 0){
- swap(index, j);
- index = j;
- }
- }
- }
- }else if(choice == 3){
- for(int i = 0; i < size(); i++){
- int index = i;
- for(int j = i-1; j >= 0; j--){
- if(get(index).calculateDistance(latitude, longitude) < get(j).calculateDistance(latitude, longitude)){
- swap(index, j);
- index = j;
- }
- }
- }
- }else if(choice == 4){
- for(int i = 0; i < size(); i++){
- int index = i;
- for(int j = i-1; j >= 0; j--){
- if(get(index).calculateDistance(latitude, longitude) > get(j).calculateDistance(latitude, longitude)){
- swap(index, j);
- index = j;
- }
- }
- }
- }else{
- //INVALID
- System.out.println("Invalid option.");
- }
- }
- public void add(Restaurant restaurant){
- data.add(restaurant);
- }
- public Restaurant get(int index){
- return data.get(index);
- }
- public void remove(String name){
- Restaurant temp = null;
- for(Restaurant r : data){
- if(r.getName().toLowerCase().equals(name.toLowerCase())){
- temp = r;
- }
- }
- data.remove(temp);
- }
- public void search(String name, double latitude, double longitude){
- if(name == null) return;
- for(Restaurant r : data){
- if(r.getName().toLowerCase().contains(name.toLowerCase())){
- r.toString(latitude, longitude);
- }
- }
- }
- public boolean searchMenu(String item){
- boolean found = false;
- for(Restaurant r : data){
- for(String food : r.getMenu()){
- if(food.contains(item.replaceAll(" ", ""))){
- found = true;
- System.out.println(r.getName() + " serves " + food);
- }
- }
- }
- return found;
- }
- public void print(double latitude, double longitude){
- System.out.println("");
- for(Restaurant r : data){
- r.toString(latitude, longitude);
- }
- }
- public void displayRestaurants(){
- int i = 1;
- System.out.println("");
- for(Restaurant r : data){
- System.out.println(i + ") " + r.getName());
- i++;
- }
- }
- public int size(){
- return data.size();
- }
- public void clear(){
- for(int i = data.size()-1; i >= 0; i--){
- remove(get(i).getName());
- }
- }
- private void swap(int i, int j){
- Restaurant temp = get(i);
- data.set(i, get(j));
- data.set(j, temp);
- }
- public boolean exists(String name){
- if(name == null) return false;
- for(Restaurant r : data){
- if(r.getName().toLowerCase().contains(name.toLowerCase())){
- return true;
- }
- }
- return false;
- }
- }
- class Restaurant {
- private String name;
- private String address;
- private double latitude;
- private double longitude;
- private ArrayList<String> menu;
- public Restaurant(String name, String address, double latitude, double longitude, ArrayList<String> menu){
- this.name = name;
- this.address = address;
- this.latitude = latitude;
- this.longitude = longitude;
- this.menu = menu;
- }
- public String getName(){
- return name;
- }
- public String getAddress(){
- return address;
- }
- public double getLatitude(){
- return latitude;
- }
- public double getLongitude(){
- return longitude;
- }
- public ArrayList<String> getMenu(){
- return menu;
- }
- public void toString(double latitude, double longitude){
- System.out.println(name + ", located " + calculateDistance(latitude, longitude) + " miles away at " + address);
- }
- public double calculateDistance(double lat1, double long1){
- lat1 = Math.toRadians(lat1); // 34.021160
- double lat2 = Math.toRadians(this.getLatitude());
- long1 = Math.toRadians(long1); // -118.287132
- double long2 = Math.toRadians(this.getLongitude());
- DecimalFormat df = new DecimalFormat("##.#");
- df.setRoundingMode(RoundingMode.CEILING);
- double d = 3963.0 * Math.acos((Math.sin(lat1) * Math.sin(lat2)) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(long2 - long1));
- return Double.parseDouble(df.format(d));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement