***** PROVIDED CODE ******* ReadAccountFile.java import java.nio.file.Paths; imp
ID: 3707361 • Letter: #
Question
***** PROVIDED CODE *******
ReadAccountFile.java
import java.nio.file.Paths;
import java.nio.file.Files;
import java.io.ObjectInputStream;
import java.io.EOFException;
import java.io.IOException;
public class ReadAccountFile
{
private static ObjectInputStream input;
public static void main( String[] args)
{
openFile();
readRecords();
closeFile();
}
public static void openFile()
{
try
{
input = new ObjectInputStream(
Files.newInputStream(
Paths.get( "Accounts.ser" ) ) );
}
catch ( IOException ioException )
{
System.err.println( "Error opening file." );
}
}
public static void readRecords()
{
AccountRecord record;
System.out.printf( "%-10s%-12s%-12s%10s ", "Account","First Name", "Last Name", "Balance" );
try
{
while ( true )
{
record = ( AccountRecord ) input.readObject();
System.out.printf( "%-10d%-12s%-12s%10.2f ",
record.getAccount(), record.getFirstName(),
record.getLastName(), record.getBalance() );
}
}
catch ( EOFException endOfFileException )
{
return;
}
catch ( ClassNotFoundException classNotFoundException )
{
System.err.println( "Unable to create object." );
}
catch ( IOException ioException )
{
System.err.println( "Error during reading from file." );
}
}
public static void closeFile()
{
try
{
if ( input != null )
input.close();
}
catch ( IOException ioException )
{
System.err.println( "Error closing file." );
System.exit( 1 );
}
}
}
UpdateAccountBalance.java
// UpdateAccountBalance
// Read a file of AccountRecord objects sequentially with ObjectInputStream,
// modify the account balance for each object, then output each to a new serialized file
// Import statements for Input/Output classes for handling serialized objects
// ADD necessary import statements here
// Import statements for necessary Exception classes
// ADD necessary Exception class import statements here
public class UpdateAccountBalance
{
oldAccts; // Initial declaration/reference of serialized input stream
newAccts; // Initial declaration/reference of serialized output stream
/*
"Mainline" of process
Invoke method to open all files needed in process
Invoke method to process input & create output
Invoke method to close all files used in process
** THERE ARE NO CHANGES TO BE MADE HERE **
*/
public static void main( String[] args)
{
try
{
openFiles();
processRecords();
closeFiles();
}
catch( Exception oops )
{
System.err.printf( "Something went wrong. You'll need to debug this and try again." );
}
}
// Open the input & output files, based on the initial declarations above
// Add code required to complete instantiation of stream objects and open associated files.
public static void openFiles( ) // ADD appropriate clause here for exception handling
{
try // open input and output files
{
"Accounts.ser"
"ModAccounts.ser"
}
catch ( ) // ADD Exception class and Exception object name
{
// ADD code to output appropriate message and rethrow the exception to main
// You do not need to CHAIN the exception, a rethrow will suffice
}
}
/*
Process records
Deserialize an AccountRecord from input
Modify the value of the balance instance variable
Serialize the updated AccountRecord to the output
Look for notes indicating where code must be added
*/
public static void processRecords( ) // ADD appropriate clause here for exception handling
{
// declare a 'holding' space for the DESERIALIZED objects
AccountRecord record;
// headings for screen output generated during update
System.out.println( "Updated Account Records" );
System.out.printf( "%-10s%-12s%-12s%10s ", "Account","First Name", "Last Name", "Balance" );
try
{
while ( true ) // essentially infinite loop; will be ended by EOFException DO NOT CHANGE
{
// ADD code to deserialize object
setAccount( getBalance( ) - 20 ); // COMPLETE code to update account balance
// display updated record contents DO NOT CHANGE
System.out.printf( "%-10d%-12s%-12s%10.2f%n",
record.getAccount( ),
record.getFirstName( ),
record.getLastName( ),
record.getBalance( ) );
// ADD code to serialize modified object
}
}
catch ( ) // ADD Exception class & Exception object name
{
// ADD code to PROPERLY TERMINATE WHILE LOOP
}
catch ( ) // ADD Exception class and Exception object name
{
// ADD code to output appropriate message and rethrow exception
}
catch ( ) // ADD Exception class & Exception object name
{
// ADD code to output appropriate message and rethrow exception
}
}
// Close the files used in process
public static void closeFiles()
{
try // close input file
{
if ( oldAccts != null )
// ADD code to close the "old" file here
if ( newAccts != null)
// ADD code to close the "new" file here
}
catch ( ) // ADD Exception class and Exception object name
{
System.err.println( "Error closing file." ); // NO OTHER CHANGES NEEDED HERE
// ADD statement to rethrow the exception.
}
}
}
AccountRecord.java
// AccountRecord class
// It implements the interface Serializable, allowing the object to be written out, or stored, as SERIALIZED OBJECTS.
// The interface also allows the reading in (or DESERIALIZATION) of those objects.
import java.io.Serializable;
public class AccountRecord implements Serializable
{
private int account;
private String firstName;
private String lastName;
private double balance;
public AccountRecord()
{
}
public AccountRecord( int acct,
String first,
String last,
double bal )
{
setAccount( acct );
setFirstName( first );
setLastName( last );
setBalance( bal );
}
public void setAccount( int acct )
{
account = acct;
}
public int getAccount()
{
return account;
}
public void setFirstName( String first )
{
firstName = first;
}
public String getFirstName()
{
return firstName;
}
public void setLastName( String last )
{
lastName = last;
}
public String getLastName()
{
return lastName;
}
public void setBalance( double bal )
{
balance = bal;
}
public double getBalance()
{
return balance;
}
public String toString()
{
String stuff = getFirstName() + " " + getLastName();
return stuff;
}
}
Explanation / Answer
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
public class UpdateAccountBalance {
private static ObjectInputStream oldAccts; // Initial declaration/reference
// of serialized input stream
private static ObjectOutputStream newAccts; // Initial declaration/reference
// of serialized output stream
/*
* "Mainline" of process
*
* Invoke method to open all files needed in process Invoke method to
* process input & create output Invoke method to close all files used in
* process
**
* THERE ARE NO CHANGES TO BE MADE HERE **
*/
public static void main(String[] args) {
try {
openFiles();
processRecords();
closeFiles();
}
catch (Exception oops) {
System.err.printf("Something went wrong. You'll need to debug this and try again.");
}
}
// Open the input & output files, based on the initial declarations above
// Add code required to complete instantiation of stream objects and open
// associated files.
public static void openFiles() // ADD appropriate clause here for exception
// handling
{
try {
oldAccts = new ObjectInputStream(Files.newInputStream(Paths.get("Accounts.ser")));
newAccts = new ObjectOutputStream(Files.newOutputStream(Paths.get("ModAccounts.ser")));
} catch ( IOException ioException )
{
System.err.println( "Error opening file." );
}
}
/*
* Process records Deserialize an AccountRecord from input Modify the value
* of the balance instance variable Serialize the updated AccountRecord to
* the output
*
* Look for notes indicating where code must be added
*
*/
public static void processRecords() // ADD appropriate clause here for
// exception handling
{
// declare a 'holding' space for the DESERIALIZED objects
AccountRecord record;
// headings for screen output generated during update
System.out.println("Updated Account Records");
System.out.printf("%-10s%-12s%-12s%10s ", "Account", "First Name", "Last Name", "Balance");
try {
while (true) // essentially infinite loop; will be ended by
// EOFException DO NOT CHANGE
{
// ADD code to deserialize object
record = (AccountRecord) oldAccts.readObject();
record.setBalance(record.getBalance() - 20); // COMPLETE code to
// update
// account
// balance
newAccts.writeObject(record);
// display updated record contents DO NOT CHANGE
System.out.printf("%-10d%-12s%-12s%10.2f%n", record.getAccount(), record.getFirstName(),
record.getLastName(), record.getBalance());
// ADD code to serialize modified object
}
}
catch ( EOFException endOfFileException )
{
return;
}
catch (ClassNotFoundException classNotFoundException) {
System.err.println( "Unable to create object." );
} catch (IOException ioException) {
System.err.println( "Error during reading from file." );
}
}
// Close the files used in process
public static void closeFiles() {
try // close input file
{
if (oldAccts != null) {
oldAccts.close();
}
// ADD code to close the "old" file here
if (newAccts != null) {
newAccts.close();
}
// ADD code to close the "new" file here
}
catch (IOException ioException) // ADD Exception class and Exception
// object name
{
System.err.println("Error closing file."); // NO OTHER CHANGES
// NEEDED HERE
// ADD statement to rethrow the exception.
}
}
}
/* Output
Updated Account Records
Account First Name Last Name Balance
100 Alan Jones 328.17
300 Mary Smith 7.19
500 Sam Sharp -20.00
700 Suzy Green -34.22
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.