Songlist.java is a class that keeps track of songs in file named songfile. Write a function: public Song getSong(String Songname) which will return the song associated with the name of the song passed if it is present in the library or null if its not? The filename is passed in as command line argument.
Song.java
public class Song {
private String name, composer;
private double runTime;
public Song()
{
this.name = "";
this.composer = "";
this.runTime = 0;
}
public Song(String name, String composer, double runTime)
{
this.name = name;
this.composer = composer;
this.runTime = runTime;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getComposer() {
return composer;
}
public void setComposer(String composer) {
this.composer = composer;
}
public double getRunTime() {
return runTime;
}
public void setRunTime(double runTime) {
this.runTime = runTime;
}
@Override
public String toString()
{
return("Name: " + this.name + ", Composer: " + this.composer + ",
Runtime: " + this.runTime + " sec");
}
}
SongList.java
import java.util.ArrayList;
public class SongList {
private ArrayList<Song> songs;
public SongList()
{
this.songs = new ArrayList<>();
}
public void addSong(Song song)
{
if(song != null)
this.songs.add(song);
}
public Song getSong(String songName)
{
Song song = null;
for(int i = 0; i < this.songs.size(); i++)
{
if(songs.get(i).getName().equals(songName))
{
song = songs.get(i);
break;
}
}
return song;
}
public void displayAllSongs()
{
for(Song song : this.songs)
{
System.out.println(song.toString());
}
System.out.println();
}
}
SongTrackerDriver.java (Main class)
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class SongTrackerDriver {
public static void main(String[] args)
{
// args[0] is the user input file name
String fileName = args[0].trim();
// counter for songs read from the file
int count = 0;
// a SongList object to record all the song objects
SongList songList = new SongList();
// Scanner object to read the file
Scanner fileReader;
// read all songs from the file
// and add to the songList object using the songList.add(..)
method
try
{
fileReader = new Scanner(new File(fileName));
while(fileReader.hasNextLine())
{
String line = fileReader.nextLine().trim();
String[] data = line.split(",");
String songName = data[0];
String composer = data[1];
int runTime = Integer.parseInt(data[2]);
songList.addSong(new Song(songName, composer, runTime));
count++;
}
fileReader.close();
}catch(FileNotFoundException fnfe){
System.out.println(fileName + " couldn't be found!");
System.exit(0);
}
System.out.println(count + " songs read from the file!");
// prompt user to enter a song name to search
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter a song name to search: ");
String userSongName = sc.nextLine().trim();
// call the songList.getSong(..) method and store the result
returned into a Song variable
Song result = songList.getSong(userSongName);
// check whether the result is null or a song object
if(result == null)
System.out.println("No records found with the name " +
userSongName);
else
System.out.println("Match found:\n" + result.toString());
}
}
************************************************************** SCREENSHOT *************************************************************
INPUT FILE (songlist.txt) : This file needs to be created in the project directory before running the program. However, data inside the file may vary but each data fields need to be separated by comma.
CONSOLE OUTPUT :
Get Answers For Free
Most questions answered within 1 hours.