Web Analytics Made Easy -
StatCounter Need help using an array of objects - CodingForum

Announcement

Collapse
No announcement yet.

Need help using an array of objects

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need help using an array of objects

    Hello everyone. I have a project due tomorrow and it is not working correctly. I am not getting any errors but the output isnt right. The main problem arises when I open 2 accounts and then deposit an amount. I think my array setup is incorrect. The main program is called Bank3 and there is also a class called BankAccount. I dont have any arrays declared in BankAccount and that might be a problem. Could anybody please tell me what to do to fix it? I would really appreciate help from you guys. Thanks a lot and here is my main program:

    Code:
    public class Bank3 {
        public static void main(String[] args) {
    	int numaccount = 0;
    	int currentacc=0;
    	String[] currentaccount = new String[100];
    	currentaccount[0] = "None selected";
    	String[] accountnumber = new String[100];
    	double[] accountbalance = new double[100];
    	BankAccount  yourAccount = new BankAccount();
    	while(true) {
    		System.out.println();
    		System.out.println("--------------------------------------------------------------");
    		System.out.println("| Commands:   o  -  Open account         c  -  Close account |");
    		System.out.println("|             d  -  Deposit              w  -  Withdraw      |");
    		System.out.println("|             s  -  Select account       q  -  Quit          |");
    		System.out.println("--------------------------------------------------------------");
    		System.out.println();
    		System.out.println("Current account :  " + currentaccount[currentacc] + "   Balance: $" 
    + accountbalance [currentacc]);
    		System.out.println();
    		SimpleIO.prompt("Enter command: ");
    		String command = SimpleIO.readLine();
    		System.out.println();
    	if(command.equalsIgnoreCase("o")) {
    		SimpleIO.prompt("Enter new account number: ");
    		String newaccnum = SimpleIO.readLine();
    		accountnumber [numaccount] = newaccnum;
    		currentaccount[numaccount] = accountnumber[numaccount];
    		SimpleIO.prompt("Enter initial balance: ");
    		String newaccbal = SimpleIO.readLine();
    		double newbal = Convert.toDouble(newaccbal);
    		yourAccount.deposit(newbal);
    		accountbalance [numaccount] = yourAccount.getbalance();
    		numaccount++;
    		currentacc = numaccount-1;
    		
    	}
    	else if(command.equalsIgnoreCase("c")) {
    		if(numaccount == 0)
    			System.out.println("Please select an account");
    		else {
    			currentaccount[numaccount] = "None selected";
    			numaccount--;
    		}
    		numaccount--;
    	}
    	else if(command.equalsIgnoreCase("d")) {
    		if(numaccount == 0)
    			System.out.println("Please select an account");
    		else {
    			SimpleIO.prompt("Enter amount to deposit: ");
    			String depos = SimpleIO.readLine();
    			double amount = Convert.toDouble(depos);
        			yourAccount.deposit(amount);
    			accountbalance [numaccount] = yourAccount.getbalance();
    		}
    	}
    	else if(command.equalsIgnoreCase("w")) {
    		if(numaccount == 0)
    			System.out.println("Please select an account");
    		else {
    			SimpleIO.prompt("Enter amount to withdraw: ");
    			String with = SimpleIO.readLine();
    			double amt = Convert.toDouble(with);                                         
        			yourAccount.withdraw(amt);                
    			accountbalance [numaccount] = yourAccount.getbalance();
    		}
    	}
    	else if(command.equalsIgnoreCase("s")) {
    		SimpleIO.prompt("Enter account number: ");
    		String search = SimpleIO.readLine();
    		int i;
    		for(i=0; i<numaccount; i++) {
    			accountnumber = yourAccount.getnumber();
    			if(accountnumber.equals(search))
    				break; }
    		if(i><numaccount)
    			currentaccount[numaccount] = accountnumber;
    	}
    	else if(command.equalsIgnoreCase("q")) {
    		break;
    	}
    	else {
    		System.out.println("Command was not recognized; please try again.");
    		System.out.println();
    	}
        }}
    }

    Here is the BankAccount class:

    Code:
    public class BankAccount {
        private 
        private String acctnumber;
        private double acctbalance;
        public BankAccount(String newaccnum, double newbal) {
    	acctnumber = newaccnum;
    	acctbalance = newbal;
    	acctbalance = Math.round(newbal * 100) / 100.00;
    	}
        public BankAccount() {
    	acctnumber = "None selected";
    	acctbalance = 0;
    	}
        public BankAccount(double amount) {
    	amount = 0;
    	}
    	public BankAccount(String newaccnum) {
    	acctnumber = newaccnum;
    	}
        public void deposit (double amount) {
    	acctbalance += amount;
    	}
        public void withdraw (double amount) {
    	acctbalance -= amount;
    	}
        public String getnumber (String newaccnum) {
    	return acctnumber;
        }
        public double getbalance () {
    	return acctbalance;
        }
    }

    One of the problems is that I am using parallel arrays. I have no idea how to use arrays of objects. So if anyone could tell me how to do it in this program. If you have any questions or want any clarifications just ask me. THanks a lot for helping me,

    Aun

  • #2
    My do better on the JAVA forum, you have posted on JavaScript forum
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

    Comment


    • #3
      oh sorry. im in a hurry so didnt even look. you guys can delete this thread. thanks vwphillips

      Comment

      Working...
      X