Examination & Test - Lập trình C# - Lập trình C Sharp
1. Tạo 1 lớp giao diện IInfor chứa hàm showInfor
2. Tạo lớp đối tượng People gồm các thuộc tính tên, tuổi, địa chỉ kế thừa từ lớp IInfor
- Tạo hàm input
Tạo lớp Car gồm các thuộc tính tên và màu -> kế thừa từ lớp IInfor
- Tạo hàm input
Viết code cho các lớp trên.
3. Trọng phương thức main của lớp Test tạo 2 đối tượng People và Car.
- tạo phương thức như sau.
public static void showInfor(List<IInfor> a) -> hàm này hiển thị thông tin tất cả đối tượng.
Viết chương trình và sử dụng hàm showInfor trong phương thức main.
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![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 03:26:56
#Car.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Lesson05.Exam
{
class Car : IInfor
{
public string Name { get; set; }
public string Color { get; set; }
public Car()
{
}
public Car(string name, string color)
{
Name = name;
Color = color;
}
public void Input()
{
Console.WriteLine("Nhap ten xe: ");
Name = Console.ReadLine();
Console.WriteLine("Nhap mau xe: ");
Color = Console.ReadLine();
}
public void showInfor()
{
Console.WriteLine("Ten xe: {0}, mau: {1}", Name, Color);
}
}
}
#IInfor.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Lesson05.Exam
{
interface IInfor
{
public void showInfor();
}
}
#People.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Lesson05.Exam
{
class People : IInfor
{
public string Name { get; set; }
public int Age { get; set; }
public string Address { get; set; }
public People()
{
}
public People(string name, int age, string address)
{
Name = name;
Age = age;
Address = address;
}
public void Input()
{
Console.WriteLine("Nhap ten: ");
Name = Console.ReadLine();
Console.WriteLine("Nhap tuoi: ");
Age = Utility.ReadInt();
Console.WriteLine("Nhap dia chi: ");
Address = Console.ReadLine();
}
public void showInfor()
{
Console.WriteLine("Ten: {0}, tuoi: {1}, dia chi: {2}", Name, Age, Address);
}
}
}
#Program.cs
using System;
using System.Collections.Generic;
namespace Lesson05.Exam
{
class Program
{
static List<IInfor> inforList;
static void Main(string[] args)
{
inforList = new List<IInfor>();
int c;
do
{
showMenu();
c = Utility.ReadInt();
switch(c)
{
case 1:
InputPeople();
break;
case 2:
InputCar();
break;
case 3:
showInfor(inforList);
break;
case 4:
Console.WriteLine("Thoat!!!");
break;
default:
Console.WriteLine("Nhap sai!!!");
break;
}
} while (c != 4);
}
private static void showInfor(List<IInfor> a)
{
Console.WriteLine("======================================");
Console.WriteLine("Tat ca thong tin: ");
for(int i = 0; i < a.Count; i++)
{
a[i].showInfor();
}
}
private static void InputCar()
{
Console.WriteLine("======================================");
Console.WriteLine("Nhap so luong xe can them: ");
int N = Utility.ReadInt();
for(int i = 0; i < N; i++)
{
Car car = new Car();
car.Input();
inforList.Add(car);
}
}
private static void InputPeople()
{
Console.WriteLine("======================================");
Console.WriteLine("Nhap so luong nguoi can them: ");
int n = Utility.ReadInt();
for(int i = 0; i < n; i++)
{
People people = new People();
people.Input();
inforList.Add(people);
}
}
static void showMenu()
{
Console.WriteLine("======================================");
Console.WriteLine("1. Nhap thong tin nguoi mua");
Console.WriteLine("2. Nhap thong tin xe");
Console.WriteLine("3. Hien thi toan bo thong tin");
Console.WriteLine("4. Thoat");
Console.WriteLine("Chon: ");
}
}
}
#Utility.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Lesson05.Exam
{
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-14 03:22:02
#Car.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Test
{
class Car : IInfor
{
public Car()
{
}
public string Name { get; set; }
public string Color { get; set; }
public Car(string name, string color)
{
Name = name;
Color = color;
}
public void Input()
{
Console.WriteLine("Nhap ten xe:");
Name = Console.ReadLine();
Console.WriteLine("Mau xe:");
Color = Console.ReadLine();
}
public void ShowInfor()
{
Console.WriteLine("Ten xe: {0}, Mau xe: {1}", Name, Color);
}
}
}
#IInfor.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Test
{
interface IInfor
{
public void ShowInfor();
}
}
#People .cs
using System;
using System.Collections.Generic;
using System.Text;
using Test.Utils;
namespace Test
{
class People : IInfor
{
public string FullName { get; set; }
public int Age { get; set; }
public string Address { get; set; }
public People()
{
}
public People(string fullName, int age, string address)
{
FullName = fullName;
Age = age;
Address = address;
}
public void Input()
{
Console.WriteLine("Nhap ho va ten:");
FullName = Console.ReadLine();
Console.WriteLine("Nhap tuoi:");
Age = Utility.ReadInt();
Console.WriteLine("Nhap dia chi:");
Address = Console.ReadLine();
}
public void ShowInfor()
{
Console.WriteLine("Full Name: {0}, Age: {1}, Address: {2}", FullName, Age, Address);
}
}
}
#Program.cs
using System;
using System.Collections.Generic;
namespace Test
{
class Program
{
static void Main(string[] args)
{
List<IInfor> infors = new List<IInfor>();
People people = new People();
Car car = new Car();
people.Input();
infors.Add(people);
car.Input();
infors.Add(car);
ShowInfor(infors);
}
private static void ShowInfor(List<IInfor> infors)
{
foreach(var item in infors)
{
item.ShowInfor();
}
}
}
}
![Đào Mạnh Dũng [C2010L]](https://www.gravatar.com/avatar/6a111fa53fd75dc87034660a8857df16.jpg?s=80&d=mm&r=g)
Đào Mạnh Dũng
2021-10-10 07:10:52
#Car.cs
using System;
namespace _1505
{
internal class Car : IInfor
{
public string name, color;
public Car()
{
}
public Car(string name, string color)
{
this.name = name;
this.color = color;
}
public void input()
{
Console.WriteLine("nhap name : ");
this.name = Console.ReadLine();
Console.WriteLine("nhap color : ");
this.color = Console.ReadLine();
}
public void showInfor()
{
System.Console.WriteLine(this);
}
public override string ToString()
{
return "Car{" + "name=" + name + ", color=" + color + '}';
}
}
}
#IInfor.cs
namespace _1505
{
internal interface IInfor
{
public void showInfor();
}
}
#People .cs
using System;
namespace _1505
{
internal class People : IInfor
{
public string name, address;
public int age;
public People()
{
}
public People(string name, int age, string address)
{
this.name = name;
this.age = age;
this.address = address;
}
public void input()
{
Console.WriteLine("nhap name : ");
this.name = Console.ReadLine();
Console.WriteLine("nhap age : ");
this.age = Utility.ReadInt();
Console.WriteLine("nhap address : ");
this.address = Console.ReadLine();
}
public void showInfor()
{
Console.WriteLine(this);
}
public override string ToString()
{
return "People{" + "name=" + name + ", age=" + age + ", address=" + address + '}';
}
}
}
#Test.cs
using System.Collections.Generic;
namespace _1505
{
internal class Test
{
public static void showInfor(List<IInfor> infor)
{
System.Console.WriteLine("-----------------------");
foreach (var item in infor)
{
item.showInfor();
System.Console.WriteLine("-----------------------");
}
}
private static void Main(string[] args)
{
List<IInfor> infor = new List<IInfor>();
People people = new People();
Car car = new Car();
people.input();
car.input();
infor.Add(people);
infor.Add(car);
showInfor(infor);
}
}
}
#Utility.cs
using System;
namespace _1505
{
internal class Utility
{
public static int ReadInt()
{
int integer = 0;
while (true)
{
try
{
integer = int.Parse(Console.ReadLine());
break;
}
catch (Exception)
{
Console.Write("du lieu dau vao khong hop le, nhap lai : ");
}
}
return integer;
}
}
}
![Đào Mạnh Dũng [C2010L]](https://www.gravatar.com/avatar/6a111fa53fd75dc87034660a8857df16.jpg?s=80&d=mm&r=g)
Đào Mạnh Dũng
2021-10-10 07:10:51
#Car.cs
using System;
namespace _1505
{
internal class Car : IInfor
{
public string name, color;
public Car()
{
}
public Car(string name, string color)
{
this.name = name;
this.color = color;
}
public void input()
{
Console.WriteLine("nhap name : ");
this.name = Console.ReadLine();
Console.WriteLine("nhap color : ");
this.color = Console.ReadLine();
}
public void showInfor()
{
System.Console.WriteLine(this);
}
public override string ToString()
{
return "Car{" + "name=" + name + ", color=" + color + '}';
}
}
}
#IInfor.cs
namespace _1505
{
internal interface IInfor
{
public void showInfor();
}
}
#People .cs
using System;
namespace _1505
{
internal class People : IInfor
{
public string name, address;
public int age;
public People()
{
}
public People(string name, int age, string address)
{
this.name = name;
this.age = age;
this.address = address;
}
public void input()
{
Console.WriteLine("nhap name : ");
this.name = Console.ReadLine();
Console.WriteLine("nhap age : ");
this.age = Utility.ReadInt();
Console.WriteLine("nhap address : ");
this.address = Console.ReadLine();
}
public void showInfor()
{
Console.WriteLine(this);
}
public override string ToString()
{
return "People{" + "name=" + name + ", age=" + age + ", address=" + address + '}';
}
}
}
#Test.cs
using System.Collections.Generic;
namespace _1505
{
internal class Test
{
public static void showInfor(List<IInfor> infor)
{
System.Console.WriteLine("-----------------------");
foreach (var item in infor)
{
item.showInfor();
System.Console.WriteLine("-----------------------");
}
}
private static void Main(string[] args)
{
List<IInfor> infor = new List<IInfor>();
People people = new People();
Car car = new Car();
people.input();
car.input();
infor.Add(people);
infor.Add(car);
showInfor(infor);
}
}
}
#Utility.cs
using System;
namespace _1505
{
internal class Utility
{
public static int ReadInt()
{
int integer = 0;
while (true)
{
try
{
integer = int.Parse(Console.ReadLine());
break;
}
catch (Exception)
{
Console.Write("du lieu dau vao khong hop le, nhap lai : ");
}
}
return integer;
}
}
}
![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-05 09:34:05
#Car.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace ExTestCSharp
{
class Car : IInfor
{
public string Name { get; private set; }
public string Color { get; private set; }
public void input()
{
Console.WriteLine("Enter Name:");
Name = Console.ReadLine();
Console.WriteLine("Enter Color:");
Color = Console.ReadLine();
}
public void showInfor()
{
Console.WriteLine("Car:");
Console.WriteLine("\tName: {0}\n\tColor: {1}", Name, Color);
}
}
}
#IInfor.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace ExTestCSharp
{
interface IInfor
{
void showInfor();
}
}
#People.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace ExTestCSharp
{
class People : IInfor
{
public string Name { get; private set; }
public int Age { get; private set; }
public string Address { get; private set; }
public void input()
{
Console.WriteLine("Enter Name:");
Name = Console.ReadLine();
Console.WriteLine("Enter Age:");
Age = Utility.ReadInt();
Console.WriteLine("Enter Address:");
Address = Console.ReadLine();
}
public void showInfor()
{
Console.WriteLine("Student:");
Console.WriteLine("\tName: {0}\n\tAge: {1}\n\tAddress: {2}", Name, Age, Address);
}
}
}
#Program.cs
using System;
using System.Collections.Generic;
namespace ExTestCSharp
{
class Program
{
public static List<IInfor> info = new List<IInfor>();
public static void showInfor(List<IInfor> a)
{
foreach (IInfor i in a)
{
i.showInfor();
}
}
static void Main(string[] args)
{
People p = new People();
p.input();
//p.showInfor();
info.Add(p);
Car c = new Car();
c.input();
//c.showInfor();
info.Add(c);
showInfor(info);
}
}
}
#Utility.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace ExTestCSharp
{
class Utility
{
public static int ReadInt()
{
string str = Console.ReadLine();
bool check()
{
try { int.Parse(str); }
catch (Exception)
{
Console.WriteLine("Input is not a number");
return false;
}
return true;
}
while (!check())
{
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-05 09:18:34
#Car.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ss7.B1
{
class Car : IInfor
{
public string Name { get; set; }
public string Color { get; set; }
public Car()
{
}
public Car(string name , string color)
{
this.Name = name;
this.Color = color;
}
public void input()
{
Console.WriteLine("Enter Name :");
Name = Console.ReadLine();
Console.WriteLine("Enrer Color :");
Color = Console.ReadLine();
}
public void showInfo()
{
Console.WriteLine("Name :{0}, Color :{1}", Name, Color);
}
}
}
#IInfor.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ss7.B1
{
interface IInfor
{
public void showInfo();
}
}
#People.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ss7.B1
{
class People : IInfor
{
public string Name { get; set; }
public int Age { get; set; }
public string Address { get; set; }
public People()
{
}
public People(string name, int age, string address)
{
this.Name = name;
this.Age = age;
this.Address = address;
}
public void input()
{
Console.WriteLine("Enter Name :");
Name = Console.ReadLine();
Console.WriteLine("Enter Age :");
Age = int.Parse(Console.ReadLine());
Console.WriteLine("Enter Address :");
Address = Console.ReadLine();
}
public void showInfo()
{
Console.WriteLine("Name :{0}, Age :{1}, Address :{2}",Name,Age,Address);
}
}
}
#Program.cs
using Ss7.B1;
using System;
using System.Collections.Generic;
namespace Ss7
{
class Program
{
public static List<IInfor> inf = new List<IInfor>();
public static void showInfo(List<IInfor> a)
{
foreach(IInfor i in a)
{
i.showInfo();
}
}
static void Main(string[] args)
{
People a = new People();
a.input();
inf.Add(a);
Car car = new Car();
car.input();
inf.Add(car);
showInfo(inf);
}
}
}
![vuong huu phu [T2008A]](https://www.gravatar.com/avatar/307a5cf29780afab49706dc8b15b86c6.jpg?s=80&d=mm&r=g)
vuong huu phu
2021-05-29 07:38:51
using System;
using System.Collections.Generic;
namespace Examination_test
{
class Program
{
static void Main(string[] args)
{
List<IInfor> a = new List<IInfor>();
Car c = new Car();
People p = new People();
c.input();
p.input();
a.Add(c);
a.Add(p);
show(a);
}
public static void show(List<IInfor> a) {
foreach (IInfor i in a) {
i.showinfor();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Examination_test
{
interface IInfor
{
void showinfor();
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Examination_test
{
class People : IInfor
{
public String Ten { set; get; }
public int tuoi { set; get; }
public String diachi { set; get; }
public People() { }
public void input() {
Console.WriteLine("Nhap ten ");
Ten = Console.ReadLine();
Console.WriteLine("Nhap tuoi");
tuoi = Int32.Parse(Console.ReadLine());
Console.WriteLine("Nhap dia chi");
diachi = Console.ReadLine();
}
public void showinfor()
{
Console.WriteLine(Ten + "/" + tuoi + "/" + diachi);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Examination_test
{
class Car:IInfor
{
public String ten { set; get; }
public String mau { set; get; }
public Car() { }
public void input() {
Console.WriteLine("Nhap ten xe:");
ten = Console.ReadLine();
Console.WriteLine("Nhap mau xe");
mau = Console.ReadLine();
}
public void showinfor()
{
Console.WriteLine(ten + "||" + mau);
}
}
}
![Do Trung Duc [T2008A]](https://www.gravatar.com/avatar/2973ac07124f066b4605c535e8d39a99.jpg?s=80&d=mm&r=g)
Do Trung Duc
2021-05-29 03:58:29
using System;
using System.Collections.Generic;
using System.Text;
namespace Lesson7.Exam
{
class People : IShowInfor
{
public string Name { get; set; }
public int Age { get; set; }
public string Address { get; set; }
public People (){ }
public void Input()
{
Console.WriteLine("Nhap thong tin nguoi");
Console.WriteLine("Nhap ten:");
Name = Console.ReadLine();
Console.WriteLine("Nhap tuoi:");
Age = Int32.Parse(Console.ReadLine());
Console.WriteLine("Nhap dia chi:");
Address = Console.ReadLine();
}
public void ShowInfor()
{
Console.WriteLine("Ten: {0}, Tuoi: {1}, Dia chi: {2}", Name, Age, Address);
}
}
}
![Do Trung Duc [T2008A]](https://www.gravatar.com/avatar/2973ac07124f066b4605c535e8d39a99.jpg?s=80&d=mm&r=g)
Do Trung Duc
2021-05-29 03:58:16
using System;
using System.Collections.Generic;
using System.Text;
namespace Lesson7.Exam
{
class Car : IShowInfor
{
public string Name { get; set; }
public string Color { get; set; }
public Car() { }
public void Input()
{
Console.WriteLine("Nhap thong tin xe");
Console.WriteLine("Nhap ten:");
Name = Console.ReadLine();
Console.WriteLine("Nhap mau:");
Color = Console.ReadLine();
}
public void ShowInfor()
{
Console.WriteLine("Ten: {0}, Mau: {1}", Name, Color);
}
}
}
![Do Trung Duc [T2008A]](https://www.gravatar.com/avatar/2973ac07124f066b4605c535e8d39a99.jpg?s=80&d=mm&r=g)
Do Trung Duc
2021-05-29 03:58:01
using System;
using System.Collections.Generic;
namespace Lesson7.Exam
{
class Program
{
static void Main(string[] args)
{
List<IShowInfor> ShowInfoList = new List<IShowInfor>();
Car car = new Car();
car.Input();
ShowInfoList.Add(car);
People people = new People();
people.Input();
ShowInfoList.Add(people);
ShowInfo(ShowInfoList);
}
public static void ShowInfo(List<IShowInfor> ShowInfoList)
{
foreach(IShowInfor ishow in ShowInfoList)
{
ishow.ShowInfor();
}
}
}
}