Parallel arrays and classes
I have a class account, with id, balance, and interest rate, which I can set using their set method.
In the last question of my homework, my teacher asked me to store the above data in parallel arrays, and then display only after an ID of 9999 is entered.
I know how parallel arrays work, but I'm not sure what's the best way of working with them & classes.
Should I create arrays in my main and store there? Or should I do it in the class (if so how) ?
My void main has this:
Code:
System.out.println("Enter an account ID");
MyAccount.setid(Input.nextInt());
while(MyAccount.getid() != 9999)
{
System.out.println("Enter an account balance");
MyAccount.setbalance(Input.nextDouble());
System.out.println("Enter an annual interest rate");
MyAccount.setannualInterestRate(Input.nextDouble());
System.out.println("Enter an account ID");
MyAccount.setid(Input.nextInt());
}
And my class Account:
Code:
import java.util.Date;
public class Account
{
private Date dateCreated = new Date();
private int id = 0;
private double balance = 0;
private double annualInterestRate = 0;
Account()
{
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
this.dateCreated = dateCreated;
}
Account(int id, double balance, double annualInterestRate)
{
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
}
int getid()
{
return id;
}
double getbalance()
{
return balance;
}
double getannualInterestRate()
{
return annualInterestRate;
}
void setid(int id)
{
this.id = id;
}
void setbalance(double balance)
{
this.balance = balance;
}
void setannualInterestRate(double annualInterestRate)
{
this.annualInterestRate = annualInterestRate;
}
Date getdateCreated()
{
return dateCreated;
}
void withdraw(double amount)
{
balance = balance - amount;
}
void deposit(double amount)
{
balance = balance + amount;
}
double getMonthlyInterestRate()
{
double monthly;
monthly = annualInterestRate / 12;
return monthly;
}
}
Please help!
Re: Parallel arrays and classes
Quote:
what's the best way of working with them & classes.
Are you asking how to get rid of the parallel arrays and move the data into a class? This will simplify your code and make it safer for many operations, like sorting.
I don't see any arrays in the code you posted. What does your posted code have to do with arrays?
Re: Parallel arrays and classes
I did it by changing the variables to arrays in the string and editing the rest of the code accordingly.
My question wasn't clear enough, sorry about that.
Re: Parallel arrays and classes
Your posted code looks inefficient. It appears to have created a MyAccount object with an invalid id and then exits the loop.
It would be better to test the id's value before creating the object with the invalid id.
I'm assuming that an id of 9999 is invalid.