Using Java Description: 1.) In this part, we are going to apply the concepts of
ID: 3874294 • Letter: U
Question
Using Java
Description:
1.) In this part, we are going to apply the concepts of Bag Interface to Online Shopping Cart. If you examine the Listing 1-2 program, you will find that the program is not complete and the type Item is missing.
2.)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. Therefore, to prepare for the future project, we need to get used to file I/O in Java.
3)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;
Assignment:
A) Refer to the sample program of Listing 1-2 on pages 41 and 42, instead of Item class, implement the Artist class 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.
ArtistID
Artist Name
1
Acconci
2
Ames
3
Aserty
4
Baron
5
Battenberg
B) Modify the above program according to the following:
1.) Read the input file “p1artists.txt” instead of hardcoding the values.
2.) Use the exception handler to ensure that artist ID is numeric.
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”.
C) Enhance the above program for the following:
1.) A tab-delimited text file named “p1arts.txt” contains the following record formats:
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.)Use an exception handler if the ArtistID does not exist in the “p1artists.txt” file. However, you still need to write this record to the output file.
4.)In the end, include the total number of the Art Works and Artists, and the total appraised value.
----------------------------
p1arts.txt
---------------------------------------------------------------------------------------------------------------------------
p1artist.txt
ArtistID
Artist Name
1
Acconci
2
Ames
3
Aserty
4
Baron
5
Battenberg
Explanation / Answer
Artist.java
public class Artist{
private int artistID;
private String artistName;
// Constructor
public Artist( int aID, String aName){
artistID=aID;
artistName=aName;
}
// Getters
public int getID(){
return artistID;
}
public String getArtistName(){
return artistName;
}
// Setters
public void setID(int aID){
artistID=aID;
}
public void setArtistName(String aName){
artistName=aName;
}
public String toString(){
return "Artist ID: " + artistID + " Artist Name: "+ artistName;
}
}
(A)
Main method to test part A
public static void main(String []args){
ArrayList<Artist> list=new ArrayList<Artist>();//Creating arraylist
int i = 0;
Artist a;
// Declare the object and initialize with
// predefined standard input object
Scanner sc = new Scanner(System.in);
for(i=0;i<6;i++){
System.out.print("Enter Artist ID: ");
int aID = sc.nextInt();
System.out.print("Enter Artist Name: ");
String name = sc.nextLine();
a = new Artist(aID,name);
list.add(a);
}
//Traversing list
for(Artist a1:list){
System.out.println(a1.toString());
}
}
(B) Main method for part 1,2,3 and 4
import java.util.Scanner;
import java.util.*;
import java.io.*;
public class HelloWorld{
public static void main(String []args){
ArrayList<Artist> list=new ArrayList<Artist>();
BufferedReader buf = null;
BufferedWriter bw = null;
FileWriter fw = null;
try{
buf = new BufferedReader(new FileReader("p1arts.txt"));
String lineJustFetched = null, aName = "";
String[] wordsArray;
int aID=0, lineNumber = 0;
Artist a;
// Read data from file
while(true){
lineJustFetched = buf.readLine();
if(lineJustFetched == null){
break;
}else{
wordsArray = lineJustFetched.split("\s");
lineNumber++;
// Convert data from file
try{
if(!"".equals(wordsArray[0])){
aID = Integer.parseInt(wordsArray[0]);
}
if(!"".equals(wordsArray[2])){
aName = wordsArray[2];
}
}catch(Exception e){ // Exception Handling
System.out.println("Error occured in line: "+lineNumber);
continue;
}
a = new Artist(aID,aName);
list.add(a);
}
}
buf.close();
}catch(Exception e){ // Exception Handling
e.printStackTrace();
}
// Write content to file
try {
fw = new FileWriter("p1artists_out1.txt");
bw = new BufferedWriter(fw);
for(Artist each : list){
bw.write(each.toString());
}
System.out.println("Done");
buf.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.