![]() |
![]() OCAU News - Wiki - QuickLinks - Pix - Sponsors |
|
|||||||
| Notices |
|
Sign up for a free OCAU account and this ad will go away! Search our forums with Google: |
![]() |
|
|
Thread Tools |
|
|
#1 |
|
Member
Join Date: Aug 2003
Location: Adelaide
Posts: 751
|
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;
}
}
|
|
|
|
| Join OCAU to remove this ad! |
|
|
#2 | |
|
Member
Join Date: Jun 2001
Location: Sydney
Posts: 4,204
|
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(); Quote:
Code:
while(! (accountType.equals("C") || accountType.equals("S"))
|
|
|
|
|
|
|
#3 |
|
Member
Join Date: Jun 2001
Location: Sydney
Posts: 4,204
|
A few other tips:
Code:
.. if (account[i].accId == accId) .. 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. |
|
|
|
|
|
#4 |
|
Member
Join Date: Jun 2003
Posts: 168
|
Also, instead of the array, look at using Vectors - dont have to worry about sizes then, it will dynamically grow and shrink
__________________
Yar |
|
|
|
|
|
#5 |
|
Member
Join Date: Dec 2001
Location: Silicon Valley
Posts: 7,281
|
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'. |
|
|
|
|
|
#6 | |
|
Member
Join Date: Jan 2002
Location: Canberra
Posts: 152
|
Quote:
__________________
fantasia fills me with smiles |
|
|
|
|
|
|
#7 | |
|
Member
Join Date: Jun 2001
Posts: 305
|
Quote:
Constructors don't need a return type. |
|
|
|
|
|
|
#8 | |
|
Member
Join Date: Jan 2002
Location: Canberra
Posts: 152
|
Quote:
missed that...
__________________
fantasia fills me with smiles |
|
|
|
|
|
|
#9 |
|
Member
Join Date: Aug 2003
Location: Adelaide
Posts: 751
|
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 |
|
|
|
|
|
#10 |
|
Member
Join Date: Jan 2002
Location: Canberra
Posts: 152
|
where are you studying in adelaide?
__________________
fantasia fills me with smiles |
|
|
|
|
|
#11 | |
|
Member
Join Date: Aug 2003
Location: Adelaide
Posts: 751
|
Quote:
|
|
|
|
|
|
|
#12 |
|
Member
Join Date: Jan 2002
Location: Canberra
Posts: 152
|
what program and year?
__________________
fantasia fills me with smiles |
|
|
|
|
|
#13 | |
|
Member
Join Date: Aug 2003
Location: Adelaide
Posts: 751
|
Quote:
|
|
|
|
|
|
|
#14 | |
|
Member
Join Date: Aug 2003
Location: Adelaide
Posts: 751
|
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'); Quote:
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();
}
}
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;
}
}
|
|
|
|
|
|
|
#15 |
|
Member
Join Date: Nov 2003
Posts: 31
|
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'); Code:
account[account.Size()-1] = new Account(1, 'c'); |
|
|
|
![]() |
| Bookmarks |
|
Sign up for a free OCAU account and this ad will go away! |
| Thread Tools | |
|
|