Create an ItemRecord class. This class must be serializable (implements serializ
ID: 3548365 • Letter: C
Question
Create an ItemRecord class. This class must be serializable (implements serializable).
The class will have 4 fields (the fields must be declared private!):
item number String
cost double
quantity int
description String
Create a constructor with 4 parameters (item number, cost, quantity, description).
Create get/set methods for all the fields.
Override the toString method to display a nicely formatted line with the values of all the fields. Use get methods to retrieve the values of the fields.
Use this format: Item Number: XXX Cost: X,XXX.XX Quantity: X,XXX Description: XXXXXXXXXXXXXXXXXXXXX
Create a text file with a minimum of 5 records. Name the file "ItemRecord_text.txt". Use NotePad to create the file.
Here is a sample data record: 123 13.75 100 Lumina Fluorescent High Lighters Remember to separate the "fields" with whitespace (space or tab)
Create an ItemRecordReport class.
The ItemRecordReport class should perform the following operations:
Create a Scanner object mapped to the input data file "ItemRecord_text.txt".
Create an ObjectOutputStream object mapped to a binary output file named "ItemRecord_binary.txt".
Your declaration should be similar to this: ObjectOutputStream binOutFile = new ObjectOutputStream( new FileOutputStream("ItemRecord_binary.txt") );
Create a loop that:
Reads a record from the text file
Stores the values read in an ItemRecord object
Writes the ItemRecord object to the binary file (writeObject method).
When all the text records have been read and stored as binary ItemRecord objects in the binary file, close both files.
Create an ObjectInputStream object mapped to the binary file "ItemRecord_binary.txt".
Your declaration should be similar to this: ObjectInputStream binInFile = new ObjectInputStream( new FileInputStream("ItemRecord_binary.txt") );
Create a loop that:
Reads an object from the binary file (using the readObject method) and stores it in an ItemRecord object. Remember you will need to do a cast.
Displays the state of the ItemRecord object using the object's toString method.
When all the objects have been read and displayed, close both files.
Create a new ItemRecord object and display it using the toString method.
Change the values of all the fields in the ItemRecord object using the object's set methods.
Display the modified ItemRecord object using the toString method
Explanation / Answer
Hi,
Please Find the programs below. All the java files and text files are constructed as requested.
ItemRecord_text.txt
123 13.75 100 LuminaFluorescentHighLighters1
124 13.76 101 LuminaFluorescentHighLighters2
125 13.76 102 LuminaFluorescentHighLighters3
126 13.77 103 LuminaFluorescentHighLighters4
127 13.78 104 LuminaFluorescentHighLighters5
ItemRecord.java
import java.io.Serializable;
public class ItemRecord implements Serializable {
String item_number;
double cost;
int quantity;
String description;
public ItemRecord() {
// TODO Auto-generated constructor stub
}
public ItemRecord(String item_number, double cost, int quantity,
String description) {
this.item_number = item_number;
this.cost = cost;
this.quantity = quantity;
this.description = description;
}
public String getItem_number() {
return item_number;
}
public void setItem_number(String item_number) {
this.item_number = item_number;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Item Number: " + this.getItem_number() + " Cost: "
+ this.getCost() + " Quantity: " + this.getQuantity()
+ " Description: " + this.getDescription();
}
}
ItemRecordReport.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ItemRecordReport {
public static void main(String[] args) {
ObjectInputStream binInFile = null;
try {
// The name of the file which we will read from
String filename = "ItemRecord_text.txt";
// Prepare to read from the file, using a Scanner object
File file = new File(filename);
Scanner in = new Scanner(file);
ObjectOutputStream binOutFile = new ObjectOutputStream(
new FileOutputStream("ItemRecord_binary.txt"));
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line = null;
while ((line = reader.readLine()) != null) {
String[] record = line.split("\s");
ItemRecord recorditem = new ItemRecord();
recorditem.setItem_number(record[0]);
recorditem.setCost(Double.parseDouble(record[1]));
recorditem.setQuantity(Integer.parseInt(record[2]));
recorditem.setDescription(record[3]);
binOutFile.writeObject(recorditem);
}
reader.close();
binOutFile.close();
binInFile = new ObjectInputStream(new FileInputStream(
"ItemRecord_binary.txt"));
ItemRecord record = new ItemRecord();
Object obj = binInFile.readObject();
System.out
.println("Displaying ItemRecord_binary.txt after deserializing");
while (obj != null) {
record = (ItemRecord) obj;
System.out.println(record.toString());
obj = binInFile.readObject();
}
binInFile.close();
} catch (Exception e) {
try {
binInFile.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
ItemRecord newRecord = new ItemRecord("128", 130, 45, "mobilephones");
System.out.println("Displaying new Item record");
System.out.println(newRecord.toString());
newRecord.setItem_number("129");
newRecord.setCost(50);
newRecord.setQuantity(49);
newRecord.setDescription("Tablets");
System.out.println("Displaying modified Item record");
System.out.println(newRecord.toString());
}
}
Output:
Displaying ItemRecord_binary.txt after deserializing
Item Number: 123 Cost: 13.75 Quantity: 100 Description: LuminaFluorescentHighLighters1
Item Number: 124 Cost: 13.76 Quantity: 101 Description: LuminaFluorescentHighLighters2
Item Number: 125 Cost: 13.76 Quantity: 102 Description: LuminaFluorescentHighLighters3
Item Number: 126 Cost: 13.77 Quantity: 103 Description: LuminaFluorescentHighLighters4
Item Number: 127 Cost: 13.78 Quantity: 104 Description: LuminaFluorescentHighLighters5
Displaying new Item record
Item Number: 128 Cost: 130.0 Quantity: 45 Description: mobilephones
Displaying modified Item record
Item Number: 129 Cost: 50.0 Quantity: 49 Description: Tablets
Place your Input file in the Parent directory.
Hope it helps.
Thanks,
Sri
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.