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

Announcement

Collapse
No announcement yet.

Java: Homework: Need help using an array of objects

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

  • Java: Homework: 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
    I think you need to work on working with objects in general before dealing with arrays of objects. You have done a pretty decent job so far of passing data back and forth to your BankAccount object but it could use some work. One thing I would point out though is that you should not create a BankAccount object until it has been requested.

    So as an example.

    BankAccount bankAccount;
    acctnumber = 12345; //obviously get this from the user
    balance = 0.00; // intially set the balance to zero.

    if (Open account requested) {
    bankAccount = new BankAccount(accntnumber, balance);
    }

    And to pass data to your bank account object you can do that a few ways. Either make some public data members available so you can set them like so

    bankAccount.acctnumber = acctnumber

    which is not necessarily the best way as you want to limit access to your data members within your objects. The other way would be to set up some more public methods to handle pulling that data into the object.

    bankAccount.setAcctNumber(acctnumber);

    In that way you can do stuff like validate the data within your object and control access to data within your object.
    Spookster
    CodingForum Supreme Overlord
    All Hail Spookster

    Comment


    • #3
      THanks a lot. You just saved my grade. Thank you so much. Its working now. I might have a couple of more problems coming up, but I hope I dont. THanks again.

      Comment

      Working...
      X