+ 2

Encapsulation - changing the code

namespace SoloLearn { class BankAccount { private double balance=0; public void Deposit(double n) { balance += n; } public void Withdraw(double n) { balance -= n; } public double GetBalance() { return balance; } } class Program { static void Main(string[] args) { BankAccount b = new BankAccount(); b.Deposit(199); b.Withdraw(42); Console.WriteLine(b.GetBalance()); } } } ____ The code prints the value of the Balance. How do I print the Deposit and Withdraw values on the screen? //Credit: $199 // Debit: $42 //Balance: $157

11th Apr 2018, 12:44 PM
AsAlbu
AsAlbu - avatar
7 Respuestas
+ 2
If you're wanting to do it via the Main method, then the most logical means is to receive the deposit/withdraw amounts as input from the user, which would be the case in a bank application. Then just simply output their input. As well, you could also create getters in your class for it. Give me a moment and I'll type up example that depicts both ways. EXAMPLE (DISPLAY VIA MAIN): https://code.sololearn.com/c9hr3bwfWUFO/#cs namespace SoloLearn { class BankAccount { private double balance=0; public void Deposit(double n) { balance += n; } public void Withdraw(double n) { balance -= n; } public double GetBalance() { return balance; } } class Program { static void Main(string[] args) { double depositAmount = 0; double withdrawlAmount = 0; BankAccount b = new BankAccount(); // Get user input for Deposit amount and display depositAmount = Convert.ToDouble(Console.ReadLine()); b.Deposit(depositAmount); Console.WriteLine("Deposit Amount: " + depositAmount); // Get user input for Withdrawl amount and display withdrawlAmount = Convert.ToDouble(Console.ReadLine()); b.Withdraw(withdrawlAmount); Console.WriteLine("Withdrawl Amount: " + withdrawlAmount); Console.WriteLine("New Balance: " + b.GetBalance()); } } }
11th Apr 2018, 1:23 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 4
https://code.sololearn.com/cHgHlxAa1xOW/#cs EXAMPLE WITH CLASS GETTERS: namespace SoloLearn { class BankAccount { private double balance = 0; private double prevDeposit = 0; private double prevWithdrawl = 0; public void Deposit(double n) { this.balance += n; this.prevDeposit = n; } public void Withdraw(double n) { this.balance -= n; this.prevWithdrawl = n; } public double GetBalance() { return this.balance; } public double getDeposit() { return this.prevDeposit; } public double getWithdrawl() { return this.prevWithdrawl; } } class Program { static void Main(string[] args) { double depositAmount = 0; double withdrawlAmount = 0; BankAccount b = new BankAccount(); // Get user input for Deposit amount and display depositAmount = Convert.ToDouble(Console.ReadLine()); b.Deposit(depositAmount); Console.WriteLine("Deposit Amount: " + b.getDeposit()); // Get user input for Withdrawl amount and display withdrawlAmount = Convert.ToDouble(Console.ReadLine()); b.Withdraw(withdrawlAmount); Console.WriteLine("Withdrawl Amount: " + b.getWithdrawl()); Console.WriteLine("New Balance: " + b.GetBalance()); } } }
11th Apr 2018, 1:24 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 4
Andre A You cannot call private values without a getter. That is the whole point of encapsulation. 😸 try making the values public.
11th Apr 2018, 1:27 PM
Manual
Manual - avatar
+ 4
Andre A press the check next to Jakob Marley's answer.
11th Apr 2018, 1:29 PM
Manual
Manual - avatar
+ 3
Same way that you did it with the balance; use Console.WriteLine(). In your scenario, you could easily put it in the class methods. EXAMPLE: public void Deposit(double n) { balance += n; Console.WriteLine(n); } public void Withdraw(double n) { balance -= n; Console.WriteLine(n); }
11th Apr 2018, 12:53 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 2
I appreciate the answer Jakob And how can I do this in the Main method?
11th Apr 2018, 12:56 PM
AsAlbu
AsAlbu - avatar
+ 2
thank you so much
11th Apr 2018, 1:28 PM
AsAlbu
AsAlbu - avatar