Lập trình C# - Quản lý Viện khoa học
Quản lý các đối tượng trong một Viện khoa học
Thiết kế chương trình quản lý các đối tượng sau trong một Viện khoa học: nhà khoa học, nhà quản lý và nhân viên phòng thí nghiệm. Một nhà khoa học cũng có thể làm công tác quản lý. Các thành phần dữ liệu của các đối tượng trên:
- Nhà khoa học: họ tên, năm sinh, bằng cấp, chức vụ, số bài báo đã công bố, số ngày công trong tháng, bậc lương
- Nhà quản lý: họ tên, năm sinh, bằng cấp, chức vụ, số ngày công trong tháng, bậc lương
- Nhân viên phòng thí nghiệm: họ tên, năm sinh, bằng cấp, lương trong tháng.
Biết rằng nhân viên phòng thí nghiệm lãnh lương khoán, còn lương của nhà khoa học và nhà quản lý bằng số ngày công trong tháng * bậc lương. Nhập, xuất danh sách nhân viên và in tổng lương đã chi trả cho từng loại đối tượng.
Trong project chúng ta tạo 4 class Institute.cs, Scientist.cs, Manager.cs, Employee.cs
Bước 1: Xây dựng class Institute.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace QuanLyCanBoKhoaHoc
{
class Institute
{
public string FullName { get; set; }
public string Degree { get; set; }
public int YearOfBirth { get; set; }
public Institute()
{
FullName = "";
Degree = "";
YearOfBirth = 0;
}
public Institute(string FullName, int YearOfBirth, string Degree)
{
this.FullName = FullName;
this.YearOfBirth = YearOfBirth;
this.Degree = Degree;
}
public void Input()
{
try
{
Console.Write("Nhập Họ và tên: ");
FullName = Console.ReadLine();
Console.Write("Nhập Năm sinh: ");
YearOfBirth = Int32.Parse(Console.ReadLine());
Console.Write("Nhập Bằng cấp: ");
Degree = Console.ReadLine();
}
catch(Exception ex)
{
Console.WriteLine("Nhập sai thông tin");
}
}
public void Display()
{
Console.Write(" {0,-20} \t\t| {1,-4} \t\t| {2,-6}\t| ", FullName, YearOfBirth, Degree);
}
}
}
Bước 2: Xây dựng class Scientist.cs
using QuanLyCanBoKhoaHoc;
using System;
using System.Collections.Generic;
using System.Text;
namespace QuanLyCanBoScientist
{
class Scientist: Institute
{
public string Position { get; set; }
public int Paper { get; set; }
public double Wage { get; set; }
public double WorkDay { get; set; }
public Scientist() : base()
{
Position = "";
Paper = 0;
WorkDay = 0;
Wage = 0;
}
public Scientist(string HoTen, int NamSinh, string BangCap, string Position, int Paper, double WorkDay, double Wage) : base(HoTen, NamSinh, BangCap)
{
this.Position = Position;
this.Paper = Paper;
this.WorkDay = WorkDay;
this.Wage = Wage;
}
public new void Input()
{
try
{
base.Input();
Console.Write("Nhập chức vụ: ");
Position = Console.ReadLine();
Console.Write("Nhập số bài báo được công bố: ");
Paper = Int32.Parse(Console.ReadLine());
Console.Write("Nhập số ngày công trong tháng: ");
WorkDay = Double.Parse(Console.ReadLine());
Console.Write("Nhập bậc lương: ");
Wage = Double.Parse(Console.ReadLine());
}
catch(Exception ex)
{
Console.WriteLine("Nhập sai thông tin");
}
}
public new void Display()
{
base.Display();
Console.WriteLine(" {0,-5}\t|{1,-5}\t\t| {2,-5}\t |", Paper, WorkDay, Wage);
}
public double TotalSalaryScientist()
{
return (WorkDay * Wage);
}
}
}
Bước 3: Xây dựng class Manager.cs
using QuanLyCanBoKhoaHoc;
using System;
using System.Collections.Generic;
using System.Text;
namespace ManagerCanBoKhoaHoc
{
class Manager:Institute
{
public string Positon { get; set; }
public double Wage { get; set; }
public double WorkDay { get; set; }
public Manager() : base()
{
Positon = "";
Wage = 0;
WorkDay = 0;
}
public Manager(string FullName, int YearOfBirth, string Degree, string Positon, double WorkDay, double Wage) : base(FullName, YearOfBirth, Degree)
{
this.Positon = Positon;
this.WorkDay = WorkDay;
this.Wage = Wage;
}
public new void Input()
{
try
{
base.Input();
Console.Write("Nhập chúc vụ: ");
Positon = Console.ReadLine();
Console.Write("Nhập số ngày công trong tháng: ");
WorkDay = Double.Parse(Console.ReadLine());
Console.Write("Nhập bậc lương: ");
Wage = Double.Parse(Console.ReadLine());
}
catch(Exception ex)
{
Console.WriteLine("Nhập sai thông tin");
}
}
public new void Display()
{
base.Display();
Console.Write("{0,-5}\t\t|{1,-5}\t\t| {2,-5}\t |", Positon, WorkDay, Wage);
}
public double SumSalaryManger()
{
return (WorkDay * Wage);
}
}
}
Bước 4: Xây dựng class Employee.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace QuanLyCanBoKhoaHoc
{
class Employee:Institute
{
public double Salary { get; set; }
public Employee() : base()
{
Salary = 0;
}
public Employee(string FullName, int YearOfBirth, string Degree, double Salary) : base(FullName, YearOfBirth, Degree)
{
this.Salary = Salary;
}
public new void Input()
{
try
{
base.Input();
Console.Write("Nhập lương tháng: ");
Salary = Double.Parse(Console.ReadLine());
}
catch(Exception ex)
{
Console.WriteLine("Nhập sai thông tin");
}
}
public new void Display()
{
base.Display();
Console.WriteLine("{0,10}|", Salary);
}
public double SumSalary()
{
return (Salary);
}
}
}
Bước 5: Xây dửng hàm Main trong Program.cs
using ManagerCanBoKhoaHoc;
using QuanLyCanBoKhoaHoc;
using System;
using System.Text;
namespace QuanLyCanBoScientist
{
class Program
{
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
int n=0, i;
double sum = 0;
Scientist[] _Scientist = new Scientist[n];
Manager[] _Manager = new Manager[n];
Employee[] _Employee = new Employee[n];
int choose;
do
{
ShowMenu();
choose = int.Parse(Console.ReadLine());
switch (choose) {
case 1:
Console.Write("Nhập số lượng nhà khoa học: ");
n = Int32.Parse(Console.ReadLine());
_Scientist = new Scientist[n];
Console.WriteLine("Nhập dữ liệu cho các nhà khoa học ");
for (i = 0; i < n; i++)
{
_Scientist[i] = new Scientist();
_Scientist[i].Input();
Console.WriteLine();
}
break;
case 2:
Console.Write("Nhập số lượng nhà quản lý: ");
n = Int32.Parse(Console.ReadLine());
_Manager = new Manager[n];
Console.WriteLine("Nhập dữ liệu cho các nhà quản lý");
for (int j = 0; j < n; j++)
{
_Manager[j] = new Manager();
_Manager[j].Input();
Console.WriteLine();
}
break;
case 3:
Console.Write("Nhập số lượng nhân viên phòng thí nghiệm: ");
n = Int32.Parse(Console.ReadLine());
_Employee = new Employee[n];
Console.WriteLine("Nhập dữ liệu cho nhân viên phòng thí nghiệm");
for (i = 0; i < n; i++)
{
_Employee[i] = new Employee();
_Employee[i].Input();
Console.WriteLine();
}
break;
case 4:
Console.Clear();
Console.WriteLine("Danh sách nhà khoa học ");
Console.WriteLine("----------------------------------------------------------------------------------------------------------------");
Console.WriteLine("Họ và tên\t\t\t| Năm sinh\t| Bằng cấp \t|Số bài báo \t|Số ngày công \t|Bậc lương| ");
Console.WriteLine("----------------------------------------------------------------------------------------------------------------");
for (i = 0; i < n; i++)
_Scientist[i].Display();
Console.WriteLine("----------------------------------------------------------------------------------------------------------------");
for (i = 0; i < n; i++)
sum = sum + _Scientist[i].TotalSalaryScientist();
Console.WriteLine("Tổng lương các nhà khoa học là: {0}", sum);
Console.WriteLine("----------------------------------------------------------------------------------------------------------------");
break;
case 5:
Console.Clear();
Console.WriteLine("\nDanh sách các nhà quản lý ");
Console.WriteLine("----------------------------------------------------------------------------------------------------------------");
Console.WriteLine("Họ và tên\t\t\t| Năm sinh\t| Bằng cấp \t|Chức vụ \t|Số ngày công \t|Bậc lương| ");
Console.WriteLine("----------------------------------------------------------------------------------------------------------------");
for (int j = 0; j < n; j++)
_Manager[j].Display();
Console.WriteLine("----------------------------------------------------------------------------------------------------------------");
for (i = 0; i < n; i++)
sum = sum + _Manager[i].SumSalaryManger();
Console.WriteLine("Tổng lương của nhà quản lý là: {0}", sum);
Console.WriteLine("----------------------------------------------------------------------------------------------------------------");
break;
case 6:
Console.Clear();
Console.WriteLine("\nDanh sách nhân viên ");
Console.WriteLine("--------------------------------------------------------------------------");
Console.WriteLine("Họ và tên\t\t\t| Năm sinh\t| Bằng cấp \t|Lương tháng| ");
Console.WriteLine("--------------------------------------------------------------------------");
for (i = 0; i < n; i++)
_Employee[i].Display();
Console.WriteLine("--------------------------------------------------------------------------");
for (i = 0; i < n; i++)
sum = sum + _Employee[i].SumSalary();
Console.WriteLine("Tong luong cua cac Nhan Vien Phong Thi Nghiem la: {0}", sum);
Console.WriteLine("--------------------------------------------------------------------------");
break;
default:
Console.WriteLine("Xin vui lòng chọn từ 1 đến 7");
break;
}
} while (choose != 7);
}
static void ShowMenu()
{
Console.WriteLine("\nCHUONG TRINH QUAN LY VIEN KHOA HOC C#");
Console.WriteLine("*************************MENU**************************");
Console.WriteLine("** 1. Nhập số lượng nhà khoa học **");
Console.WriteLine("** 2. Nhập số lượng nhà quản lý **");
Console.WriteLine("** 3. Nhập số lượng nhân viên phòng thí nghiệm **");
Console.WriteLine("** 4. Xem danh sách và tổng lương nhà khoa học **");
Console.WriteLine("** 5. Xem danh sách và tổng lương nhà quản lý **");
Console.WriteLine("** 6. Xem danh sách và tổng lương nhân viên **");
Console.WriteLine("** 7. Thoát **");
Console.WriteLine("** Chọn: **");
Console.WriteLine("*******************************************************");
}
}
}
Bước 6: Nhấn Ctrl+F5 để chạy và xem kết quả
Chọn 1 nhập số lượng nhà khoa học
Chọn 4 nhà khoa học