For Programmers: Free Programming Magazines  


Home > Archive > Java Help > October 2005 > Arrays of objects









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Arrays of objects
fb

2005-10-21, 7:03 pm

Hello, I am trying to create a new method called getAccountBalance()
that will loop through all the Transaction objects in the transactions
array, adding them if they are a “CR”, and subtracting them if they are
a “DR”. Then I return a double.

The problem is that I just can seem to access inside the objects that
are inside the array.

//The Transaction Class
public class Transaction{
//Data attributes
double amount;
String transType;
Date transDate;

//Constructors
Transaction(double amount, String transType, Date transDate){
this.amount = amount;
this.transType = transType;
this.transDate = transDate;
}
}


//The BankAccount Class
public class BankAccount{
//Data attributes
long accountNumber;
String accountType;
Transaction transArray[] = new Transaction[25];
private int nextElement = 0;


//Constructors
BankAccount(long accountNumber){
this.accountNumber = accountNumber;
accountType = "SAVINGS";

}

BankAccount(long accountNumber, String accountType){
this.accountNumber = accountNumber;
this.accountType = accountType;

}

//Methods Section
public String toString(){
String dispInfo;

dispInfo = ("Account Type: " + accountType +
"\n\nAccount Number: " + accountNumber +
"\n\nCurrent Balance: " );

return (dispInfo);
}

//Place Transaction object in array.
public void addTransaction(Transaction trans){
transArray[nextElement] = trans;
nextElement++;
}

//What thy giveth,
public void addCredit(double creditAmount){
Date curDate = new Date();
Transaction credit = new Transaction(creditAmount,"CR",curDate);
addTransaction(credit);
}

//Thy can taketh away
public void addDebit(double debitAmount){
Date curDate = new Date();
Transaction debit = new Transaction(debitAmount,"DR",curDate);
addTransaction(debit);
}

/************ Problem Here *****************/
public double getAccountBalance(){
int i;
double sum=0;
for(i=0;i<nextElement;i++){
if (transArray[i].amount.equals("CR"))
sum += transArray.amount;
else
sum -= transArray.amount;
}

return sum;
}
}
fb

2005-10-21, 7:03 pm

fb wrote:

> Hello, I am trying to create a new method called getAccountBalance()
> that will loop through all the Transaction objects in the transactions
> array, adding them if they are a “CR”, and subtracting them if they are
> a “DR”. Then I return a double.
>
> The problem is that I just can seem to access inside the objects that
> are inside the array.


<SNIP>

Sorry everyone...I forgot to cut out the less relevent methods...
My bad.
Oliver Wong

2005-10-21, 7:03 pm


"fb" <fb@noway.com> wrote in message news:sxd6f.254279$1i.173060@pd7tw2no...
> fb wrote:
>
>
> <SNIP>
>
> Sorry everyone...I forgot to cut out the less relevent methods...
> My bad.


Why don't you do that and repost the newly-trimmed code? Maybe some of
the people who decided not to help you because the code was too long will
help you once they see the shorter version.

- Oliver


Roedy Green

2005-10-21, 9:56 pm

On Fri, 21 Oct 2005 21:59:52 GMT, fb <fb@noway.com> wrote or quoted :

>Sorry everyone...I forgot to cut out the less relevent methods...
>My bad.


Not bad. Nearly always a newbie's problem is not where he thinks it
is, therefore he is no position to determine which methods are
relevant.

If there is any badness, it is not pruning the example down to a
simple complete program that still demonstrates the problem.

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
Mark Haase

2005-10-22, 9:57 pm

In article <sxd6f.254279$1i.173060@pd7tw2no>, fb <fb@noway.com> wrote:

> Sorry everyone...I forgot to cut out the less relevent methods...
> My bad.


You also didn't post the error you were getting or a stack trace, but I
know what your problem is.

Hint, look at the line where you're getting an error, and ask yourself
if an int can be equal to "CR".

In the future, don't just throw your hands up when you get an error.
Read it and try to figure out what it means. If you dont know what it
means, post a concise example here along with the error and ask what it
means.

If you learn all the common errors, the compiler will help you rather
than frustrate you.

--
|\/| /| |2 |<
mehaase(at)gmail(dot)com
fb

2005-10-23, 3:59 am

fb wrote:
> Hello, I am trying to create a new method called getAccountBalance()
> that will loop through all the Transaction objects in the transactions
> array, adding them if they are a “CR”, and subtracting them if they are
> a “DR”. Then I return a double.
>

<SNIP>

Thanks everyone, I've got everything working now. :-)
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com