Do the TODOs in SongFileAccessor.java. It inherits from FileAccessor class.
TODO 1: Implement the processLine method. When the text file is processed, each line of text will be passed to processLine . Each line contains 4 fields: title, album, artist, and play time. The album field is optional. Each field is separated by a comma.
TODO 2: Implement the songToCSVString method. This method takes a Song object as a parameter and returns a String which is the csv representation of its data. Use the Song class get methods to get the data from the Song object and compose a csv string. See the examples above.
TODO 3: Implement the getSongsAsCSV . This method takes a SongList object as a parameter. It assembles a CSV-formatted String for all of the songs in the SongList and returns that String. Call the songToCSVString method on each Song to get the csv String for that Song. Append each Song’s csv String to a result String that will be returned.
SongFileAccessor.java:
1 import java.io.*;
2 import java.util.ArrayList;
3
4 /**
5 * This class extends FileAccessor to process the song data in a
text file.
6 * The songs are stored in the file in comma-delimited(CSV)
format. Each
7 * line in the file reporesents one song. Each line in the file is
processed
8 * into a new Song object, which is placed on the SongList member
variable "songs".
9 * Another method, writeSongsToFile, takes a SongList, converts
its songs to a single
10 * CSV formatted String and writes it to the text file.
11 **/
12 public class SongFileAccessor extends FileAccessor{
13
14 SongList songs;
15 /* After the call to the superclass constructor, initialize the
member variable "songs"
16 to a new SongList.
17 */
18 public SongFileAccessor(String f) throws IOException{
19 super(f);
20 songs = new SongList();
21 }
22
23 /* This method implements the abstract method processLine in the
FileAccessor class.
24 It uses the String class method split to parse the line into
individual Strings.
25 The split method is passed "," as a parameter since the comma is
the delimeter.
26
27 Each line of the file has this format:
28 title,album,artist,playTime
29 Where title, artist and playTime are required, and album is
optional. An example of
30 a line with no album:
31 title,,artist,playTime
32
33 You may assume that the title, artist and playTime fields will
always be present. After
34 the line has been tokenized, the array returned by split
contains the data needed to create a
35 new Song object which is then added to the SongList object
songs.
36 You have to check the length of the second token to determine if
the album field is present and based
37 on that length which Song constructor to call.
38 */
39 public void processLine(String line){
40 // TODO 1: Implement this method.
41
42
43
44 }
45
46 /* Formats the data in a Song object into a String in CSV
format.
47 * See the text files and instructions for examples of this
format.
48 */
49 public String songToCSVString(Song song){
50 // TODO 2: Implement this method.
51 return null;
52 }
53
54 /* This method returns a String of all songs in the song
list
55 in CSV format. Each song must be put into CSV format, and
56 a line break inserted between songs. Calls
songToCSVString.
57 */
58 public String getSongsAsCSV(SongList songList){
59 // TODO 3: Implement this method.
60
61
62
63 return null;
64 }
65
66 /* Returns the SongList member variable "songs".
67 */
68 public SongList getSongList(){
69 return songs;
70 }
71
72 /* This method writes the data in a SongList object to the text
file.
73 The songs on the list must first be converted into a String in
CSV
74 format. Then this method calls the writeToFile method, passing
it the
75 CSV String and the fileName.
76 */
77 public void writeSongsToFile(SongList songs) throws
IOException{
78 writeToFile(getSongsAsCSV(songs), fileName);
79 }
80
81 // This main can be run for testing SongFileAccessor.
82 public static void main(String[] args)throws IOException{
83 // need a file to pass in, but can ignore it and enter your own
inputs.
84 String dataFile = "songfiles/test0songs.txt";
85 SongFileAccessor sfa = new SongFileAccessor(dataFile);
86 Song song1 = new Song("Kashmir", "Physical Graffiti", "Led
Zepplin", 8.37);
87 // Test songToCSVString
88 System.out.println("****** Test songToCSVString ******");
89 String songCSVstr = sfa.songToCSVString(song1);
90 System.out.println("songToCSVString output: "+
songCSVstr);
91 System.out.println("correct output: Kashmir,Physical
Graffiti,Led Zepplin,8.37");
92 System.out.println(" ");
93 // Test getSongsAsCSV
94 System.out.println("****** Test getSongsAsCSV ******");
95 SongList songList = new SongList();
96 songList.addSong(new Song("What About Us", "Beautiful Trauma",
"P!nk", 4.31));
97 String songsCSVstr = sfa.getSongsAsCSV(songList);
98 System.out.println("songToCSVString output: "+
songsCSVstr);
99 System.out.println("correct output: What About Us,Beautiful
Trauma,P!nk,4.31");
100 System.out.println("-note: should see a space between the above
because a line break is added in getSongsAsCSV.");
101 System.out.println(" ");
102 // Test processLine
103 System.out.println("****** Test processLine ******");
104 String csvLine = "Like It Is,The Blue,Yusef Lateef,7.36";
105 sfa.processLine(csvLine);
106 SongList songs = sfa.getSongList();
107 ArrayList<Song> songArrayList =
songs.getSongList();
108 Song testSong = songArrayList.get(0);
109 System.out.println("title test: "+"Like It Is"+ " -
"+testSong.getTitle());
110 System.out.println("album test: "+"The Blue"+ " -
"+testSong.getAlbum());
111 System.out.println("artist test: "+"Yusef Lateef"+ " -
"+testSong.getArtist());
112 System.out.println("playtime test: 7.36"+ " -
"+testSong.getPlayTime());
113 }
114 }
FileAccessor.java:
1 import java.util.Scanner;
2 import java.io.*;
3
4 /**
5 * This abstract class contains the functionality of reading lines
from a text file
6 * and writing a String to a text file. Its abstract method,
processLine, is intended to
7 * be implemented by subclasses so they can process each line in
their specialized manner.
8 * This class utilizes a Scanner member variable to read from a
text file. The file name is
9 * a protected member variable and is available directly to its
subclasses. A PrintWriter is used
10 * in the writeToFile method to write a String to the file.
11 */
12 public abstract class FileAccessor{
13 protected String fileName;
14 Scanner scan;
15
16 /* Constructor that initializes the Scanner and opens the
file.
17 * An IOException is thrown if the file cannot be opened or is
not found.
18 */
19 public FileAccessor(String fName) throws IOException{
20 fileName = fName;
21 scan = new Scanner(new FileReader(fileName));
22 }
23
24 /* Assumes the Scanner instance contains the lines of text from
the file.
25 * The lines are read from the scanner, and processLine is called
in each line.
26 */
27 public void processFile() {
28 while(scan.hasNext()){
29 processLine(scan.nextLine());
30 }
31 scan.close();
32 }
33
34 /* This method must be implemented by any subclass. This allows
the
35 * subclass to handle the line of text in its own specialized
manner.
36 */
37 public abstract void processLine(String line);
38
39 /* This method utilizes a PrintWriter to write a String to a
text file.
40 * An IOException is thrown if the file cannot be opened or
found.
41 */
42 public void writeToFile(String data, String fileName) throws
IOException{
43 PrintWriter pw = new PrintWriter(fileName);
44 pw.print(data);
45 pw.close();
46 }
47
48 }
SongFileAccessor.java After Completing TODO's:
package song;
import java.io.*;
import java.util.ArrayList;
/**
* This class extends FileAccessor to process the song data in a text file. The
* songs are stored in the file in comma-delimited(CSV) format. Each line in the
* file reporesents one song. Each line in the file is processed into a new Song
* object, which is placed on the SongList member variable "songs". Another
* method, writeSongsToFile, takes a SongList, converts its songs to a single
* CSV formatted String and writes it to the text file.
**/
public class SongFileAccessor extends FileAccessor {
SongList songs;
/*
* After the call to the superclass constructor, initialize the member variable
* "songs" to a new SongList.
*/
public SongFileAccessor(String f) throws IOException {
super(f);
songs = new SongList();
}
/*
* This method implements the abstract method processLine in the FileAccessor
* class. It uses the String class method split to parse the line into
* individual Strings. The split method is passed "," as a parameter since the
* comma is the delimeter.
*
* Each line of the file has this format: title,album,artist,playTime Where
* title, artist and playTime are required, and album is optional. An example of
* a line with no album: title,,artist,playTime
*
* You may assume that the title, artist and playTime fields will always be
* present. After the line has been tokenized, the array returned by split
* contains the data needed to create a new Song object which is then added to
* the SongList object songs. You have to check the length of the second token
* to determine if the album field is present and based on that length which
* Song constructor to call.
*/
public void processLine(String line) {
// TODO 1: Implement this method.
//split to parse the line into individual Strings
String[]fields=line.split(",");
String title=fields[0];//title
String album=fields[1];//album
String artist=fields[2];//artist
double playTime=Double.parseDouble(fields[3]);//duration
Song s;
//checking whether album is there or not
if(album.length()==0)//if second field length is zero(no album)
{
//create song object by passing title,artist,playTime
s=new Song(title,artist,playTime);
}
else
{
//create song object by passing title,album,artist,playTime
s=new Song(title,album,artist,playTime);
}
//add song to list
songs.addSong(s);
}
/*
* Formats the data in a Song object into a String in CSV format. See the text
* files and instructions for examples of this format.
*/
public String songToCSVString(Song song) {
// TODO 2: Implement this method.
String s="";
s=s+song.getTitle()+","+song.getAlbum()+","+song.getArtist()+","+song.getPlayTime();
return s;
}
/*
* This method returns a String of all songs in the song list in CSV format.
* Each song must be put into CSV format, and a line break inserted between
* songs. Calls songToCSVString.
*/
public String getSongsAsCSV(SongList songList) {
String s="";
ArrayList<Song>list=songList.getSongList();
for(Song song:list)
{
s+=songToCSVString(song)+"\n";
}
return s;
}
/*
* Returns the SongList member variable "songs".
*/
public SongList getSongList() {
return songs;
}
/*
* This method writes the data in a SongList object to the text file. The songs
* on the list must first be converted into a String in CSV format. Then this
* method calls the writeToFile method, passing it the CSV String and the
* fileName.
*/
public void writeSongsToFile(SongList songs) throws IOException {
writeToFile(getSongsAsCSV(songs), fileName);
}
// This main can be run for testing SongFileAccessor.
public static void main(String[] args) throws IOException {
// need a file to pass in, but can ignore it and enter your own inputs.
String dataFile = "songfiles/test0songs.txt";
SongFileAccessor sfa = new SongFileAccessor(dataFile);
Song song1 = new Song("Kashmir", "Physical Graffiti", "Led Zepplin", 8.37);
// Test songToCSVString
System.out.println("****** Test songToCSVString ******");
String songCSVstr = sfa.songToCSVString(song1);
System.out.println("songToCSVString output: " + songCSVstr);
System.out.println("correct output: Kashmir,Physical Graffiti,Led Zepplin,8.37");
System.out.println(" ");
// Test getSongsAsCSV
System.out.println("****** Test getSongsAsCSV ******");
SongList songList = new SongList();
songList.addSong(new Song("What About Us", "Beautiful Trauma", "P!nk", 4.31));
String songsCSVstr = sfa.getSongsAsCSV(songList);
System.out.println("songToCSVString output: " + songsCSVstr);
System.out.println("correct output: What About Us,Beautiful Trauma,P!nk,4.31");
System.out
.println("-note: should see a space between the above because a line break is added in getSongsAsCSV.");
System.out.println(" ");
// Test processLine
System.out.println("****** Test processLine ******");
String csvLine = "Like It Is,The Blue,Yusef Lateef,7.36";
sfa.processLine(csvLine);
SongList songs = sfa.getSongList();
ArrayList<Song> songArrayList = songs.getSongList();
Song testSong = songArrayList.get(0);
System.out.println("title test: " + "Like It Is" + " - " + testSong.getTitle());
System.out.println("album test: " + "The Blue" + " - " + testSong.getAlbum());
System.out.println("artist test: " + "Yusef Lateef" + " - " + testSong.getArtist());
System.out.println("playtime test: 7.36" + " - " + testSong.getPlayTime());
}
}
Useful Image:
Get Answers For Free
Most questions answered within 1 hours.