public static void main(String args[]) throws IOException {

    // Create a Scanner to read keyboard input.
    Scanner keyboard = new Scanner(System.in);

    // Ask user to enter annual interest rate
    System.out.print("Enter the savings account's " + "annual interest rate: ");
    double interestRate = keyboard.nextDouble();

    // close keyboard
    keyboard.close();

    DepositAndWithdrawalFiles depositAndWithdrawalFiles = new DepositAndWithdrawalFiles();
    SavingsAccount savingsAccount = depositAndWithdrawalFiles.new SavingsAccount(500, interestRate);

    // create a path to the deposit file
    Path depositPath =
        Paths.get("src/main/resources/com/levelup/java/exercises/beginner/Deposits.txt")
            .toAbsolutePath();

    // sum all lines in file and setting value in saving account
    double totalDeposits = Files.lines(depositPath).mapToDouble(Double::valueOf).sum();

    savingsAccount.deposit(totalDeposits);

    // create a path to the withdrawls file
    Path withdrawlsPath =
        Paths.get("src/main/resources/com/levelup/java/exercises/beginner/Withdrawls.txt")
            .toAbsolutePath();

    // sum all lines in file and setting value in saving account
    double totalWithdrawls = Files.lines(withdrawlsPath).mapToDouble(Double::valueOf).sum();

    savingsAccount.withdraw(totalWithdrawls);

    // Get the balance before adding interest.
    double priorBalance = savingsAccount.getAccountBalance();

    // Add the interest.
    savingsAccount.addInterest();

    // Get the interest earned.
    double interestEarned = savingsAccount.getLastAmountOfInterestEarned();

    // Create a DecimalFormat object for formatting output.
    DecimalFormat dollar = new DecimalFormat("#,##0.00");

    // Show user interest earned and balance.
    System.out.println("Interest earned: $" + dollar.format(interestEarned));

    System.out.println("Prior balance: $" + dollar.format(priorBalance));

    System.out.println("Ending balance: $" + dollar.format(savingsAccount.getAccountBalance()));
  }
Esempio n. 2
0
  public static void main(String[] args) {
    BankAccount ba1 = new ChequeAccount("Jack Black", "Hockey");
    ChequeAccount ca1 = new ChequeAccount("Chris Weidman", "Bunnies");
    ChequeAccount ca2 = new ChequeAccount("Ross Geller", "Dinosaurs");

    ba1.deposit(100);
    ((ChequeAccount) ba1).writeCheque(23);

    SavingsAccount sa1 = new SavingsAccount("David Letterman");
    SavingsAccount sa2 = new SavingsAccount("Jimmy Fallon");
    sa1.deposit(25);
    System.out.println("" + ba1 + ca1 + ca2 + sa1 + sa2);

    BalancePrinter bp = new BalancePrinter();
    bp.reportPrinter(ba1);
    bp.reportPrinter(ca1);
    bp.reportPrinter(ca2);
    bp.reportPrinter(sa1);
    bp.reportPrinter(sa2);
  } // main