Question
I need help with this problem. I posted this before and the information was helpful but I accidentally posted the wrong portion. So, here we
I need help with this problem. I posted this before and the information was helpful but I accidentally posted the wrong portion. So, here we are again.
The Rochester Bank maintains customer records in a random access file. Write an application that creates 10,000 blank records and then allows the user to enter customer account information, including an account number that is 9999 or less, a last name, and a balance. Insert each new record into a data file at a location that a last name, and a balance. Insert each new record into a data file at a location that is equal to the account number. Assume that the user will not enter invalid account numbers. Force each name to eight character, padding it with spaces or truncating it if necessary. Also assume that the user will not enter a bank balance greater than 99,000.00. Save file as CreateBankFile.java
Part 2;
Create an application that uses the file created by the user located above and allows the user to enter an account- number order. Save file as ReadBankAccountsSequentially.java
Here's what I have done:
CreateBankFile.java //Compiles and excutes just fine
import java.nio.file.*; import java.io.*; import java.nio.channels.FileChannel; import java.nio.ByteBuffer; import static java.nio.file.StandardOpenOption.*; import java.util.Scanner; import java.text.*; public class CreateBankFile { public static void main(String[] args) { Scanner input = new Scanner(System.in);
Path filename = Paths.get("AccountRecords.txt"); Path file = filename.toAbsolutePath();
final int NUMBER_OF_RECORDS = 10000;
final String ACCOUNT_NUMBER_FORMAT = "0000"; final String NAME_FORMAT = " "; final int NAME_LENGTH = NAME_FORMAT.length(); final String BALANCE_FORMAT = "00000.00"; final String delimiter = ",";
String defaultRecord = ACCOUNT_NUMBER_FORMAT + delimiter + NAME_FORMAT + delimiter + BALANCE_FORMAT + System.getProperty("line.separator"); final int RECORD_SIZE = defaultRecord.length();
FileChannel fc = null; String acctString; int acct; String name; double balance; byte[] data; ByteBuffer buffer; final String QUIT = "9999"; createEmptyFile(file, defaultRecord, NUMBER_OF_RECORDS); try { fc = (FileChannel)Files.newByteChannel(file, CREATE, WRITE); System.out.print("Enter 4-digit customer account number or " + QUIT + " >> "); acctString = input.nextLine(); while(!(acctString.equals(QUIT))) { acct = Integer.parseInt(acctString); System.out.print("Enter name for customer " + acctString + " >> "); name = input.nextLine(); StringBuilder sb = new StringBuilder(name); sb.setLength(NAME_LENGTH); name = sb.toString(); System.out.print("Enter balance " + acctString + " >> "); balance = input.nextDouble(); input.nextLine(); DecimalFormat df = new DecimalFormat(BALANCE_FORMAT); String s = acctString + delimiter + name + delimiter + df.format(balance) + System.getProperty("line.separator"); data = s.getBytes(); buffer = ByteBuffer.wrap(data); fc.position(acct * RECORD_SIZE); fc.write(buffer); System.out.print(" Enter account number or " + QUIT + " >> "); acctString = input.nextLine(); }
fc.close(); }
catch(Exception e) { System.out.println("Error message: " + e); }
input.close(); } public static void createEmptyFile(Path file, String s, int lines) { try { OutputStream outputStr = new BufferedOutputStream(Files.newOutputStream(file, CREATE)); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStr)); for(int count = 0; count < lines; ++count) writer.write(s, 0, s.length()); writer.close(); }
catch(Exception e) { System.out.println("Error message: " + e); } } }
ReadBankAccountsSequentially //I can not get this part to work
import java.nio.file.*; import java.io.*; import static java.nio.file.StandardOpenOption.*; public class ReadBankAccountsSequentially { public static void main(String[] args) { Path filename = Paths.get("AccountRecords.txt"); Path file = filename.toAbsolutePath();
final String delimiter = ",";
try { InputStream iStream = new BufferedInputStream(Files.newInputStream(file)); BufferedReader reader = new BufferedReader(new InputStreamReader(iStream));
String s = reader.readLine();
while(s != null) { String[] array = s.split(delimiter); if(!((array[1].trim()).isEmpty())) System.out.println("Acct #" + array[0] + "\tAcct Holder: " + array[1]+ "\tBalance: $" + array[2]);
s = reader.readLine(); } reader.close(); } catch(Exception e) { System.out.println("Error message: " + e); } } }
Any help would be appreciated.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started