Question: I need a code that is different, but can do the same exact function as the code below. Instead of using movies we are using
I need a code that is different, but can do the same exact function as the code below. Instead of using movies we are using NBA Teams and the file we are using is "/users/KillaQ/downloads/listoffavorites.txt"
import java.io.*; import java.util.*; public class Project { public static Scanner scan = new Scanner(System.in); Scanner sc = new Scanner(System.in).useDelimiter("\ "); static LinkedList
@SuppressWarnings("resource") public static void main(String[] args) throws Exception {
BufferedReader reader = null; // create variables
ArrayList
System.out.println("Enter a file of your favorite list of movies (Use the path directory)");
String path = scan.next();
File file = new File(path); reader = new BufferedReader(new FileReader(file)); String text = null;
while ((text = reader.readLine()) != null) { myFileReader.add(text); } // adds movies to list for (int i = 0; i < myFileReader.size(); i++) { Movie movie = new Movie(myFileReader.get(i), 0, ""); list.add(movie); }
System.out.println("Movies in list are: ");
// prints list for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i).name);
} System.out.println();
System.out.println("If you would like to add/delete a movie, enter yes or no?"); String answer = ""; boolean error = false; answer = scan.next(); answer = answer.toLowerCase(); do { try { if (answer.equals("yes")) { error = false; additional(); } else if (answer.equals("no")) { error = false;
break; } else { error = true; } } catch (Exception e) { e.printStackTrace(); error = true; } } while (error == true);
boolean rankError = false; String comment = ""; int[] rank = new int[list.size()]; for (int i = 0; i < list.size(); i++) { rank[i] = 0; }
for (int i = 0; i < list.size(); i++) { Scanner sc = new Scanner(System.in); System.out.println("Enter rank for " + list.get(i).name); do{ try{ int r = sc.nextInt();
if (r > 0 && r <= list.size()) { addRank(list.get(i).name, r, rank); rankError = false;
} else { System.out.println("Number to rank is out of bounds! Try again!"); rankError = true; } }catch(Exception e){ rankError = true; e.printStackTrace(); } }while(rankError == true); System.out.println("Comments for this movie?"); String s = sc.next(); s += sc.nextLine();
list.get(i).comment = s;
} Collections.sort(list); int currentRanking = 1; for(int i = 0; i < list.size(); i++){ list.get(i).rank = currentRanking; currentRanking++; } System.out.println("Movie " + "Rank " + "Comments"); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i).name + " " + list.get(i).rank + " " + list.get(i).comment); } try { File outfile = new File("C://Users/Jason/Documents/MovieList.txt"); if (!outfile.exists()) { outfile.createNewFile(); } FileWriter fw = new FileWriter(outfile.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); for (int j = 0; j < list.size(); j++) { bw.write(list.get(j).name + " " + list.get(j).rank + " " + list.get(j).comment); bw.newLine(); } bw.close(); } catch (IOException e) { e.printStackTrace(); } boolean gameError = true; while(gameError == true){ System.out.println("Do you want to play again? Enter 'Yes' or 'No'");
String input = scan.next(); if (playagain(input) == 1) { System.out.println("Enter a file of your favorite list of movies (Use the path directory)");
String name = scan.next();
File doc = new File(name); reader = new BufferedReader(new FileReader(doc));
// adds movies to list /* for (int i = 0; i < myFileReader.size(); i++) { Movie movie = new Movie(myFileReader.get(i), 0, ""); list.add(movie); } **/ System.out.println("Movies in list are: ");
// prints list for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i).name + " " + list.get(i).rank + " " + list.get(i).comment); } System.out.println("If you would like to add/delete a movie, enter yes or no?"); Scanner keyboard = new Scanner(System.in); String title = ""; boolean fail = false; //title = scan.next(); title = keyboard.nextLine(); title = title.toLowerCase(); do { try { if (title.equals("yes")) { fail = false; additional(); } else if (title.equals("no")) { fail = false;
break; } else { fail = true; } } catch (Exception e) { e.printStackTrace(); fail = true; } } while (error == true);
boolean wrong = false; String say = ""; int[] holder = new int[list.size()]; for (int i = 0; i < list.size(); i++) { holder[i] = 0; }
for (int i = 0; i < list.size(); i++) { Scanner sc = new Scanner(System.in); System.out.println("Enter rank for " + list.get(i).name + " " + list.get(i).comment); do{ try{ int a = sc.nextInt();
if (a > 0 && a <= list.size()) { addRank(list.get(i).name, a, holder); wrong = false;
} else { System.out.println("Number to rank is out of bounds! Try again!"); wrong = true; } }catch(Exception e){ wrong = true; e.printStackTrace(); } }while(wrong==true); System.out.println("Comments for this movie?"); String y = sc.next(); y += sc.nextLine();
list.get(i).comment += (" " + y + " "); } } else if (playagain(input) == 0) { gameError = false; System.out.print("Goodbye"); break; } Collections.sort(list); int cRank = 1; for(int i = 0; i
} }
return; // if its not 0, check if all following spots are 1, if so go to // next loop // if not all spots are 1,put rank as spot and increment all // following spots } else { for (int i = r; i < list.size(); i++) { if (rank[i] == 1) { continue; } if (rank[i] == 0) { rank[i] = 1; for (int j = 0; j < list.size(); j++) { if (list.get(j).rank >= r) { list.get(j).rank++; }
if (list.get(j).name.equals(movie)) { list.get(j).rank = r; return; }
} } } // if all following spots are 1, put rank as next open spot above for (int f = list.size() - 1; f >= 0; f--) { if (rank[f] == 0) { rank[f] = 1; for (int m = 0; m < list.size(); m++) { if (list.get(m).name.equals(movie)) { list.get(m).rank = f + 1; System.out.println("Space is unavailable. Movie moved to rank " + f); return; }
} } } }
}
public static int playagain(String inp) { if (inp.equals("Yes")) { return 1; } else { return 0; } }
public static void additional() { String reply = ""; boolean addError = false; System.out.println("Add or delete?"); reply = scan.next(); do { try { if (reply.equals("add")) { // do something addError = false; System.out.println("What is the name of the movie you want to add?"); scan.nextLine(); String name = scan.nextLine(); Movie movie = new Movie(name, 0, ""); list.add(movie);
} else if (reply.equals("delete")) { // delete something addError = false; System.out.println("What movie do you want to remove?"); String name = scan.next(); int count = list.size(); for (int i = 0; i < count - 1; i++) { if (list.get(i).name.equals(name)) { list.remove(i); } }
} else { System.out.println("Enter add or delete!"); addError = true; } } catch (Exception e) { e.printStackTrace(); addError = true; } } while (addError == true); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
