Refactor bank account and calculator code with improved comments and English translations

This commit is contained in:
vista-man
2025-01-27 20:14:28 +01:00
parent bbcbef3d2f
commit ef1b5f2420
3 changed files with 54 additions and 18 deletions

View File

@@ -3,12 +3,14 @@ using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Windows.Forms; using System.Windows.Forms;
// Class representing a bank account
class Bankrekening class Bankrekening
{ {
public string Rekeningnummer { get; } public string Rekeningnummer { get; }
private decimal saldo; private decimal saldo;
private List<Transactie> transacties; private List<Transactie> transacties;
// Constructor to initialize account number and starting balance
public Bankrekening(string rekeningnummer, decimal beginsaldo) public Bankrekening(string rekeningnummer, decimal beginsaldo)
{ {
Rekeningnummer = rekeningnummer; Rekeningnummer = rekeningnummer;
@@ -16,6 +18,7 @@ class Bankrekening
transacties = new List<Transactie>(); transacties = new List<Transactie>();
} }
// Method to deposit money into the account with a description
public void Storten(decimal bedrag, string beschrijving) public void Storten(decimal bedrag, string beschrijving)
{ {
if (bedrag > 0) if (bedrag > 0)
@@ -25,10 +28,11 @@ class Bankrekening
} }
else else
{ {
throw new ArgumentException("Het bedrag moet positief zijn."); throw new ArgumentException("The amount must be positive.");
} }
} }
// Method to withdraw money from the account with a description
public void Opnemen(decimal bedrag, string beschrijving) public void Opnemen(decimal bedrag, string beschrijving)
{ {
if (bedrag > 0 && bedrag <= saldo) if (bedrag > 0 && bedrag <= saldo)
@@ -38,27 +42,31 @@ class Bankrekening
} }
else else
{ {
throw new ArgumentException("Onvoldoende saldo of ongeldig bedrag."); throw new ArgumentException("Insufficient balance or invalid amount.");
} }
} }
// Method to check the current balance
public decimal ControleerSaldo() public decimal ControleerSaldo()
{ {
return saldo; return saldo;
} }
// Method to get the transaction history
public List<Transactie> GetTransactieGeschiedenis() public List<Transactie> GetTransactieGeschiedenis()
{ {
return transacties; return transacties;
} }
} }
// Class representing a transaction
class Transactie class Transactie
{ {
public decimal Bedrag { get; } public decimal Bedrag { get; }
public string Beschrijving { get; } public string Beschrijving { get; }
public DateTime Datum { get; } public DateTime Datum { get; }
// Constructor to initialize transaction details
public Transactie(decimal bedrag, string beschrijving) public Transactie(decimal bedrag, string beschrijving)
{ {
Bedrag = bedrag; Bedrag = bedrag;
@@ -67,6 +75,7 @@ class Transactie
} }
} }
// Main form for the application
public class MainForm : Form public class MainForm : Form
{ {
private Bankrekening mijnRekening; private Bankrekening mijnRekening;
@@ -77,6 +86,7 @@ public class MainForm : Form
private Button opnemenButton; private Button opnemenButton;
private Button transactieGeschiedenisButton; private Button transactieGeschiedenisButton;
// Constructor to initialize the form and its controls
public MainForm() public MainForm()
{ {
mijnRekening = new Bankrekening("NL01BANK0123456789", 1000); mijnRekening = new Bankrekening("NL01BANK0123456789", 1000);
@@ -101,7 +111,7 @@ public class MainForm : Form
BackColor = Color.White, BackColor = Color.White,
ForeColor = Color.Black, ForeColor = Color.Black,
BorderStyle = BorderStyle.FixedSingle, BorderStyle = BorderStyle.FixedSingle,
PlaceholderText = "Aantal" PlaceholderText = "Amount"
}; };
beschrijvingTextBox = new TextBox() beschrijvingTextBox = new TextBox()
{ {
@@ -112,11 +122,11 @@ public class MainForm : Form
BackColor = Color.White, BackColor = Color.White,
ForeColor = Color.Black, ForeColor = Color.Black,
BorderStyle = BorderStyle.FixedSingle, BorderStyle = BorderStyle.FixedSingle,
PlaceholderText = "Beschrijving" PlaceholderText = "Description"
}; };
stortenButton = new Button() stortenButton = new Button()
{ {
Text = "Storten", Text = "Deposit",
Top = 140, Top = 140,
Left = 20, Left = 20,
Width = 260, Width = 260,
@@ -128,7 +138,7 @@ public class MainForm : Form
}; };
opnemenButton = new Button() opnemenButton = new Button()
{ {
Text = "Opnemen", Text = "Withdraw",
Top = 200, Top = 200,
Left = 20, Left = 20,
Width = 260, Width = 260,
@@ -140,7 +150,7 @@ public class MainForm : Form
}; };
transactieGeschiedenisButton = new Button() transactieGeschiedenisButton = new Button()
{ {
Text = "Transactiegeschiedenis", Text = "Transaction History",
Top = 260, Top = 260,
Left = 20, Left = 20,
Width = 260, Width = 260,
@@ -162,12 +172,13 @@ public class MainForm : Form
Controls.Add(opnemenButton); Controls.Add(opnemenButton);
Controls.Add(transactieGeschiedenisButton); Controls.Add(transactieGeschiedenisButton);
Text = "Bankrekening Beheer"; Text = "Bank Account Management";
Size = new Size(320, 380); Size = new Size(320, 380);
StartPosition = FormStartPosition.CenterScreen; StartPosition = FormStartPosition.CenterScreen;
BackColor = Color.White; BackColor = Color.White;
} }
// Event handler for deposit button click
private void StortenButton_Click(object sender, EventArgs e) private void StortenButton_Click(object sender, EventArgs e)
{ {
try try
@@ -183,6 +194,7 @@ public class MainForm : Form
} }
} }
// Event handler for withdraw button click
private void OpnemenButton_Click(object sender, EventArgs e) private void OpnemenButton_Click(object sender, EventArgs e)
{ {
try try
@@ -198,11 +210,12 @@ public class MainForm : Form
} }
} }
// Event handler for transaction history button click
private void TransactieGeschiedenisButton_Click(object sender, EventArgs e) private void TransactieGeschiedenisButton_Click(object sender, EventArgs e)
{ {
var transactieGeschiedenis = mijnRekening.GetTransactieGeschiedenis(); var transactieGeschiedenis = mijnRekening.GetTransactieGeschiedenis();
string geschiedenis = "Transactiegeschiedenis:\n"; string geschiedenis = "Transaction History:\n";
geschiedenis += "Datum\t\tBeschrijving\t\tBedrag\n"; geschiedenis += "Date\t\tDescription\t\tAmount\n";
geschiedenis += "---------------------------------------------\n"; geschiedenis += "---------------------------------------------\n";
foreach (var transactie in transactieGeschiedenis) foreach (var transactie in transactieGeschiedenis)
{ {
@@ -212,6 +225,7 @@ public class MainForm : Form
} }
} }
// Main program class
class Program class Program
{ {
static void Main() static void Main()
@@ -220,7 +234,7 @@ class Program
mijnRekening.Storten(100.00m, "Initial deposit"); mijnRekening.Storten(100.00m, "Initial deposit");
mijnRekening.Opnemen(50.00m, "ATM withdrawal"); mijnRekening.Opnemen(50.00m, "ATM withdrawal");
Console.WriteLine("Transactiegeschiedenis:"); Console.WriteLine("Transaction History:");
foreach (var transactie in mijnRekening.GetTransactieGeschiedenis()) foreach (var transactie in mijnRekening.GetTransactieGeschiedenis())
{ {
Console.WriteLine($"{transactie.Datum}: {transactie.Beschrijving} - {transactie.Bedrag:C}"); Console.WriteLine($"{transactie.Datum}: {transactie.Beschrijving} - {transactie.Bedrag:C}");

View File

@@ -4,17 +4,21 @@ class Program
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
// Ask the user to enter the first number
Console.WriteLine("Enter the first number:"); Console.WriteLine("Enter the first number:");
double num1 = Convert.ToDouble(Console.ReadLine()); double num1 = Convert.ToDouble(Console.ReadLine());
// Ask the user to enter an operator (+, -, *, /)
Console.WriteLine("Enter an operator (+, -, *, /):"); Console.WriteLine("Enter an operator (+, -, *, /):");
string op = Console.ReadLine(); string op = Console.ReadLine();
// Ask the user to enter the second number
Console.WriteLine("Enter the second number:"); Console.WriteLine("Enter the second number:");
double num2 = Convert.ToDouble(Console.ReadLine()); double num2 = Convert.ToDouble(Console.ReadLine());
double result = 0; double result = 0;
// Perform the operation based on the entered operator
switch (op) switch (op)
{ {
case "+": case "+":
@@ -33,15 +37,18 @@ class Program
} }
else else
{ {
// Display an error message if attempting to divide by zero
Console.WriteLine("Cannot divide by zero."); Console.WriteLine("Cannot divide by zero.");
return; return;
} }
break; break;
default: default:
// Display an error message if the entered operator is invalid
Console.WriteLine("Invalid operator."); Console.WriteLine("Invalid operator.");
return; return;
} }
// Display the result of the calculation
Console.WriteLine("The result is: " + result); Console.WriteLine("The result is: " + result);
} }
} }

