常見的數據結構。。。。
效果圖:
上代碼:
class?Program{
static?void?Main(string[]?args)
{
List<Material>?data?=?new?List<Material>();
while?(true)?{
Console.WriteLine("請輸入數據編號");
string?_id?=?Console.ReadLine();
long?sid?=?-1;
long.TryParse(_id,?out?sid);
while?(sid?<?0)?{
Console.WriteLine("錯誤編號必須大於零,請重新輸入數據編號");
_id?=?Console.ReadLine();
long.TryParse(_id,?out?sid);
}
if?(data.Count?>?0)?{
while?(data.Any(s?=>?s.ID?==?sid))
{
Console.WriteLine("錯誤編號,與其他信息重復,請重新輸入數據編號");
_id?=?Console.ReadLine();
long.TryParse(_id,?out?sid);
}
}
Console.WriteLine("請輸入數據名稱");
string?_name?=?Console.ReadLine();
while?(string.IsNullOrWhiteSpace(_name)?)
{
Console.WriteLine("數據名稱不能為空,請重新輸入數據名稱");
_name?=?Console.ReadLine();
}
Console.WriteLine("請輸入數據類別(單個英文字母)?M/B/C?資料/圖書/CD光盤");
ConsoleKeyInfo?ikey?=?Console.ReadKey();
while?(?!(ikey.Key?==?ConsoleKey.M?||?ikey.Key?==?ConsoleKey.B?||?ikey.Key?==?ConsoleKey.C))
{
Console.WriteLine("輸入錯誤");
Console.WriteLine("請輸入數據類別(單個英文字母)?M/B/C?資料/圖書/CD光盤");
ikey?=?Console.ReadKey();
}
Console.WriteLine("請輸入創建時間,不輸入為當前系統時間");
string?_crimte?=?Console.ReadLine();
DateTime?scrtime?=?DateTime.Now;
if?(!string.IsNullOrWhiteSpace(_crimte))?{
while?(!DateTime.TryParse(_crimte,?out?scrtime))?{
Console.WriteLine("時間格式無法解析,請重新嘗試");
_crimte?=?Console.ReadLine();
}
}
if?(ikey.Key?==?ConsoleKey.M)
{
data.Add(new?Material(sid,?_name,"資料",?scrtime));
}
else?if?(ikey.Key?==?ConsoleKey.B)
{
Console.WriteLine("請輸入圖書作者");
string?_author?=?Console.ReadLine();
while?(string.IsNullOrWhiteSpace(_author))
{
Console.WriteLine("圖書作者不能為空,請重新輸入作者");
_author?=?Console.ReadLine();
}
Console.WriteLine("請輸入圖書售價");
string?_price?=?Console.ReadLine();
while?(string.IsNullOrWhiteSpace(_price))
{
Console.WriteLine("圖書售價不能為空,請重新輸入售價");
_price?=?Console.ReadLine();
}
decimal?_priced?=?0;
while?(!decimal.TryParse(_price,out?_priced)?||?_priced<0)
{
Console.WriteLine("圖書售價非法,不能小於等於0,請重新輸入售價");
_price?=?Console.ReadLine();
}
data.Add(new?Book(sid,?_name,?"圖書",?scrtime,?_author,_priced));
}
else?if?(ikey.Key?==?ConsoleKey.C)
{
Console.WriteLine("請輸入光盤發行者");
string?_publiser?=?Console.ReadLine();
while?(string.IsNullOrWhiteSpace(_publiser))
{
Console.WriteLine("光盤發行者不能為空,請重新輸入光盤發行者");
_publiser?=?Console.ReadLine();
}
Console.WriteLine("請輸入光盤容量");
string?_captistr?=?Console.ReadLine();
while?(string.IsNullOrWhiteSpace(_captistr))
{
Console.WriteLine("光盤容量不能為空,請重新輸入光盤容量");
_captistr?=?Console.ReadLine();
}
double?_capti?=?0;
while?(!double.TryParse(_captistr,?out?_capti)?||?_capti?<?0)
{
Console.WriteLine("光盤容量非法,不能小於等於0,請重新輸入光盤容量");
_captistr?=?Console.ReadLine();
}
data.Add(new?Cdroom(sid,?_name,?"光盤",?scrtime,?_publiser,?_capti));
}
else?{
Console.WriteLine("意外的資料類別無法繼續,?按任意鍵退出程序");
Console.ReadKey();
return;
}
Console.WriteLine("這個資料輸入完畢,按E結束錄入,其他繼續錄入");
var?ife?=?Console.ReadKey();
if?(ife.Key?==?ConsoleKey.E)?{
break;
}
}
foreach?(var?mitem?in?data)?{
Console.WriteLine(?mitem.Show("Chinese"));
}
Console.ReadKey();
}
}
public?class?Material
{
protected?Material()
{
}
public?Material(long?id,string?name,?string?type)?{
ID?=?id;
Name?=?name;
Type?=?type;
CrTime?=?DateTime.Now;
}
public?Material(long?id,?string?name,?string?type,DateTime?crtime)
{
ID?=?id;
Name?=?name;
Type?=?type;
CrTime?=?crtime;
}
public?long?ID;
public?string?Name;
public?string?Type;
public?DateTime?CrTime;
public?new?string?GetType()?{
return?this.Type;
}
public?virtual?string?Show()
{
return?"ID:"?+?ID.ToString()?+?"Name:"?+?Name?+?"Type:"?+?Type?+?"CrTime:"?+?CrTime.ToString()?;
}
public?virtual?string?Show(string?language)?{
switch?(language)?{
case?"Chinese":?{
return?"編號:"?+?ID.ToString()?+?"名稱:"?+?Name?+?"屬性:"?+?Type?+?"創建時間:"?+?CrTime.ToString();
}
default:?return?this.Show();
}
}
}
public?class?Book?:?Material?{
public?Book():base()?{
}
public?Book(long?id,?string?name,?string?type)?:?base(?id,?name,?type)?{?}
public?Book(long?id,?string?name,?string?type,?DateTime?crtime)?:?base(id,?name,?type,?crtime)?{?}
public?Book(long?id,?string?name,?string?type,?DateTime?crtime,string?author,decimal?price)?:?base(id,?name,?type,?crtime)?{
Author?=?author;
Price?=?price;
}
public?decimal?GetPrice()?{
return?this.Price;
}
public?override?string?Show()
{
return?"ID:"?+?ID.ToString()?+?"Name:"?+?Name?+?"Type:"?+?Type?+?"CrTime:"?+?CrTime.ToString()?+?"Author:"?+?Author?+?"Price:"?+?Price.ToString();
}
public?override?string?Show(string?language)
{
switch?(language)
{
case?"Chinese":
{
return?"編號:"?+?ID.ToString()?+?"名稱:"?+?Name?+?"屬性:"?+?Type?+?"創建時間:"?+?CrTime.ToString()?+?"作者:"?+?Author?+?"價格:"?+?Price.ToString();
}
default:?return?this.Show();
}
}
public?string?Author;
public?decimal?Price;
}
public?class?Cdroom?:?Material
{
public?Cdroom()?:?base()
{
}
public?Cdroom(long?id,?string?name,?string?type)?:?base(id,?name,?type)?{?}
public?Cdroom(long?id,?string?name,?string?type,?DateTime?crtime)?:?base(id,?name,?type,?crtime)?{?}
public?Cdroom(long?id,?string?name,?string?type,?DateTime?crtime,?string?publisher,?double?capacity)?:?base(id,?name,?type,?crtime)
{
Publisher?=?publisher;
Capacity?=?capacity;
}
//getPrice和顯示全部成員變量的函數show。
public?double?GetCapacity()
{
return?this.Capacity;
}
public?override?string?Show()
{
return?"ID:"?+?ID.ToString()?+?"Name:"?+?Name?+?"Type:"?+?Type?+?"CrTime:"?+?CrTime.ToString()?+?"Publisher:"?+?Publisher?+?"Capacity:"?+?Capacity.ToString();
}
public?override?string?Show(string?language)
{
switch?(language)
{
case?"Chinese":
{
return?"編號:"?+?ID.ToString()?+?"名稱:"?+?Name?+?"屬性:"?+?Type?+?"創建時間:"?+?CrTime.ToString()?+?"發行:"?+?Publisher?+?"容量:"?+?Capacity.ToString();
}
default:?return?this.Show();
}
}
public?string?Publisher;
public?double?Capacity;
}