Ontwerp een consoletoepassing die een bankrekening simuleert. Maak hiervoor de klasse Bankrekening. Voorzie 2 methoden, namelijk Opname en Storting, en een property HuidigSaldo. Aan de gebruiker wordt telkens een bedrag gevraagd. Bij een negatief bedrag gebeurt een opname en bij een positief bedrag een storting. Wanneer de gebruiker de letter “S” invoert, wordt het saldo getoond. Bij invoer van de tekst “END” wordt het programma afgesloten.
Translated to English:
Design a console application that simulates a bank account. To do this, create the Bank account class. Provide 2 methods, namely Withdrawal and Deposit, and a CurrentBalance property. The user is always asked for an amount. A withdrawal is made with a negative amount and a deposit with a positive amount. When the user enters the letter “S”, the balance is shown. Entering the text “END” will close the program.

Tuhin PaulRank #7 · 71.9K reputationPosted Feb 14, 2023, 12:42 AM EST
Here's an example implementation of the console application as per the above statement:
class BankAccount { private double currentBalance; public BankAccount(double initialBalance = 0) { currentBalance = initialBalance; } public double CurrentBalance { get { return currentBalance; } } public void Withdrawal(double amount) { currentBalance -= amount; } public void Deposit(double amount) { currentBalance += amount; } } class Program { static void Main(string[] args) { BankAccount account = new BankAccount(); while (true) { Console.Write("Enter amount (S to show balance, END to exit): "); string input = Console.ReadLine().Trim().ToUpper(); if (input == "END") { break; } if (input == "S") { Console.WriteLine("Current balance: {0:C}", account.CurrentBalance); } else { double amount = 0; if (!double.TryParse(input, out amount)) { Console.WriteLine("Invalid input, please try again."); continue; } if (amount < 0) { account.Withdrawal(-amount); } else { account.Deposit(amount); } } } } }The BankAccount class defines the bank account with Withdrawal, Deposit, and CurrentBalance methods. The Main method of the console application creates an instance of BankAccount and enters a loop to repeatedly ask the user for an amount. If the user enters "S", the current balance is displayed. If the user enters "END", the loop is exited and the program terminates. If the user enters a valid number, the Withdrawal or Deposit method is called with the appropriate amount. If the input is not a valid number, an error message is displayed and the loop continues.
Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 14, 2023, 12:55 AM EST
You can try below code.
using System; public class BankAccount { private decimal _balance; public decimal CurrentBalance { get { return _balance; } } public void Deposit(decimal amount) { _balance += amount; Console.WriteLine("Deposit successful. Current balance: " + CurrentBalance); } public void Withdrawal(decimal amount) { if (_balance >= amount) { _balance -= amount; Console.WriteLine("Withdrawal successful. Current balance: " + CurrentBalance); } else { Console.WriteLine("Insufficient balance."); } } } class Program { static void Main(string[] args) { BankAccount bankAccount = new BankAccount(); while (true) { Console.WriteLine("Enter an amount to deposit or withdraw, or 'S' to show the current balance, or 'END' to exit:"); string input = Console.ReadLine(); switch (input.ToUpper()) { case "END": return; case "S": Console.WriteLine("Current balance: " + bankAccount.CurrentBalance); break; default: decimal amount; if (!decimal.TryParse(input, out amount)) { Console.WriteLine("Invalid input. Please enter a valid amount."); break; } if (amount > 0) { bankAccount.Deposit(amount); } else if (amount < 0) { bankAccount.Withdrawal(-amount); } break; } } } }Thanks
Naimish