英文:
Why are my values not being updated and passed correctly?
问题
So, I'm initializing these instances and using the methods in the classes to modify the values. When I print the values within the method modifying them, they print correctly, but when they are passed to the "toString" method, they print the initial values instead of the modified values.
package com.meritamerica.assignment1;
public class AccountHolder {
/** Bank Account Information */
public String firstName, middleName, lastName, ssn;
public double checkingAccountOpeningBalance, savingsAccountOpeningBalance;
/** Default Constructor */
AccountHolder() {
}
/** Custom Constructor */
AccountHolder(String firstName,
String middleName,
String lastName,
String ssn,
double checkingAccountOpeningBalance,
double savingsAccountOpeningBalance) {
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.ssn = ssn;
this.checkingAccountOpeningBalance = checkingAccountOpeningBalance;
this.savingsAccountOpeningBalance = savingsAccountOpeningBalance;
}
/** Getters and Setters */
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getMiddleName() {
return this.middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getSSN() {
return this.ssn;
}
public void setSSN(String SSN) {
this.ssn = ssn;
}
public CheckingAccount getCheckingAccount() {
CheckingAccount temp = new CheckingAccount(checkingAccountOpeningBalance);
return temp;
}
public SavingsAccount getSavingsAccount() {
SavingsAccount temp = new SavingsAccount(savingsAccountOpeningBalance);
return temp;
}
/** Converts type to String */
@Override
public String toString() {
return "Name: " + this.firstName + " " + this.middleName + " " + this.lastName + "\r\n" +
"SSN: " + ssn + "\r\n" +
"Checking Account Balance: " + this.checkingAccountOpeningBalance + "\r\n" +
"Savings Account Balance " + this.savingsAccountOpeningBalance + "\r\n";
}
}
...
英文:
So, I'm initializing these instances and using the methods in the classes to modify the values. When I print the values within the method modifying them, they print correctly but when they are passed to the "toString" method they print the initial values instead of the modified values.
package com.meritamerica.assignment1;
public class AccountHolder {
/** Bank Account Information */
public String firstName, middleName, lastName, ssn;
public double checkingAccountOpeningBalance, savingsAccountOpeningBalance;
/** Default Constructor */
AccountHolder(){
}
/** Custom Constructor */
AccountHolder
(String firstName,
String middleName,
String lastName,
String ssn,
double checkingAccountOpeningBalance,
double savingsAccountOpeningBalance)
{
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.ssn = ssn;
this.checkingAccountOpeningBalance = checkingAccountOpeningBalance;
this.savingsAccountOpeningBalance = savingsAccountOpeningBalance;
}
/** Getters and Setters */
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getMiddleName() {
return this.middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getSSN() {
return this.ssn;
}
public void setSSN(String SSN) {
this.ssn = ssn;
}
public CheckingAccount getCheckingAccount() {
CheckingAccount temp = new CheckingAccount(checkingAccountOpeningBalance);
return temp;
}
public SavingsAccount getSavingsAccount() {
SavingsAccount temp = new SavingsAccount(savingsAccountOpeningBalance);
return temp;
}
/** Converts type to String */
@Override
public String toString() {
return "Name: " + this.firstName + " " + this.middleName + " " + this.lastName + "\r\n" +
"SSN: " + ssn + "\r\n" +
"Checking Account Balance: " + this.checkingAccountOpeningBalance + "\r\n" +
"Savings Account Balance " + this.savingsAccountOpeningBalance + "\r\n";
}
}
package com.meritamerica.assignment1;
public class CheckingAccount {
public double openingBalance, interestRate, futureBalance;
CheckingAccount(
double openingBalance)
{
this.openingBalance = openingBalance;
this.interestRate = 0.0001;
}
public double getBalance() {
return this.openingBalance;
}
public double getInterestRate() {
return this.interestRate;
}
public boolean withdraw(double amount) {
if(amount < openingBalance && amount > 0) {
openingBalance -= amount;
return true;
}else {
System.out.println("Not enough money!!!");
return false;
}
}
public boolean deposit(double amount) {
if(amount > 0) {
openingBalance += amount;
return true;
}else {
System.out.println("Cannot deposit a negative amount");
return false;
}
}
public double futureValue(int years) {
futureBalance = (openingBalance * Math.pow(1.0 + interestRate, years));
return futureBalance;
}
@Override
public String toString() {
return "Checking Account Balance: " + getBalance() + "\r\n" +
"Checking Account Interest Rate: " + getInterestRate() + "\r\n" +
"Checking Account Balance in 3 years " + futureValue(3);
}
}
package com.meritamerica.assignment1;
public class SavingsAccount {
public double openingBalance, interestRate, futureBalance;
SavingsAccount(
double openingBalance)
{
this.openingBalance = openingBalance;
this.interestRate = 0.01;
}
public double getBalance() {
return this.openingBalance;
}
public double getInterestRate() {
return this.interestRate;
}
public boolean withdraw(double amount) {
if(amount < openingBalance && amount > 0) {
openingBalance -= amount;
return true;
}else {
System.out.println("Not enough money!!!");
return false;
}
}
public boolean deposit(double amount) {
if(amount > 0) {
openingBalance += amount;
return true;
}else {
System.out.println("Cannot deposit a negative amount");
return false;
}
}
public double futureValue(int years) {
futureBalance = (openingBalance * Math.pow(1.0 + interestRate, years));
return futureBalance;
}
@Override
public String toString() {
return "Checking Account Balance: " + this.openingBalance + "\r\n" +
"Checking Account Interest Rate: " + this.interestRate + "\r\n" +
"Checking Account Balance in 3 years " + this.futureBalance;
}
}
package com.meritamerica.assignment1;
public class MeritAmericaBankApp {
public static void main(String[] args) {
AccountHolder john = new AccountHolder
("John",
"James",
"Doe",
"123-45-6789",
100.0,
1000.0);
System.out.println(john);
john.getCheckingAccount().deposit(500.0);
john.getCheckingAccount().withdraw(800.0);
john.getCheckingAccount().futureValue(3);
System.out.println(john.getCheckingAccount());
System.out.println(john.getSavingsAccount());
答案1
得分: 5
每次调用 getCheckingAccount
和 getSavingsAccount
时,都会创建新的 CheckingAccount
和 SavingsAccount
对象。
在 AccountHolder
构造函数中创建 CheckingAccount
和 SavingsAccount
对象,并在后续使用它们。
CheckingAccount checkingAccount;
SavingsAccount savingsAccount;
AccountHolder (String firstName, String middleName, String lastName, String ssn,
double checkingAccountOpeningBalance,
double savingsAccountOpeningBalance) {
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.ssn = ssn;
this.checkingAccount = new CheckingAccount(checkingAccountOpeningBalance);
this.savingsAccount = new SavingsAccount(savingsAccountOpeningBalance);
}
public CheckingAccount getCheckingAccount() {
return checkingAccount;
}
public SavingsAccount getSavingsAccount() {
return savingsAccount;
}
英文:
You are creating a new CheckingAccount
and SavingsAccount
objects each time you call getCheckingAccount
and getSavingsAccount
.
Create a CheckingAccount
and SavingsAccount
object in the AccountHolder
constructor and use it.
CheckingAccount checkingAccount;
SavingsAccount savingsAccount;
AccountHolder (String firstName, String middleName, String lastName, String ssn,
double checkingAccountOpeningBalance,
double savingsAccountOpeningBalance) {
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.ssn = ssn;
this.checkingAccount = new CheckingAccount(checkingAccountOpeningBalance);
this.savingsAccount = new CheckingAccount(savingsAccountOpeningBalance);
}
public CheckingAccount getCheckingAccount() {
return checkingAccount;
}
public SavingsAccount getSavingsAccount() {
return savingsAccount;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论