You are on page 1of 15

Báo cáo thực hành – Kỹ thuật lập trình nâng cao

Họ và tên: Nguyễn Thị Minh Hân

MSSV: 21200081

Lớp: 21DienTu

Lab 2: Kế thùa và Đa hình


Bài 1:

Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab3_KeThuaDaHinh
{
public class UserInformation
{
private string userName;
private uint userAge;
private uint userID;
private double baseSalary;
public UserInformation(string name, uint age, uint id)
{
UserName = name;
UserAge = age;
UserID = id;
}
public UserInformation(string name, uint age, uint id, double bSalary): this(name, age, id)
{
BaseSalary = bSalary;
}
public double BaseSalary
{
get { return baseSalary; }

1
Báo cáo thực hành – Kỹ thuật lập trình nâng cao

set { baseSalary = value; }


}
public string UserName
{
get { return userName; }
set { userName = value; }
}
public uint UserAge
{
get { return userAge; }
set
{
if (value < 18)
throw new Exception("User's age must be larger than 18!");
else
userAge = value;
}
}
public uint UserID
{
get { return userID; }
set
{
if ((value < 1000) || (value > 9999))
throw new Exception("Invalid ID!");
else
userID = value;
}
}
public override string ToString()
{
string str;
str = "Your name is " + UserName.ToString();
str += ("\nYour age is " + UserAge.ToString());
str += ("\nYour ID is " + UserID.ToString());
return str;
}
public virtual double GetSalary()
{
return BaseSalary;
}

2
Báo cáo thực hành – Kỹ thuật lập trình nâng cao

Code

3
Báo cáo thực hành – Kỹ thuật lập trình nâng cao

Code

Hình ảnh chụp kết quả

4
Báo cáo thực hành – Kỹ thuật lập trình nâng cao

Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab3_KeThuaDaHinh
{
internal class Program
{
static void Swap(UserInformation[] array, int i, int j)
{
UserInformation temp = array[i];
array[i] = array[j];
array[j] = temp;
}
static int Partition(UserInformation[] array, int low, int high)
{
double pivot = array[high].GetSalary();
int i = low - 1;
for (int j = low; j < high; j++)
{
if (array[j].GetSalary() < pivot)
{
i++;
Swap(array, j, i);
}
}

5
Báo cáo thực hành – Kỹ thuật lập trình nâng cao

Swap(array, i+1, high);


return i + 1;
}
static void QuickSort(UserInformation[] array, int low, int high)
{
if (low < high)
{
int pi = Partition(array, low, high);
QuickSort(array, low, pi - 1);
QuickSort(array, pi +1, high);
}
}
static void Main(string[] args)
{
UserInformation[] users = new UserInformation[6] { new Employee(" Employee A", 20, 1001, 30000000),
new Employee(" Employee B", 21, 1002, 10000000),
new Manager(" Manager A", 20, 1003, 12000000),
new Manager(" Managere B", 25, 1004, 10000000),
new Director(" Director A", 30, 1005, 15000000),
new Director(" Director A", 25, 1006, 15000000),
};
QuickSort(users, 0, users.Length-1);
foreach (var user in users)
{
Console.WriteLine(user.ToString());
Console.WriteLine($"Base Salary = {user.BaseSalary}; Salary = {user.GetSalary()}" );
//Console.ReadLine();
}
Console.ReadLine();
}
}
}

Hình ảnh chụp kết quả

6
Báo cáo thực hành – Kỹ thuật lập trình nâng cao

Bài 2

Code

7
Báo cáo thực hành – Kỹ thuật lập trình nâng cao

Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab3_KeThuaDaHinh
{
internal class BankAccount
{
private double balance;
private bool isPasswordVerified;

public BankAccount(double initialBalance)


{
balance = initialBalance;
isPasswordVerified = false;
}
// Property Balance with access permission check
public double Balance
{
get
{

8
Báo cáo thực hành – Kỹ thuật lập trình nâng cao

CheckAccessPermission();
return balance;
}
set
{
CheckAccessPermission();
balance = value;
}
}
// Method to check access permission and throw AccessDeniedException if needed
private void CheckAccessPermission()
{
if (!isPasswordVerified)
{
throw new AccessDeniedException("Access denied: Password verification required.");
}
}
// Method to verify password
public void VerifyPassword(string password)
{
// Simulate password verification process
if (password == "correctpassword")
{
isPasswordVerified = true;
}
else
{
isPasswordVerified = false;
}
}
}
}

9
Báo cáo thực hành – Kỹ thuật lập trình nâng cao

Information

Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab3_KeThuaDaHinh
{
public class UserInformation
{
private string userName;
private uint userAge;
private uint userID;
private double baseSalary;

public UserInformation(string name, uint age, uint id)


{
UserName = name;
UserAge = age;
UserID = id;
}

public UserInformation(string name, uint age, uint id, double bSalary) : this(name, age, id)
{
BaseSalary = bSalary;
}

public double BaseSalary


{
get { return baseSalary; }
set { baseSalary = value; }
}

public string UserName


{
get { return userName; }

10
Báo cáo thực hành – Kỹ thuật lập trình nâng cao

set { userName = value; }


}

public uint UserAge


{
get { return userAge; }
set
{
if (value < 18)
throw new UserAccountException("User's age must be larger than 18!");
else
userAge = value;
}
}

public uint UserID


{
get { return userID; }
set
{
if ((value < 1000) || (value > 9999))
throw new UserAccountException("Invalid ID!");
else
userID = value;
}
}

public override string ToString()


{
string str;
str = "Your name is " + UserName.ToString();
str += ("\nYour age is " + UserAge.ToString());
str += ("\nYour ID is " + UserID.ToString());
return str;
}

public virtual double GetSalary()


{
return BaseSalary;
}
}
}

11
Báo cáo thực hành – Kỹ thuật lập trình nâng cao

Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab3_KeThuaDaHinh
{
public class Program
{
static void Main(string[] args)
{
try
{
BankAccount alexAccount = null;

12
Báo cáo thực hành – Kỹ thuật lập trình nâng cao

GetUserBankAccountInformation(out alexAccount);
Console.WriteLine("Please type in your password");
string password = Console.ReadLine();
alexAccount.VerifyPassword(password);
Console.WriteLine(alexAccount.CalculateInterestMoney());
}
catch (AccessDeniedException)
{
Console.WriteLine("Access denied!");
}
catch (OverflowException ex)
{
Console.WriteLine(ex.StackTrace);
}
catch (UserAccountException ex)
{
Console.WriteLine(ex.Message);
}

// Hiển thị thông điệp và chờ người dùng nhấn một phím bất kỳ trước khi đóng cửa sổ terminal
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}

static void GetUserBankAccountInformation(out BankAccount account)


{
string userName;
uint userAge;
uint userID;
double balance;
string password;

Console.Write("Enter UserName: ");


userName = Console.ReadLine();

Console.Write("Enter UserAge: ");


while (!uint.TryParse(Console.ReadLine(), out userAge))
{
Console.WriteLine("Invalid input. Please enter a valid age.");
Console.Write("Enter UserAge: ");
}

// Throwing UserAccountException for invalid user age


if (userAge < 18)
{
throw new UserAccountException("User's age must be larger than 18!");
}

Console.Write("Enter UserID: ");


while (!uint.TryParse(Console.ReadLine(), out userID))
{
Console.WriteLine("Invalid input. Please enter a valid ID.");
Console.Write("Enter UserID: ");
}

// Throwing UserAccountException for invalid user ID


if (userID < 1000 || userID > 9999)
{
throw new UserAccountException("Invalid ID!");

13
Báo cáo thực hành – Kỹ thuật lập trình nâng cao

Console.Write("Enter Balance: ");


while (!double.TryParse(Console.ReadLine(), out balance))
{
Console.WriteLine("Invalid input. Please enter a valid balance.");
Console.Write("Enter Balance: ");
}

Console.Write("Enter password: ");


password = Console.ReadLine();

account = new BankAccount(balance);

account.VerifyPassword(password);
}
}
}

Hình ảnh chụp kết quả

14
Báo cáo thực hành – Kỹ thuật lập trình nâng cao

15

You might also like