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 String toString(){
String Str;
Str ="Account Number :" + accNum + "\n"+
"Account Balance :" + x.format(balance) + "\n";
return Str;
}
}//End of class Account
public class TestAccountxx{
private static final int Max = 1000;
static Account[]accList= new Account[Max];
static int u = 0;
public static int getAccount(int accNum){//using sequential search to find and return account if exist
for(int j = 0; j < accList.length; j++){
if(accList[j] != null) {
if(accList[j].getAccountNum() == accNum)
//return accList[j];
return j;
}
} // end for
return -1;
}
public static void insertIntoArray(Account p)
{//Insert Objects into account
accList[u] = p;
u++;
}
public static void deleteAccount(int accNum)//delete account from accList
{
int size = accList.length;
for(int i = accNum; i < size; i++){
accList[i] = accList[i + 1];
size--;
}
}
public static void transaction(int accNum, double amount,int transType)
{
if (transType == 1){
accList[accNum].debit(amount);
}
if (transType == 2){
accList[accNum].credit(amount);
}
}
public static void insertion_srt(){
int lo, hi;
lo = 0;
hi = accList.length;
for(int j = lo + 1; j <= hi; j++){
Account hold = accList[j];
int k = j - 1;
while(k >= 0 && hold.getAccountNum().compareTo(accList[k].getAccountNum())< 0){
accList[k+1] = accList[k];
--k;
}//End While
accList[k + 1] = hold;
}//End For
}//End Insertion Sort
public static void main(String[] args){
Account a = new Account(123456,5255.17);
insertIntoArray(a);
insertion_srt();
Account b = new Account(188468,19218.20);
insertion_srt();
insertIntoArray(b);
Account c = new Account(217794,1002.62);
insertIntoArray(c);
Account d = new Account(998283,20203.75);
insertIntoArray(d);
//Create Transactions
int x = getAccount(123456);
transaction(x,1255.17,1);
int y = getAccount(188468);
transaction(y,2781.80,2);
int z = getAccount(217794);
transaction(z,2000.00,1);
int n = getAccount(998283);
transaction(n,125.00,1);
int w = getAccount(998283);
deleteAccount(w);
int p = getAccount(123457);
if(p ==-1){
System.out.println("Account 123457 does not exist");
}else{
deleteAccount(p);
}
for(int i = 0; i < 3; i++){
System.out.println(accList[i].toString());
}
}//end main
}//end class TestAccount
