C
C#2y ago
GIGA BRAIN

❔ OOP Help

BankAccount.cs
namespace Classes;

public class BankAccount
{
// These are fields / properties / characteristics
public string Number { get; set; }
public string Owner { get; set; }
public decimal Balance {
get
{
decimal balance = 0;
foreach (var item in allTransactions)
{
balance += item.Amount;
}
return balance;
}
}

// private = it can only be accessed by the code in THIS class (BankAccount)
// static = this is shared by all of the BankAccount objects
private static int accountNumberSeed = 1234567890;

private List<Transaction> allTransactions = new List<Transaction>();

// This is a constructor to assign values / initialize objects of this class type
public BankAccount(string name, decimal initialBalance)
{
Number = accountNumberSeed.ToString();
accountNumberSeed++;

Owner = name;
// MakeDeposit(initialBalance, DateTime.Now, "Initial balance");
}

// These are methods
public void MakeDeposit(decimal amount, DateTime date, string note)
{
if (amount <= 0)
{
throw new ArgumentOutOfRangeException(nameof(amount), "Amount of deposit must be positive");
var deposit = new Transaction(amount, date, note);
allTransactions.Add(deposit);
}
}

public void MakeWithdrawal(decimal amount, DateTime date, string note)
{
if (amount <= 0)
{
throw new ArgumentOutOfRangeException(nameof(amount), "Amount of withdrawal must be positive");
}
if (Balance - amount < 0)
{
throw new InvalidOperationException("Not enough sufficient funds for this withdrawal");
}
var withdrawal = new Transaction(-amount, date, note);
allTransactions.Add(withdrawal);
}
}
namespace Classes;

public class BankAccount
{
// These are fields / properties / characteristics
public string Number { get; set; }
public string Owner { get; set; }
public decimal Balance {
get
{
decimal balance = 0;
foreach (var item in allTransactions)
{
balance += item.Amount;
}
return balance;
}
}

// private = it can only be accessed by the code in THIS class (BankAccount)
// static = this is shared by all of the BankAccount objects
private static int accountNumberSeed = 1234567890;

private List<Transaction> allTransactions = new List<Transaction>();

// This is a constructor to assign values / initialize objects of this class type
public BankAccount(string name, decimal initialBalance)
{
Number = accountNumberSeed.ToString();
accountNumberSeed++;

Owner = name;
// MakeDeposit(initialBalance, DateTime.Now, "Initial balance");
}

// These are methods
public void MakeDeposit(decimal amount, DateTime date, string note)
{
if (amount <= 0)
{
throw new ArgumentOutOfRangeException(nameof(amount), "Amount of deposit must be positive");
var deposit = new Transaction(amount, date, note);
allTransactions.Add(deposit);
}
}

public void MakeWithdrawal(decimal amount, DateTime date, string note)
{
if (amount <= 0)
{
throw new ArgumentOutOfRangeException(nameof(amount), "Amount of withdrawal must be positive");
}
if (Balance - amount < 0)
{
throw new InvalidOperationException("Not enough sufficient funds for this withdrawal");
}
var withdrawal = new Transaction(-amount, date, note);
allTransactions.Add(withdrawal);
}
}
54 Replies
GIGA BRAIN
GIGA BRAINOP2y ago
Hi I'm following this tutorial on OOP from Microsoft, but for some reason my initialBalance is not being updated with the correct value even though I have the right parameters for the function. I've been looking through the code but can't seem to figure out what is wrong. Can someone help me out please? Program.cs
using Classes;

var account = new BankAccount("Josh", 100000);
Console.WriteLine($"Account {account.Number} was created for {account.Owner} with a starting balance of ${account.Balance}.");

account.MakeWithdrawal(500, DateTime.Now, "Rent payment");
Console.WriteLine(account.Balance);
account.MakeDeposit(100, DateTime.Now, "Friend paid me back");
Console.WriteLine(account.Balance);
using Classes;

var account = new BankAccount("Josh", 100000);
Console.WriteLine($"Account {account.Number} was created for {account.Owner} with a starting balance of ${account.Balance}.");

account.MakeWithdrawal(500, DateTime.Now, "Rent payment");
Console.WriteLine(account.Balance);
account.MakeDeposit(100, DateTime.Now, "Friend paid me back");
Console.WriteLine(account.Balance);
Angius
Angius2y ago
I don't see you ever update initialBalance
GIGA BRAIN
GIGA BRAINOP2y ago
Transaction.cs
namespace Classes;

public class Transaction
{
// These are fields, or data elements to enforce validation or other rules
public decimal Amount { get; }
public DateTime Date { get; }
public string Notes { get; }

// This is a constructor to assign values / initialize objects of this class type
public Transaction(decimal amount, DateTime date, string note)
{
Amount = amount;
Date = date;
Notes = note;
}


}
namespace Classes;

