0% found this document useful (0 votes)
17 views

Lab6 ProgamacionII

The document describes exercises to create subclasses of a bank account class and generate reports on customer accounts. It involves creating savings and checking account subclasses, adding accounts to customer objects, and running batch processes. The exercises help students learn object-oriented programming concepts.

Uploaded by

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

Lab6 ProgamacionII

The document describes exercises to create subclasses of a bank account class and generate reports on customer accounts. It involves creating savings and checking account subclasses, adding accounts to customer objects, and running batch processes. The exercises help students learn object-oriented programming concepts.

Uploaded by

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

INF514. Lenguaje de Programación 2.

Exercise 1: Creating Bank Account Subclasses (Level 1)

“Task 1 – Modifying the Account Class

Código Fuente:

package com.mybank.domain;

public class Account {


protected double balance;

protected Account(double initBalance) {


balance = initBalance;
}

public void withdraw(double amount) {


if (amount > 0 && amount <= balance) {
balance -= amount;
} else {
System.out.println("Invalid withdrawal amount");
}
}

public void deposit(double amount) {


if (amount > 0) {
balance += amount;
} else {
System.out.println("Invalid deposit amount");
}
}

public double getBalance() {


return balance;
}
}

Task 2 – Creating the SavingsAccount Class


Código Fuente:

package com.mybank.domain;

public class SavingsAccount extends Account {


private double interestRate;

public SavingsAccount(double initBalance, double interestRate) {


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

Task 3 – Creating the CheckingAccount Class

Código Fuente:

package com.mybank.domain;

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);
}

@Override
public void withdraw(double amount) {
if (amount > 0) {
if (balance < amount) {
double overdraftNeeded = amount - balance;
if (overdraftAmount < overdraftNeeded) {
System.out.println("Insufficient funds, withdrawal not allowed.");
} else {
balance = 0.0;
overdraftAmount -= overdraftNeeded;
}
} else {
balance -= amount;
}
} else {
System.out.println("Invalid withdrawal amount.");
}
}
}

Task 4 – Deleting the Current TestBanking Class

Task 5 – Copying the TestBanking Class

Task 6 – Compiling the TestBanking Class


Task 7 – Running the TestBanking Program
Exercise 2: Creating a Heterogeneous Collection of Customer Accounts (Level 2)

Task 1 – Modifying the Customer Class

Código Fuente:

package com.mybank.domain;

public class Customer {

private String firstName;


private String lastName;
private Account[] accounts; // Array to hold multiple accounts
private int numberOfAccounts; // To keep track of the next accounts array index

// Constructor
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.accounts = new Account[10]; // Initialize the accounts array with a size of 10
this.numberOfAccounts = 0; // Initialize the numberOfAccounts to 0
}

// Getter methods
public String getFirstName() {
return firstName;
}

public String getLastName() {


return lastName;
}

public int getNumOfAccounts() {


return numberOfAccounts;
}

// Method to add an account to the accounts array


public void addAccount(Account account) {
if (numberOfAccounts < accounts.length) {
accounts[numberOfAccounts] = account;
numberOfAccounts++;
} else {
System.out.println("Cannot add more accounts. Limit reached.");
}
}

// Method to get an account based on the index parameter


public Account getAccount(int index) {
if (index >= 0 && index < numberOfAccounts) {
return accounts[index];
} else {
System.out.println("Invalid account index.");
return null;
}
}
}

Task 2 – Copying and Completing the CustomerReport Class


Código Fuente:

package com.mybank.report;

import com.mybank.domain.*;

public class CustomerReport {

private Bank bank;

public CustomerReport() {
}

public Bank getBank() {


return bank;
}

public void setBank(Bank bank) {


this.bank = bank;
}

public void generateReport() {

// Print report header


System.out.println("\t\t\tCUSTOMERS REPORT");
System.out.println("\t\t\t================");

// For each customer...


for (int cust_idx = 0; cust_idx < bank.getNumOfCustomers(); cust_idx++) {
Customer customer = bank.getCustomer(cust_idx);

// Print the customer's name


System.out.println();
System.out.println("Customer: " + customer.getLastName() + ", " + customer.getFirstName());

// For each account for this customer...


for (int acct_idx = 0; acct_idx < customer.getNumOfAccounts(); acct_idx++) {
Account account = customer.getAccount(acct_idx);
String account_type = "";

// Determine the account type


if (account instanceof SavingsAccount) {
account_type = "Savings Account";
} else if (account instanceof CheckingAccount) {
account_type = "Checking Account";
}

// Print the current balance of the account


System.out.println(account_type + ": $" + account.getBalance());
}
}
}
}

Task 3 – Copying the TestReport Class


Task 4 – Compiling the TestReport Class
Task 5 – Running the TestReport Program
Exercise 3: Creating a Batch Program (Advanced)

Task 1 – Modifying the SavingsAccount Class

Código Fuente:

package com.mybank.domain;

public class SavingsAccount extends Account {


private double interestRate;

public SavingsAccount(double initBalance, double interestRate) {


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

public void accumulateInterest() {


double interest = getBalance() * (interestRate / 12);
deposit(interest);
}
}

Task 2 – Creating the AccumulateSavingsBatch Class

Código Fuente:

package com.mybank.batch;

import com.mybank.domain.*;

public class AccumulateSavingsBatch {

private Bank bank;

// Add a setter method for the bank instance variable


public void setBank(Bank bank) {
this.bank = bank;
}

// Add a method to perform the batch operation


public void doBatch() {
// For each customer in the bank
for (int custIdx = 0; custIdx < bank.getNumOfCustomers(); custIdx++) {
Customer customer = bank.getCustomer(custIdx);
// For each account in the customer
for (int acctIdx = 0; acctIdx < customer.getNumOfAccounts(); acctIdx++) {
Account account = customer.getAccount(acctIdx);

// Check if the account is a SavingsAccount


if (account instanceof SavingsAccount) {
// Call the accumulateInterest method for SavingsAccount
SavingsAccount savingsAccount = (SavingsAccount) account;
savingsAccount.accumulateInterest();
}
}
}
}
}

Task 3 – Copying the TestBatch Class

Task 4 – Compiling the TestBatch Class


Task 5 – Running the TestBatch Program

You might also like