0% found this document useful (0 votes)
55 views4 pages

Codigo Do Robinho

This document defines a BankAccount class with account number and balance fields. It also defines SavingsAccount and CheckingAccount subclasses that extend BankAccount. The BankSystem class contains a main method that creates sample accounts, performs transactions, and handles exceptions when insufficient funds are encountered.

Uploaded by

Andrei Deon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views4 pages

Codigo Do Robinho

This document defines a BankAccount class with account number and balance fields. It also defines SavingsAccount and CheckingAccount subclasses that extend BankAccount. The BankSystem class contains a main method that creates sample accounts, performs transactions, and handles exceptions when insufficient funds are encountered.

Uploaded by

Andrei Deon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

import java.util.

ArrayList;
import java.util.List;

class BankAccount {
private String accountNumber;
private double balance;

public BankAccount(String accountNumber) {


this.accountNumber = accountNumber;
this.balance = 0.0;
}

public String getAccountNumber() {


return accountNumber;
}

public double getBalance() {


return balance;
}
class BankAccount {
private String accountNumber;
private double balance;

public BankAccount(String accountNumber) {


this.accountNumber = accountNumber;
this.balance = 0.0;
}
class BankAccount {
private String accountNumber;
private double balance;

public BankAccount(String accountNumber) {


this.accountNumber = accountNumber;
this.balance = 0.0;
}
class BankAccount {
private String accountNumber;
private double balance;

public BankAccount(String accountNumber) {


this.accountNumber = accountNumber;
this.balance = 0.0;
}
class BankAccount {
private String accountNumber;
private double balance;

public BankAccount(String accountNumber) {


this.accountNumber = accountNumber;
this.balance = 0.0;
}
class BankAccount {
private String accountNumber;
private double balance;

public BankAccount(String accountNumber) {


this.accountNumber = accountNumber;
this.balance = 0.0;
}
class BankAccount {
private String accountNumber;
private double balance;

public BankAccount(String accountNumber) {


this.accountNumber = accountNumber;
this.balance = 0.0;
}
class BankAccount {
private String accountNumber;
private double balance;

public BankAccount(String accountNumber) {


this.accountNumber = accountNumber;
this.balance = 0.0;
}
class BankAccount {
private String accountNumber;
private double balance;

public BankAccount(String accountNumber) {


this.accountNumber = accountNumber;
this.balance = 0.0;
}
class BankAccount {
private String accountNumber;
private double balance;

public BankAccount(String accountNumber) {


this.accountNumber = accountNumber;
this.balance = 0.0;
}
class BankAccount {
private String accountNumber;
private double balance;

public BankAccount(String accountNumber) {


this.accountNumber = accountNumber;
this.balance = 0.0;
}
class BankAccount {
private String accountNumber;
private double balance;

public BankAccount(String accountNumber) {


this.accountNumber = accountNumber;
this.balance = 0.0;
}

public void deposit(double amount) {


if (amount > 0) {
balance += amount;
System.out.println("Deposit: $" + amount);
} else {
System.out.println("Invalid deposit amount.");
}
}
public void withdraw(double amount) throws InsufficientFundsException {
if (amount > 0 && balance >= amount) {
balance -= amount;
System.out.println("Withdrawal: $" + amount);
} else {
throw new InsufficientFundsException("Insufficient funds for
withdrawal.");
}
}
}

class SavingsAccount extends BankAccount {


private double interestRate;

public SavingsAccount(String accountNumber, double interestRate) {


super(accountNumber);
this.interestRate = interestRate;
}

public double getInterestRate() {


return interestRate;
}

public void addInterest() {


double interest = getBalance() * interestRate / 100;
deposit(interest);
System.out.println("Interest added: $" + interest);
}
}

class CheckingAccount extends BankAccount {


private double overdraftLimit;

public CheckingAccount(String accountNumber, double overdraftLimit) {


super(accountNumber);
this.overdraftLimit = overdraftLimit;
}

public double getOverdraftLimit() {


return overdraftLimit;
}

@Override
public void withdraw(double amount) throws InsufficientFundsException {
if (amount > 0 && getBalance() + overdraftLimit >= amount) {
double overdraft = amount - getBalance();
if (overdraft > 0) {
System.out.println("Overdraft: $" + overdraft);
balance = 0.0;
throw new InsufficientFundsException("Insufficient funds for
withdrawal. Used overdraft.");
} else {
super.withdraw(amount);
}
} else {
throw new InsufficientFundsException("Insufficient funds for
withdrawal.");
}
}
}

class InsufficientFundsException extends Exception {


public InsufficientFundsException(String message) {
super(message);
}
}

public class BankSystem {


public static void main(String[] args) {
List<BankAccount> accounts = new ArrayList<>();

SavingsAccount savingsAccount = new SavingsAccount("SAV123", 2.5);


CheckingAccount checkingAccount = new CheckingAccount("CHK456", 1000);

accounts.add(savingsAccount);
accounts.add(checkingAccount);

savingsAccount.deposit(1000);
checkingAccount.deposit(500);

try {
savingsAccount.addInterest();

savingsAccount.withdraw(200);
savingsAccount.withdraw(2000); // This will throw an exception
} catch (InsufficientFundsException e) {
System.out.println("Exception: " + e.getMessage());
}

try {
checkingAccount.withdraw(1200);
checkingAccount.withdraw(2000); // This will throw an exception
} catch (InsufficientFundsException e) {
System.out.println("Exception: " + e.getMessage());
}

System.out.println("Final account balances:");


for (BankAccount account : accounts) {
System.out.println(account.getAccountNumber() + ": $" +
account.getBalance());
}
}
}

You might also like