JAVA HELP . . Write a Java console application that manages song data. The appli
ID: 3809987 • Letter: J
Question
JAVA HELP
.
.
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.
.
.
HW6.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
Here is the code for Song.java:
class Song
{
static int songCount = 0;
static double totalCost = 0.0;
String title;
int yearReleased;
double downloadCost;
Song()
{
songCount++;
title = "(not set)";
yearReleased = -1;
downloadCost = -1;
}
Song(String title, int yearReleased, double downloadCost)
{
songCount++;
totalCost = totalCost + downloadCost;
this.title = title;
this.yearReleased = yearReleased;
this.downloadCost = downloadCost;
}
public String getTitle()
{
return title;
}
public static int getSongCount()
{
return songCount;
}
public static double getTotalCost()
{
return totalCost;
}
public void setTitle(String title)
{
this.title = title;
}
public void setYearReleased(int yearReleased)
{
this.yearReleased = yearReleased;
}
public void setDownloadCost(double downloadCost)
{
this.downloadCost = downloadCost;
}
public boolean equals(Song other)
{
if(other.title == title && other.yearReleased == yearReleased && other.downloadCost == downloadCost)
return true;
return false;
}
public String toString()
{
return "Title: " + title + " Year Released: " + yearReleased + " Download Cost: " + downloadCost + " ";
}
}
And the code for SongsList.java is:
import java.util.*;
class SongList
{
public static void main(String[] args)
{
ArrayList songList = new ArrayList();
Scanner sc = new Scanner(System.in);
String junk, title;
int option;
while(true)
{
System.out.println("Music Magicians Menu");
System.out.println("1 – Add song");
System.out.println("2 – Delete song");
System.out.println("3 – List songs");
System.out.println("4 – Exit");
System.out.print("Enter an option: ");
option = sc.nextInt();
switch(option)
{
case 1:
System.out.print("Enter the title of the song: ");
title = sc.next();
System.out.print("Enter the year the song released: ");
int year = sc.nextInt();
System.out.print("Enter the download cost of the song: ");
double cost = sc.nextDouble();
junk = sc.nextLine();
Song song = new Song(title, year, cost);
songList.add(song);
break;
case 2:
System.out.print("Enter the title of the song: ");
title = sc.next();
junk = sc.nextLine();
for(int i = 0; i < songList.size(); i++)
if(((Song)(songList.get(i))).getTitle() == title)
{
songList.remove(i);
break;
}
break;
case 3:
for(int i = 0; i < songList.size(); i++)
System.out.println(songList.get(i));
System.out.println("Number of songs: " + Song.getSongCount());
System.out.println("Total Cost: " + Song.getTotalCost());
break;
case 4: return;
default: System.out.println("Invalid menu option. Try again.");
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.