public void withdraw(double amount) { if (uttakigjen > 0) { super.withdraw(amount); uttakigjen--; } else { throw new IllegalStateException("Ikke lov til å ta ut mer i år."); } }
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())); }