public class Transaction
{
// These are fields, or data elements to enforce validation or other rules
public decimal Amount { get; }
public DateTime Date { get; }
public string Notes { get; }

// This is a constructor to assign values / initialize objects of this class type
public Transaction(decimal amount, DateTime date, string note)
{
Amount = amount;
Date = date;
Notes = note;
}


}
uhh let me look for that
Angius
Angius2y ago
If you want some value to be updated... you need to update it
GIGA BRAIN
GIGA BRAINOP2y ago
am I not updating it with the makedeposit function?
Angius
Angius2y ago
No Value types get passed by value, copied if you will Reference types get passed by reference, where updating one updates all $refvsvalue
MODiX
MODiX2y ago
Angius#1586
REPL Result: Success
void TryUpdate(int x)
{
x = 420;
}

var num = 69;
TryUpdate(num);
num
void TryUpdate(int x)
{
x = 420;
}

var num = 69;
TryUpdate(num);
num
Result: int
69
69
Compile: 475.810ms | Execution: 28.738ms | React with ❌ to remove this embed.
GIGA BRAIN
GIGA BRAINOP2y ago
that makes sense
MODiX
MODiX2y ago
Angius#1586
REPL Result: Success
class Foo { public int Val { get; set; } }

void TryUpdate(Foo f)
{
f.Val = 420;
}

var num = new Foo { Val = 69 };
TryUpdate(num);
num
class Foo { public int Val { get; set; } }

void TryUpdate(Foo f)
{
f.Val = 420;
}

var num = new Foo { Val = 69 };
TryUpdate(num);
num
Result: Foo
{
"val": 420
}
{
"val": 420
}
Compile: 501.797ms | Execution: 45.733ms | React with ❌ to remove this embed.
Angius
Angius2y ago
And for reference types it works
GIGA BRAIN
GIGA BRAINOP2y ago
i dont understand where i'm not updating initialBalance though
Angius
Angius2y ago
Well... you never update it
GIGA BRAIN
GIGA BRAINOP2y ago
I initialize it with var account = new BankAccount("Josh", 100000);
Angius
Angius2y ago
Yeah
GIGA BRAIN
GIGA BRAINOP2y ago
ima try and figure out where i need to update it
Angius
Angius2y ago
Using this constructor
public BankAccount(string name, decimal initialBalance)
{
Number = accountNumberSeed.ToString();
accountNumberSeed++;

Owner = name;
// MakeDeposit(initialBalance, DateTime.Now, "Initial balance");
}
public BankAccount(string name, decimal initialBalance)
{
Number = accountNumberSeed.ToString();
accountNumberSeed++;

Owner = name;
// MakeDeposit(initialBalance, DateTime.Now, "Initial balance");
}
GIGA BRAIN
GIGA BRAINOP2y ago
oh
Angius
Angius2y ago
The constructor sets the number and the owner Doesn't set the balance
GIGA BRAIN
GIGA BRAINOP2y ago
so I would do Balance = initialBalance right?
Angius
Angius2y ago
Yep
GIGA BRAIN
GIGA BRAINOP2y ago
but it doesn't work for some reason the tutorial had me delete that when i did this code:
Angius
Angius2y ago
But... Balance property doesn't have a setter
GIGA BRAIN
GIGA BRAINOP2y ago
i see how would i add the setter to it? the tutorial made the getter a method public decimal Balance { get { decimal balance = 0; foreach (var item in allTransactions) { balance += item.Amount; } return balance; } } i tried adding set; to the end but it wouldn't work
Angius
Angius2y ago
You seem to store the balance of the account in a kinda hidden way, where you keep the list of all transactions, and calculate balance based on that So you'd need to create a new deposit transaction But then we run into issues with your withdrawal and deposit methods, in that they never add the transaction to the list of transactions They just create a new transaction and peace out
GIGA BRAIN
GIGA BRAINOP2y ago
ok i'm trying to make sense of this i'll try and work on it some more
Angius
Angius2y ago
Sure thing
GIGA BRAIN
GIGA BRAINOP2y ago
alright so i kept looking at the microsoft tutorial and all my code was identical to it so I figured maybe it's for an older version of .net that isn't compatible with my version i'm. not sure if that's the case but anyways i decided to delete the get method inside of balance and i added a setter to it like you said and just had simple += and +- for the balance
GIGA BRAIN
GIGA BRAINOP2y ago
GIGA BRAIN
GIGA BRAINOP2y ago
the output works fine now, but i'm not sure how I would relate this to the list do you have any ideas on how I would add a calculation to a list?
Angius
Angius2y ago
Well, you're not using a list now The previous code stored a list of transactions
GIGA BRAIN
GIGA BRAINOP2y ago
facts
Angius
Angius2y ago
Some added money, some removed money Calculating the balance is as simple as iterating over that list and summing up the money added and removed Setting balance... is not done You never set it, since the balance is an outcome of calculating the transactions
GIGA BRAIN
GIGA BRAINOP2y ago
so to even get to iterating over the list in the first place, I would have to add entries to it that are negative (withdrawal) and positive (deposit)?
Angius
Angius2y ago
yep
GIGA BRAIN
GIGA BRAINOP2y ago
alright i'll work on that thanks for all the help btw
Angius
Angius2y ago
Which you seem to do with
allTransactions.Add(deposit);
allTransactions.Add(deposit);
and
allTransactions.Add(withdrawal);
allTransactions.Add(withdrawal);
GIGA BRAIN
GIGA BRAINOP2y ago
how would you know where to set up the functionality to calculate the balance? i think i have the adding to list functionality figured out, it just ended up being the same thing as the tutorial though lol
GIGA BRAIN
GIGA BRAINOP2y ago
GIGA BRAIN
GIGA BRAINOP2y ago
but like you said earlier the balance calculation was in a weird place
Angius
Angius2y ago
You have the functionality to calculate balance in the getter of Balance it seems
GIGA BRAIN
GIGA BRAINOP2y ago
yeah but didn't you say it was in a weird place?
Angius
Angius2y ago
Not really It's fine to have it in the getter
GIGA BRAIN
GIGA BRAINOP2y ago
so then it all comes back to the idea where my setter is messed up or me not updating the balance
Angius
Angius2y ago
You don't ever update balance You don't need the setter You store the values in the list of transactions
GIGA BRAIN
GIGA BRAINOP2y ago
Angius
Angius2y ago
Then, the balance getter calculates it on the fly
GIGA BRAIN
GIGA BRAINOP2y ago
so this isn't updating the balance?
Angius
Angius2y ago
If it doesn't have a setter, then no, it doesn't If you want to keep the tutorial's way of storing transactions and calculating balance on the fly, what you need to do, is add a new deposit transaction with the initialbalance value That's how you store an initial amount of money in the account
GIGA BRAIN
GIGA BRAINOP2y ago
im literally so dumb all the stuff you said makes sense now and the code works but i feel like its literally the same stuff i had before like in the tutorial
namespace Classes;

