Lập trình C# - Quản lý thu nhập
Xây dựng các lớp để quản lý thu nhập hàng tháng của một cơ quan
Xây dựng các lớp để quản lý thu nhập hàng tháng của một cơ quan, biết rằng: Cơ quan có 2 dạng người được hưởng lương: biên chế thì hưởng lương theo quỹ lương của nhà nước, và hợp đồng thì hưởng lương theo số giờ làm việc.
- Mỗi người trong công ty đều có các thông tin sau: Hoten, số CMND, Phòng ban.
- Biên chế: có thông tin riêng là Bậc lương
- Hợp đồng: có thông tin riêng là Số giờ, Tiền công 01 giờ.
Chương trình cho phép nhập vào 1 danh sách các nhân viên theo biên chế, 1 danh sách các nhân viên theo hợp đồng và cuối cùng in ra lương của từng nhân viên, tổng lương của các nhân viên thuộc dạng biên chế, tổng lương của các nhân viên thuộc dạng hợp đồng.
Bước 1: Xây dựng Class Employee (Nhân Viên)
public class Employee
{
public string FullName { get; set; }
public string Room { get; set; }
public string Identification { get ; set; }
public Employee()
{
FullName = "";
Identification = "";
Room = "";
}
public Employee(string FullName, string Identification, string Room)
{
this.FullName = FullName;
this.Identification = Identification;
this.Room = Room;
}
public void Input()
{
Console.Write("Nhập họ và tên:");
FullName = Console.ReadLine();
Console.Write("Nhập số CMND: ");
Identification = Console.ReadLine();
Console.Write("Nhập phòng ban: ");
Room = Console.ReadLine();
}
public void Display() {
Console.Write(" {0,-20} \t\t| {1,-4} \t\t| {2,-6}\t| ", FullName, Identification, Identification);
}
}
Bước 2: Xây dựng Class Payroll (Biên chế)
public class Payroll : Employee
{
double _salaryscale;
double _salary;
public double SalaryScale
{
get
{
return _salaryscale;
}
set
{
if (value >= 0)
{
this._salaryscale = value;
}
else
{
Console.WriteLine("Nhập bậc lương phải là số dương");
}
}
}
public double Salary
{
get
{
return _salary;
}
set
{
if (value >= 0)
{
this._salary = value;
}
else
{
Console.WriteLine("Nhập lương phải là số dương");
}
}
}
public Payroll() : base()
{
SalaryScale = 0;
Salary = 0;
}
public Payroll(string FullName, string Identification, string Room, double SalaryScale, double Salary) : base(FullName, Identification, Room)
{
this.SalaryScale = SalaryScale;
this.Salary = Salary;
}
public new void Input()
{
base.Input();
try
{
Console.Write("Nhập bậc lương: ");
SalaryScale = Double.Parse(Console.ReadLine());
Console.Write("Nhập lương cơ bản: ");
Salary = Double.Parse(Console.ReadLine());
}
catch (Exception ex) {
Console.WriteLine("Nhập lương cơ bản hoặc bậc lương là số");
}
}
public new void Display()
{
base.Display();
Console.WriteLine(" {0}|",SalaryScale * Salary);
}
public double Sum()
{
return (Salary * SalaryScale);
}
}
Bước 3: Xây dựng Class Contact (Hợp đồng)
public class Contract : Employee
{
double _hour;
public double Hour
{
get
{
return _hour;
}
set
{
if (value >= 0)
{
this._hour = value;
}
else
{
Console.WriteLine("Nhập giờ là số dương");
}
}
}
double _wage;
public double Wage
{
get
{
return _wage;
}
set
{
if (value >= 0)
{
this._wage = value;
}
else
{
Console.WriteLine("Nhâp tiền công là số dương");
}
}
}
public Contract() : base()
{
Hour = 0;
Wage = 0;
}
public Contract(string FullName, string Identification, string Room, double Hour, double Wage) : base(FullName, Identification, Room)
{
this.Hour = Hour;
this.Wage = Wage;
}
public new void Input()
{
base.Input();
try
{
Console.Write("Nhập số giờ làm: ");
Hour = Double.Parse(Console.ReadLine());
Console.Write("Nhập tiền công: ");
Wage = Double.Parse(Console.ReadLine());
}
catch(Exception ex)
{
Console.WriteLine("Nhâp giờ làm hoặc tiền công là số");
}
}
public new void Display()
{
base.Display();
Console.WriteLine("{0,10}|", Hour * Wage);
}
public double Sum()
{
return (Hour * Wage);
}
}
Bước 4: Trong Program code test như sau:
using System;
using System.Text;
namespace QuanLyEmployee
{
class Program
{
public class Employee
{
public string FullName { get; set; }
public string Room { get; set; }
public string Identification { get ; set; }
public Employee()
{
FullName = "";
Identification = "";
Room = "";
}
public Employee(string FullName, string Identification, string Room) {
this.FullName = FullName;
this.Identification = Identification;
this.Room = Room;
}
public void Input()
{
Console.Write("Nhập họ và tên:");
FullName = Console.ReadLine();
Console.Write("Nhập số CMND: ");
Identification = Console.ReadLine();
Console.Write("Nhập phòng ban: ");
Room = Console.ReadLine();
}
public void Display() {
Console.Write(" {0,-20} \t\t| {1,-4} \t\t| {2,-6}\t| ", FullName, Identification, Identification);
}
}
public class Payroll : Employee
{
double _salaryscale;
double _salary;
public double SalaryScale
{
get
{
return _salaryscale;
}
set
{
if (value >= 0)
{
this._salaryscale = value;
}
else
{
Console.WriteLine("Nhập bậc lương phải là số dương");
}
}
}
public double Salary
{
get
{
return _salary;
}
set
{
if (value >= 0)
{
this._salary = value;
}
else
{
Console.WriteLine("Nhập lương phải là số dương");
}
}
}
public Payroll() : base()
{
SalaryScale = 0;
Salary = 0;
}
public Payroll(string FullName, string Identification, string Room, double SalaryScale, double Salary) : base(FullName, Identification, Room)
{
this.SalaryScale = SalaryScale;
this.Salary = Salary;
}
public new void Input()
{
base.Input();
try
{
Console.Write("Nhập bậc lương: ");
SalaryScale = Double.Parse(Console.ReadLine());
Console.Write("Nhập lương cơ bản: ");
Salary = Double.Parse(Console.ReadLine());
}
catch (Exception ex) {
Console.WriteLine("Nhập lương cơ bản hoặc bậc lương là số");
}
}
public new void Display()
{
base.Display();
Console.WriteLine(" {0}|",SalaryScale * Salary);
}
public double Sum()
{
return (Salary * SalaryScale);
}
}
public class Contract : Employee
{
double _hour;
public double Hour
{
get
{
return _hour;
}
set
{
if (value >= 0)
{
this._hour = value;
}
else
{
Console.WriteLine("Nhập giờ là số dương");
}
}
}
double _wage;
public double Wage
{
get
{
return _wage;
}
set
{
if (value >= 0)
{
this._wage = value;
}
else
{
Console.WriteLine("Nhâp tiền công là số dương");
}
}
}
public Contract() : base()
{
Hour = 0;
Wage = 0;
}
public Contract(string FullName, string Identification, string Room, double Hour, double Wage) : base(FullName, Identification, Room)
{
this.Hour = Hour;
this.Wage = Wage;
}
public new void Input()
{
base.Input();
try
{
Console.Write("Nhập số giờ làm: ");
Hour = Double.Parse(Console.ReadLine());
Console.Write("Nhập tiền công: ");
Wage = Double.Parse(Console.ReadLine());
}
catch(Exception ex)
{
Console.WriteLine("Nhâp giờ làm hoặc tiền công là số");
}
}
public new void Display()
{
base.Display();
Console.WriteLine("{0,10}|", Hour * Wage);
}
public double Sum()
{
return (Hour * Wage);
}
}
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
int n=0, i, choose;
double sum = 0;
Payroll[] _Payroll = new Payroll[n];
Contract[] _Contract = new Contract[n];
Console.Clear();
do
{
ShowMenu();
choose = int.Parse(Console.ReadLine());
switch (choose)
{
case 1:
Console.Write("Nhập số lượng nhân viên biên chế: ");
n = Int32.Parse(Console.ReadLine());
_Payroll = new Payroll[n];
Console.WriteLine("Nhập thông tin cho từng nhân viên biên chế");
for (i = 0; i < n; i++)
{
_Payroll[i] = new Payroll();
_Payroll[i].Input();
}
break;
case 2:
Console.Write("Nhập số lượng nhân viên hợp đồng:");
n = Int32.Parse(Console.ReadLine());
_Contract = new Contract[n];
Console.WriteLine();
Console.WriteLine("Nhập thông tin cho từng nhân viên hợp đồng");
for (i = 0; i < n; i++)
{
_Contract[i] = new Contract();
_Contract[i].Input();
}
break;
case 3:
Console.Clear();
Console.WriteLine("\nDanh sách nhân viên biên chế ");
Console.WriteLine("-----------------------------------------------------------------------------------");
Console.WriteLine("Họ và tên\t\t\t| Số CMND\t\t| Phòng ban \t|Lương tháng| ");
Console.WriteLine("-----------------------------------------------------------------------------------");
for (i = 0; i < n; i++)
_Payroll[i].Display();
Console.WriteLine("-----------------------------------------------------------------------------------");
for (i = 0; i < n; i++)
sum = sum + _Payroll[i].Sum();
Console.WriteLine("Tổng lương của nhân viên biên chế là: {0}", sum);
Console.WriteLine("-----------------------------------------------------------------------------------");
break;
case 4:
Console.Clear();
Console.WriteLine("\nDanh sách nhân viên hợp đồng ");
Console.WriteLine("-------------------------------------------------------------------------------------");
Console.WriteLine("Họ và tên\t\t\t| Số CMND\t\t| Phòng ban \t|Lương tháng| ");
Console.WriteLine("-------------------------------------------------------------------------------------");
for (i = 0; i < n; i++)
_Contract[i].Display();
Console.WriteLine("-------------------------------------------------------------------------------------");
for (i = 0; i < n; i++)
sum = sum + _Contract[i].Sum();
Console.WriteLine("Tổng lương của nhân viên hợp đồng là: {0}", sum);
Console.WriteLine("-------------------------------------------------------------------------------------");
break;
default:
Console.WriteLine("Xin vui lòng chọn từ 1 đến 5");
break;
}
} while(choose!= 5);
Console.ReadKey();
}
static void ShowMenu()
{
Console.WriteLine("\nCHƯƠNG TRÌNH QUẢN LÝ NHÂN VIÊN C#");
Console.WriteLine("*********************************MENU************************************");
Console.WriteLine("** 1. Nhập danh sách nhân viên biên chế **");
Console.WriteLine("** 2. Nhập danh sách nhân viên hợp đồng **");
Console.WriteLine("** 3. Xem danh sách và tổng lương nhân viên biên chế **");
Console.WriteLine("** 4. Xem danh sách và tổng lương nhân viên hợp đồng **");
Console.WriteLine("** 5. Thoát. **");
Console.WriteLine("** Chon : **");
Console.WriteLine("*************************************************************************");
}
}
}
Bước 5: Nhấn Ctrl+F5 để chạy và xem kết quả
Chọn 1 nhập thông nhân viên biên chế
Chọn 4 xem thông nhân viên biên chế và tổng lương