Пример #1
0
 @Override
 public void setBank(Bank b) {
   bankRef = b;
   if (bankRef != null) {
     bankName.setText(bankRef.getName());
     bankCode.setText(bankRef.getCode());
   }
 }
  /**
   * Entry point for program.
   *
   * @param args the user can provide a filename.txt to read/store banking info.
   */
  public static void main(String[] args) {
    theBank = null;
    try {
      try {
        in = new Scanner(System.in);

        // To be used for reading from and writing to file
        DataParser bankParser = new DataParser();

        String filename = null;
        String bankName = null;
        // Retrieve the filename from args if there is one supplied
        if (args.length > 0) {
          filename = args[0];
          // Reads in bank data from file
          bankParser.readData(filename);
          bankName = bankParser.getBankName();
          LinkedList<BankAccount> accounts = bankParser.getAccounts();
          LinkedList<BankCustomer> customers = bankParser.getCustomers();

          theBank = new Bank(bankName, accounts, customers);
        } else {
          // Asks user for bank name and filename
          System.out.println("Welcome! I'm a bank, but I forgot my name.");
          System.out.print("What's my name?: ");
          bankName = in.nextLine();

          boolean isValidFilename = false;
          while (!isValidFilename) {
            System.out.print(
                "Please provide a filename to store all banking data (filename.txt): ");
            filename = in.nextLine();

            // Checks that filename has extension txt
            String validFilename =
                ".+[.]txt"; // filename must contain at least 1 of any character followed by .txt
            if (filename.matches(validFilename)) {
              isValidFilename = true;
            }
          }
          theBank = new Bank(bankName);
        }

        System.out.println("\n\nWelcome to " + theBank.getName() + "!");

        // Displays a menu of available actions and asks user to choose an action
        boolean done = false;
        while (!done) {
          done = menu();
        }

        System.out.println("Have a splendid day!");

        // Write all banking data to file
        bankParser.writeData(
            filename,
            theBank.getName(),
            theBank.getTotalBalance(),
            theBank.getAllAccounts(),
            theBank.getAllCustomers());

      } finally {
        if (in != null) {
          in.close();
        }
      }
    } catch (FileNotFoundException e) {
      System.out.println("\nThe bank data file was not found.");
    } catch (BadDataException e) {
      System.out.println("\nThe data provided in the file is corrupt.");
    }
  }