By GokiSoft.com|
15:16 14/10/2021|
C Sharp
[Share Code] Chương trình quản lý Mèo C# (C Sharp) | Chương trình quản lý Cat C# (C Sharp) - AAHN C2009G2
Bài tập - Chương trình quản lý Mèo C# (C Sharp) | Chương trình quản lý Cat C# (C Sharp)
#Program.cs
using System;
using cat.color;
using cat.exam;
using cat.manager;
namespace cat
{
class UsingManagerCat
{
static void Main(string[] args)
{
int choose;
do
{
ShowMenu();
choose = int.Parse(Console.ReadLine());
switch(choose)
{
case 1:
ColorManager.GetInstance().Nhap();
break;
case 2:
ManagerCat.GetInstance().Nhap();
break;
case 3:
ManagerCat.GetInstance().HienThi();
break;
case 4:
ManagerCat.GetInstance().SortByColor();
ManagerCat.GetInstance().HienThi();
break;
case 5:
ManagerCat.GetInstance().SearchByLoai();
break;
case 6:
ManagerCat.GetInstance().SortByIndexColor();
ManagerCat.GetInstance().HienThi();
break;
case 7:
Console.WriteLine("Thoat!!!");
break;
default:
Console.WriteLine("Nhap sai!!!");
break;
}
} while (choose != 7);
}
static void ShowMenu()
{
Console.WriteLine("1. Nhap vao ma mau");
Console.WriteLine("2. Nhap N meo");
Console.WriteLine("3. Hien thi thong meo");
Console.WriteLine("4. Sap xep theo mau");
Console.WriteLine("5. Tim kiem theo loai");
Console.WriteLine("6. Sap xep theo bang mau ColorManager");
Console.WriteLine("7. Thoat");
Console.WriteLine("Chon: ");
}
}
}
#ICat.cs
using System;
namespace cat.exam
{
public interface ICat
{
void Nhap();
void HienThi();
}
}
#CatDetail.cs
using System;
using System.Collections.Generic;
using cat.color;
namespace cat.exam
{
public class CatDetail : ICat
{
public string Loai { get; set; }
public string Mau { get; set; }
public string NoiSong { get; set; }
public int ColorIndex { get; set; }
public CatDetail()
{
}
public CatDetail(string loai, string mau, string noiSong)
{
Loai = loai;
Mau = mau;
NoiSong = noiSong;
}
public void HienThi()
{
Console.WriteLine("Loai: {0}, mau: {1}, noi song: {2}", Loai, Mau, NoiSong);
}
public void Nhap()
{
Console.WriteLine("===========================");
Console.WriteLine("Nhap loai: ");
Loai = Console.ReadLine();
ColorManager.GetInstance().HienThi();
Console.WriteLine("Nhap mau: ");
while(true)
{
Mau = Console.ReadLine();
bool isFind = false;
List<string> colorList = ColorManager.GetInstance().ColorList;
for (int i=0;i< colorList.Count;i++)
{
string color = colorList[i];
if (color == Mau)
{
isFind = true;
ColorIndex = i;
break;
}
}
//foreach(string color in ColorManager.GetInstance().ColorList)
//{
// if(color == Mau)
// {
// isFind = true;
// break;
// }
//}
if(isFind)
{
break;
} else
{
Console.WriteLine("Nhap sai!!! Yeu cau nhap lai!!!");
}
}
Console.WriteLine("Nhap noi song: ");
NoiSong = Console.ReadLine();
}
}
}
#ColorManager.cs
using System;
using System.Collections.Generic;
using cat.exam;
namespace cat.color
{
public class ColorManager : ICat
{
public List<string> ColorList { get; set; }
private static ColorManager _instance = null;
private ColorManager()
{
ColorList = new List<string>();
}
public static ColorManager GetInstance()
{
if(_instance == null)
{
_instance = new ColorManager();
}
return _instance;
}
public void Nhap()
{
string option;
do
{
Console.WriteLine("Nhap ma mau vao: ");
string color = Console.ReadLine();
ColorList.Add(color);
Console.WriteLine("Ban co tiep tuc hay ko Y/n?");
option = Console.ReadLine().ToUpper();
} while (option != "N");
}
public void HienThi()
{
Console.WriteLine("Danh sach ma mau: ");
foreach(string color in ColorList)
{
Console.WriteLine(" " + color);
}
}
public int GetColorIndex(string color)
{
for(int i=0;i<ColorList.Count;i++) {
if(ColorList[i] == color)
{
return i;
}
}
return -1;
}
}
}
#ManagerCat.cs
using System;
using System.Collections.Generic;
using cat.exam;
namespace cat.manager
{
public class ManagerCat
{
public List<CatDetail> CatList { get; set; }
private static ManagerCat _instance = null;
private ManagerCat()
{
CatList = new List<CatDetail>();
}
public static ManagerCat GetInstance()
{
if(_instance == null)
{
_instance = new ManagerCat();
}
return _instance;
}
public void Nhap()
{
Console.WriteLine("Nhap so meo can them N = ");
int N = int.Parse(Console.ReadLine());
for(int i=0;i<N;i++)
{
CatDetail catDetail = new CatDetail();
catDetail.Nhap();
CatList.Add(catDetail);
}
}
internal void SortByColor()
{
CatList.Sort((c1, c2) => {
return c1.Mau.CompareTo(c2.Mau);
});
Console.WriteLine("Sap xep thanh cong!!!");
}
internal void SortByIndexColor()
{
CatList.Sort((c1, c2) => {
if(c1.ColorIndex > c2.ColorIndex)
{
return 1;
}
return -1;
});
Console.WriteLine("Sap xep thanh cong!!!");
}
internal void SearchByLoai()
{
Console.WriteLine("Nhap loai can tim: ");
string loai = Console.ReadLine();
int count = 0;
foreach(CatDetail cat in CatList)
{
if(cat.Loai == loai)
{
cat.HienThi();
count++;
}
}
if(count > 0)
{
Console.WriteLine("So meo tim thay: {0}", count);
} else
{
Console.WriteLine("Ko tim thay meo phu hop");
}
}
public void HienThi()
{
Console.WriteLine("=== Danh sach N meo");
foreach(CatDetail cat in CatList)
{
cat.HienThi();
}
}
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)