logo

Design and Testing of Bank Account and Loan Account Classes in Java

Design a BankAccount class with private variables for name, balance, and account number. Implement methods for getting and setting the variables, as well as depositing and withdrawing funds.

8 Pages1340 Words24 Views
   

Added on  2023-01-17

About This Document

This document provides a detailed explanation of the design and testing of Bank Account and Loan Account classes in Java. It includes the code for the classes, along with explanations of the methods and their functionalities. The document also includes screenshots of the code and references for further reading.

Design and Testing of Bank Account and Loan Account Classes in Java

Design a BankAccount class with private variables for name, balance, and account number. Implement methods for getting and setting the variables, as well as depositing and withdrawing funds.

   Added on 2023-01-17

ShareRelated Documents
Q1
Requirements
Design
Required Private Variables: 1. Name, 2. Balance, 3. Account Number. Initialise the Balance to
0.
Constructor to take values String (Name) and Double (Account Number). Within the bounds
of the constructor set the variables Name and Account Number.
Create a GetName and GetAccountNumber and GetBalance Methods. These return the
values of the variables
Create a deposit Method. Take an inbound amount, ensure it is greater than 0, add to
balance. If a negative number is sent take no action
Create withdraw method. If the amount is less than the balance, subtract the amount from
the balance. In this way the balance should never go below 0, as the deposit can only be
positive and the withdrawl can never be more than the balance.If the withdraw amount is
greater than balance take no action
Create a toString method that returns a string in the format (Name): Balance = (X)
Testing
Bank Account Code
//Bank Account Assignment
//Alan Burke- 04417461
public class BankAccount {
private double accountNumber; //declare private variables
private double balance;
private String name;
public BankAccount(String name,double accountNumber) { //create
constructor to take input values
this.accountNumber = accountNumber; //set the variables equal the
inbound values, let balance = 0
this.name = name;
balance = 0;
}
Design and Testing of Bank Account and Loan Account Classes in Java_1
public double getAccountNumber() { //get accountnumber method
return accountNumber;
}
public double getBalance() { //get balance method
return balance;
}
public String getName() { //get the account name
return name;
}
public void withdraw(double amount) { //withdraw method, ensure the
amout is less than or equal to the balance
if (amount <= balance) {
balance = balance - amount;
}
}
public void deposit(double amount) { //deposit amount. Ensure the
amount is not a negative value
if (amount > 0) {
balance = balance + amount;
}
}
public String toString() { //display the information of the account
details
return String.format("%s:%s %.2f",
getName(), "balance =", getBalance());
}
//added for part 2
protected void setBalance(double balance) { //protected set balance
method. Allows for the balance to be a negative number
this.balance = balance;
}
}
Bank Account Test
public class BankAccountTest {
Design and Testing of Bank Account and Loan Account Classes in Java_2
public static void main(String[] args) {
BankAccount Patrick = new BankAccount("Patrick Dempsey", 123456 );
//send details to the bank account program for the following customers
BankAccount John = new BankAccount("John May", 345678);
Patrick.deposit(20000.00); //deposit the following cash amounts
John.deposit(100.00);
System.
out.println(Patrick.toString()); //Display the account information
System.
out.println(John.toString());
John.withdraw(35); //withdraw the following amounts
System.
out.println(John.toString()); //dispaly the account details
}
}
Design and Testing of Bank Account and Loan Account Classes in Java_3

End of preview

Want to access all the pages? Upload your documents or become a member.

Related Documents
OOPS Programming Concept: Bank Account Simulation Program in Java with Inheritance and Encapsulation
|10
|1366
|278

public class Employee {.
|2
|292
|253

This class defines a record which is specifically for storing in.
|2
|410
|256

Gadget, Mobile and MP3 Player Classes in Java
|16
|1551
|57

ASSIGNMENT ON SEMESTER – A
|28
|6556
|27