Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.60 KB | None | 0 0
  1. package mohibmir_CSCI201L_Assignment1;
  2. import com.google.gson.*;
  3.  
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.lang.reflect.Array;
  9. import java.math.RoundingMode;
  10. import java.text.DecimalFormat;
  11. import java.text.NumberFormat;
  12. import java.text.ParseException;
  13. import java.util.ArrayList;
  14. import java.util.LinkedList;
  15. import java.util.Locale;
  16. import java.util.Queue;
  17. import java.util.Scanner;
  18. import java.util.Stack;
  19.  
  20. public class Main {
  21.  
  22. public static void main(String[] args) {
  23. Scanner input = new Scanner(System.in);
  24. RestaurantList rList = null;
  25.  
  26. Gson gson = new GsonBuilder().setPrettyPrinting().create();
  27. String fileName = null;
  28. while(rList == null){
  29. System.out.print("Please enter the name of enter the name of the JSON file: ");
  30. fileName = input.nextLine();
  31. try {
  32. rList = gson.fromJson(new FileReader(fileName), RestaurantList.class);
  33. } catch (JsonSyntaxException e) {
  34. // e.printStackTrace();
  35. System.out.println("Errors found in JSON file.");
  36. } catch (JsonIOException e) {
  37. // e.printStackTrace();
  38. System.out.println("Errors found in JSON file.");
  39. } catch (FileNotFoundException e) {
  40. // e.printStackTrace();
  41. System.out.println("File not found. Please try again.");
  42. }
  43. }
  44.  
  45. System.out.print("\nWhat is your latitude? ");
  46. double latitude = getDouble("What is your latitude?");
  47.  
  48. System.out.print("\nWhat is your longitude? ");
  49. double longitude = getDouble("\nWhat is your longitude? ");
  50.  
  51. boolean exited = false;
  52. while(!exited){
  53. printMenu();
  54. int option = getInt("\nWhat would you like to do? ");
  55. if(option == 1){
  56. rList.print(latitude, longitude);
  57. }else if(option == 2){
  58. String name = searchRestaurant(rList);
  59. rList.search(name, latitude, longitude);
  60. }else if(option == 3){
  61. searchItem(rList);
  62. }else if(option == 4){
  63. addRestaurant(rList, latitude, longitude);
  64. }else if(option == 5){
  65. removeRestaurant(rList);
  66. }else if(option == 6){
  67. sortRestaurants(rList, latitude, longitude);
  68. }else if(option == 7){
  69. System.out.println("\nWould you like to save your edits? \n\t1) Yes \n\t2) No");
  70. int save = getInt("\nWould you like to save your edits? \n\t1) Yes \n\t2) No");
  71. if(save == 1){
  72. String json = gson.toJson(rList);
  73.  
  74. boolean saved = true;
  75. try {
  76. FileWriter writer = new FileWriter(fileName);
  77. writer.write(json);
  78. writer.close();
  79. }catch (IOException e){
  80. System.out.println("Error: File couldn't be saved");
  81. saved = false;
  82. }
  83.  
  84. if(saved) System.out.println("Your edits have been saved to " + fileName);
  85.  
  86. }
  87. exited = true;
  88. System.out.println("Thank you for using my program! :D");
  89.  
  90. }else{
  91.  
  92. System.out.println("\nThat's not a valid option.");
  93. }
  94. }
  95.  
  96. }
  97.  
  98. private static void sortRestaurants(RestaurantList rList, double latitude, double longitude) {
  99.  
  100. System.out.print("\n\t1) A to Z \n\t2) Z to A \n\t3) Closest to farthest \n\t4) Farthest to closest"
  101. + "\nHow would you like to sort by? ");
  102. int choice = getInt("\n\t1) A to Z \n\t2) Z to A \n\t3) Closest to farthest \n\t4) Farthest to closest"
  103. + "\nHow would you like to sort by? ");
  104. while(choice < 0 || choice > 4){
  105. System.out.println("\nInvalid choice, try again.");
  106. System.out.print("\n\t1) A to Z \n\t2) Z to A \n\t3) Closest to farthest \n\t4) Farthest to closest"
  107. + "\nHow would you like to sort by? ");
  108. choice = getInt("\n\t1) A to Z \n\t2) Z to A \n\t3) Closest to farthest \n\t4) Farthest to closest"
  109. + "\nHow would you like to sort by? ");
  110. }
  111.  
  112. rList.sort(choice, latitude, longitude);
  113. }
  114.  
  115. private static void removeRestaurant(RestaurantList rList) {
  116. if(rList.size() == 0){
  117. System.out.println("No restaurants to remove. ");
  118. return;
  119. }
  120.  
  121. rList.displayRestaurants();
  122.  
  123. System.out.print("\nWhich restaurant would you like to remove? ");
  124. int choice = getInt("\nWhich restaurant would you like to remove? ");
  125.  
  126. while(choice < 1 || choice > rList.size()){
  127. System.out.println("\nInvalid choice.");
  128. rList.displayRestaurants();
  129. System.out.print("\nWhich restaurant would you like to remove? ");
  130. choice = getInt("\nWhich restaurant would you like to remove? ");
  131. }
  132.  
  133. String name = rList.get(choice-1).getName();
  134. rList.remove(name);
  135.  
  136. System.out.println("\n" + name + " is now removed.");
  137.  
  138.  
  139. }
  140.  
  141. private static void printMenu() {
  142. System.out.print("\n\t1) Display all restaurants \n\t2) Search for a restaurant \n\t3) Search for a menu item");
  143. System.out.print("\n\t4) Add a new restaurant \n\t5) Remove a restaurant \n\t6) Sort restaurants \n\t7) Exit");
  144. System.out.print("\nWhat would you like to do? ");
  145. }
  146.  
  147. private static boolean searchItem(RestaurantList rList){
  148. Scanner input = new Scanner(System.in);
  149.  
  150. System.out.print("\nWhat menu item would you like to search for? ");
  151.  
  152. String item = input.nextLine();
  153. // if item not found must ask again.
  154. while(rList.searchMenu(item) == false){
  155. System.out.println("\nNo restaurant nearby serves " + item + ".\n");
  156. System.out.print("What menu item would you like to search for? ");
  157. item = input.nextLine();
  158. }
  159. return false;
  160. }
  161.  
  162. private static String searchRestaurant(RestaurantList rList){
  163.  
  164. if(rList.size() == 0){
  165. System.out.println("No restaurants to search for.");
  166. return null;
  167. }
  168.  
  169. Scanner input = new Scanner(System.in);
  170.  
  171. String name = null;
  172. while(!rList.exists(name)){
  173. System.out.print("\nWhat is the name of the restaurant you would like to search for? ");
  174. name = input.nextLine();
  175. }
  176.  
  177. return name;
  178.  
  179. }
  180.  
  181. private static Restaurant addRestaurant(RestaurantList rList, double lati, double longi) {
  182. Scanner input = new Scanner(System.in);
  183.  
  184. System.out.print("\nWhat is the name of the restaurant you would like to add? ");
  185. String name = input.nextLine();
  186.  
  187. System.out.print("\nWhat is the address for " + name + "? ");
  188. String address = input.nextLine();
  189.  
  190. System.out.print("\nWhat is the latitude for " + name + "? ");
  191. double latitude = getDouble("\nWhat is the latitude for " + name + "? ");
  192.  
  193. System.out.print("\nWhat is the longitude for " + name + "? ");
  194. double longitude = getDouble("\nWhat is the longitude for " + name + "? ");
  195.  
  196. System.out.print("\nWhat does " + name + " serve? ");
  197. ArrayList<String> menu = new ArrayList<String>();
  198. menu.add(input.nextLine());
  199.  
  200. boolean running = true;
  201. while(running){
  202.  
  203. System.out.println("\n\t1) Yes\n\t2) No\nDoes " + name + " serve anything else? ");
  204. int choice = getInt("\n\t1) Yes\n\t2) No\nDoes " + name + " serve anything else? ");
  205.  
  206. if(choice == 1){
  207. System.out.print("What does " + name + " serve? ");
  208. String item = input.nextLine();
  209. menu.add(item);
  210.  
  211. }else if(choice == 2){
  212. running = false;
  213.  
  214. }else{
  215. System.out.println("That's not a choice!");
  216. }
  217. }
  218.  
  219. Restaurant r = new Restaurant(name, address, latitude, longitude, menu);
  220. rList.add(r);
  221.  
  222. String items = "";
  223.  
  224.  
  225. for(int i = 0; i < menu.size(); i++){
  226. items += menu.get(i);
  227. if(i != menu.size()-1){
  228. items += ", ";
  229. }else{
  230. items += ".";
  231. }
  232. }
  233.  
  234. System.out.println("There is now an entry for " + name + ", located " + r.calculateDistance(lati, longi) + " miles away at " + address);
  235. System.out.println(name + " serves " + items);
  236.  
  237. return null;
  238. }
  239.  
  240. public static int getInt(String msg){
  241. Scanner input = new Scanner(System.in);
  242.  
  243. boolean valid = false;
  244. int i = 0;
  245. while(!valid){
  246. valid = true;
  247. try{
  248. String in = input.nextLine();
  249. i = Integer.parseInt(in.trim());
  250. }catch(NumberFormatException nf){
  251. System.out.println("\nNot an integer.");
  252. valid = false;
  253. if(msg != null) System.out.print(msg);
  254. }
  255. }
  256. return i;
  257. }
  258.  
  259. public static double getDouble(String msg){
  260. Scanner input = new Scanner(System.in);
  261.  
  262. boolean valid = false;
  263. double i = 0.0;
  264. while(!valid){
  265. valid = true;
  266. try{
  267. String in = input.nextLine();
  268. NumberFormat nf = NumberFormat.getInstance(Locale.US);
  269. i = nf.parse(in).doubleValue();
  270.  
  271. i = Double.parseDouble(in.trim());
  272. }catch(NumberFormatException nf){
  273. System.out.println("\nNot a Double.");
  274. valid = false;
  275. if(msg != null) System.out.println(msg);
  276. } catch (ParseException e) {
  277. System.out.println("\nNot a Double.");
  278. valid = false;
  279. if(msg != null) System.out.print(msg); }
  280. }
  281. return i;
  282. }
  283.  
  284. }
  285.  
  286. class RestaurantList {
  287. private ArrayList<Restaurant> data;
  288.  
  289. public RestaurantList(){
  290.  
  291. }
  292.  
  293. public RestaurantList(ArrayList<Restaurant> data){
  294. this.data = data;
  295. }
  296.  
  297. public void sort(int choice, double latitude, double longitude) {
  298. Restaurant[] temp = new Restaurant[size()];
  299. if(choice == 1){
  300. for(int i = 0; i < size(); i++){
  301. int index = i;
  302. for(int j = i-1; j >= 0; j--){
  303. if(get(index).getName().compareToIgnoreCase(get(j).getName()) < 0){
  304. swap(index, j);
  305. index = j;
  306. }
  307. }
  308.  
  309. }
  310. }else if(choice == 2){
  311. for(int i = 0; i < size(); i++){
  312. int index = i;
  313. for(int j = i-1; j >= 0; j--){
  314. if(get(index).getName().compareToIgnoreCase(get(j).getName()) > 0){
  315. swap(index, j);
  316. index = j;
  317. }
  318. }
  319.  
  320. }
  321. }else if(choice == 3){
  322. for(int i = 0; i < size(); i++){
  323. int index = i;
  324. for(int j = i-1; j >= 0; j--){
  325. if(get(index).calculateDistance(latitude, longitude) < get(j).calculateDistance(latitude, longitude)){
  326. swap(index, j);
  327. index = j;
  328. }
  329. }
  330.  
  331. }
  332. }else if(choice == 4){
  333. for(int i = 0; i < size(); i++){
  334. int index = i;
  335. for(int j = i-1; j >= 0; j--){
  336. if(get(index).calculateDistance(latitude, longitude) > get(j).calculateDistance(latitude, longitude)){
  337. swap(index, j);
  338. index = j;
  339. }
  340. }
  341. }
  342. }else{
  343. //INVALID
  344. System.out.println("Invalid option.");
  345. }
  346. }
  347.  
  348. public void add(Restaurant restaurant){
  349. data.add(restaurant);
  350. }
  351.  
  352. public Restaurant get(int index){
  353. return data.get(index);
  354. }
  355.  
  356. public void remove(String name){
  357. Restaurant temp = null;
  358. for(Restaurant r : data){
  359. if(r.getName().toLowerCase().equals(name.toLowerCase())){
  360. temp = r;
  361. }
  362. }
  363. data.remove(temp);
  364. }
  365.  
  366. public void search(String name, double latitude, double longitude){
  367. if(name == null) return;
  368.  
  369. for(Restaurant r : data){
  370.  
  371. if(r.getName().toLowerCase().contains(name.toLowerCase())){
  372. r.toString(latitude, longitude);
  373. }
  374. }
  375.  
  376. }
  377.  
  378. public boolean searchMenu(String item){
  379.  
  380. boolean found = false;
  381. for(Restaurant r : data){
  382. for(String food : r.getMenu()){
  383. if(food.contains(item.replaceAll(" ", ""))){
  384. found = true;
  385. System.out.println(r.getName() + " serves " + food);
  386. }
  387. }
  388. }
  389.  
  390. return found;
  391. }
  392.  
  393. public void print(double latitude, double longitude){
  394. System.out.println("");
  395. for(Restaurant r : data){
  396. r.toString(latitude, longitude);
  397. }
  398. }
  399.  
  400. public void displayRestaurants(){
  401. int i = 1;
  402. System.out.println("");
  403. for(Restaurant r : data){
  404. System.out.println(i + ") " + r.getName());
  405. i++;
  406. }
  407. }
  408.  
  409. public int size(){
  410. return data.size();
  411. }
  412.  
  413. public void clear(){
  414. for(int i = data.size()-1; i >= 0; i--){
  415. remove(get(i).getName());
  416. }
  417. }
  418.  
  419. private void swap(int i, int j){
  420. Restaurant temp = get(i);
  421. data.set(i, get(j));
  422. data.set(j, temp);
  423. }
  424.  
  425. public boolean exists(String name){
  426. if(name == null) return false;
  427.  
  428. for(Restaurant r : data){
  429. if(r.getName().toLowerCase().contains(name.toLowerCase())){
  430. return true;
  431. }
  432. }
  433. return false;
  434. }
  435.  
  436. }
  437.  
  438. class Restaurant {
  439. private String name;
  440. private String address;
  441. private double latitude;
  442. private double longitude;
  443. private ArrayList<String> menu;
  444.  
  445. public Restaurant(String name, String address, double latitude, double longitude, ArrayList<String> menu){
  446. this.name = name;
  447. this.address = address;
  448. this.latitude = latitude;
  449. this.longitude = longitude;
  450. this.menu = menu;
  451. }
  452.  
  453. public String getName(){
  454. return name;
  455. }
  456.  
  457. public String getAddress(){
  458. return address;
  459. }
  460.  
  461. public double getLatitude(){
  462. return latitude;
  463. }
  464.  
  465. public double getLongitude(){
  466. return longitude;
  467. }
  468.  
  469. public ArrayList<String> getMenu(){
  470. return menu;
  471. }
  472.  
  473. public void toString(double latitude, double longitude){
  474. System.out.println(name + ", located " + calculateDistance(latitude, longitude) + " miles away at " + address);
  475. }
  476.  
  477. public double calculateDistance(double lat1, double long1){
  478. lat1 = Math.toRadians(lat1); // 34.021160
  479. double lat2 = Math.toRadians(this.getLatitude());
  480. long1 = Math.toRadians(long1); // -118.287132
  481. double long2 = Math.toRadians(this.getLongitude());
  482.  
  483. DecimalFormat df = new DecimalFormat("##.#");
  484. df.setRoundingMode(RoundingMode.CEILING);
  485.  
  486. double d = 3963.0 * Math.acos((Math.sin(lat1) * Math.sin(lat2)) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(long2 - long1));
  487.  
  488. return Double.parseDouble(df.format(d));
  489.  
  490. }
  491.  
  492.  
  493. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement