Account Class
public class Account {
Person per = new Person();
Person person;
String accountName;
double accountBalance;
private String accountNumber;
private static int numAccounts;
public Account(String accountName, double accountBalance, String accountNumber){
this.accountName = accountName;
this.accountBalance = 0;
this.accountNumber = per.getId();
numAccounts++;}
public Account(){
this.accountName = "";
this.accountBalance = 0;
this.accountNumber = per.getId();
;}
public String getaccountName() {
return accountName;}
public int getnumAccounts() {
return numAccounts;}
public double getaccountbalance(){
return accountBalance;}
public void withdrawal(double amount) {
accountBalance -= amount;}
//deposit method
public void deposit(double amount){
accountBalance += amount;}
//inquiry
public double inquiry(){
return inquiry();}
public String toString(){
return "Name: " + accountName +"\nBalance: " + accountBalance;
}
}
-----------------------------------------------------------------
AccountTest class
import javax.swing.JOptionPane;
public class AccountTest {
final static int SIZE = 10;
private static Account[] accountList = new Account[SIZE];
public static void main(String[] args) {
int count1 = 0;
while(count1 == 0){
Person per = new Person();
String names = JOptionPane.showInputDialog("Enter Name");
double balance = Double.parseDouble(JOptionPane.showInputDialog("Enter Balance"));
Account ac = new Account(names, balance, per.getId());
accountList[ac.getnumAccounts() - 1] = ac;
Account activeAC = new Account();
String name = JOptionPane.showInputDialog("Enter name to search ");
// search by name
int foundIndex = findAccount(name);
if (foundIndex < 0) {JOptionPane.showConfirmDialog(null,"Account was not found. Do you want to create new account?");}
// if yes go back to the beginning (creating account)
else {activeAC = accountList[foundIndex];JOptionPane.showMessageDialog(null, name+ " found at " + foundIndex);}
// deposit, withdraw.... options
// Ask for checking or saving
String depositorchecking = JOptionPane.showInputDialog("Checking or Saving?"+ "\nEnter 1 for checking. Enter 2 for saving");
if (depositorchecking.equalsIgnoreCase("1")) {
// deposit-----------------------------------
String amt = JOptionPane.showInputDialog("Enter amount to deposit");
double amount = Double.parseDouble(amt);
double dbalance = balance + amount;
if (amount < 0) {JOptionPane.showMessageDialog(null,"Wrong Input (negative amount)");}
else {JOptionPane.showMessageDialog(null, "$" + amount+ " has been deposited"+ "\nYour balance now is: " + "$ " + dbalance);}
// withdraw--------------------------------------
String amt1 = JOptionPane.showInputDialog("Enter amount to withdraw");
double amount1 = Double.parseDouble(amt1);
double wbalance = dbalance - amount1;
if (amount1 > dbalance) {JOptionPane.showMessageDialog(null,"Insufficient funds");}
else if (amount1 < 0) {JOptionPane.showMessageDialog(null, "Invalid Input");}
else {JOptionPane.showMessageDialog(null, "$" + amount1+ " has been withdrawn"+ "\nYour balance now is: " + "$ " + wbalance);}}
else if (depositorchecking.equalsIgnoreCase("2")) {
// deposit-----------------------------------
String amt = JOptionPane.showInputDialog("Enter amount to deposit");
double amount = Double.parseDouble(amt);
double dbalance = balance + amount;
if (amount > 0) {JOptionPane.showMessageDialog(null, "$" + amount+ " has been deposited"+ "\nYour balance now is: " + dbalance);}
else {JOptionPane.showMessageDialog(null, "Wrong Input");}}
else JOptionPane.showMessageDialog(null, "Wrong Input");
count1 = JOptionPane.showConfirmDialog(null, "Do you want to continue");}
JOptionPane.showMessageDialog(null, "Goodbye");}
private static int findAccount(String name) {
// TODO Auto-generated method stub
int i = 0;
while (i < accountList[0].getnumAccounts()) {
if (name.equals(accountList[i].getaccountName())) {
return i;
}
i++;
}
return -1;
}
}

New Topic/Question
Reply



MultiQuote




|