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

View File

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

View File

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