Code:
import java.text.DecimalFormat;
public class Account{
//class Variables
private static int NumAccounts = 0;
private static final double Interest = 0.05;
//Instant Variables
private long accNum;
private double balance;
DecimalFormat x = new DecimalFormat("$00,000.00");//Format Balance
public Account(long accN, double bal){//Constructor
accNum = accN;
balance = bal;
NumAccounts++;
}
public int GetNumAccounts(){
return NumAccounts;
}
public double getBalance(){
return balance;
}
public long getAccountNum(){
return accNum;
}
public void debit(double amount){
if(balance >= amount){
balance -= amount;
}
}
public void credit(double amount){
balance += amount;
}
public void setNumAccounts(int numAccounts){//deincrement accounts
numAccounts--;
}
public String toString(){
String Str;
Str ="Account Number :" + accNum + "\n"+
"Account Balance :" + x.format(balance) + "\n";
return Str;
}
}//End of class Account
import java.io.*;
import java.util.*;
public class TestAccount{
private static final int Max = 1000;
static Account[]accList= new Account[Max];
public static Account getAccount(Account z){//using binary search to find and return account if exist
int lo = 0;
int hi = accList.length - 1;
while(lo <= hi){
int mid = (lo + hi)/2;
int q = accList[mid].compareTo(z);
if(q==0){
return accList[mid];
}
if(q < 0){
mid = hi - 1;
}else{
mid = lo + 1;
}
}//end while
return null;
}//end getaccoutn;
public static void insertIntoArray(Account p)
{//Insert Objects into account
int numAccounts = Account.GetNumAccounts();
accList[numAccounts - 1] = p;
int lo = 0;
int hi = numAccounts -1;
for(int j = lo + 1; j <= hi; j++){
Account hold = accList[j];
int k = j - 1;
while (k >= 0 && hold.getAccountNum()< accList[k].getAccountNum()){
accList[k + 1] = accList[k];
--k;
}//End While
accList[k + 1] = hold;
}//End For
} //End Insertion Sort
public static void transaction(int accNum, double amount,int transType)
{
if (transType == 1){
accList[accNum].debit(amount);
}
if (transType == 2){
accList[accNum].credit(amount);
}
}//end transaction
public static void deleteAccount(long accNum){
int j;
for(int i = 0; j < Account.GetNumAccounts(); i++){
if(accList[j].getAccountNum()==Account.getAccountNum()){
Account.setNumAccount();
break;
if(i == Account.getNumAccounts()){
System.out.println("Print error message");
}
for(j = i; j < Account.getAccountNum(); j++){
accList[j] = accList[j + 1];
}
}//end if
}//end for
}//end function
public static void main(String[] args){
Account a = new Account(123456,5255.17);
insertIntoArray(a);
Account b = new Account(188468,19218.20);
insertIntoArray(b);
Account c = new Account(217794,1002.62);
insertIntoArray(c);
Account d = new Account(998283,20203.75);
insertIntoArray(d);
//Create Transactions
Account q = getAccount(a);
if(q!=null){
transaction(123456,1255.17,1);
}else{
System.out.println("Account does not exist");
}
transaction(188468,2781.80,2);
transaction(217794,2000.00,1);
transaction(998283,125.00,1);
for(int i = 0; i < 4; i++){
System.out.println(accList[i].toString());
}
}//end main
}//end class TestAccount
Question
First, I am to create an Account class (works ok) . Next, I am to test the Account class by creating a TestAccount class with the following methods:
getAccount(Account) //returns an Account from the array accList if the account exist(Using the binarySearch)
insertIntoArray(Account)//insert an object into accList using and Insertion sort to maintain order.
deleteAccount(accNum)return type void. Delete and account from the array if exist.
transaction(accNum, amount, transType) return type void. Receives an account from the array and perform debit(1) or credit(2).
printAccounts()// prints the accounts (works ok)
