Trying to Make a Program with Java to Save Music Scores
Anonymous in /c/coding_help
545
report
Hi! I'm trying to make a program in Java with the idea to save and load information from music scores. I have a couple of problems and I don't know if anyone could help me.<br><br>First of all, I have a problem adding scores to the program. I have no idea how the program is supposed to store all the information and save everything correctly. Also, I don't know how to count the total number of scores. As far as I understand, I need to implement a program that counts how many scores are there in the database.<br><br>Secondly, I need to load the scores. In this case, I don't know how to sort the scores by date so that the latest scores are shown first. Also, I don't know how to save a file with all the scores and load it.<br><br>I'm familiar with the code for the program, but I don't know enough to solve these two problems.<br><br>Here is an example of how the program should work:<br><br>```<br>public class MusicScoreApp {<br> public static void main(String[] args) throws FileNotFoundException, IOException {<br> <br> ArrayList<Score> scores = new ArrayList<>();<br> ArrayList<String> genres = new ArrayList<>();<br> ArrayList<String> moods = new ArrayList<>();<br> <br> // Read from scores file<br> genGenre(genres);<br> genMood(moods);<br> File scoreFile = new File("scores.txt");<br> if (!scoreFile.exists()) {<br> scoreFile.createNewFile();<br> }<br> if (scoreFile.length() != 0) {<br> scores = readScores(scoreFile);<br> }<br> <br> // Start the program<br> scores = run(scores, genres, moods);<br> <br> // Save scores to file<br> saveScores(scores, scoreFile);<br> }<br> // readScore reads a score from the console<br> public static Score readScore(ArrayList<String> genres, ArrayList<String> moods) {<br> Scanner s = new Scanner(System.in);<br> System.out.println("Enter score name: ");<br> String scoreName = s.nextLine();<br> System.out.println("Enter genre: ");<br> String genre = s.nextLine();<br> System.out.println("Enter mood: ");<br> String mood = s.nextLine();<br> System.out.println("Enter date: ");<br> String date = s.nextLine();<br> Score score = new Score(scoreName, genre, mood, date);<br> return score;<br> }<br> // addScore adds score to the scores ArrayList<br> public static ArrayList<Score> addScore(ArrayList<Score> scores, ArrayList<String> genres, ArrayList<String> moods, ArrayList<String> actions) {<br> Score score = readScore(genres, moods);<br> scores.add(score);<br> System.out.println("Score added successfully!");<br> return scores;<br> }<br> // delScore deletes score from the scores ArrayList<br> public static ArrayList<Score> delScore(ArrayList<Score> scores, ArrayList<String> genres, ArrayList<String> moods, ArrayList<String> actions, Scanner s) {<br> System.out.println("Enter the number of the score to delete: ");<br> int scoreNum = s.nextInt();<br> s.nextLine();<br> if (scoreNum > scores.size()) {<br> System.out.println("Score not found!");<br> return scores;<br> }<br> scores.remove(scoreNum-1);<br> System.out.println("Score deleted!");<br> return scores;<br> }<br> // printScore prints a score<br> public static void printScore(Score score) {<br> System.out.println("Score Name: " + score.getScoreName());<br> System.out.println("Genre: " + score.getGenre());<br> System.out.println("Mood: " + score.getMood());<br> System.out.println("Date: " + score.getDate());<br> }<br> // printScores prints all scores<br> public static void printScores(ArrayList<Score> scores) {<br> for (int i = 0; i < scores.size(); i++) {<br> System.out.println("Score # " + (i+1));<br> printScore(scores.get(i));<br> }<br> }<br> // saveScores saves the scores into scores.txt<br> public static void saveScores(ArrayList<Score> scores, File scoreFile) throws IOException {<br> FileWriter writer = new FileWriter(scoreFile);<br> for (Score score: scores) {<br> writer.write(score.getScoreName() + "\n");<br> writer.write(score.getGenre() + "\n");<br> writer.write(score.getMood() + "\n");<br> writer.write(score.getDate() + "\n\n");<br> }<br> writer.close();<br> }<br> // readScores reads the scores from scores.txt<br> public static ArrayList<Score> readScores(File scoreFile) throws FileNotFoundException {<br> Scanner s = new Scanner(scoreFile);<br> ArrayList<Score> scores = new ArrayList<>();<br> while (s.hasNextLine()) {<br> String scoreName = s.nextLine();<br> String genre = s.nextLine();<br> String mood = s.nextLine();<br> String date = s.nextLine();<br> Score score = new Score(scoreName, genre, mood, date);<br> scores.add(score);<br> s.nextLine(); // For the newline after each score<br> }<br> s.close();<br> return scores;<br> }<br> // genGenre generates genres<br> public static void genGenre(ArrayList<String> genres) {<br> genres.add("Classical");<br> genres.add("Jazz");<br> genres.add("Rock");<br> genres.add("Electronic");<br> genres.add("Hip Hop");<br> genres.add("Other");<br> }<br> // genMood generates moods<br> public static void genMood(ArrayList<String> moods) {<br> moods.add("Happy");<br> moods.add("Sad");<br> moods.add("Angry");<br> moods.add("Calm");<br> moods.add("Scary");<br> moods.add("Other");<br> }<br> // genActions generates actions<br> public static void genActions(ArrayList<String> actions) {<br> actions.add("Add a score");<br> actions.add("Delete a score");<br> actions.add("View scores");<br> actions.add("Quit the program");<br> }<br> // actionMenu prints the actions<br> public static void actionMenu(ArrayList<String> actions) {<br> for (int i = 0; i < actions.size(); i++) {<br> System.out.println((i+1) + ". " + actions.get(i));<br> }<br> }<br> // chooseAction chooses an action<br> public static int chooseAction(Scanner s, ArrayList<String> actions) {<br> System.out.println("Choose an action: ");<br> int action = s.nextInt();<br> s.nextLine();<br> if (action > actions.size()) {<br> System.out.println("Invalid action. Please try again.");<br> actionMenu(actions);<br> action = chooseAction(s, actions);<br> }<br> return action;<br> }<br> // run runs the program<br> public static ArrayList<Score> run(ArrayList<Score> scores, ArrayList<String> genres, ArrayList<String> moods) {<br> ArrayList<String> actions = new ArrayList<>();<br> genActions(actions);<br> Scanner s = new Scanner(System.in);<br> while (true) {<br> actionMenu(actions);<br> int action = chooseAction(s, actions);<br> if (action == 1) {<br> scores = addScore(scores, genres, moods, actions);<br> } else if (action == 2) {<br> scores = delScore(scores, genres, moods, actions, s);<br> } else if (action == 3) {<br> printScores(scores);<br> } else if (action == 4) {<br> break;<br> }<br> }<br> return scores;<br> }<br>}<br>```<br><br>```<br>public class Score {<br> private String scoreName;<br> private String genre;<br> private String mood;<br> private String date;<br> <br> // Constructor<br> public Score(String scoreName, String genre, String mood, String date) {<br> this.scoreName = scoreName;<br> this.genre = genre;<br> this.mood = mood;<br> this.date = date;<br> }<br> // Getters<br> public String getScoreName() {<br> return this.scoreName;<br> }<br> public String getGenre() {<br> return this.genre;<br> }<br> public String getMood() {<br> return this.mood;<br> }<br> public String getDate() {<br> return this.date;<br> }<br>}<br>```<br><br>If someone knows how to add scores to the program and load scores, it would be a great help!
Comments (11) 18800 👁️