public class BankAccount
{
// These are fields / properties / characteristics
public string Number { get; }
public string Owner { get; set; }
public decimal Balance
{
get
{
decimal balance = 0;
foreach (var item in allTransactions)
{
balance += item.Amount;
}
return balance;
}
}

// private = it can only be accessed by the code in THIS class (BankAccount)
// static = this is shared by all of the BankAccount objects
private static int accountNumberSeed = 1234567890;

// This is a constructor to assign values / initialize objects of this class type
public BankAccount(string name, decimal initialBalance)
{
Number = accountNumberSeed.ToString();
accountNumberSeed++;
Owner = name;
MakeDeposit(initialBalance, DateTime.Now, "Initial balance");
}
private List<Transaction> allTransactions = new List<Transaction>();

// These are methods
public void MakeDeposit(decimal amount, DateTime date, string note)
{
var deposit = new Transaction(amount, date, note);
allTransactions.Add(deposit);
}

public void MakeWithdrawal(decimal amount, DateTime date, string note)
{
var withdrawal = new Transaction((amount * -1), date, note);
allTransactions.Add(withdrawal);
}
}
namespace Classes;

public class BankAccount
{
// These are fields / properties / characteristics
public string Number { get; }
public string Owner { get; set; }
public decimal Balance
{
get
{
decimal balance = 0;
foreach (var item in allTransactions)
{
balance += item.Amount;
}
return balance;
}
}

// private = it can only be accessed by the code in THIS class (BankAccount)
// static = this is shared by all of the BankAccount objects
private static int accountNumberSeed = 1234567890;

// This is a constructor to assign values / initialize objects of this class type
public BankAccount(string name, decimal initialBalance)
{
Number = accountNumberSeed.ToString();
accountNumberSeed++;
Owner = name;
MakeDeposit(initialBalance, DateTime.Now, "Initial balance");
}
private List<Transaction> allTransactions = new List<Transaction>();

// These are methods
public void MakeDeposit(decimal amount, DateTime date, string note)
{
var deposit = new Transaction(amount, date, note);
allTransactions.Add(deposit);
}

public void MakeWithdrawal(decimal amount, DateTime date, string note)
{
var withdrawal = new Transaction((amount * -1), date, note);
allTransactions.Add(withdrawal);
}
}
would you say this is good now?
Angius
Angius2y ago
Yeah, LGTM
GIGA BRAIN
GIGA BRAINOP2y ago
thank you very much 🙂
blinkbat
blinkbat2y ago
just preference but I'd keep my constructor at the top of the class
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server