[Examination] Kiểm tra 30 phút - Chương trình quản lý thiết bị phòng lab cho trường Aptech Việt Nam - Lập trình C# - Khoá học lập trình C Sharp
Bạn được yêu cầu thiết kế chương trình quản lý thiết bị phòng lab cho trường Aptech như sau.
Yêu cầu thiết kế các class object
1. Device -> Gồm các thuộc tính tên thiết bị, mã thiết bị, ngày mua
2. PC kế thừa từ Device
3. Laptop kế thừa từ Device
Viết getter/setter, hàm tạo và phương thức Nhập và Xem thông tin.
Tạo 1 lớp Test viết yêu cầu sau:
Tao 1 function đặt tên là static int CountDevice(List<Device> deviceList, string type) -> Trả về số thiết bị cần xem (type nhận các giá trị: PC -> trả về số PC trong mảng deviceList, Laptop -> trả về số Laptop trong mảng deviceList)
Trong phương thức Main -> Tạo ngẫu nhiên N thiết bị PC & Laptop yêu cầu sử dụng phương thức CountDevice trên để đếm số PC và Laptop.
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Hoàng Thiện Thanh [community,AAHN-C2009G]](https://www.gravatar.com/avatar/58e377dde293f6da38c0b5168578557a.jpg?s=80&d=mm&r=g)
Hoàng Thiện Thanh
2021-10-18 06:54:35
#Program.cs
using LabManager.Models;
using LabManager.Utils;
using System;
using System.Collections.Generic;
namespace LabManager
{
public class Program
{
public static List<Device> DeviceList = new List<Device>();
public static List<string> dvc = new List<string> { "PC", "Laptop" };
static void ShowMenu()
{
Console.WriteLine("1. Input new devices\n" +
"2. Count devices by type\n" +
"3. Display all devices\n" +
"4. Exit\n");
}
static void Input()
{
Console.WriteLine("Enter number of devices:");
int N = Utility.ReadInt();
for (int i = 0; i < N; i++)
{
Console.WriteLine("Choose type of Devices (Laptop, PC)");
string str = Console.ReadLine();
while (!str.Equals(dvc))
{
Console.WriteLine("Device do not match the following types");
str = Console.ReadLine();
}
switch (str)
{
case "Laptop":
Device d = new Laptop();
d.Input();
DeviceList.Add(d);
break;
case "PC":
d = new PC();
d.Input();
DeviceList.Add(d);
break;
}
}
}
public static int CountDevices(List<Device> DeviceList, string Type)
{
int count = 0;
foreach(Device d in DeviceList)
{
if (Type.Equals(d.Type()))
{
count++;
}
}
return count;
}
static void Display()
{
foreach(Device d in DeviceList)
{
d.Display();
}
}
static void Main(string[] args)
{
int choose;
do
{
ShowMenu();
choose = Utility.ReadInt();
switch (choose)
{
case 1:
Input();
break;
case 2:
string Type = Console.ReadLine();
while (!Type.Equals(dvc))
{
Console.WriteLine("Type does not match");
Type = Console.ReadLine();
}
int count = CountDevices(DeviceList, Type);
Console.WriteLine("Number of {0} devices is {1}", Type, count);
break;
case 3:
Display();
break;
case 4:
Console.WriteLine("Exited");
break;
default:
Console.WriteLine("Wrong input");
break;
}
} while (choose != 4);
}
}
}
#Device.cs
using LabManager.Utils;
using System;
using System.Collections.Generic;
using System.Text;
namespace LabManager.Models
{
public abstract class Device
{
public string DeviceName { get; set; }
public string DeviceCode { get; set; }
public DateTime DateBought { get; set; }
public abstract string Type();
public Device()
{
}
public Device(string DeviceName, string DeviceCode, DateTime DateBought)
{
this.DeviceName = DeviceName;
this.DeviceCode = DeviceCode;
this.DateBought = DateBought;
}
public Device(string DeviceName, string DeviceCode, string DateBought)
{
this.DeviceName = DeviceName;
this.DeviceCode = DeviceCode;
this.DateBought = DateTime.Parse(DateBought);
}
public virtual void Input()
{
Console.WriteLine("Input Device Name: ");
DeviceName = Console.ReadLine();
Console.WriteLine("Input Device Code: ");
DeviceCode = Console.ReadLine();
Console.WriteLine("Input Purchased Date: ");
DateBought = Utility.ReadDate();
}
public virtual void Display()
{
Console.WriteLine("Device Name: " + DeviceName + "\nDevice Code: " + DeviceCode + "\nDate Bought: " + DateBought.ToString("dd/MM/yyyy"));
}
}
}
#Laptop.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace LabManager.Models
{
public class Laptop : Device
{
public Laptop()
{
}
public Laptop(string DeviceName, string DeviceCode, DateTime BoughtDate) : base(DeviceName, DeviceCode, BoughtDate)
{
}
public override string Type()
{
return "Laptop";
}
}
}
#PC.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace LabManager.Models
{
public class PC : Device
{
public PC()
{
}
public PC(string DeviceName, string DeviceCode, DateTime BoughtDate) : base(DeviceName, DeviceCode, BoughtDate)
{
}
public override string Type()
{
return "PC";
}
}
}
#Utility.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace LabManager.Utils
{
public class Utility
{
public static DateTime ReadDate()
{
string str = Console.ReadLine();
DateTime date = new DateTime();
bool check()
{
int ERR = 0;
try { date = DateTime.Parse(str); } catch (Exception) { ERR++; }
try { if (ERR == 1) date = Convert.ToDateTime(str); } catch (Exception) { ERR++; }
if (ERR == 2) { return false; }
return true;
}
while (!check())
{
Console.WriteLine("Date time is a wrong input");
str = Console.ReadLine();
}
return date;
}
public static int ReadInt()
{
string str = Console.ReadLine();
bool check()
{
try { int.Parse(str); } catch (Exception) { return false; }
return true;
}
while (!check())
{
Console.WriteLine("Input cannot be numeric parsed");
str = Console.ReadLine();
}
return int.Parse(str);
}
}
}
![Nguyễn Việt Hoàng [community,AAHN-C2009G]](https://www.gravatar.com/avatar/bdbde8074d82148dcda6927ccb016466.jpg?s=80&d=mm&r=g)
Nguyễn Việt Hoàng
2021-10-17 15:46:37
#Device.cs
using Ss12.Ultitis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ss12.B2
{
abstract class Device
{
public string DeviceName { get; set; }
public string RollDevice { get; set; }
public DateTime Day { get; set; }
public Device()
{
}
public Device(string devicename, string rolldevice, DateTime day)
{
this.DeviceName = devicename;
this.RollDevice = rolldevice;
this.Day = day;
}
public abstract string Type();
public void Input()
{
Console.WriteLine("Enter Device Name :");
DeviceName = Console.ReadLine();
Console.WriteLine("Enter RollNo Device :");
RollDevice = Console.ReadLine();
Console.WriteLine("Enter Day :");
Day = Ultility.ConvertStringToDateTime(Console.ReadLine());
}
public void Display()
{
Console.WriteLine("Device Name :{0}, RollNo Device :{1}, Day :{2}", DeviceName, RollDevice, Day);
}
}
}
#Laptop.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ss12.B2
{
class Laptop : Device
{
public Laptop()
{
}
public void Input()
{
base.Input();
}
public void Display()
{
base.Display();
}
public override string Type()
{
return "Laptop";
}
}
}
#Mainss.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ss12.B2
{
class Mainss
{
public static List<Device> DeviceList = new List<Device>();
static int CountDevice(List<Device> deviceList, string type)
{
int count = 0;
foreach (Device d in DeviceList)
{
if (d.Type().Equals(type))
{
count++;
}
}
return count;
}
static void Input()
{
int sum;
Console.WriteLine("Enter Sum Of Device :");
sum = int.Parse(Console.ReadLine());
for (int i = 0; i < sum; i++)
{
Console.WriteLine("Enter Type :");
string type = Console.ReadLine();
switch (type)
{
case "Laptop":
Device l = new Laptop();
l.Input();
DeviceList.Add(l);
break;
case "PC":
Device p = new PC();
p.Input();
DeviceList.Add(p);
break;
default:
Console.WriteLine("Value must be (Laptop or PC)");
break;
}
}
}
public static void Main(string[] args)
{
Input();
Console.WriteLine("Sum Device :");
string type = Console.ReadLine();
int count = CountDevice(DeviceList, type);
Console.WriteLine(count);
}
}
}
#PC.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ss12.B2
{
class PC : Device
{
public PC()
{
}
public void Input()
{
base.Input();
}
public void Display()
{
base.Display();
}
public override string Type()
{
return "PC";
}
}
}
#Ultility.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ss12.Ultitis
{
class Ultility
{
public static DateTime ConvertStringToDateTime(string str)
{
DateTime outputDateTimeValue;
if (DateTime.TryParseExact(str, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out outputDateTimeValue))
{
return outputDateTimeValue;
}
return new DateTime();
}
public static string ConvertDateTimeToString(DateTime dateTime)
{
return dateTime.ToString("dd/MM/yyyy");
}
}
}
![Nguyễn Hùng Anh [community,C2009G]](https://www.gravatar.com/avatar/32d1c530d2d2c1f3cd46282949f78ebf.jpg?s=80&d=mm&r=g)
Nguyễn Hùng Anh
2021-10-14 01:27:58
#Device.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Lesson04.exam
{
public abstract class Device
{
public string Name { get; set; }
public string Id { get; set; }
public string BuyDate { get; set; }
public abstract string Type();
public Device()
{
}
public Device(string name, string id, string buyDate)
{
Name = name;
Id = id;
BuyDate = buyDate;
}
public virtual void Input()
{
Console.WriteLine("Nhap ten thiet bi: ");
Name = Console.ReadLine();
Console.WriteLine("Nhap ma thiet bi: ");
Id = Console.ReadLine();
Console.WriteLine("Nhap ngay mua thiet bi: ");
BuyDate = Console.ReadLine();
}
public virtual void Display()
{
Console.WriteLine("Ten: " + Name + ", ma thiet bi: " + Id + ", ngay mua: " + BuyDate);
}
}
}
#Laptop.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Lesson04.exam
{
class Laptop : Device
{
public Laptop(string name, string id, string buyDate, string type) : base(name, id, buyDate)
{
}
public Laptop()
{
}
public override void Input()
{
base.Input();
}
public override void Display()
{
base.Display();
}
public override string Type()
{
return "Laptop";
}
}
}
#PC.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Lesson04.exam
{
class PC : Device
{
public PC(string name, string id, string buyDate, string type) : base(name, id, buyDate)
{
}
public PC()
{
}
public override void Input()
{
base.Input();
}
public override void Display()
{
base.Display();
}
public override string Type()
{
return "PC";
}
}
}
#Program.cs
using System;
using System.Collections.Generic;
namespace Lesson04.exam
{
class Program
{
static List<Device> DeviceList = new List<Device>();
static void Main(string[] args)
{
Input();
Find();
}
private static void Find()
{
Console.WriteLine("Nhap vao loai thiet bi can tim: ");
string type = Console.ReadLine();
if(type == "PC" || type == "Laptop")
{
Console.WriteLine("So luong thiet bi {0} la {1}", type, Test.CountDevice(DeviceList, type));
} else
{
Console.WriteLine("Nhap sai!!! Vui long nhap PC hoac Laptop");
}
}
static void Input()
{
Console.WriteLine("Nhap so luong thiet bi can nhap: ");
int N = Utility.ReadInt();
for(int i = 0; i < N; i++)
{
Device device;
Console.WriteLine("***Chon loai thiet bi can them: ");
Console.WriteLine("1. PC");
Console.WriteLine("2. Laptop");
Console.WriteLine("Chon: ");
int c = Utility.ReadInt();
switch (c)
{
case 1:
device = new PC();
device.Input();
DeviceList.Add(device);
break;
case 2:
device = new Laptop();
device.Input();
DeviceList.Add(device);
break;
default:
Console.WriteLine("Nhap sai!!!");
break;
}
}
}
}
}
#Test.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Lesson04.exam
{
class Test
{
public static int CountDevice(List<Device> deviceList, string type)
{
int count = 0;
foreach(Device device in deviceList)
{
if (type == device.Type())
{
count++;
}
}
return count;
}
}
}
#Utility.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Lesson04.exam
{
public class Utility
{
public static int ReadInt()
{
int value;
while(true)
{
try
{
value = int.Parse(Console.ReadLine());
return value;
} catch(Exception ex)
{
Console.WriteLine("Nhap sai!!!");
}
}
}
}
}
![Hieu Ngo [community,C2009G]](https://www.gravatar.com/avatar/cee2973101b9cf9580ef561ff3eeecf0.jpg?s=80&d=mm&r=g)
Hieu Ngo
2021-10-12 03:46:01
#Device.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace TestExam
{
class Device
{
public Device()
{
}
public string Name { get; set; }
public string RollNo { get; set; }
public string Date { get; set; }
public Device(string name, string rollNo, string date)
{
Name = name;
RollNo = rollNo;
Date = date;
}
public virtual void Input()
{
Console.WriteLine("Nhap ten thiet bi");
Name = Console.ReadLine();
Console.WriteLine("Nhap ma thiet bi");
RollNo = Console.ReadLine();
Console.WriteLine("Nhap ngay mua");
Date = Console.ReadLine();
}
public virtual void Display()
{
Console.WriteLine("Ten thiet bi: {0}, Ma thiet bi: {1}, Ngay mua: {2}", Name, RollNo, Date);
}
}
}
#Laptop.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace TestExam
{
class Laptop : Device
{
public Laptop()
{
}
public Laptop(string name, string rollNo, string date) : base(name, rollNo, date)
{
}
public override void Display()
{
base.Display();
}
public override void Input()
{
base.Input();
}
}
}
#PC.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace TestExam
{
class PC : Device
{
public PC()
{
}
public PC(string name, string rollNo, string date) : base(name, rollNo, date)
{
}
public override void Display()
{
base.Display();
}
public override void Input()
{
base.Input();
}
}
}
#Program.cs
using System;
using System.Collections.Generic;
namespace TestExam
{
class Program
{
static void Main(string[] args)
{
List<Device> devices = new List<Device>();
Console.WriteLine("Number of Device:");
int n = Utility.ReadInt();
int i = 0;
while (i < n)
{
Device device = new Device();
device.Input();
devices.Add(device);
i++;
}
Console.WriteLine("Number Of Laptop: {0}", Test.CountDevice(devices, "Laptop"));
Console.WriteLine("Number Of PC:{0}", Test.CountDevice(devices, "PC"));
}
}
}
#Test.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace TestExam
{
class Test
{
public static int CountDevice(List<Device> devices, string type)
{
int count = 0;
foreach(Device device in devices)
{
if(type == device.RollNo)
{
count++;
}
}
return count;
}
}
}
#Utility.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace TestExam
{
public class Utility
{
public static int ReadInt()
{
int value;
while(true)
{
try
{
value = int.Parse(Console.ReadLine());
return value;
} catch (Exception ex)
{
Console.WriteLine("Nhap sai!!!");
}
}
}
}
}
![Nguyễn Quốc Anh [community,C2010G]](https://www.gravatar.com/avatar/cd23a75bbaf19ea66d345e6b133cb7c0.jpg?s=80&d=mm&r=g)
Nguyễn Quốc Anh
2021-10-04 08:51:56
#Device.cs
using System;
namespace Lesson04
{
public class Device
{
public String Name { get; set; }
public String ID { get; set; }
public String Date { get; set; }
public Device() { }
public Device(string name, string id, string date)
{
Name = name;
ID = id;
Date = date;
}
public virtual void Input()
{
Console.WriteLine("Enter Device Name: ");
Name = Console.ReadLine();
Console.WriteLine("Enter Device ID: ");
ID = Console.ReadLine();
Console.WriteLine("Enter Date: ");
Date = Console.ReadLine();
}
public virtual void Display()
{
Console.WriteLine("Name = {0}, ID = {1}, Date = {2}", Name, ID, Date);
}
}
}
#Laptop.cs
namespace Lesson04
{
public class Laptop : Device
{
public Laptop() { }
public Laptop(string name, string id, string date) : base(name, id, date)
{
}
public override void Input()
{
base.Input();
}
public override void Display()
{
base.Display();
}
}
}
#PC.cs
namespace Lesson04
{
public class PC : Device
{
public PC()
{ }
public PC(string name, string id, string date) : base(name, id, date)
{ }
public override void Input()
{
base.Input();
}
public override void Display()
{
base.Display();
}
}
}
#Program.cs
using System;
using System.Collections.Generic;
namespace Lesson04
{
class Test
{
static void Main(string[] args)
{
List<Device> deviceList = new List<Device>();
Console.WriteLine("Enter number of device: ");
int N = int.Parse(Console.ReadLine());
int i = 0;
while (i < N)
{
Device a = new Device();
a.Input();
deviceList.Add(a);
i++;
}
Console.WriteLine("Number of Laptop = {0}", CountDevice(deviceList, "Laptop"));
Console.WriteLine("Number of PC = {0}", CountDevice(deviceList, "PC"));
}
static int CountDevice(List<Device> deviceList, string type)
{
int count = 0;
Device device = new Device();
for (int i = 0; i < deviceList.Count; i++)
{
if (type == device.ID)
{
count++;
}
}
return count;
}
}
}
![Nguyễn Hoàng Hiệp [community,C2010G]](https://www.gravatar.com/avatar/8838f54079683f30f23ba7b170396c08.jpg?s=80&d=mm&r=g)
Nguyễn Hoàng Hiệp
2021-10-04 04:45:50
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Test
{
static void Main(string[] args)
{
List<Main.Device> devices = new List<Main.Device>();
Console.WriteLine("Nhap so thiet bi: ");
int N = int.Parse(Console.ReadLine());
for (int i = 0; i < N; i++)
{
Main.Device a = new Device();
a.Input();
devices[i] = a;
}
Console.WriteLine("Number of Laptop = {0}", CountDevice(devices, "Laptop"));
Console.WriteLine("Number of PC = {0}", CountDevice(devices, "PC"));
}
static int CountDevice(List<Device> deviceList, string type)
{
int count = 0;
Device device = new Device();
for (int i = 0; i < deviceList.Count; i++)
{
if (type == device.ID)
{
count++;
}
}
return count;
}
}
![PHẠM VĂN LIÊM [C2010G]](https://www.gravatar.com/avatar/dd86ba4e56b73bc1514e889b2ac18c6d.jpg?s=80&d=mm&r=g)
PHẠM VĂN LIÊM
2021-10-04 04:28:31
#Laptop.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Exam
{
class Laptop : Device
{
public Laptop()
{
}
public override string MName()
{
return "Laptop";
}
public Laptop(string name, string roll, string date) : base(name, roll, date)
{
}
public override void Input()
{
base.Input();
}
public override void Display()
{
base.Display();
}
}
}
#PC.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Exam
{
class PC : Device
{
public override string MName()
{
return "PC";
}
public PC()
{
}
public PC(string name, string roll, string date) :base(name, roll, date) {
}
public override void Input()
{
base.Input();
}
public override void Display()
{
base.Display();
}
}
}
#Program.cs
using System;
using System.Collections.Generic;
namespace Exam
{
class Program
{
static List<Device> deviceList = new List<Device>();
static void Main(string[] args)
{
int choose;
do
{
Console.WriteLine("1:Nhap device pc.");
Console.WriteLine("2:Nhap device lap top.");
Console.WriteLine("3:Nhap device tim.");
Console.WriteLine("4:Thoat.");
while(true)
{
try
{
choose = int.Parse(Console.ReadLine());
return;
} catch
{
Console.WriteLine("Vui long nhap lai");
}
}
switch(choose)
{
case 1:
Console.WriteLine("Ban muon nhap bao nhieu thiet bi PC");
int n = int.Parse(Console.ReadLine());
for(int i = 0; i < n; i++)
{
Device pc = new PC();
pc.Input();
deviceList.Add(pc);
}
break;
case 2:
Console.WriteLine("Ban muon nhap bao nhieu thiet bi Laptop");
int k = int.Parse(Console.ReadLine());
for (int i = 0; i < n; i++)
{
Device lt = new Laptop();
lt.Input();
deviceList.Add(lt);
}
break;
case 3:
int input;
string device;
do
{
Console.WriteLine("1:Xem device Pc");
Console.WriteLine("2:Xem device laptop");
input = int.Parse(Console.ReadLine());
switch(input)
{
case 1:
device = "PC";
break;
case 2:
device = "Laptop";
break;
default:
Console.WriteLine("Please input again!");
break;
}
} while(input != 1 && input != 2);
CountDevice(deviceList, device);
break;
case 4:
Console.WriteLine("Thoat");
break;
default:
Console.WriteLine("Please input again!");
break;
}
} while (choose != 4);
}
static int CountDevice(List<Device> deviceList, string type)
{
int count = 0;
foreach (Device x in deviceList)
{
if (x.MName === type)
{
count++;
}
}
return count;
}
}
}
#Device.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Exam
{
public abstract class Device
{
public abstract string MName();
public string nameDevice { get; set; }
public string rollDevice { get; set; }
public string dateBuy { get; set; }
public Device()
{
}
public Device(string name, string roll, string date)
{
this.nameDevice = name;
this.rollDevice = roll;
this.dateBuy = date;
}
public virtual void Input()
{
Console.WriteLine("NHap ten device");
nameDevice = Console.ReadLine();
Console.WriteLine("Nhap ma thiet bi");
rollDevice = Console.ReadLine();
Console.WriteLine("Nhap ngay mua");
dateBuy = Console.ReadLine();
}
public virtual void Display()
{
Console.WriteLine("Name Device: {0}, Roll: {1}, dateBuy: {2}", nameDevice, rollDevice, dateBuy);
}
}
}