Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MusicCD, that can represent the important properties of a music CD. The properti

ID: 3630203 • Letter: M

Question

MusicCD, that can represent the important properties of a music CD. The properties, at a minimum, are the title of the CD (a String), the name of the band or artist (a String), the number of tracks on the CD (an int), and the year of publishing or copyright (an int). The MusicCD class will contain a constructor that has parameters for each of the properties, getters and setters for all properties, and a method to display a CD.

The database, MusicDB, should be an array of database records. Choose the array large enough to hold as many CDs as you think you might own. You will then implement methods to display the entire database of CDs, to add a new CD to the database, to find and display a CD given its name by searching through all records in the database, and to remove a CD from the database given its name.

A third class, TestMusicDB should contain a main method that accepts and executes commands from a user until the user enters the command to quit. I suggest that the commands be just a single character, such as 'q' for quit, 'a' for add, 'f' for find, etc.

Output should be similar to this:
Welcome to your personal music database. The legal commands are
q for quit
d for display
f for find
a for add
r for remove

What is your command?
a
Name of CD: Whiskey On A Sunday
Name of group or artist: Flogging Molly
Number of tracks: 12
Copyright year: 2006
This CD has been added to your music database.

What is your command?
q
Goodbye.

Explanation / Answer

I included only minimal commenting for the methods in the CD and DB classes, the main is self explanatory. Adjust commenting to your needs, and give the code a read and a couple runs to fully understand it. Used an override of the toString method to display CDs rather than a redundant custom display method. You can do that though if that's what the requirements dictate. I hope this helps. Make sure to copy-paste it into notepad or your java IDE before reading, cramster messes up formatting.
Also, I think I came across a similar assignment yesterday or the day before and this is what I shared with them so you probably want to look around for answers on non-expired similar questions before posting your own, you might find what you want.
Good luck.

MusicCD.java

public class MusicCD {

//propoerties of Music CD
private String title;
private String artist;
private int numTracks;
private int publishYear;

//Constructor
public MusicCD(String t, String a, int n, int p) {
title = t;
artist = a;
numTracks = n;
publishYear = p;
}

//Getter - title
public String getTitle() {
return this.title;
}

//Setter - title
public void setTitle(String t) {
this.title = t;
}

//Getter - artist
public String getArtist() {
return this.artist;
}

//Setter - artist
public void setArtist(String a) {
this.artist = a;
}

//Getter - number of tracks
public int getNumTracks() {
return this.numTracks;
}

//Setter - number of tracks
public void setNumTracks(int n) {
this.numTracks = n;
}

//Getter - publishing/copyright year
public int getPublishYear() {
return this.publishYear;
}

//Setter - publishing/copyright year
public void setPublishYear(int p) {
this.publishYear = p;
}

//Display a CD by overriding the toString method of Object class
@Override
public String toString() {
return this.getTitle() + " by " + this.getArtist() + " - " + this.getNumTracks() + " tracks (" + this.getPublishYear() + ")";
}
}

MusicDB.java

public class MusicDB {

private MusicCD[] records;
private int size;

public MusicDB() {
records = new MusicCD[100];
size = 0;
}

public void addCD(MusicCD cd) {
records[size] = cd;
size++;
System.out.println("This CD has been added to you music database.");
}

public void displayDB() {
//If the array contains something - display
if (size > 0) {
for (int i = 0; i < size; i++) {
System.out.println(records[i].toString());
}
} //Else - display DB Empty message
else {
System.out.println("Music DB is empty.");
}
}

public void findCD(String title) {
int found = -1;
//Find the array index of the CD to display
for (int i = 0; i < size; i++) {
if (records[i].getTitle().equals(title)) {
found = i;
break; // break from the loop since index is found
}
}
//If the CD does exist in the database - display it
if (found > -1) {
System.out.println(records[found].toString());
} // if it doesn't exist - print a Not FOUND message
else {
System.out.println("Record not found.");
}
}

public void removeCD(String title) {
int found = -1;
//Find the array index of the CD to remove
for (int i = 0; i < size; i++) {
if (records[i].getTitle().equals(title)) {
found = i;
break; // break from the loop since index is found
}
}
//If the CD does exist in the database - remove it and shift records forward to fill its place
if (found > -1) {

records[found] = null;
for (int i = found; i < size-1 ; i++) {
records[i] = records[i+1];
}
records[size - 1] = null;

size--; // decrease size by 1

System.out.println("CD record removed.");
} // if it doesn't exist - print a Not FOUND message
else {
System.out.println("Record not found.");
}
}
}

TestMusicDB.java

import java.util.Scanner;

public class TestMusicDB {

public static void main(String[] args) {
Scanner textScanner = new Scanner(System.in);
Scanner intScanner = new Scanner(System.in);
String menu = "";

MusicDB musicdb = new MusicDB();

do
{
System.out.println("Welcome to your personal music database. The legal commands are:");
System.out.println("q for quit");
System.out.println("d for display");
System.out.println("f for find");
System.out.println("a for add");
System.out.println("r for remove");
menu = textScanner.nextLine();

if(menu.equals("d"))
{
musicdb.displayDB();
}
else if(menu.equals("f"))
{
System.out.print(" Name of CD to find: ");
musicdb.findCD(textScanner.nextLine());
}
else if(menu.equals("a"))
{
MusicCD cdToAdd = new MusicCD("","",0,0);
System.out.print(" Name of CD: ");
cdToAdd.setTitle(textScanner.nextLine());
System.out.print(" Name of group or artist: ");
cdToAdd.setArtist(textScanner.nextLine());
System.out.print(" Number of tracks: ");
cdToAdd.setNumTracks(intScanner.nextInt());
System.out.print(" Copyright year: ");
cdToAdd.setPublishYear(intScanner.nextInt());
musicdb.addCD(cdToAdd);
}
else if(menu.equals("r"))
{
System.out.print(" Name of CD to remove: ");
musicdb.removeCD(textScanner.nextLine());
}
else if(menu.equals("q"))
{
System.out.println("Goodbye.");
}
else
{
System.out.println("Unknown command. Please enter a legal command.");
}

}while(menu.compareToIgnoreCase("q") != 0);
}
}