I need a code that is different, but can do the same exact function as the code
ID: 3828386 • Letter: I
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 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<Movie> list = new LinkedList<Movie>();
@SuppressWarnings("resource")
public static void main(String[] args) throws Exception {
BufferedReader reader = null; // create variables
ArrayList<String> myFileReader = new ArrayList<String>();
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 <list.size(); i++){
list.get(i).rank = cRank;
cRank++;
}
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 output = new File("C://Users/Jason/Documents/MovieList.txt");
if(!output.exists()){
output.createNewFile();
}
FileWriter f = new FileWriter(output.getAbsoluteFile());
BufferedWriter b = new BufferedWriter(f);
for(int j =0; j < list.size(); j++){
b.write(list.get(j).name + " " + list.get(j).rank + " " + list.get(j).comment);
b.newLine();
}
b.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void addRank(String movie, int r, int[] rank) {
ListIterator<Movie> ptr = list.listIterator();
// if its 0, puts rank CORRECT
if (rank[r - 1] == 0) {
rank[r - 1] = 1;
for (int i = 0; i < list.size(); i++) {
if (list.get(i).name.equals(movie)) {
list.get(i).rank = r;
}
}
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);
}
}
Explanation / Answer
Please see below changes code.
there were some logical mistakes in original code that is also resolved in this code.
Project Class:
package demoPractice;
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<Team> list = new LinkedList<Team>();
@SuppressWarnings("resource")
public static void main(String[] args) throws Exception {
BufferedReader reader = null; // create variables
ArrayList<String> myFileReader = new ArrayList<String>();
System.out.println("Enter a file of your favorite list of NBA teams (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 teams to list
for (int i = 0; i < myFileReader.size(); i++) {
Team team = new Team(myFileReader.get(i), 0, "");
list.add(team);
}
System.out.println("Teams in list are: ");
// prints list
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getName());
}
System.out.println();
System.out.println("If you would like to add/delete a Team, 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).getName());
do{
try{
int r = sc.nextInt();
if (r > 0 && r <= list.size()) {
addRank(list.get(i).getName(), 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 Team?");
String s = sc.next();
s += sc.nextLine();
list.get(i).setComment(s);
}
Collections.sort(list);
int currentRanking = 1;
for(int i = 0; i < list.size(); i++){
list.get(i).setRank(currentRanking);
currentRanking++;
}
System.out.println("team " + "Rank " + "Comments");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getName() + " " + list.get(i).getRank() + " " + list.get(i).getComment());
}
try {
File outfile = new File("C://users/KillaQ/downloads/listoffavorites.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).getName() + " " + list.get(j).getRank() + " " + list.get(j).getComment());
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 teams (Use the path directory)");
String name = scan.next();
File doc = new File(name);
reader = new BufferedReader(new FileReader(doc));
// adds teams to list
/* for (int i = 0; i < myFileReader.size(); i++) {
Team team = new Team(myFileReader.get(i), 0, "");
list.add(team);
}
**/
System.out.println("teams in list are: ");
// prints list
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getName() + " " + list.get(i).getRank() + " " + list.get(i).getComment());
}
System.out.println("If you would like to add/delete a team, 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).getName() + " " + list.get(i).getComment());
do{
try{
int a = sc.nextInt();
if (a > 0 && a <= list.size()) {
addRank(list.get(i).getName(), 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 team?");
String y = sc.next();
y += sc.nextLine();
String temp=list.get(i).getComment();
temp+= (" " + y + " ");
list.get(i).setComment(temp) ;
}
} else if (playagain(input) == 0) {
gameError = false;
System.out.print("Goodbye");
break;
}
Collections.sort(list);
int cRank = 1;
for(int i = 0; i <list.size(); i++){
list.get(i).setRank(cRank);
cRank++;
}
System.out.println("team " + "Rank " + "Comments");
for(int i = 0; i < list.size(); i++){
System.out.println(list.get(i).getName() + " " + list.get(i).getRank() + " " + list.get(i).getComment());
}
try{
File output = new File("C://users/KillaQ/downloads/listoffavorites.txt");
if(!output.exists()){
output.createNewFile();
}
FileWriter f = new FileWriter(output.getAbsoluteFile());
BufferedWriter b = new BufferedWriter(f);
for(int j =0; j < list.size(); j++){
b.write(list.get(j).getName() + " " + list.get(j).getRank() + " " + list.get(j).getComment());
b.newLine();
}
b.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void addRank(String team, int r, int[] rank) {
ListIterator<Team> ptr = list.listIterator();
// if its 0, puts rank CORRECT
if (rank[r - 1] == 0) {
rank[r - 1] = 1;
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getName().equals(team)) {
list.get(i).setRank(r);
}
}
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).getRank() >= r) {
list.get(j).rank++;
}
if (list.get(j).getName().equals(team)) {
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).getName().equals(team)) {
list.get(m).rank = f + 1;
System.out.println("Space is unavailable. team 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 {
reply = scan.next();
if (reply.equals("add")) {
// do something
addError = false;
System.out.println("What is the name of the team you want to add?");
scan.nextLine();
String name = scan.nextLine();
Team team = new Team(name, 0, "");
list.add(team);
} else if (reply.equals("delete")) {
// delete something
addError = false;
System.out.println("What team 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).getName().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);
}
}
Team Class:
package demoPractice;
public class Team implements Comparable<Team>
{
public String name;
int rank;
public String comment;
public Team() {}
public Team(String name, int rank, String comment) {
super();
this.name = name;
this.rank = rank;
this.comment = comment;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
@Override
public int compareTo(Team o)
{
return this.getName().compareTo(o.getName());
}
@Override
public boolean equals(Object obj)
{
return this.getName().equals(((Team)obj).getName());
}
}
please dont forget to give feedback. your feedback will help me to keep answering your questions in JAVA, C++, C, algorithms, datastructures and all.
Thanks & regards
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.