I need help with this java program. I am having trouble understating arrays of o
ID: 3671791 • Letter: I
Question
I need help with this java program. I am having trouble understating arrays of objects. Can someone help me with this assignment?
Develop three classes called Bank, Account, and Customer to store account information and its customer information for a bank.
The Bank class can have a maximum of five accounts. For the openAccount()
method, your class should add a new account with the account number, accNum, and the corresponding information. Additionally, your method should put the customer information with the SSN number in the account object. If the account number is already taken by another object or maximum accounts (= five) are already used in the bank, your method should return false. Furthermore, if a customer with the SSN already has an account in the bank, your method should return false as well.
For the closeAccount() method, your class should delete the account with the account number,
accNum, from the bank. If the account number doesn’t exist in the bank, your method should
return false.
For the accountInfo() method, your class should display the account information with the corresponding customer information. If the account number doesn’t exist in the bank, your method should return false.
For the updateBalance() method, your class should change the balance with the new amount for the account number. If the account number doesn’t exist in the bank or the balance is a negative number, your method should return false.
For the updateAddress() method, your class should update the address of the customer with the
account number. If the account number doesn’t exist in the bank, your method should return
false.
For accType, “1” is checking and “2” is savings.
accountholder holds the customer object for the account.
One Account object can have only one account holder (= Customer).
Main class
public class BankDemo
{
public static void main(String[] args)
{
Bank csumbBank = new Bank("CSUMB");
System.out.println(" ========== Three New Accounts ==========");
csumbBank.openAccount("Tom Smith", "123 University Center 93955",
77777, 1000, 1, 10.0);
csumbBank.openAccount("Alice Smith", "123 University Center 93955",
88888, 2000, 1, 50.25);
csumbBank.openAccount("Joe Otter", "2440 Ocean Avenue 93900",
99999, 3000, 2, 100.25);
System.out.println(" ========== Bank Info ==========");
csumbBank.bankInfo();
System.out.println(" ========== Close Account ==========");
System.out.println(csumbBank.closeAccount(1000));
System.out.println(csumbBank.closeAccount(7000));
System.out.println("========== Account Info ==========");
csumbBank.accountInfo(2000);
System.out.println(csumbBank.accountInfo(7000));
}
}
Bank name: String accounts: Account D If necessary. add more variables + Bank(String) +openAccount (name: String, addr.String, SSnint, + closecAccount(acNumint): boolean + updatcAddress(accNumint, addr.String): boolean + bankInfo0: void If necessary, add more methods.Explanation / Answer
BankDemo.java
public class BankDemo{
public static void main(String[] args){
Bank csumbBank = new Bank("CSUMB");
System.out.println(" ========== Three New Accounts ==========");
csumbBank.openAccount("Tom Smith", "123 University Center 93955",
77777, 1000, 1, 10.0);
csumbBank.openAccount("Alice Smith", "123 University Center 93955",
88888, 2000, 1, 50.25);
csumbBank.openAccount("Joe Otter", "2440 Ocean Avenue 93900",
99999, 3000, 2, 100.25);
System.out.println(" ========== Bank Info ==========");
csumbBank.bankInfo();
System.out.println(" ========== Close Account ==========");
System.out.println(csumbBank.closeAccount(1000));
System.out.println(csumbBank.closeAccount(7000));
System.out.println("========== Account Info ==========");
csumbBank.accountInfo(2000);
System.out.println(csumbBank.accountInfo(7000));
}
}
Account.java
public class Account {
// Create class Variables
private int accNum;
private int accType;
private Customer accHolder;
private double balance;
public Account(int accNum, int accType, Customer accHolder, double balance) {
this.accNum = accNum;
this.accType = accType;
this.accHolder = accHolder;
this.balance = balance;
}
// Create setters and getters
public int getAccNumber(){
return accNum;
}
public int getAccType(){
return accType;
}
public Customer getCustomer(){
return accHolder;
}
public double getBalance(){
return balance;
}
public void setBalance(double balance){
this.balance = balance;
}
}
Customer.java
public class Customer {
// Create class variables
private String name;
private int ssn;
private String address;
public Customer(String name, String address, int ssn) {
this.name = name;
this.address = address;
this.ssn = ssn;
}
// Create getters and setters
public int getSSN(){
return ssn;
}
public String getName(){
return name;
}
public String getAddress(){
return address;
}
public void setAddress(String addr){
this.address = addr;
}
}
Bank.java
public class Bank {
// Create class variables
private static final int MAX_SIZE = 5;
private static final int CHECKING = 1;
private static final int SAVING = 2;
private Account[] accounts = new Account[MAX_SIZE];
private int currIndex = 0;
private int currSize = 0;
private String name;
public Bank(String name) {
this.name = name;
}
/**
* Creates an account in the bank if there still is room. If ssn or account number
* are found in the bank then it's just return false
*/
public boolean openAccount(String name, String addr, int ssn, int accNum,
int accType, double balance){
if(currSize == MAX_SIZE)
return false;
if(isAccountNumberAlreadyUsed(accNum) || isSSNAlreadyUsed(ssn))
return false;
Customer customer = new Customer(name, addr, ssn);
Account acc = new Account(accNum, accType, customer, balance);
// Find spot that is empty and assign it there
for(int i=0; i < accounts.length; i++){
if(accounts[i] == null){
accounts[i] = acc;
break;
}
}
currSize++;
return true;
}
/**
* Closses account number if it exists. Returns true if it was succesfull, false other wise
*/
public boolean closeAccount(int accNum){
// Account number doesn't exist
if (!isAccountNumberAlreadyUsed(accNum))
return false;
for (int i = 0; i < accounts.length; i++) {
if(accounts[i].getAccNumber() == accNum){
accounts[i] = null;
break;
}
}
return true;
}
/**
* Prints all information in the bank, includes Account and Customer information
*/
public boolean accountInfo(int accNum){
if (!isAccountNumberAlreadyUsed(accNum))
return false;
// get the balance from account
for(int i=0; i < accounts.length; i++)
if(accounts[i] != null && accounts[i].getAccNumber() == accNum){
double balance = accounts[i].getBalance();
Customer customer = accounts[i].getCustomer();
int accType = accounts[i].getAccType();
// Account Info
System.out.print("Account Info: ");
System.out.println("Account Number: " + accNum);
System.out.println(((accType == CHECKING)?" Checking":" Saving")+" account");
System.out.println(" Balance: $" + balance);
// Customer Info
System.out.println("Customer: " + customer.getName());
System.out.println(" " + customer.getAddress());
System.out.println(" SSN: " + customer.getSSN());
}
return true;
}
/**
* Sets new amount to an account number. Returns true if succesful, false other wise
*/
public boolean updateBalance(int accNum, double newAmount){
if(!isAccountNumberAlreadyUsed(accNum) || newAmount < 0)
return false;
for(int i=0; i < accounts.length; i++)
if(accounts[i] != null && accounts[i].getAccNumber() == accNum)
accounts[i].setBalance(newAmount);
return true;
}
/**
* Updates address for account number.Returns true if succesful, false other wise
*/
public boolean updateAddress(int accNum, String addr){
if(!isAccountNumberAlreadyUsed(accNum))
return false;
for(int i=0; i < accounts.length; i++)
if(accounts[i] != null && accounts[i].getAccNumber() == accNum)
accounts[i].getCustomer().setAddress(addr);
return true;
}
/**
* Prints all information in the bank. Including account and customer.
*/
public void bankInfo(){
System.out.println("Bank Name: " + name);
System.out.println("Number of Accounts: " + currSize);
double total = 0;
for (int i = 0; i < accounts.length; i++) {
if(accounts[i] != null){
int ssn = accounts[i].getCustomer().getSSN();
int accNum = accounts[i].getAccNumber();
int accType = accounts[i].getAccType();
double balance = accounts[i].getBalance();
String name = accounts[i].getCustomer().getName();
System.out.println(" " + accNum + ": $" + balance + " - " +
name + ": " + ssn);
total += balance;
}
}
System.out.println("Bank Total Balance: $" + total);
}
/**
* Checks if account number is alraedy in use. Returns true if it is, false if not
*/
public boolean isAccountNumberAlreadyUsed(int accNum){
for(int i=0; i < accounts.length; i++)
if(accounts[i] != null && accounts[i].getAccNumber() == accNum)
return true;
return false;
}
/**
* Checks if SSN is already in use. Returns true if it is, false if not
*/
public boolean isSSNAlreadyUsed(int ssn){
for(int i=0; i < accounts.length; i++)
if(accounts[i] != null && accounts[i].getCustomer().getSSN() == ssn)
return true;
return false;
}
}
output
========== Three New Accounts ==========
========== Bank Info ==========
Bank Name: CSUMB
Number of Accounts: 3
1000: $10.0 - Tom Smith: 77777
2000: $50.25 - Alice Smith: 88888
3000: $100.25 - Joe Otter: 99999
Bank Total Balance: $160.5
========== Close Account ==========
true
false
========== Account Info ==========
Account Info: Account Number: 2000
Checking account
Balance: $50.25
Customer: Alice Smith
123 University Center 93955
SSN: 88888
false