Public Class TestBanking
-
Upload
gerardo-gomez-garcia -
Category
Documents
-
view
103 -
download
0
Transcript of Public Class TestBanking
![Page 1: Public Class TestBanking](https://reader036.fdocuments.in/reader036/viewer/2022082915/544ac0c7b1af9feb3d8b4aaa/html5/thumbnails/1.jpg)
Programación Orientada a Objetos
public class TestBanking {
public static void main(String[] args) {
Customer customer;
Account account;
Bank.addCustomer("Diego", "Diaz");
customer = Bank.getCustomer(0);
customer.addAccount(new SavingsAccount(500.00, 0.05));
customer.addAccount(new CheckingAccount(200.00, 500.00));
Bank.addCustomer("Estefani", "Diaz");
customer = Bank.getCustomer(1);
customer.addAccount(new SavingsAccount(100.00, 0.02));
Bank.addCustomer("Leticia", "Garcia");
customer = Bank.getCustomer(2);
customer.addAccount(new SavingsAccount(600.00, 0.07));
Bank.addCustomer("Felipe", "Diaz");
customer = Bank.getCustomer(3);
customer.addAccount(new SavingsAccount(100.00, 0.03));
customer = Bank.getCustomer(0);
account = customer.getAccount(1);
System.out.println("Cliente [" + customer.getLastName()
+ ", " + customer.getFirstName() + "]"
DIAZ GARCIA DIEGO DANIEL ISC
![Page 2: Public Class TestBanking](https://reader036.fdocuments.in/reader036/viewer/2022082915/544ac0c7b1af9feb3d8b4aaa/html5/thumbnails/2.jpg)
Programación Orientada a Objetos
+ " has a checking balance of "
+ account.getBalance()
+ " with a 500.00 overdraft protection.");
try {
System.out.println("Checking Acct [Diego Diaz] : deposito 1400.00");
account.deposit(1400.00);
System.out.println("Checking Acct [Diego Diaz] : Retiro 200.00");
account.withdraw(200.00);
System.out.println("Checking Acct [Diego Diaz] : deposito 1700.50");
account.deposit(1700.50);
System.out.println("Checking Acct [Diego Diaz] : Retiro 125.62");
account.withdraw(125.62);
System.out.println("Checking Acct [Diego Diaz] : Retiro 600.00");
account.withdraw(600.00);
} catch (OverdraftException e1) {
System.out.println("Exception: " + e1.getMessage()
+ " Deficit: " + e1.getDeficit());
} finally {
System.out.println("Cliente [" + customer.getLastName()
+ ", " + customer.getFirstName() + "]"
+ " has a checking balance of "
+ account.getBalance());
}
System.out.println();
customer = Bank.getCustomer(1);
DIAZ GARCIA DIEGO DANIEL ISC
![Page 3: Public Class TestBanking](https://reader036.fdocuments.in/reader036/viewer/2022082915/544ac0c7b1af9feb3d8b4aaa/html5/thumbnails/3.jpg)
Programación Orientada a Objetos
account = customer.getAccount(0);
System.out.println("Cliente [" + customer.getLastName()
+ ", " + customer.getFirstName() + "]"
+ " has a savings balance of "
+ account.getBalance());
try {
System.out.println("Savings Acct [Estefani Diaz] : Retiro 450.00");
account.withdraw(450.00);
System.out.println("Savings Acct [Estefani Diaz] : deposito 79.00");
account.deposit(79.00);
System.out.println("Savings Acct [Estefani Diaz] : Retiro 2000.00");
account.withdraw(2000.00);
} catch (OverdraftException e1) {
System.out.println("Exception: " + e1.getMessage()
+ " Deficit: " + e1.getDeficit());
} finally {
System.out.println("Cliente [" + customer.getLastName()
+ ", " + customer.getFirstName() + "]"
+ " has a savings balance of "
+ account.getBalance());
}
System.out.println();
customer = Bank.getCustomer(2);
account = customer.getAccount(0);
System.out.println("Cliente [" + customer.getLastName()
DIAZ GARCIA DIEGO DANIEL ISC
![Page 4: Public Class TestBanking](https://reader036.fdocuments.in/reader036/viewer/2022082915/544ac0c7b1af9feb3d8b4aaa/html5/thumbnails/4.jpg)
Programación Orientada a Objetos
+ ", " + customer.getFirstName() + "]"
+ " has a savings balance of "
+ account.getBalance());
try {
System.out.println("Savings Acct [Leticia Garcia] : Retiro 100.00");
account.withdraw(100.00);
System.out.println("Savings Acct [Leticia Garcia] : deposito 255.00");
account.deposit(255.00);
System.out.println("Savings Acct [Leticia Garcia] : Retiro 1650.00");
account.withdraw(1650.00);
} catch (OverdraftException e1) {
System.out.println("Exception: " + e1.getMessage()
+ " Deficit: " + e1.getDeficit());
} finally {
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFirstName() + "]"
+ " has a savings balance of "
+ account.getBalance());
}
System.out.println();
customer = Bank.getCustomer(3);
account = customer.getAccount(0);
System.out.println("Cliente [" + customer.getLastName()
+ ", " + customer.getFirstName() + "]"
+ " has a savings balance of "
DIAZ GARCIA DIEGO DANIEL ISC
![Page 5: Public Class TestBanking](https://reader036.fdocuments.in/reader036/viewer/2022082915/544ac0c7b1af9feb3d8b4aaa/html5/thumbnails/5.jpg)
Programación Orientada a Objetos
+ account.getBalance());
try {
System.out.println("Savings Acct [Felipe Diaz] : deposito 15000.00");
account.deposit(15000.00);
System.out.println("Savings Acct [Felipe Diaz] : deposito 500.00");
account.deposit(500.00);
System.out.println("Savings Acct [Felipe Diaz] : Retiro 1000.00");
account.withdraw(1000.00);
System.out.println("Savings Acct [Felipe Diaz] : deposito 420.00");
account.deposit(420.00);
System.out.println("Savings Acct [Felipe Diaz] : Retiro 6000.00");
account.withdraw(6000.00);
System.out.println("Savings Acct [Felipe Diaz] : Retiro 8000.00");
account.withdraw(8000.00);
} catch (OverdraftException e1) {
System.out.println("Exception: " + e1.getMessage()
+ " Deficit: " + e1.getDeficit());
} finally {
System.out.println("Cliente [" + customer.getLastName()
+ ", " + customer.getFirstName() + "]"
+ " has a savings balance of "
+ account.getBalance());
}
}
}
DIAZ GARCIA DIEGO DANIEL ISC
![Page 6: Public Class TestBanking](https://reader036.fdocuments.in/reader036/viewer/2022082915/544ac0c7b1af9feb3d8b4aaa/html5/thumbnails/6.jpg)
Programación Orientada a Objetos
public class Bank
{
private static Customer[] customers = new Customer[10];
private static int numberOfCustomers = 0;
public Bank()
{
customers = new Customer[10];
numberOfCustomers = 0;
}
public static void addCustomer(String f, String l)
{
int i = numberOfCustomers++;
customers[i] = new Customer(f,l);
}
public static int getNumOfCustomers() {
return numberOfCustomers;
}
public static Customer getCustomer(int customer_index) {
return customers[customer_index];
}
}
DIAZ GARCIA DIEGO DANIEL ISC
![Page 7: Public Class TestBanking](https://reader036.fdocuments.in/reader036/viewer/2022082915/544ac0c7b1af9feb3d8b4aaa/html5/thumbnails/7.jpg)
Programación Orientada a Objetos
public class OverdraftException extends Exception {
private final double deficit;
public OverdraftException(String msg, double deficit) {
super(msg);
this.deficit = deficit;
}
public double getDeficit() {
return deficit;
}
}
DIAZ GARCIA DIEGO DANIEL ISC
![Page 8: Public Class TestBanking](https://reader036.fdocuments.in/reader036/viewer/2022082915/544ac0c7b1af9feb3d8b4aaa/html5/thumbnails/8.jpg)
Programación Orientada a Objetos
public class Customer
{
private String firstName;
private String lastName;
private Account[] accounts;
private int numberOfAccounts;
public Customer(String f, String l) {
firstName = f;
lastName = l;
accounts = new Account[10];
numberOfAccounts = 0;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public int getNumOfAccounts() {
return numberOfAccounts;
}
DIAZ GARCIA DIEGO DANIEL ISC
![Page 9: Public Class TestBanking](https://reader036.fdocuments.in/reader036/viewer/2022082915/544ac0c7b1af9feb3d8b4aaa/html5/thumbnails/9.jpg)
Programación Orientada a Objetos
public Account getAccount(int account_index) {
return accounts[account_index];
}
public void addAccount(Account acct) {
int i = numberOfAccounts++;
accounts[i] = acct;
}
}
DIAZ GARCIA DIEGO DANIEL ISC
![Page 10: Public Class TestBanking](https://reader036.fdocuments.in/reader036/viewer/2022082915/544ac0c7b1af9feb3d8b4aaa/html5/thumbnails/10.jpg)
Programación Orientada a Objetos
public class Account {
protected double balance;
protected Account(double initBalance) {
balance = initBalance;
}
public double getBalance() {
return balance;
}
public void deposit(double amt) {
balance = balance + amt;
}
public void withdraw(double amt) throws OverdraftException {
if ( amt <= balance ) {
balance = balance - amt;
} else {
throw new OverdraftException("Insufficient funds", amt - balance);
}
}
}
DIAZ GARCIA DIEGO DANIEL ISC
![Page 11: Public Class TestBanking](https://reader036.fdocuments.in/reader036/viewer/2022082915/544ac0c7b1af9feb3d8b4aaa/html5/thumbnails/11.jpg)
Programación Orientada a Objetos
public class CheckingAccount extends Account {
private double overdraftAmount;
public CheckingAccount(double initBalance, double overdraftAmount) {
super(initBalance);
this.overdraftAmount = overdraftAmount;
}
public CheckingAccount(double initBalance) {
this(initBalance, 0.0);
}
public void withdraw(double amount) throws OverdraftException {
if ( balance < amount ) {
double overdraftNeeded = amount - balance;
if ( overdraftAmount < overdraftNeeded ) {
throw new OverdraftException("Insufficient funds for overdraft protection",
overdraftNeeded);
} else {
balance = 0.0;
overdraftAmount -= overdraftNeeded;
DIAZ GARCIA DIEGO DANIEL ISC
![Page 12: Public Class TestBanking](https://reader036.fdocuments.in/reader036/viewer/2022082915/544ac0c7b1af9feb3d8b4aaa/html5/thumbnails/12.jpg)
Programación Orientada a Objetos
}
} else {
// Yes, there is enough balance to cover the amount
// Proceed as usual
balance = balance - amount;
}
}
}
DIAZ GARCIA DIEGO DANIEL ISC
![Page 13: Public Class TestBanking](https://reader036.fdocuments.in/reader036/viewer/2022082915/544ac0c7b1af9feb3d8b4aaa/html5/thumbnails/13.jpg)
Programación Orientada a Objetos
public class SavingsAccount extends Account
{
private double interestRate;
public SavingsAccount(double initBalance, double interestRate)
{
super(initBalance);
this.interestRate = interestRate;
}
}
DIAZ GARCIA DIEGO DANIEL ISC