Reputation: 1
myAccount[0] = new Calculations(firstName, lastName, pin);
myAccount[0] = new Calculations(totalBalance);
Hello! I've been practicing with array of objects, but I ran into a problem. I have 2 classes (Drive and Calculations). Both above lines of code are in the drive class, but in 2 different methods. In the first method I have the first line of code, and in the second method I have the second line of code. I'm trying to store all 4 in the first index, but I guess totalBalance overwrites the firstName, lastName, and pin which I suppose makes sense. So I guess the question is how do I store multiple values in the first object if I need to use separate methods? Also in case you're wondering why I don't just do it all in one method, it's because I want to display just the firstName, lastName, and pin in one panel, and then all 4 in another panel(firstName, lastName, and pin display null while totalBalance displays the the correct output which is why I figure the second line of code overwrites the first). Thanks in advance!
Upvotes: 0
Views: 55
Reputation: 9041
You should still instantiate a Calculations
object with all 4 parameters and just have two different display methods:
- One display method that displays just the firstName, secondName, and pin
- One display method that displays all 4 fields.
public static void main(String[] args) throws Exception {
Calculations[] myAccounts = new Calculations[5];
myAccounts[0] = new Calculations("John", "Doe", 1234, 100.00);
System.out.println("Summary");
System.out.println(myAccounts[0].DisplaySummary());
System.out.println("Full Info");
System.out.println(myAccounts[0].DisplayFull());
}
public static class Calculations {
String firstName = null;
String lastName = null;
int pin = 0;
double totalBalance = 0;
DecimalFormat formatter;
public Calculations(String fn, String ln, int p, double tb) {
firstName = fn;
lastName = ln;
pin = p;
totalBalance = tb;
formatter = new DecimalFormat("#,###.00");
}
public String DisplaySummary() {
return String.format("First Name: %s\r\nLast Name: %s\r\nPin: %d\r\n", firstName, lastName, pin);
}
public String DisplayFull() {
return DisplaySummary() + "Total Balance: $" + formatter.format(totalBalance);
}
}
Results:
Upvotes: 1
Reputation: 289
You can create setter and getter methods, that way you can safely set values properly.
Example:
public class Calculations {
String firstName = null;
String lastName = null;
int pin = 0;
int totalBalance = 0;
public Calculations(String fn, String ln, int p) {
firstName = fn;
lastName = ln;
pin = p;
}
public void setTotalBalance(int total) {
totalBalance = total;
}
public int getTotalBalance() {
return totalBalance;
}
}
After that you can just do:
myAccount[0] = new Calculations(firstName, lastName, pin);
myAccount[0].setTotalBalance(totalBalance);
Hope this helps.
Upvotes: 0
Reputation: 1438
To store multiple values in the first index or any index in an array you can have a multiple dimensional array. For example
myAccount[0][0] = new Calculations(firstName, lastName, pin);
myAccount[0][1] = new Calculations(totalBalance);
This would be initialized as Calculations myAccount[][] = new Calculations[amount][2];
Upvotes: 0