By GokiSoft.com|
08:37 14/10/2021|
C Sharp
[Video] Chường trình quản lý sở thú - Lập trình C# - Lập trình C Sharp - C2009G
Chường trình quản lý sở thú - Lập trình C# - Lập trình C Sharp
#Program.cs
using System;
namespace BT1734
{
class Program
{
static Zoo zoo;
static void Main(string[] args)
{
zoo = new Zoo();
int choose;
do
{
ShowMenu();
choose = Utility.ReadInt();
switch(choose)
{
case 1:
zoo.ThemChuong();
break;
case 2:
zoo.XoaChuong();
break;
case 3:
ThemConVat();
break;
case 4:
XoaConVat();
break;
case 5:
XemThongTin();
break;
case 6:
LuuJSONFile();
break;
case 7:
//DocJSONFile();
break;
case 8:
Console.WriteLine("Thoat!!!");
break;
default:
Console.WriteLine("Nhap sai!!!");
break;
}
} while (choose != 8);
}
//private static void DocJSONFile()
//{
// //B1. Doc noi dung json tu file zoo.json
// string json = System.IO.File.ReadAllText(@"zoo.json");
// if(json != null && json != "")
// {
// zoo = Newtonsoft.Json.JsonConvert.DeserializeObject<Zoo>(json);
// }
// Console.WriteLine("Doc noi dung thanh cong!!!");
//}
private static void LuuJSONFile()
{
//B1. Convert zoo object -> json string
string json = Newtonsoft.Json.JsonConvert.SerializeObject(zoo);
//B2. Luu json string -> FILE
System.IO.File.WriteAllText(@"zoo.json", json);
Console.WriteLine("Luu thong tin thanh cong!!!");
}
private static void XemThongTin()
{
Console.WriteLine("=== Xem thong tin");
foreach(Chuong chuong in zoo.DanhSachChuong)
{
chuong.XemThongTin();
}
}
private static void XoaConVat()
{
Console.WriteLine("Nhap Ma Chuong Can Xoa Dong Vat: ");
int machuong = Utility.ReadInt();
foreach (Chuong chuong in zoo.DanhSachChuong)
{
if (chuong.MaChuong == machuong)
{
Console.WriteLine("Nhap ten dong vat can xoa: ");
string name = Console.ReadLine();
chuong.XoaConVat(name);
return;
}
}
Console.WriteLine("Khong tim thay chuong cho ma: " + machuong);
}
private static void ThemConVat()
{
Console.WriteLine("Nhap Ma Chuong Can Them Dong Vat: ");
int machuong = Utility.ReadInt();
foreach(Chuong chuong in zoo.DanhSachChuong)
{
if(chuong.MaChuong == machuong)
{
chuong.ThemConVat();
return;
}
}
Console.WriteLine("Khong tim thay chuong cho ma: " + machuong);
}
static void ShowMenu()
{
Console.WriteLine("1. Them chuong");
Console.WriteLine("2. Xoa chuong");
Console.WriteLine("3. Them con vat");
Console.WriteLine("4. Xoa con vat");
Console.WriteLine("5. Xem thong tin");
Console.WriteLine("6. Luu zoo.json");
Console.WriteLine("7. Doc zoo.json");
Console.WriteLine("8. Thoat");
Console.WriteLine("Chon: ");
}
}
}
#Utility.cs
using System;
namespace BT1734
{
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!!!");
}
}
}
}
}
#Animal.cs
using System;
namespace BT1734
{
public abstract class Animal
{
public string Ten { get; set; }
public int Tuoi { get; set; }
public string MoTa { get; set; }
public Animal()
{
}
public Animal(string ten, int tuoi, string moTa)
{
Ten = ten;
Tuoi = tuoi;
MoTa = moTa;
}
public void NhapThongTin()
{
Console.WriteLine("Nhap ten: ");
Ten = Console.ReadLine();
Console.WriteLine("Nhap mo ta: ");
MoTa = Console.ReadLine();
Console.WriteLine("Nhap tuoi: ");
Tuoi = Utility.ReadInt();
}
public void XemThongTin()
{
Console.WriteLine("Ten: {0}, tuoi: {1}, mo ta: {2}", Ten, Tuoi, MoTa);
}
public abstract void TiengKeu();
}
}
#Dog.cs
using System;
namespace BT1734
{
public class Dog : Animal
{
public Dog()
{
}
public override void TiengKeu()
{
Console.WriteLine("Dog ...");
}
}
}
#Cat.cs
using System;
namespace BT1734
{
public class Cat : Animal
{
public Cat()
{
}
public override void TiengKeu()
{
Console.WriteLine("Cat ...");
}
}
}
#Tiger.cs
using System;
namespace BT1734
{
public class Tiger : Animal
{
public Tiger()
{
}
public override void TiengKeu()
{
Console.WriteLine("Tiger ...");
}
}
}
#Chuong.cs
using System;
using System.Collections.Generic;
namespace BT1734
{
public class Chuong
{
public int MaChuong { get; set; }
public List<Animal> AnimalList { get; set; }
public Chuong()
{
AnimalList = new List<Animal>();
}
public void NhapThongTin()
{
Console.WriteLine("Nhap ma chuong: ");
MaChuong = Utility.ReadInt();
Console.WriteLine("Nhap so dong vat can them: ");
int N = Utility.ReadInt();
for(int i=0;i<N;i++)
{
ThemConVat();
}
}
public void XemThongTin()
{
Console.WriteLine("=== Ma chuong: {0}", MaChuong);
Console.WriteLine("Danh sach dong vat");
foreach(Animal animal in AnimalList)
{
animal.XemThongTin();
}
}
public void ThemConVat()
{
Console.WriteLine("=== Chon dong vat can them: ");
Console.WriteLine("1. Tiger");
Console.WriteLine("2. Dog");
Console.WriteLine("3. Cat");
Console.WriteLine("Chon: ");
int choose = Utility.ReadInt();
Animal animal;
switch(choose)
{
case 1:
animal = new Tiger();
break;
case 2:
animal = new Dog();
break;
default:
animal = new Cat();
break;
}
animal.NhapThongTin();
AnimalList.Add(animal);
}
/**
* Xet TH: nhieu dong vat trung ten -> yeu cau xoa tat ca dong vat co ten la "name"
*/
public void XoaConVat(string name)
{
for(int i=0;i<AnimalList.Count;i++) {
if(AnimalList[i].Ten == name)
{
//Mang dong vat = {A, B, K, K, C, D}
//Xoa dong vat co ten: K
//i = 2 => K (3) => K(2)
AnimalList.RemoveAt(i);
i--;
}
}
}
}
}
#Zoo.cs
using System;
using System.Collections.Generic;
namespace BT1734
{
public class Zoo
{
public List<Chuong> DanhSachChuong { get; set; }
public Zoo()
{
DanhSachChuong = new List<Chuong>();
}
public void ThemChuong()
{
Console.WriteLine("Nhap so chuong can them: ");
int N = Utility.ReadInt();
for(int i=0;i<N;i++)
{
Chuong chuong = new Chuong();
chuong.NhapThongTin();
DanhSachChuong.Add(chuong);
}
}
public void XoaChuong()
{
Console.WriteLine("Nhap Ma Chuong can xoa: ");
int machuong = Utility.ReadInt();
foreach(Chuong chuong in DanhSachChuong)
{
if(chuong.MaChuong == machuong)
{
DanhSachChuong.Remove(chuong);
Console.WriteLine("Xoa thanh cong!!!");
return;
}
}
Console.WriteLine("Khong tim thay machuong can xoa!!!");
}
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)