How to prime multiple blank records and then add data to them

231 views Asked by At

I've been struggling trying to understand this area in java, so I figured perhaps someone online could help with it. Basically, I need to "prime" 9,000 blank records (for a bank) and then choose to enter data in specific areas (i.e. account number, name, balance).

I know I have to use for loops but I don't know where they should go. Currently, I can create 9,000 records, but after entering data for one record it just duplicates 9,0000 times, instead of putting the record in one specific spot and leaving the rest blank.

import java.nio.file.*;
import java.io.*;
import static java.nio.file.StandardOpenOption.*;
import java.util.Scanner;

public class WriteEmployeeFile
{
  public static void main(String[] args)
  {
    Scanner input = new Scanner(System.in);
    File myFile = new File ("C:\\Users\\USSI\\Desktop\\CustomerRecords.txt");
    Path file = Paths.get("C:\\Users\\USSI\\Desktop\\CustomerRecords.txt");
    String s = "000, ,00.00" +
    System.getProperty("line.separator");
    String delimiter = ",";
    int account;
    String name;
    double balance;
    final int QUIT = 999;
    final int MAX = 9001;

    try
    {
        OutputStream output = new
        BufferedOutputStream(Files.newOutputStream(file, CREATE));
        BufferedWriter writer = new
        BufferedWriter(new OutputStreamWriter(output));
        System.out.print("Enter client account number: ");
        account = input.nextInt();

        while(account != QUIT)
        {
            System.out.print("Enter last name for client #" +
            account + ": ");
            input.nextLine();
            name = input.nextLine();
            System.out.print("Enter account balance: ");
            balance = input.nextDouble();
            s = account + delimiter + name + delimiter + balance;

            for(int count = 0; count < MAX; ++count)
                writer.write(s, 0, s.length());

            writer.newLine();
            System.out.print("Enter next ID number or " +
            QUIT + " to quit: ");
            account = input.nextInt();
        }
        writer.close();
    }
    catch(Exception e)
    {
        System.out.println("Message: " + e);
    }
 }
}

EDIT: These are my assignment instructions (if someone was curious or if I didn't explain it clearly.

The Winter Park Bank maintains customer records in a random access file. Write an application that creates 9,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 is equal to the account number. Assume that the user will not enter invalid account numbers. Force each name to eight characters, 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 the file as WinterParkBankFile.java.

1

There are 1 answers

1
Attila Neparáczki On

Not sure what you want, but to give the idea of instantinating 9000 bank accounts and then access them by their place in the list they are put:

class BankAccount {
    int number;
    int balance;
    String name;

    public static void main(String[] args) {

        List<BankAccount> accounts = new ArrayList<BankAccount>();

        for (int i = 0; i < 9000; i++) {
            accounts.add(new BankAccount());
        }

        // fill these variables from console however you want
        int i = 0;
        int balanceReadFromConsole = 0;
        int numberReadFromConsole = 0;

        accounts.get(i).name = "Whatever you want to set as the i-th account's name";
        accounts.get(i).balance = balanceReadFromConsole;
        accounts.get(i).number = numberReadFromConsole;

    }
}