View File

@@ -16,31 +16,39 @@ class Program : Form
public Program() public Program()
{ {
// Initialize the random number to guess
numberToGuess = random.Next(1, 101); numberToGuess = random.Next(1, 101);
// Set up the form
this.Text = "Gokspel"; this.Text = "Gokspel";
this.Size = new System.Drawing.Size(300, 200); this.Size = new System.Drawing.Size(300, 200);
// Create and add the prompt label
Label promptLabel = new Label(); Label promptLabel = new Label();
promptLabel.Text = "Raad het getal tussen 1 en 100:"; promptLabel.Text = "Raad het getal tussen 1 en 100:";
promptLabel.Location = new System.Drawing.Point(10, 20); promptLabel.Location = new System.Drawing.Point(10, 20);
promptLabel.AutoSize = true; promptLabel.AutoSize = true;
this.Controls.Add(promptLabel); this.Controls.Add(promptLabel);
// Create and add the input box
inputBox = new TextBox(); inputBox = new TextBox();
inputBox.Location = new System.Drawing.Point(10, 50); inputBox.Location = new System.Drawing.Point(10, 50);
this.Controls.Add(inputBox); this.Controls.Add(inputBox);
// Create and add the guess button
guessButton = new Button(); guessButton = new Button();
guessButton.Text = "Gok"; guessButton.Text = "Gok";
guessButton.Location = new System.Drawing.Point(10, 80); guessButton.Location = new System.Drawing.Point(10, 80);
guessButton.Click += new EventHandler(GuessButton_Click); guessButton.Click += new EventHandler(GuessButton_Click);
this.Controls.Add(guessButton); this.Controls.Add(guessButton);
// Create and add the result label
resultLabel = new Label(); resultLabel = new Label();
resultLabel.Location = new System.Drawing.Point(10, 110); resultLabel.Location = new System.Drawing.Point(10, 110);
resultLabel.AutoSize = true; resultLabel.AutoSize = true;
this.Controls.Add(resultLabel); this.Controls.Add(resultLabel);
// Create and add the restart button (initially hidden)
restartButton = new Button(); restartButton = new Button();
restartButton.Text = "Opnieuw"; restartButton.Text = "Opnieuw";
restartButton.Location = new System.Drawing.Point(10, 140); restartButton.Location = new System.Drawing.Point(10, 140);
@@ -48,6 +56,7 @@ class Program : Form
restartButton.Visible = false; restartButton.Visible = false;
this.Controls.Add(restartButton); this.Controls.Add(restartButton);
// Set up the confetti timer
confettiTimer = new System.Windows.Forms.Timer(); confettiTimer = new System.Windows.Forms.Timer();
confettiTimer.Interval = 30; confettiTimer.Interval = 30;
confettiTimer.Tick += new EventHandler(ConfettiTimer_Tick); confettiTimer.Tick += new EventHandler(ConfettiTimer_Tick);
@@ -60,38 +69,40 @@ class Program : Form
{ {
if (userGuess < numberToGuess) if (userGuess < numberToGuess)
{ {
resultLabel.Text = "Te laag! Probeer opnieuw."; resultLabel.Text = "Too low! Try again.";
} }
else if (userGuess > numberToGuess) else if (userGuess > numberToGuess)
{ {
resultLabel.Text = "Te hoog! Probeer opnieuw."; resultLabel.Text = "Too high! Try again.";
} }
else else
{ {
resultLabel.Text = "Gefeliciteerd! Je hebt het juiste getal geraden."; resultLabel.Text = "Congratulations! You guessed the correct number.";
StartConfetti(); StartConfetti();
restartButton.Visible = true; restartButton.Visible = true; // Show the restart button
} }
} }
else else
{ {
resultLabel.Text = "Ongeldige invoer. Voer een getal in."; resultLabel.Text = "Invalid input. Please enter a number.";
} }
} }
private void RestartButton_Click(object sender, EventArgs e) private void RestartButton_Click(object sender, EventArgs e)
{ {
// Reset the game
numberToGuess = random.Next(1, 101); numberToGuess = random.Next(1, 101);
resultLabel.Text = ""; resultLabel.Text = "";
inputBox.Text = ""; inputBox.Text = "";
confettiList.Clear(); confettiList.Clear();
confettiTimer.Stop(); confettiTimer.Stop();
restartButton.Visible = false; restartButton.Visible = false; // Hide the restart button
this.Invalidate(); this.Invalidate();
} }
private void StartConfetti() private void StartConfetti()
{ {
// Initialize confetti
confettiList.Clear(); confettiList.Clear();
for (int i = 0; i < 100; i++) for (int i = 0; i < 100; i++)
{ {
@@ -102,6 +113,7 @@ class Program : Form
private void ConfettiTimer_Tick(object sender, EventArgs e) private void ConfettiTimer_Tick(object sender, EventArgs e)
{ {
// Update confetti positions
for (int i = 0; i < confettiList.Count; i++) for (int i = 0; i < confettiList.Count; i++)
{ {
confettiList[i].Update(); confettiList[i].Update();
@@ -112,6 +124,7 @@ class Program : Form
protected override void OnPaint(PaintEventArgs e) protected override void OnPaint(PaintEventArgs e)
{ {
base.OnPaint(e); base.OnPaint(e);
// Draw confetti
foreach (var confetti in confettiList) foreach (var confetti in confettiList)
{ {
e.Graphics.FillEllipse(new SolidBrush(confetti.Color), confetti.Position.X, confetti.Position.Y, confetti.Size, confetti.Size); e.Graphics.FillEllipse(new SolidBrush(confetti.Color), confetti.Position.X, confetti.Position.Y, confetti.Size, confetti.Size);
@@ -136,6 +149,7 @@ class Confetti
public Confetti(Random random, Size clientSize) public Confetti(Random random, Size clientSize)
{ {
// Initialize confetti properties
Position = new PointF(random.Next(clientSize.Width), random.Next(clientSize.Height)); Position = new PointF(random.Next(clientSize.Width), random.Next(clientSize.Height));
SpeedY = (float)(random.NextDouble() * 2 + 1); SpeedY = (float)(random.NextDouble() * 2 + 1);
SpeedX = (float)(random.NextDouble() * 2 - 1); SpeedX = (float)(random.NextDouble() * 2 - 1);
@@ -145,7 +159,8 @@ class Confetti
public void Update() public void Update()
{ {
// Update confetti position and apply gravity
Position = new PointF(Position.X + SpeedX, Position.Y + SpeedY); Position = new PointF(Position.X + SpeedX, Position.Y + SpeedY);
SpeedY += 0.1f; SpeedY += 0.1f; // gravity effect
} }
} }