Description: In designing interactive application like shopping activities, it i
ID: 3882243 • Letter: D
Question
Description:
In designing interactive application like shopping activities, it is desirable to have the
test data stored in a file to speed up the testing time. So, to prepare for the future
project, we need to get used to file I/O in Java.
There is a tab-delimited text file named “p1artists.txt” which is to be used as an
input file. Each record consists of the following two
fields:
a. artistID: integer.
b. artistName: String;
Implement the Artistclass that contains the following:
1.The 2 fields mentioned above in “p1artists.txt” file
2. The Constructor that accepts the 2 fields when the record of an artist is created.
3. Getters and setters for the 2 fields
4. The toString() method.
5. Test the program with the following data hardcoded inside the program.
ArtistID Artist Name
1 Acconci
2 Ames
3 Aserty
4 Baron
5 Battenberg
ii. Modify the above program according to the following:
1. Read the input file “p1artists.txt”.
2. Use the exception handler to ensure that the file is indeed available and ArtistID is numerical.
3. If the input is incorrect, print the line number and its contents, but do not stop
4. Send the output to a text file named “p1artists_out1.txt”.
iii. Enhance the above program for the following:
1. A tab-delimited text file named “p1arts.txt” contains the following fields:
ArtID Title ArtistID Appraised Value
1001 Red Rock Mountain 50 18000
1002 Offerings 52 10000
1003 Spring Flowers 12 2400
2. Assume that this file contains no error. You are going to read “p1artists.txt” into an array, and then process “p1arts.txt” to add the artist name to the output.
Name this output file “p1arts_out.txt”. Example follows:
ArtID Title Artist ArtistID Appraised Value
1001 Red Rock Mountain Mogan 50 18000
1002 Offerings Novarre 52 10000
1003 Spring Flowers Chico 12 2400
3. In the end, include the total number of the Art Works and Artists, and the total appraised value
Explanation / Answer
Artist.java--contains main method also
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Artist {
// Instance variables
private int artistID; // id of artist
private String artistName; // name of artist
/**
* Constructor
*
* @param name
* @param number
*/
Artist(int artistID, String artistName) {
this.artistID = artistID;
this.artistName = artistName;
}
public static void main(String[] args) {
//Give the file path where the files exists, two files p1arts.txt and p1artists.txt
//p1arts.txt contains details of artIdId, title, artistId, Appraised value, p1artists.txt contains artistiId and ArtistName
File artistFile = new File("D:/workspace/Test/src/arts/p1artists.txt");
File artFile = new File("D:/workspace/Test/src/arts/p1arts.txt");
BufferedWriter writer;
Scanner scanArtist;
Scanner scanArt;
//to know the line number in the artists file
int lineNumber = 0;
try {
//Cretes a writer object to write the contents to the out file
writer = new BufferedWriter(new FileWriter("p1arts_out.txt"));
//reads the contents in the artistfile
scanArtist = new Scanner(artistFile);
ArrayList<Artist> artists = new ArrayList<Artist>();
while (scanArtist.hasNextLine()) {
lineNumber++;
//check if the first part of each line which is artistId is integer or not, if not error message is displayed.
if (scanArtist.hasNextInt()) {
Artist artist = new Artist(Integer.parseInt(scanArtist.next()), scanArtist.next());
artists.add(artist);
} else {
System.out.println("Invalid Artist ID..." + scanArtist.next() + " at line " + lineNumber);
scanArtist.next();
}
}
//reads the contents of the art file
scanArt = new Scanner(artFile);
//skips the first line as it contains heading
scanArt.nextLine();
//writes the heading to the out file in the given format
writer.write(String.format("%-5s %-20s %-10s %-3s %10s", "ArtID","Title","Artist","ArtistID", "Appraised Value"));
//reads each line and gets the artist name of the artist ID from the artists Array List and writes to out file in the given format
while (scanArt.hasNextLine()) {
writer.write(System.getProperty("line.separator"));
String line=scanArt.nextLine();
String tokens[] = line.split("\s\s+");
int artistid = Integer.parseInt(tokens[2]);
String artistName = null;
for (Artist artist : artists) {
if (artist.artistID == artistid) {
artistName = artist.getArtistName();
}
}
writer.write(String.format("%-5s %-20s %-10s %-3s %10s", tokens[0],tokens[1],artistName,artistid, tokens[3]));
}
scanArtist.close();
scanArt.close();
writer.flush();
writer.close();
} catch (FileNotFoundException e) {
System.err.println("File not found");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* return the artistName
*
* @return
*/
public String getArtistName() {
return artistName;
}
/**
* set the artistName
*
* @param artistName
*/
public void setArtistName(String artistName) {
this.artistName = artistName;
}
/**
* return the artistId
*
* @return
*/
public int getArtistID() {
return artistID;
}
/**
* set the artistId
*
* @param artistId
*/
public void setArtistID(int artistId) {
this.artistID = artistId;
}
/**
* toString method
*/
public String toString() {
return +artistID + " " + artistName;
}
}
p1artists.txt
1 Acconci
2 Ames
g Aserty
4 Baron
5 Battenberg
50 Mogan
52 Novarre
12 Chico
p1arts.txt
ArtID Title ArtistID Appraised Value
1001 Red Rock Mountain 50 18000
1002 Offerings 52 10000
1003 Spring Flowers 12 2400
Sample output
p1arts_out.txt
ArtID Title Artist ArtistID Appraised Value
1001 Red Rock Mountain Mogan 50 18000
1002 Offerings Novarre 52 10000
1003 Spring Flowers Chico 12 2400