0% found this document useful (0 votes)
14 views5 pages

Mid

The document contains C# code for a payroll application. It defines classes for different types of employees (hourly, salaried, commission-based) with properties like name, wage, hours worked, etc. and methods to calculate earnings. The MainWindow class handles the user interface, allowing the user to add employees by selecting the type and entering details, then view and clear employee information.

Uploaded by

gtguide101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Mid

The document contains C# code for a payroll application. It defines classes for different types of employees (hourly, salaried, commission-based) with properties like name, wage, hours worked, etc. and methods to calculate earnings. The MainWindow class handles the user interface, allowing the user to add employees by selecting the type and entering details, then view and clear employee information.

Uploaded by

gtguide101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

// MainWindow.xalm.

cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;

namespace MTCyrusTabakhi
{
public partial class MainWindow : Window
{
private List<Employee> employees = new List<Employee>();

public MainWindow()
{
InitializeComponent();
this.Loaded += MainWindow_Loaded;
}

private void MainWindow_Loaded(object sender, RoutedEventArgs e)


{
UpdateInputFieldsBasedOnSelection();
HourlyRadioButton.IsChecked = true;
}

private void RadioButton_Checked(object sender, RoutedEventArgs e)


{
if (!this.IsLoaded) return;
UpdateInputFieldsBasedOnSelection();
}

private void CalculateButton_Click(object sender, RoutedEventArgs e)


{
try
{
string name = NameTextBox.Text.Trim();
ValidateInputNotEmpty(name, "Employee Name");

Employee employee = null;

if (HourlyRadioButton.IsChecked == true)
{
decimal hoursWorked = ParseDecimal(HoursWorkedTextBox.Text,
"Hours Worked");
decimal hourlyWage = ParseDecimal(HourlyWageTextBox.Text,
"Hourly Wage");
employee = new HourlyEmployee(name, hoursWorked, hourlyWage);
}
else if (CommissionRadioButton.IsChecked == true)
{
decimal grossSales = ParseDecimal(GrossSalesTextBox.Text,
"Gross Sales");
decimal commissionRate =
ParseDecimal(CommissionRateTextBox.Text, "Commission Rate");
employee = new CommissionEmployee(name, grossSales,
commissionRate);
}
else if (SalaryRadioButton.IsChecked == true)
{
decimal weeklySalary = ParseDecimal(WeeklySalaryTextBox.Text,
"Weekly Salary");
employee = new SalariedEmployee(name, weeklySalary);
}

if (employee != null)
{
employees.Add(employee);
EmployeeListBox.Items.Add(employee.EmployeeName);
DisplayEmployeeDetails(employee);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK,
MessageBoxImage.Error);
}
}

private void ClearButton_Click(object sender, RoutedEventArgs e)


{
ClearInputFields();
}

private void CloseButton_Click(object sender, RoutedEventArgs e)


{
Close();
}

private void EmployeeListBox_SelectionChanged(object sender,


System.Windows.Controls.SelectionChangedEventArgs e)
{
if (EmployeeListBox.SelectedItem != null)
{
string selectedName = EmployeeListBox.SelectedItem.ToString();
var selectedEmployee = FindEmployeeByName(selectedName);
if (selectedEmployee != null)
{
DisplayEmployeeDetails(selectedEmployee);
}
}
}

private void UpdateInputFieldsBasedOnSelection()


{
var isHourly = HourlyRadioButton.IsChecked == true;
var isCommission = CommissionRadioButton.IsChecked == true;

HoursWorkedLabel.Visibility = isHourly ? Visibility.Visible :


Visibility.Collapsed;
HoursWorkedTextBox.Visibility = isHourly ? Visibility.Visible :
Visibility.Collapsed;
HourlyWageLabel.Visibility = isHourly ? Visibility.Visible :
Visibility.Collapsed;
HourlyWageTextBox.Visibility = isHourly ? Visibility.Visible :
Visibility.Collapsed;
GrossSalesLabel.Visibility = isCommission ? Visibility.Visible :
Visibility.Collapsed;
GrossSalesTextBox.Visibility = isCommission ? Visibility.Visible :
Visibility.Collapsed;
CommissionRateLabel.Visibility = isCommission ? Visibility.Visible :
Visibility.Collapsed;
CommissionRateTextBox.Visibility = isCommission ? Visibility.Visible :
Visibility.Collapsed;

WeeklySalaryLabel.Visibility = !isHourly && !isCommission ?


Visibility.Visible : Visibility.Collapsed;
WeeklySalaryTextBox.Visibility = !isHourly && !isCommission ?
Visibility.Visible : Visibility.Collapsed;
}

private void DisplayEmployeeDetails(Employee employee)


{
GrossEarningsTextBox.Text = employee.GrossEarnings.ToString("C2");
TaxTextBox.Text = employee.Tax.ToString("C2");
NetEarningsTextBox.Text = employee.NetEarnings.ToString("C2");

// Reset based on the type of employee


UpdateInputFieldsBasedOnSelection();
}

private Employee FindEmployeeByName(string name)


{
return employees.FirstOrDefault(emp => emp.EmployeeName == name);
}

private decimal ParseDecimal(string input, string fieldName)


{
if (!decimal.TryParse(input, out decimal result) || result < 0)
{
throw new ArgumentException($"{fieldName} must be a positive
number.");
}
return result;
}

private void ValidateInputNotEmpty(string input, string fieldName)


{
if (string.IsNullOrWhiteSpace(input))
{
throw new ArgumentException($"{fieldName} cannot be left blank.");
}
}

private void ClearInputFields()


{
NameTextBox.Clear();
HoursWorkedTextBox.Clear();
HourlyWageTextBox.Clear();
GrossSalesTextBox.Clear();
CommissionRateTextBox.Clear();
WeeklySalaryTextBox.Clear();
GrossEarningsTextBox.Clear();
TaxTextBox.Clear();
NetEarningsTextBox.Clear();
EmployeeListBox.UnselectAll();
HourlyRadioButton.IsChecked = true;
}
}
}

// Employee.cs

public abstract class Employee


{
public EmployeeType Type { get; protected set; }
public string EmployeeId { get; protected set; }
public string EmployeeName { get; set; }

public abstract decimal GrossEarnings { get; }


public virtual decimal Tax => GrossEarnings * 0.20m;
public virtual decimal NetEarnings => GrossEarnings - Tax;

protected Employee(string name)


{
EmployeeName = name;
EmployeeId = Guid.NewGuid().ToString();
}
}

//enum.cs

public enum EmployeeType


{
HourlyEmployee,
CommissionEmployee,
SalariedEmployee
}

//HourlyEmployee.cs

public class HourlyEmployee : Employee


{
public decimal HoursWorked { get; set; }
public decimal HourlyWage { get; set; }

public HourlyEmployee(string name, decimal hoursWorked, decimal hourlyWage) :


base(name)
{
HoursWorked = hoursWorked;
HourlyWage = hourlyWage;
Type = EmployeeType.HourlyEmployee;
}

public override decimal GrossEarnings =>


HoursWorked <= 40 ? HoursWorked * HourlyWage :
40 * HourlyWage + (HoursWorked - 40) * HourlyWage * 1.5m;
}

//SalariedEmployee.cs
public class SalariedEmployee : Employee
{
public decimal WeeklySalary { get; set; }

public SalariedEmployee(string name, decimal weeklySalary) : base(name)


{
WeeklySalary = weeklySalary;
Type = EmployeeType.SalariedEmployee;
}

public override decimal GrossEarnings => WeeklySalary;


}

//ComissionEmployee.cs

public class CommissionEmployee : Employee


{
public decimal GrossSales { get; set; }
public decimal CommissionRate { get; set; }

public CommissionEmployee(string name, decimal grossSales, decimal


commissionRate) : base(name)
{
GrossSales = grossSales;
CommissionRate = commissionRate;
Type = EmployeeType.CommissionEmployee;
}

public override decimal GrossEarnings => GrossSales * CommissionRate;


}

You might also like