Question

Write a Java program that: 1. Decide on a list of n favorites (songs, bands, movies,...

Write a Java program that:

1. Decide on a list of n favorites (songs, bands, movies, video games, etc.) (between 5 and 10 favorites is fine).

2. Write a program that lists the n favorites but with a small twist.

3. Read in a file of n favorites (sample file in the Resources/Sample File area)...Please make your own.

4. Print a list of all n favorites to the user (so they know what is coming)

5. Ask if the user wishes to add or delete elements to/from the list.

- If they wish to add an element, read in the element from the console

- If they wish to remove an element, prompt for the item to be removed.

6. Print the next favorite from the list and for each favorite....

- 1st prompt for the user’s ranking of the favorite

- 2nd prompt for the user’s comments (for this round)

7. Slot the favorite into the final list using the supplied flowchart

8. Inform the user of the placement of the latest element.

9. After all n favorites, print the list to the screen

10. When complete, write back to the input file, adding the new comments to the previous rounds of comments. The file should be able to be consumed by your program for the next round of play.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
abc

Solution:

code:

package abc;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

public class Favourite {

public static void main(String[] args) {
//normal arrays are of fixed length
//ArrayLists are dynamic
//for list from file
ArrayList<String> originalMovieList = new ArrayList<>();
// for adding and removing movies
ArrayList<String> movies = new ArrayList<>();
//for ratings
ArrayList<String> ratings = new ArrayList<>();
// for comments
ArrayList<String> comments = new ArrayList<>();
//stores the user choice
int choice = 0;
// to read input from console
Scanner keyboard = new Scanner(System.in);
try {
// to read from file
Scanner fileReader = new Scanner(new File("favourites.txt"));
//reading all data from file
while(fileReader.hasNextLine())
{
String movieDetail = fileReader.nextLine();
originalMovieList.add(movieDetail);
String tokens[] = movieDetail.split("\\|");
movies.add(tokens[0]);
ratings.add(tokens[1]);
comments.add(tokens[2]);
}
fileReader.close();
//printing all the favorites to the screen
System.out.println("--------List of favorites--------\n");
for(String string: movies)
System.out.println(string);
String fav;
while(true)
{
System.out.print("\n1. Add a favorite\n2. Remove a favorite\n3. Move to Adding rating and comments\nEnter your choice: ");
choice = Integer.valueOf(keyboard.nextLine());
if(choice != 1 && choice != 2)
break;
System.out.print("\nEnter a favorite: ");
fav = keyboard.nextLine();
if(choice == 1)
{
while(fav.length() <20)
fav+= " ";
movies.add(fav);
ratings.add(" ");
comments.add(" ");
originalMovieList.add(fav);
System.out.println("Added favorite: " + fav);
}
else if(choice ==2)
{
for(int i=0; i<movies.size(); i++)
{
if(movies.get(i).contains(fav))
{
movies.remove(i);
originalMovieList.remove(i);
ratings.remove(i);
comments.remove(i);
originalMovieList.remove(i);
}
}
}
}
for(int i=0; i<movies.size(); i++)
{
System.out.println("Favorite: " + movies.get(i));
System.out.print("Enter your rating: ");
String rating = keyboard.nextLine();
while(rating.length()<5)
rating += " ";
System.out.print("Enter your comment: ");
String comment = keyboard.nextLine();
ratings.set(i, " " + rating);
comments.set(i, comments.get(i) +"; " + comment);
originalMovieList.set(i, movies.get(i) + "|" + ratings.get(i) + "|" + comments.get(i));
}
PrintWriter writer = new PrintWriter(new File("favorites.txt"));
System.out.println("\nFinal favorite list\n");
for(String string: originalMovieList)
{
writer.write(string + "\n");
System.out.println(string);
}
writer.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

Add a comment
Know the answer?
Add Answer to:
Write a Java program that: 1. Decide on a list of n favorites (songs, bands, movies,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • *Use Java to create this program* For this assignment, you will be building a Favorite Songs...

    *Use Java to create this program* For this assignment, you will be building a Favorite Songs application. The application should have a list with the items displayed, a textbox for adding new items to the list, and four buttons: Add, Remove, Load, and Save. The Add button takes the contents of the text field (textbox) and adds the item in it to the list. Your code must trim the whitespace in front of or at the end of the input...

  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

  • in java Complete Program that simulates a login procedure. The program reads a list of names,...

    in java Complete Program that simulates a login procedure. The program reads a list of names, email addresses and passwords from a file p3.txt. Store the information in parallel array lists names, emails and passwords. Your program will prompt the user for their email address. If the email is not in the system, prompt the user to try again. Provide the option to quit. If the email is found in the system, prompt the user to enter their password. After...

  • This program is part 1 of a larger program. Eventually, it will be a complete Flashcard...

    This program is part 1 of a larger program. Eventually, it will be a complete Flashcard game. For this part, the program will Prompt the user for a file that contains the questions – use getline(cin, fname) instead of cin >> fname to read the file name from the user. This will keep you in sync with the user’s input for later in the program The first line is the number of questions (just throw this line away this week)...

  • Write a program that performs the following: - Reads from the file "myfile.txt" using readlines() -...

    Write a program that performs the following: - Reads from the file "myfile.txt" using readlines() - Prints out the contents of the file "myfile.txt", that was read into a list by readlines(). Note that this should not look like a list, so you will need to loop through the list created by readlines and print the text. - Use the try/except method to create the file if it does not exist - If the file does not exist, prompt the...

  • C++ Write a C++ program that user is able to choose the function of their wanting from this menu : Users will be asked...

    C++ Write a C++ program that user is able to choose the function of their wanting from this menu : Users will be asked to enter the movies they like along with the Genre and one Actor/Actress per movie. the entering process should look like this: Note that if the user enters yes, the program should start entering the next movie with its properties. Available movies must be entered as follow: The program should enter as many available movies as...

  • package _solution; /** This program demonstrates how numeric types and operators behave in Java Do Task...

    package _solution; /** This program demonstrates how numeric types and operators behave in Java Do Task #1 before adding Task#2 where indicated. */ public class NumericTypesOriginal { public static void main (String [] args) { //TASK #2 Create a Scanner object here //identifier declarations final int NUMBER = 2 ; // number of scores int score1 = 100; // first test score int score2 = 95; // second test score final int BOILING_IN_F = 212; // boiling temperature double fToC;...

  • MIPS CODE required to write an assembly program to find the maximum of an array of integers by...

    required to write an assembly program to find the maximum of anarray of integers by doing the following:1. Prompt user to input array size n (n <= 10)2. Prompt user to input element values of array A one by one3. Display the result on the console.This program must at least include one function. The main program will read the valuesof the array (as user inputs the element values) and stores them in the memory (datasegments section) and at the end...

  • This program is in C++ Write a program that validates charge account numbers read in from...

    This program is in C++ Write a program that validates charge account numbers read in from the file Charges.txt following this assignment (A23 Data). Use a selection sort function on the array to sort the numbers then use a binary search to validate the account numbers. Print out the sorted list of account numbers so you can see the valid ones. Prompt the user to enter an account number and the validate that number. If the account number entered is...

  • In C++ Sorted List of User Entered Numbers 2. Write a program that reads in a...

    In C++ Sorted List of User Entered Numbers 2. Write a program that reads in a list of integers into a vector with base type int. Provide the facility to read this vector from an input file. Make sure to ask the user for a filename. The output is a two-column list. The first column is a list of the distinct vector elements. The second column is the count of the number of occurrences for each element. The list should...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT