I need help setting up this code for an assignment

I am just trying to get this work done, I need help with the first few steps of the code.
No description
11 Replies
AppleCyder (NovaSprite)
Here is what I have so far...
namespace SavingsAccount
{
public class SavingsAccount
{
private static double annualInterestRate;
private double savingsBalance;

System.String savingsAccountName
{
get => getSavingsAccountName;
set => setSavingsAccountName = value;
}

}
}
namespace SavingsAccount
{
public class SavingsAccount
{
private static double annualInterestRate;
private double savingsBalance;

System.String savingsAccountName
{
get => getSavingsAccountName;
set => setSavingsAccountName = value;
}

}
}
For now, I am focusing on completing: -Create SavingsAccount class with static variable annualInterestRate and private instance variables savingsBalance (double) and savingsAccountName (string). -Create a mutator method to set the savingsAccountName. Call this method setSavingsAccountName. The method should take a string argument and return void. -Create an accessor method to retrieve the savingsAccountName. Call this method getSavingsAccountName. The method should take no arguments and return a string (i.e. the savingsAccountName). How am I doing so far? It's bad I know but this is me attempting it without much knowledge of C#
Angius
Angius9mo ago
You have no mutator methods here Your code will not compile Whoever gave you the assignment is mentally stuck with 1999 Java, but it really doesn't seem like they want you to use properties Rather, they want you to create getter and setter methods Also, no need for System.String, just the string alias works
AppleCyder (NovaSprite)
A mutator method is just setter right?
Angius
Angius9mo ago
It's a method that mutates the state of the class Normally, in sane circumstances, you would use a property with a setter indeed But the assignment clearly states you need a method
AppleCyder (NovaSprite)
What would a mutator method look like then?
Angius
Angius9mo ago
A method That changes the value of the field There's nothing special about it Just a regular ol' method
AppleCyder (NovaSprite)
Ohh so it's those lines that have public static void at the top right?? and using
get => getSavingsAccountName;
set => setSavingsAccountName = value;
get => getSavingsAccountName;
set => setSavingsAccountName = value;
is never going to work is it? Is this better?
public double SavingsBalance
{
get => Return SavingsBalance;
set => SavingsBalance = value;
}
public double SavingsBalance
{
get => Return SavingsBalance;
set => SavingsBalance = value;
}
Jimmacle
Jimmacle9mo ago
think about what happens when you try to get the value of SavingsBalance here you're telling it to look at SavingsBalance, which needs to look at SavingsBalance, which needs to look at SavingsBalance... you need a backing field that actually stores the value to avoid infinite recursion you can either do that explicitly or if you don't have any special logic just use an auto-property like
public double SavingsBalance { get; set; }
public double SavingsBalance { get; set; }
AppleCyder (NovaSprite)
So like this...?
public double SavingsBalance
{
get => Return SavingsBalance;
set => SavingsBalance = value * CalculateMonthlyInterest (value);
}
public double SavingsBalance
{
get => Return SavingsBalance;
set => SavingsBalance = value * CalculateMonthlyInterest (value);
}
Angius
Angius9mo ago
That is still a property
AppleCyder (NovaSprite)
Oh ok thank you guys
Want results from more Discord servers?
Add your server