



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
One of the most anticipated features of Java 8 is lambda expressions. These expressions provide a clear and concise way to represent one method interface using an expression. They are particularly useful for working with collections, as they enable you to iterate, filter, and extract data more efficiently.
Typology: Exercises
1 / 5
This page cannot be seen from the preview
Don't miss anything!




import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; class BankAccount { private String accountNumber; private String firstName, middleName, lastName, address, email; private double balance; public BankAccount(String firstName, String middleName, String lastName, String address, String email) { this.accountNumber = generateAccountNumber(); this.firstName = firstName; this.middleName = middleName; this.lastName = lastName; this.address = address; this.email = email; this.balance = 0; displayAccountInfo(); } private void displayAccountInfo() { System.out.println("\nAccount created successfully!"); System.out.println("Account Number: " + accountNumber + "\n"); } public String getAccountNumber() { return accountNumber; } public void deposit(double amount) { if (amount > 0) { balance += amount; saveTransaction("Deposit", amount); } } public void withdraw(double amount) { if (amount > 0 && balance - amount >= 500) { balance -= amount; saveTransaction("Withdrawal", amount); } else {
System.out.println("Invalid withdrawal amount."); } } public double balanceInquiry() { return balance; } public void printAccountSummary() { System.out.println("ACCOUNT SUMMARY\n"); System.out.println("Account Number: " + accountNumber); System.out.println("Name: " + firstName + " " + middleName + " " + lastName); System.out.println("Address: " + address); System.out.println("Email: " + email); System.out.println("Balance: PHP " + balance); System.out.println(); } private void saveTransaction(String transactionType, double amount) { try (FileWriter writer = new FileWriter("BankTransactions.txt", true)) { writer.write("TRANSACTION DETAILS\n"); writer.write("Account Number: " + accountNumber + "\n"); writer.write("Transaction Type: " + transactionType + "\n"); writer.write("Amount: PHP " + amount + "\n"); writer.write("\n"); } catch (IOException e) { e.printStackTrace(); } } private String generateAccountNumber() { int NewAccountNumber = 1; String formattedNumber = String.format("%010d" , NewAccountNumber); NewAccountNumber++; return formattedNumber; } } class BankAccountSimulation {
private static void displayMenu() { System.out.println("\nChoose an option:"); System.out.println("1. Deposit"); System.out.println("2. Withdraw"); System.out.println("3. Balance Inquiry"); System.out.println("4. Account Summary"); System.out.println("5. Create a New Account"); System.out.println("6. Exit"); System.out.print("Enter your choice (1-6): "); } private static BankAccount createAccount(Scanner scanner) { System.out.print("\nEnter your first name: "); String firstName = scanner.nextLine(); System.out.print("Enter your middle name: "); String middleName = scanner.nextLine(); System.out.print("Enter your last name: "); String lastName = scanner.nextLine(); System.out.print("Enter your address: "); String address = scanner.nextLine(); System.out.print("Enter your email address: "); String email = scanner.nextLine(); return new BankAccount(firstName, middleName, lastName, address, email); } private static void performTransaction(BankAccount account, String transactionType, Scanner scanner) { System.out.print("Enter the amount for " + transactionType + ": PHP "); double amount = scanner.nextDouble(); scanner.nextLine(); switch (transactionType) { case "Deposit": account.deposit(amount); break; case "Withdrawal": account.withdraw(amount); break; } }
private static void performBalanceInquiry(BankAccount account) { double balance = account.balanceInquiry(); System.out.println("Current Balance: PHP " + balance); } }