You have been hired to write a Java console application that manages song data.
ID: 3807288 • Letter: Y
Question
You have been hired to write a Java console application that manages song data. The application has the following two classes:
Song.java
This class represents one song and includes:
Fields
static songCount – initialize to 0 in declaration.
static totalCost – initialize to 0 in declaration.
title
yearReleased
downloadCost
A constructor with no parameters that sets the fields, respectively, to these values:
songCount = songCount + 1
title = "(not set)"
yearReleased = -1
downloadCost = -1
A constructor with three parameters that sets the fields, respectively, to these values:
songCount = songCount + 1
totalCost = totalCost + downloadCost
title – set from parameter
yearReleased – set from parameter
downloadCost – set from parameter
Getter methods for each field (declare the getters for songCount and totalCost static).
Setter methods for each non-static field.
equals method that compares title, yearReleased, and downloadCost for equality.
toString method for returning instance variable values only.
Menu.java
This class contains the main method and uses the other class to manage song data. Store data in an array list called songs. Present the following menu to the user:
Music Magicians Menu
1 – Add song
2 – Delete song
3 – List songs
4 – Exit
Enter an option:
Here are what the options do:
Add song prompts for and gets from the user a song title, year released, and download cost. If the song already exists in the array list, it prints an error message. If the song doesn’t exist in the array list, it creates a song object using the three-parameter constructor, stores it in the array list, and prints a message. After reading the download cost, clear the keyboard buffer with statement junk = keyboard.nextLine();
Delete song prompts for and gets from the user a song title and searches the array list for the title. If the song doesn’t exist in the array list, it prints an error message. If the song already exists in the array list, it removes it from the array list and prints a message. After reading the song title, clear the keyboard buffer with statement junk = keyboard.nextLine();
List songs shows all song data in formatted columns. It then lists the number of songs and the total download cost.
Exit clears the array list and ends the application.
Continue to process menu options until the user enter 4.
Explanation / Answer
Main.java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
List<Song> list = new ArrayList<Song>();
while(true){
System.out.println("Enter Your Choice 1. Add Song 2. Delete Song 3. List Songs 4. Exit");
int choice = keyboard.nextInt();
keyboard.nextLine();
switch(choice){
case 1: addSong(list,keyboard);break;
case 2: deleteSong(list,keyboard);break;
case 3: listSongs(list);break;
case 4: list.clear();System.exit(0);break;
default: System.out.println("Invalid Choice");
}
}
}
public static void addSong(List<Song> list, Scanner keyboard){
System.out.println("Enter Song Title :");
String title = keyboard.nextLine();
System.out.println("Enter Song Year Released :");
int yearReleased = keyboard.nextInt();
System.out.println("Enter Song Download Cost :");
int downloadCost = keyboard.nextInt();
Song s = new Song(title, yearReleased, downloadCost);
list.add(s);
System.out.println("Song added successfully");
keyboard.nextLine();
}
public static void deleteSong(List<Song> list, Scanner keyboard){
System.out.println("Enter Song Title :");
String title = keyboard.nextLine();
boolean found = false;
for(int i=0; i<list.size() && !found; i++){
if(list.get(i).getTitle().equals(title)){
list.remove(list.get(i));
found = true;
}
}
if(!found) {
System.out.println("Song does not exist");
}
}
public static void listSongs(List<Song> list){
System.out.println("List of Songs :");
for(Song s: list){
System.out.println(s.toString());
}
}
}
Song.java
public class Song {
private static int songCount;
private static int totalCost;
private String title;
private int yearReleased;
private int downloadCost;
public Song(){
songCount = songCount + 1;
title = "(not set)";
yearReleased = -1;
downloadCost = -1;
}
public Song(String title , int yearReleased, int downloadCost){
songCount = songCount + 1;
totalCost = totalCost + downloadCost;
this.title = title;
this.yearReleased = yearReleased;
this.downloadCost = downloadCost;
}
public static int getSongCount() {
return songCount;
}
public static void setSongCount(int songCount) {
Song.songCount = songCount;
}
public static int getTotalCost() {
return totalCost;
}
public static void setTotalCost(int totalCost) {
Song.totalCost = totalCost;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getYearReleased() {
return yearReleased;
}
public void setYearReleased(int yearReleased) {
this.yearReleased = yearReleased;
}
public int getDownloadCost() {
return downloadCost;
}
public void setDownloadCost(int downloadCost) {
this.downloadCost = downloadCost;
}
// Overriding equals() to compare two Complex objects
@Override
public boolean equals(Object o) {
// If the object is compared with itself then return true
if (o == this) {
return true;
}
/* Check if o is an instance of Complex or not
"null instanceof [type]" also returns false */
if (!(o instanceof Song)) {
return false;
}
// typecast o to Complex so that we can compare data members
Song c = (Song) o;
// Compare the data members and return accordingly
return Integer.compare(yearReleased, c.yearReleased) == 0
&& Integer.compare(downloadCost , c.downloadCost ) == 0 && title.compareTo(c.title) == 0;
}
public String toString(){
return "Title : "+title+" Year Released : "+yearReleased+" Dwnload Cost : "+downloadCost+" Total Cost : "+totalCost+" Song Count : "+songCount;
}
}
Output:
Enter Your Choice
1. Add Song
2. Delete Song
3. List Songs
4. Exit
1
Enter Song Title :
aaaa
Enter Song Year Released :
2000
Enter Song Download Cost :
100
Song added successfully
Enter Your Choice
1. Add Song
2. Delete Song
3. List Songs
4. Exit
1
Enter Song Title :
bbbb
Enter Song Year Released :
2010
Enter Song Download Cost :
200
Song added successfully
Enter Your Choice
1. Add Song
2. Delete Song
3. List Songs
4. Exit
3
List of Songs :
Title : aaaa Year Released : 2000 Dwnload Cost : 100 Total Cost : 300 Song Count : 2
Title : bbbb Year Released : 2010 Dwnload Cost : 200 Total Cost : 300 Song Count : 2
Enter Your Choice
1. Add Song
2. Delete Song
3. List Songs
4. Exit
2
Enter Song Title :
bbbb
Enter Your Choice
1. Add Song
2. Delete Song
3. List Songs
4. Exit
3
List of Songs :
Title : aaaa Year Released : 2000 Dwnload Cost : 100 Total Cost : 300 Song Count : 2
Enter Your Choice
1. Add Song
2. Delete Song
3. List Songs
4. Exit
2
Enter Song Title :
dddd
Song does not exist
4
Enter Your Choice
1. Add Song
2. Delete Song
3. List Songs
4. Exit
4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.