Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create an ItemRecord class. This class must be serializable (implements serializ

ID: 3761658 • 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

import java.io.Serializable;
public class ItemRecord implements Serializable
{
private String itemNumber;
private double cost;
private int quantity;
private String description;
public ItemRecord()
{
this("",0.0,0,"");
}
public ItemRecord(String number, double price, int quant, String desc)
{
setItemNumber(number);
setCost(price);
setQuantity(quant);
setDescription(desc);
}
public void setItemNumber(String number)
{
itemNumber = number;
}
public String getItemNumber()
{
return itemNumber;
}
public void setCost(double price)
{
cost = price;
}
public double getCost()
{
return cost;
}
public void setQuantity(int quant)
{
quantity = quant;
}
public int getQuantity()
{
return quantity;
}
public void setDescription(String desc)
{
description = desc;
}
public String getDescription()
{
return description;
}
public static void printHeading()
{
System.out.println("Item Cost Quantity Description");
System.out.print("Number");
System.out.println();
}
public String toString()
{
return this.getItemNumber()+String.format(" $%6.2f ",+this.getCost())+String.format("%4d ",+this.getQuantity())+this.getDescription();
}
}


import java.io.*;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Scanner;
public class ItemRecordReport
{
private ObjectOutputStream output;
private ObjectInputStream binInFile;//reads binary
private Scanner input;
public void openInputBinaryFile()
{
try
{
binInFile = new ObjectInputStream(new FileInputStream("ItemRecord_binary.txt"));
}
catch (IOException ioException)
{
System.out.println("Error opening file.");
}
}
public void openOutputBinaryFile()
{
try
{
output = new ObjectOutputStream(new FileOutputStream("ItemRecord_binary.txt"));
}
catch (IOException ioException)
{
System.err.println("Error opening file");
}
}
public void readInputTextRecords()
{
ItemRecord record;
String itemNumber;
double cost;
int quantity;
String description;
try
{
input = new Scanner(new File("ItemRecord_text.txt"));
}
catch (IOException e)
{
System.out.println("Error file not found");
}
while(input.hasNext())
{
try
{
itemNumber = input.next();
cost = input.nextDouble();
quantity = input.nextInt();
description = input.nextLine();
record = new ItemRecord(itemNumber,cost,quantity,description);
output.writeObject(record);
}
catch (IOException ioException)
{
System.out.println("Error writing to binary file.");
return;
}
}
}
public void readInputBinaryRecords()
{
openInputBinaryFile();
ItemRecord otherRecord;
try
{   
while (true)
{
otherRecord = (ItemRecord) binInFile.readObject();
System.out.println(otherRecord.toString());
}
}
catch (EOFException e)
{
return;
}
catch (ClassNotFoundException e)
{
System.err.println("Unable to create object.");
}
catch (IOException ioException)
{
System.err.println("Error reading from binary file.");
}
}
public void closeBinaryOutputFile()
{
try
{
if (output !=null )
output.close();
}
catch ( IOException ioException)
{
System.out.println( "Error closing file");
System.exit(1);
}
}
public void closeBinaryInputFile()
{
try
{
if (binInFile != null )
binInFile.close();
else
System.out.println("No records were written to the binary file!");
}
catch ( IOException ioException)
{
System.out.println( "Error closing file");
System.exit(1);
}
}
public static void main(String[] args)
{
ItemRecordReport object = new ItemRecordReport();
ItemRecord.printHeading();
object.openOutputBinaryFile();
object.readInputTextRecords();
object.closeBinaryOutputFile();
object.openInputBinaryFile();
object.readInputBinaryRecords();
object.closeBinaryInputFile();
Scanner inFile;
ItemRecord[] itemRecord = new ItemRecord[5];
itemRecord[x] = new ItemRecord();
for (int x = 0; x <= 4; x++)
{
itemRecord[x].setItemNumber("");
itemRecord[x].setCost(0);
itemRecord[x].setQuantity(0);
itemRecord[x].setDescription("");
}
try
{
inFile = new Scanner(new File("itemRecord.txt"));
while(inFile.hasNext())
{
for(int count =0; count<=4; count++)
{
itemRecord[count].setItemNumber(inFile.next());
itemRecord[count].setCost(inFile.nextDouble());
itemRecord[count].setQuantity(inFile.nextInt());
itemRecord[count].setDescription(inFile.next());
}
}
}
catch (IOException e)
{
System.out.println("Error file not found");
}
try
{
ObjectOutputStream binOutFile = new ObjectOutputStream(new FileOutputStream("ItemRecord_binary.txt"));
}
catch (IOException e)
{
System.err.println("error opening file");
}
System.out.println(itemRecord.toString());
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote