Overclockers Australia Forums
OCAU News - Wiki - QuickLinks - Pix - Sponsors  

Go Back   Overclockers Australia Forums > Software Topics > Programming & Software Development

Notices


Sign up for a free OCAU account and this ad will go away!
Search our forums with Google:
Reply
 
Thread Tools
Old 7th April 2004, 6:42 PM   #1
Dingod Thread Starter
Member
 
Dingod's Avatar
 
Join Date: Aug 2003
Location: Adelaide
Posts: 751
Default Java: creating objects in an array of objects

Hey guys,

Im coding doing my bank program and have progressed alot since last time I posted here but have again encoutered some problems. This time its down the bottem of the BankManager class where Im trying to create objects of the Account class and store them in the Accounts array created here... if someone can guide me on how to do that it would be much appreciated.

Also with the statement :

while (// accountType.equals("S") || accountType.equals("C"));

how do I make that so that its tests to see if the accountType does not equal "S" or "C"??



BankManager Class
Code:
// 

import java.io.*;
import java.util.*;


class 	BankManager extends Keyboard
 {
		Account [] account = new Account [50];
		String menuChoice, roomNum, capacity, roomType;
		boolean decision = true;
		String accIdString;
		int accId;
		String accountType;
	
		// put whatever fucking variables are meant to be in here
		
public void exe()
{
	do 
	{
		showMenu();
			
		System.out.print("/n>>>>> Please select : ");
		String menuChoiceInput;
		
		menuChoiceInput = readLine().toUpperCase();
		char menuChoice ;
		menuChoice	 = menuChoiceInput.charAt(0);
			
		switch (menuChoice)
		{
				case 'C':
			
			//	createAccount()
			
				case 'R':
			
				//Records a transaction
			
				case 'D':
			
				//Displays information of an account
			
				case 'A':
			
				//Display information of all accounts
			
				case 'Q': //quits the program
					decision = false;
					break;
				
		 		default:
		 			System.out.println ("/n ????? Incorrect command");
		 }
		}
		while (decision);
}


	
		
		public void displayAccInf()
		{
			
		}
		
		public void displayAccInfALL()
		{
			
			
		}
		
		
		public int findAcc(int accId)
		{
			int currentValue;
			int indexAccount=-1;
			for (int i=0; i<account.length; i++)
			{
							
				if (account[i].accId == accId)
					indexAccount=i;
			}		
			return indexAccount;
		
		}
		
		
	private void showMenu()
		{
			System.out.println ("/n ***************************");
			System.out.println ("C --- Create a new account");
			System.out.println ("R --- Record a transaction");
			System.out.println ("D --- Display information of an account");
			System.out.println ("A --- Display information of all accounts");
			System.out.println ("Q --- Quit the program");
		}
	
	
			
	private void createAccount()
	{
		System.out.print("/n>>>>> Please enter Account ID : ");
		accIdString=readLine();
		accId = Integer.valueOf(accIdString).intValue();
		findAcc(accId);
		
		
	
		if (accId == -1)
			System.out.println ("/n????? The room number has been used.");
			
		else{
			do{
				System.out.print ("/n>>>>> Please enter account type: ");
				AccountType=readLine().toUpperCase();}
		while (accountType.equals("S") || accountType.equals("C"));

		if (roomType.equals("C"))
	//		how do I create a new account object in the Account array here?
		
	}
}
}

Account Class:

Code:
public class Account
{
	int accId;
	char accType;
	int numTransactions=0;
	double balance = 100;
	Transaction [] transaction = new Transaction [50];
	
	public void Account (int accId, char accType)
	{
		this.accId = accId;
		
		this.accType = accType;
	}

		
}
Dingod is offline   Reply With Quote

Join OCAU to remove this ad!
Old 7th April 2004, 7:54 PM   #2
xsive
Member
 
xsive's Avatar
 
Join Date: Jun 2001
Location: Sydney
Posts: 4,204
Default

You need to check the number of created items in your array and then add the new object at the next position. You can achieve this by:

Code:
account[account.size()-1] = new Account();
This will create an account in the next available array position. If you solution involves deleting Accounts, you will need to pack the array each time an Account is removed.

Quote:
Also with the statement :

while (// accountType.equals("S") || accountType.equals("C"));

how do I make that so that its tests to see if the accountType does not equal "S" or "C"??
Try
Code:
while(! (accountType.equals("C") || accountType.equals("S"))
the ! represents NOT.
__________________
Quote:
Originally Posted by Lucifers Mentor View Post
Oooh! Oooh! Can I be evil and German? PPPPLLlleeeaassseeeeeeeeee??!!!???
xsive is offline   Reply With Quote
Old 7th April 2004, 8:01 PM   #3
xsive
Member
 
xsive's Avatar
 
Join Date: Jun 2001
Location: Sydney
Posts: 4,204
Default

A few other tips:

Code:
..
if (account[i].accId == accId)
..
Preceed your variable declarations in the Account class by the keyword "private". This will ensure the variables are not directly accessible by foreign classes.
No other class should have any business directly modifying or calling any other class' variables. Maintain encapsulation at all times. Its not only good OO design but it makes your code easier more legible and maintainable.
Create get and set accessor methods for each private variable to allow external modification (where appropriate).
These methods should be public.

Although its sometimes necessary to break encapsulation, 99% of the time you can avoid it by using good OO design principles.
__________________
Quote:
Originally Posted by Lucifers Mentor View Post
Oooh! Oooh! Can I be evil and German? PPPPLLlleeeaassseeeeeeeeee??!!!???
xsive is offline   Reply With Quote
Old 7th April 2004, 8:25 PM   #4
YarrKen
Member
 
Join Date: Jun 2003
Posts: 168
Default

Also, instead of the array, look at using Vectors - dont have to worry about sizes then, it will dynamically grow and shrink
__________________
Yar
YarrKen is offline   Reply With Quote
Old 8th April 2004, 11:38 AM   #5
Bangers
Member
 
Bangers's Avatar
 
Join Date: Dec 2001
Location: Silicon Valley
Posts: 7,281
Default

And your Constructor to Account class is wrong, you don't give it a return type. Just "public Account()"
__________________
There's a story about a golfer who sinks a 30-meter putt and someone says: 'Gee, that was lucky' and the golfer says, 'Yes, amazing how lucky you get when you practice 8 hours a day for 20 years'.
Bangers is offline   Reply With Quote
Old 8th April 2004, 7:46 PM   #6
nida
Member
 
nida's Avatar
 
Join Date: Jan 2002
Location: Canberra
Posts: 152
Default

Quote:
Originally posted by Bangers
And your Constructor to Account class is wrong, you don't give it a return type. Just "public Account()"
there is no return type. the information given in the brackets there are parameters necessary to create an account object. the account class is perfectly fine as far as i can see.
__________________
fantasia fills me with smiles
nida is offline   Reply With Quote
Old 8th April 2004, 8:19 PM   #7
PoyZ
Member
 
Join Date: Jun 2001
Posts: 305
Default

Quote:
public void Account (int accId, char accType)
Bangers meant that the void keyword is not required.

Constructors don't need a return type.
PoyZ is offline   Reply With Quote
Old 8th April 2004, 8:27 PM   #8
nida
Member
 
nida's Avatar
 
Join Date: Jan 2002
Location: Canberra
Posts: 152
Default

Quote:
Originally posted by PoyZ
Bangers meant that the void keyword is not required.

Constructors don't need a return type.
aah, *pulls away in shame*

missed that...
__________________
fantasia fills me with smiles
nida is offline   Reply With Quote
Old 8th April 2004, 8:38 PM   #9
Dingod Thread Starter
Member
 
Dingod's Avatar
 
Join Date: Aug 2003
Location: Adelaide
Posts: 751
Default

lol I just had a thought that if someone else is doing the same subject as me they could come here and steal my code

hahaha no where near finished anyway and its due 2morrow...

/me gets back to work
Dingod is offline   Reply With Quote
Old 8th April 2004, 9:51 PM   #10
nida
Member
 
nida's Avatar
 
Join Date: Jan 2002
Location: Canberra
Posts: 152
Default

where are you studying in adelaide?
__________________
fantasia fills me with smiles
nida is offline   Reply With Quote
Old 8th April 2004, 9:58 PM   #11
Dingod Thread Starter
Member
 
Dingod's Avatar
 
Join Date: Aug 2003
Location: Adelaide
Posts: 751
Default

Quote:
Originally posted by nida
where are you studying in adelaide?
Mawson Lakes and Magill, Uni Sa
Dingod is offline   Reply With Quote
Old 8th April 2004, 10:07 PM   #12
nida
Member
 
nida's Avatar
 
Join Date: Jan 2002
Location: Canberra
Posts: 152
Default

what program and year?
__________________
fantasia fills me with smiles
nida is offline   Reply With Quote
Old 9th April 2004, 12:05 AM   #13
Dingod Thread Starter
Member
 
Dingod's Avatar
 
Join Date: Aug 2003
Location: Adelaide
Posts: 751
Default

Quote:
Originally posted by nida
what program and year?
MBIC 2nd year.... why? plz dont stalk me
Dingod is offline   Reply With Quote
Old 9th April 2004, 12:10 AM   #14
Dingod Thread Starter
Member
 
Dingod's Avatar
 
Join Date: Aug 2003
Location: Adelaide
Posts: 751
Default

hrmm i've encountered yet another problem *sigh* some help please asap as its due in 2morrow but im probably doing to stay up 2night night until its done.

My problem is with this line
Code:
account[accountSize()] = new Account(1, 'c');
I've only got the 1 and 'c' in there for testing purposes... this is the error I get:

Quote:
symbol : constructor Account (int,char)
location: class Account
account[accountSize()] = new Account(1, 'c');
it seems right to me.. any tips/help/pointers will be much appreciated.

the account class is at the bottom

Bank Manager Class:
Code:
	private void createAccount()
	{
		System.out.print("/n>>>>> Please enter Account ID : ");
		accIdString=readLine();
		accId = Integer.valueOf(accIdString).intValue();
		findAcc(accId);
		
		
	
		if (accId == -1)
		{
			
				System.out.print ("/n>>>>> Please enter account type: ");
				accountType=readLine().toUpperCase();}
				accountTypeChar = accountType.charAt(0);
		
		if ((accountType.equals("S") || accountType.equals("C")))
		{
			account[accountSize()] = new Account(1, 'c');
			execute();
		}
		else
		{
			execute();
		}
	}
Account Class:

Code:
public class Account
{
	int accId;
	char accType;
	int numTransactions=0;
	double balance = 100;
	Transaction [] transaction = new Transaction [50];
	
	public void Account (int accId, char accType)
	{
		this.accId = accId;
		
		this.accType = accType;
	}


		
}
Dingod is offline   Reply With Quote
Old 9th April 2004, 12:35 AM   #15
Sprinkleh
Member
 
Sprinkleh's Avatar
 
Join Date: Nov 2003
Posts: 31
Default

This is what others have already said but you havent done it so..
in account class remove the void word in the constructor.. they dont return anything.

and for this line
Code:
account[accountSize()] = new Account(1, 'c');
make it
Code:
account[account.Size()-1] = new Account(1, 'c');
Sprinkleh is offline   Reply With Quote
Reply

Bookmarks

Sign up for a free OCAU account and this ad will go away!

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +10. The time now is 9:00 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd. -
OCAU is not responsible for the content of individual messages posted by others.
Other content copyright Overclockers Australia.
OCAU is hosted by Internode!