介面interface抽象類abstract作業(2018/07/08)

介面interface抽象類abstract作業(2018/07/08)

1.【練習題】抽象類、繼承、介面綜合
設計一個系統:
XXX門的實現過程:
流程:
設計一張抽象的門Door,那麼對於這張門來說,就應該擁有所有門的共性,開門openDoor()和關門closeDoor();然後對門進行另外的功能設計,防盜–theftproof()、防水–waterproof()、防彈–bulletproof()、防火、防鏽……
要求:利用繼承、抽象類、介面的知識設計該門
程式碼:

using System;
using System.Collections.Generic;
namespace cchoop
{
class Program
{
static void Main(string[] args)
{
RoomDoor roomDoor = new RoomDoor();
roomDoor.OpenDoor();
roomDoor.CloseDoor();
roomDoor.OnBulletProof();
roomDoor.OnFireProof();
roomDoor.OnTheftProof();
}
}
}
class RoomDoor : Door, ITheftProof, IWaterProof, IBulletProof, IFireProof, IRustProof
{
public override void CloseDoor()
{
Console.WriteLine("關門");
}
public override void OpenDoor()
{
Console.WriteLine("開門");
}
public void OnTheftProof()
{
Console.WriteLine("防賊");
}
public void OnWaterProof()
{
Console.WriteLine("防水");
}
public void OnBulletProof()
{
Console.WriteLine("防彈");
}
public void OnFireProof()
{
Console.WriteLine("防火");
}
public void OnRustProof()
{
Console.WriteLine("防鏽");
}
}
abstract class Door
{
public abstract void OpenDoor();
public abstract void CloseDoor();
}
//防盜--theftproof()
interface ITheftProof
{
void OnTheftProof();
}
//防水--waterproof()
interface IWaterProof
{
void OnWaterProof();
}
//防彈--bulletproof()
interface IBulletProof
{
void OnBulletProof();
}
//防火fireproof
interface IFireProof
{
void OnFireProof();
}
//防鏽rust-proof
interface IRustProof
{
void OnRustProof();
}

2.這裡寫圖片描述
程式碼:

using System;
using System.Collections.Generic;
namespace cchoop
{
class Program
{
static void Main(string[] args)
{
}
}
/// <summary>
/// 青蛙:遊、跳
/// </summary>
class Frog : Animal, ISwim, IJump
{
public override void Eat()
{
Console.WriteLine("青蛙吃");
}
public override void Sleep()
{
Console.WriteLine("青蛙睡覺");
}
public override void Move()
{
Console.WriteLine("青蛙移動");
}
public void OnSwimm()
{
Console.WriteLine("青蛙遊");
}
public void OnJump()
{
Console.WriteLine("青蛙跳");
}
}
/// <summary>
/// 鯊魚:遊
/// </summary>
class Shark : Animal, ISwim
{
public override void Eat()
{
Console.WriteLine("鯊魚吃");
}
public override void Sleep()
{
Console.WriteLine("鯊魚睡覺");
}
public override void Move()
{
Console.WriteLine("鯊魚移動");
}
public void OnSwimm()
{
Console.WriteLine("鯊魚遊");
}
}
/// <summary>
/// 袋鼠:跳、裝寶寶
/// </summary>
class kangaroo : Animal, IJump
{
public override void Eat()
{
Console.WriteLine("袋鼠吃");
}
public override void Sleep()
{
Console.WriteLine("袋鼠睡覺");
}
public override void Move()
{
Console.WriteLine("袋鼠移動");
}
public void OnJump()
{
Console.WriteLine("袋鼠跳");
}
public void BringBaby()
{
Console.WriteLine("袋鼠裝寶寶");
}
}
/// <summary>
/// 貓頭鷹:飛
/// </summary>
class Owl : Animal, IFly
{
public override void Eat()
{
Console.WriteLine("貓頭鷹吃");
}
public override void Sleep()
{
Console.WriteLine("貓頭鷹睡覺");
}
public override void Move()
{
Console.WriteLine("貓頭鷹移動");
}
public void OnFly()
{
Console.WriteLine("貓頭鷹飛行");
}
}
/// <summary>
/// 天鵝:飛、遊
/// </summary>
class Swan : Animal, IFly, ISwim
{
public override void Eat()
{
Console.WriteLine("天鵝吃");
}
public override void Sleep()
{
Console.WriteLine("天鵝睡覺");
}
public override void Move()
{
Console.WriteLine("天鵝移動");
}
public void OnFly()
{
Console.WriteLine("天鵝飛行");
}
public void OnSwimm()
{
Console.WriteLine("天鵝游泳");
}
}
/// <summary>
/// 飛行員:遊、飛、跳
/// </summary>
class Pilot : Animal, ISwim, IFly, IJump
{
public override void Eat()
{
Console.WriteLine("飛行員吃");
}
public override void Sleep()
{
Console.WriteLine("飛行員睡覺");
}
public override void Move()
{
Console.WriteLine("飛行員移動");
}
public void OnFly()
{
Console.WriteLine("飛行員飛行");
}
public void OnSwimm()
{
Console.WriteLine("飛行員游泳");
}
public void OnJump()
{
Console.WriteLine("飛行員跳");
}
}
/// <summary>
/// 動物抽象類
/// </summary>
abstract class Animal
{
public abstract void Eat();
public abstract void Sleep();
public abstract void Move();
}
interface ISwim
{
void OnSwimm();
}
interface IFly
{
void OnFly();
}
interface IJump
{
void OnJump();
}
}

100. 課程設計《圖書管理系統》
功能模組 1>系統登陸
正確的使用者名稱為admin,密碼為admin123,判斷使用者名稱和密碼是否正確,如果正確,進入到主介面,
否則輸出使用者名稱或密碼不正確的提示。如果連續錯誤輸入三次,拒絕輸入,退出系統。
2>登陸之後,顯示以下的介面:
歡迎使用圖書管理系統。請輸入快捷鍵進入相關的功能模組
1.圖書錄入(A) 2.圖書查詢(Q)
3.圖書借閱(B) 4.圖書歸還(R)
5.退出系統(X)
3>如果輸入A,回車後,將登陸圖書錄入模組。錄入圖書的檔案資訊(如下圖), 其中圖書編號為4位,且不能重複。最後輸入完成並回車後,詢問是否繼續錄入,輸入Y將繼續錄入。 否則返回主介面。
4>如果輸入Q,回車後,將進入查詢的介面,可以查詢藏書,形成圖書列表(如下表,示例資料)
編號 書名 出版社 類別 借閱狀態 借閱人 學生證號
------------------------------------------------------------------------------------
0123 c#入門經典 人民郵電出版社 計算機軟體 借出 張三 A123456
------------------------------------------------------------------------------------
A789 c#高階程式設計 北京大學出版社 計算機軟體 借出 張宇 A123789
------------------------------------------------------------------------------------
S234 c#入門經典 人民郵電出版社 計算機軟體 在館 ------------------------------------------------------------------------------------
2345 大學英語 人民文學出版社 外語 在館 ------------------------------------------------------------------------------------
共有圖書4冊,已借出2冊,在館2冊
請輸入操作命令:
輸入M,將回到主介面 輸入X,將退出系統 輸入A,進入圖書錄入 輸入B,進入圖書借閱 輸入R,進入圖書歸還
否則顯示無效的輸入,繼續接收使用者的輸入
5>如果輸入B,回車後,進入圖書借閱的介面 輸入學生證號和姓名,如下圖: 學生證號: 姓名:
判斷學生證號不能超過12位,不能小於6位。
還將判斷,如果該學生證號有借出的圖書(沒有歸還),給出提示:你借閱的《書名》沒有歸還,請歸還後再借閱。接收使用者輸入的操作命令。 輸入M,將回到主介面 輸入X,將退出系統 輸入A,進入圖書錄入 輸入B,進入圖書借閱 輸入R,進入圖書歸還
通過驗證後,進入如下的介面:
請輸入要借閱的書籍(支援模糊查詢,如輸入c#,將查詢未借出的C#入門經典和c#高階程式設計,IndexOf方法,如沒有輸入,直接回車,將查詢出全部未借出的圖書)形成列表
編號 書名 出版社 類別
S234 c#入門經典 人民郵電出版社 計算機軟體
2345 大學英語 人民文學出版社 外語
輸入編號後(如2345),回車後,將輸出 書名:大學英語,編號:2345已借閱成功。 如果輸入的編號在上面的列表中不存在,給出提示,繼續等待使用者輸入編號。 完成後,提示請輸入操作命令: 輸入M,將回到主介面 輸入X,將退出系統 輸入A,進入圖書錄入 輸入B,進入圖書借閱 輸入R,進入圖書歸還
否則顯示無效的輸入,繼續接收使用者的輸入
6>圖書歸還
輸入學生證號(如下),判斷學生證號不能超過12位,不能小於6位。 學生證號:
還將判斷,如果該學生證號沒有借出的圖書,給出提示:你沒有借閱的圖書。繼續提示使用者輸入學生證號
否則,將出現下面的提示: 你借出的圖書是:
編號 書名 出版社 類別 ----------------------------------------------------
2345 大學英語 人民文學出版社 外語 你確實要歸還該圖書嗎?(Y確定歸還,N不歸還) 輸入Y,輸出《書名》已歸還,歡迎借閱。 輸入N,輸出《書名》暫不歸還
否則顯示無效的輸入,繼續等待使用者的輸入。 完成後,請輸入操作命令:
輸入M,將回到主介面 輸入X,將退出系統 輸入A,進入圖書錄入 輸入B,進入圖書借閱 輸入R,進入圖書歸還

程式碼:

using System;
using System.Collections.Generic;
namespace cchoop
{
class Program
{
static void Main(string[] args)
{
View view = new View();
view.Menu();
}
}
///////////////////////////////////////////////////////////////////////////////////
#region 顯示層
class View
{
private bool isLogin = false;   //是否登入
private string CurBorrowerId;   //儲存當前使用者Id
private BookService bookService;
private BorrowerService borrowerService;
public View()
{
bookService = new BookService();
borrowerService = new BorrowerService();
}
public void LoginIn()
{
Console.WriteLine("=================使用者登入介面========================");
Console.WriteLine("1.登入(L) 2.註冊(R未實現) 3..退出(Q)");
int op = Tools.GetCharByConsole(new Char[] { 'L', 'Q' });
switch (op)
{
case 'L':
Console.Write("請輸入使用者名稱:");
string account = Tools.GetStringByConsole();
Console.Write("請輸入密碼:");
string password = Tools.GetStringByConsole();
int msg = -1;
while (msg != 0)
{
msg = borrowerService.Login(account, password);
if (msg == 1)
{
Console.Clear();
Console.WriteLine("密碼錯誤!");
this.Menu();
}
else if (msg == 2)
{
Console.Clear();
Console.WriteLine("不存在使用者名稱!");
this.Menu();
}
else if (msg == 0)
{
isLogin = true;
this.CurBorrowerId = borrowerService.GetBorrowerByAccount(account).ID;
Console.WriteLine("登入成功!");
BackMenu();
}
}
break;
case 'R':
//TODO
//不想寫了
break;
case 'Q':
Environment.Exit(0);
break;
}
}
public void Menu()
{
if (isLogin == false)
{
this.LoginIn();
}
Console.WriteLine("=================圖書管理系統========================");
Console.WriteLine("1.圖書錄入(A) 2.圖書查詢(Q)");
Console.WriteLine("3.圖書借閱(B) 4.圖書歸還(R)");
Console.WriteLine("5.登出(E)");
Console.WriteLine("------------------------------------------------------");
Console.Write("請輸入:");
int op = Tools.GetCharByConsole(new Char[] { 'A', 'Q', 'B', 'R', 'E' });
switch (op)
{
case 'A':
this.BookEntry();
break;
case 'Q':
this.BookSerach();
break;
case 'B':
this.BookBorrowing();
break;
case 'R':
this.BookBack();
break;
case 'E':
this.LoginOut();
break;
}
}
//圖書錄入
public void BookEntry()
{
Console.WriteLine("圖書錄入==========================================");
Console.Write("請輸入圖書名稱:");
string bookName = Tools.GetStringByConsole();
Console.Write("請輸入圖書出版社:");
string bookPublishing = Tools.GetStringByConsole();
Console.WriteLine("請選擇圖書類別:");
string bookCategory = null;
Console.WriteLine("1."   BookCategoryType.ComputerHardware   "\t2."   BookCategoryType.ComputerSoftware);
Console.Write("請選擇編號:");
char op = Tools.GetCharByConsole(new Char[] { '1', '2' });
switch (op)
{
case '1':
bookCategory = BookCategoryType.ComputerHardware;
break;
case '2':
bookCategory = BookCategoryType.ComputerSoftware;
break;
}
Book book = new Book(bookName, bookPublishing, bookCategory);
bookService.BookEntry(book);
Console.WriteLine("錄入成功!");
BackMenu();
}
//圖書查詢
public void BookSerach()
{
Console.WriteLine("圖書查詢==========================================");
Console.WriteLine("1.查詢所有 2.根據圖書名查詢 3.根據圖書ID查詢");
Char opChar = Tools.GetCharByConsole(new Char[] { '1', '2', '3' });
switch (opChar)
{
case '1':
Dictionary<string, Book>.ValueCollection books = bookService.BookSerachAll();
if (books.Count == 0)
{
Console.WriteLine("未找到圖書");
}
else
{
Console.WriteLine("================================================================================");
Console.WriteLine("編號"   "\t"   "書名"   "\t"   "出版社"   "\t"   "類別"   "\t"   "借閱狀態"   "\t"   "借閱人"   "\t"   "學生證號");
Console.WriteLine("--------------------------------------------------------------------------------");
foreach (var item in books)
{
Console.WriteLine(item.ID   "\t"   item.Name   "\t"   item.Publishing   "\t"   item.BookCategory   "\t"   item.BorrowingStatus   "\t"   item.BorrowerName   "\t"   item.BorrowerId);
}
}
break;
case '2':
Console.Write("請輸入圖書Id:");
string bookId = Tools.GetStringByConsole();
Book book = bookService.BookSerachByBookId(bookId);
if (book == null)
{
Console.WriteLine("未找到圖書");
}
else
{
Console.WriteLine("================================================================================");
Console.WriteLine("編號"   "\t"   "書名"   "\t"   "出版社"   "\t"   "類別"   "\t"   "借閱狀態"   "\t"   "借閱人"   "\t"   "學生證號");
Console.WriteLine("--------------------------------------------------------------------------------");
Console.WriteLine(book.ID   "\t"   book.Name   "\t"   book.Publishing   "\t"   book.BookCategory   "\t"   book.BorrowingStatus   "\t"   book.BorrowerName   "\t"   book.BorrowerId);
}
break;
case '3':
Console.Write("請輸入圖書名:");
string bookName = Tools.GetStringByConsole();
Book book3 = bookService.BookSerachByBookName(bookName);
if (book3 == null)
{
Console.WriteLine("未找到圖書");
}
else
{
Console.WriteLine("================================================================================");
Console.WriteLine("編號"   "\t"   "書名"   "\t"   "出版社"   "\t"   "類別"   "\t"   "借閱狀態"   "\t"   "借閱人"   "\t"   "學生證號");
Console.WriteLine("--------------------------------------------------------------------------------");
Console.WriteLine(book3.ID   "\t"   book3.Name   "\t"   book3.Publishing   "\t"   book3.BookCategory   "\t"   book3.BorrowingStatus   "\t"   book3.BorrowerName   "\t"   book3.BorrowerId);
}
break;
}
BackMenu();
}
//圖書借閱
public void BookBorrowing()
{
Console.WriteLine("圖書借閱==============================================");
List<string> bookIds = borrowerService.GetAllBorrowedBookIds(CurBorrowerId);
if (bookIds.Count == 0)
{
Console.WriteLine("所有圖書資訊------------------------");
Dictionary<string, Book>.ValueCollection books = bookService.BookSerachAll();
if (books.Count == 0)
{
Console.WriteLine("沒有圖書,無法借書!");
}
else
{
Console.WriteLine("================================================================================");
Console.WriteLine("編號"   "\t"   "書名"   "\t"   "出版社"   "\t"   "類別"   "\t"   "借閱狀態"   "\t"   "借閱人"   "\t"   "學生證號");
Console.WriteLine("--------------------------------------------------------------------------------");
foreach (var item in books)
{
Console.WriteLine(item.ID   "\t"   item.Name   "\t"   item.Publishing   "\t"   item.BookCategory   "\t"   item.BorrowingStatus   "\t"   item.BorrowerName   "\t"   item.BorrowerId);
}
Console.WriteLine("================================================================================");
Console.Write("請輸入要借閱的圖書Id:");
string bookId = Tools.GetStringByConsole();
Console.WriteLine(borrowerService.BookBorrowing(CurBorrowerId, bookId));
}
}
else
{
Console.WriteLine("無法借書,請先歸還借過的圖書!");
}
BackMenu();
}
//圖書歸還
public void BookBack()
{
Console.WriteLine("圖書歸還==========================================");
List<string> bookIds = borrowerService.GetAllBorrowedBookIds(CurBorrowerId);
if (bookIds.Count == 0)
{
Console.WriteLine("沒有借過圖書,無法歸還!");
}
else
{
Console.WriteLine("所有借過的圖書資訊------------------------");
Console.WriteLine("================================================================================");
Console.WriteLine("編號"   "\t"   "書名"   "\t"   "出版社"   "\t"   "類別"   "\t"   "借閱狀態"   "\t"   "借閱人"   "\t"   "學生證號");
Console.WriteLine("--------------------------------------------------------------------------------");
foreach (var item in bookIds)
{
Book book = bookService.BookSerachByBookId(item);
Console.WriteLine(book.ID   "\t"   book.Name   "\t"   book.Publishing   "\t"   book.BookCategory   "\t"   book.BorrowingStatus   "\t"   book.BorrowerName   "\t"   book.BorrowerId);
}
Console.WriteLine("================================================================================");
Console.Write("請輸入要歸還的圖書Id:");
string bookId = Tools.GetStringByConsole();
Console.WriteLine(borrowerService.BookBack(CurBorrowerId, bookId));
}
BackMenu();
}
//登出
public void LoginOut()
{
isLogin = false;
CurBorrowerId = null;
BackMenu();
}
//返回主選單
public void BackMenu()
{
Console.WriteLine("請按任意鍵返回主選單");
Console.ReadKey();
Console.Clear();
this.Menu();
}
}
#endregion
///////////////////////////////////////////////////////////////////////////////////
#region 業務邏輯層
//功能:BookService:1.圖書錄入(A) 2.圖書查詢(根據bookId或者bookName查詢)(Q) ...
//BorrowerService:3.圖書借閱(只能通過圖書id借閱)(B) 4.圖書歸還(R)  註冊使用者 驗證登入...
class BookService
{
BookCache bookCache = Caches.BookCache;
/// <summary>
/// 圖書錄入
/// </summary>
/// <param name="book">錄入的圖書</param>
public void BookEntry(Book book)
{
bookCache.CreatBook(book);
}
/// <summary>
/// 根據圖書名查詢圖書
/// </summary>
/// <param name="bookName">圖書名</param>
public Book BookSerachByBookName(string bookName)
{
return bookCache.GetBookByBookName(bookName);
}
/// <summary>
/// 根據圖書Id查詢圖書
/// </summary>
/// <param name="bookId">圖書Id</param>
public Book BookSerachByBookId(string bookId)
{
return bookCache.GetBookByBookId(bookId);
}
public Dictionary<string, Book>.ValueCollection BookSerachAll()
{
return bookCache.GetAllBooks();
}
}
class BorrowerService
{
BorrowerCache borrowerCache = Caches.BorrowerCache;
BookCache bookCache = Caches.BookCache;
/// <summary>
/// 圖書借閱(根據圖書id借閱圖書)
/// </summary>
/// <param name="borrowerId">借閱人Id</param>
/// <param name="bookId">圖書Id</param>
/// <returns>借書結果資訊</returns>
public string BookBorrowing(string borrowerId, string bookId)
{
//不存在該圖書
if (!bookCache.IsExist(bookId))
{
return "不存在id為"   bookId   "的圖書";
}
//圖書已經借出
if (bookCache.IsBorrowering(bookId))
{
return "id為"   bookId   "的圖書已經借出";
}
//1.將圖書新增到借閱人的借閱列表中去
Borrower borrower = borrowerCache.GetBorrowerByBorrowerId(borrowerId);
borrower.BorrowedBookIdList.Add(bookId);
//2.更新圖書借閱狀態為:借閱,並且將借書人的名稱和Id儲存
Book book = bookCache.GetBookByBookId(bookId);
book.BorrowingStatus = 1;
book.BorrowerId = borrowerId;
book.BorrowerName = borrower.Name;
return "借書成功";
}
/// <summary>
/// 根據圖書Id還書
/// </summary>
/// <param name="borrowerId">借閱人Id</param>
/// <param name="bookId">圖書Id</param>
/// <returns>還書結果資訊</returns>
public string BookBack(string borrowerId, string bookId)
{
//不存在該圖書
if (!bookCache.IsExist(bookId))
{
return "不存在id為"   bookId   "的圖書";
}
//使用者未借這本書
Borrower borrower = borrowerCache.GetBorrowerByBorrowerId(borrowerId);
if (borrower.BorrowedBookIdList.IndexOf(bookId) == -1)
{
return "你未借id為"   bookId   "的圖書";
}
else   //借了
{
//1.將圖書從借閱人的借閱列表中刪去
borrower.BorrowedBookIdList.Remove(bookId);
//2.更新圖書借閱狀態為:未借閱,將借書人和借書人Id置空
Book book = bookCache.GetBookByBookId(bookId);
book.BorrowingStatus = 0;
book.BorrowerName = null;
book.BorrowerId = null;
}
return "還書成功";
}
public List<string> GetAllBorrowedBookIds(string borrowedId)
{
return borrowerCache.GetBorrowerByBorrowerId(borrowedId).BorrowedBookIdList;
}
/// <summary>
/// 使用者登入
/// </summary>
/// <param name="account">使用者名稱</param>
/// <param name="password">密碼</param>
/// <returns></returns>
public int Login(string account, string password)
{
int message = 0;
if (borrowerCache.IsExistAccount(account))
{
if (borrowerCache.Verify(account, password))
{
message = 0;      //登入成功
}
else
{
message = 1;       //密碼錯誤
}
}
else
{
message = 2;        //不存在使用者名稱
}
return message;
}
/// <summary>
/// 通過使用者名稱獲取使用者資訊
/// </summary>
/// <param name="account">使用者名稱</param>
/// <returns>使用者</returns>
public Borrower GetBorrowerByAccount(string account)
{
return borrowerCache.GetBorrowerByBorrowerAccount(account);
}
/// <summary>
/// 使用者註冊
/// </summary>
/// <param name="borrower">註冊的使用者</param>
public void Register(Borrower borrower)
{
borrowerCache.CreatBorrower(borrower);
}
}
#endregion
///////////////////////////////////////////////////////////////////////////////////
#region 資料模型層
//包含的模型:圖書Book、借閱人Borrower
/// <summary>
/// 圖書類別常量
/// </summary>
class BookCategoryType
{
public const string ComputerSoftware = "計算機軟體";
public const string ComputerHardware = "計算機硬體";
}
/// <summary>
/// 圖書 類
/// </summary>
class Book
{
public string ID { get; private set; } //圖書Id  系統自動生成
public string Name { get; set; }        //圖書名
public string Publishing { get; set; }   //圖書出版社
public string BookCategory { get; set; }  //圖書類別
public int BorrowingStatus { get; set; }   //0代表未借出,1代表借出
public string BorrowerName { get; set; }   //借閱人
public string BorrowerId { get; set; }    //借閱人Id
public Book()
{
this.ID = Tools.CreatStringId();
}
public Book(string name, string publishing, string bookCategory)
{
this.ID = Tools.CreatStringId();
this.Name = name;
this.Publishing = publishing;
this.BookCategory = bookCategory;
}
}
/// <summary>
/// 借閱人 類
/// </summary>
class Borrower
{
public string ID { get; private set; }  //借閱人Id 自動生成
public string Account { get; set; }     //借閱人賬戶名
public string Password { get; set; }    //借閱人賬戶的密碼
public string Name { get; set; }        //借閱人的真實姓名
public List<string> BorrowedBookIdList { get; private set; }  //借閱的圖書Id
public Borrower()
{
this.ID = Tools.CreatStringId();
this.BorrowedBookIdList = new List<string>();
}
public Borrower(string account, string password, string name)
{
this.ID = Tools.CreatStringId();
this.Account = account;
this.Password = password;
this.Name = name;
this.BorrowedBookIdList = new List<string>();
}
}
#endregion
///////////////////////////////////////////////////////////////////////////////////
#region 資料快取層
static class Caches
{
public static BookCache BookCache { get; private set; }
public static BorrowerCache BorrowerCache { get; private set; }
static Caches()
{
BookCache = new BookCache();
BorrowerCache = new BorrowerCache();
}
}
/// <summary>
/// 圖書快取類
/// </summary>
class BookCache
{
//初始化
public BookCache()
{
}
private Dictionary<string, Book> BookDict = new Dictionary<string, Book>();
/// <summary>
/// 建立一本圖書
/// </summary>
/// <param name="book">建立的圖書</param>
public void CreatBook(Book book)
{
BookDict.Add(book.ID, book);
}
/// <summary>
/// 通過圖書Id得到圖書
/// </summary>
/// <param name="bookId">圖書Id</param>
/// <returns>得到的圖書</returns>
public Book GetBookByBookId(string bookId)
{
if (!BookDict.ContainsKey(bookId))
{
return null;
}
return BookDict[bookId];
}
/// <summary>
/// 通過圖書名得到圖書
/// </summary>
/// <param name="bookName">圖書名</param>
/// <returns>得到的圖書</returns>
public Book GetBookByBookName(string bookName)
{
Book book = null;
foreach (var item in BookDict.Values)
{
if (item.Name == bookName)
{
book = item;
}
}
return book;
}
/// <summary>
/// 返回所有圖書集合
/// </summary>
/// <returns>圖書集合</returns>
public Dictionary<string, Book>.ValueCollection GetAllBooks()
{
return BookDict.Values;
}
/// <summary>
/// 是否存在圖書(根據圖書Id)
/// </summary>
/// <param name="bookId">圖書Id</param>
/// <returns>是否存在圖書</returns>
public bool IsExist(string bookId)
{
return BookDict.ContainsKey(bookId);
}
public bool IsBorrowering(string bookId)
{
bool isBorrowering = false;
Book book = BookDict[bookId];
if (book.BorrowingStatus == 1)  //1代表借出
{
isBorrowering = true;
}
return isBorrowering;
}
}
/// <summary>
/// 借閱人快取類
/// </summary>
class BorrowerCache
{
//儲存借閱人賬號名和借閱人資料模型
private Dictionary<string, Borrower> AccountBorrowerDict = new Dictionary<string, Borrower>();
//儲存借閱人Id和借閱人資料模型
private Dictionary<string, Borrower> IdBorrowerDict = new Dictionary<string, Borrower>();
//初始化
public BorrowerCache()
{
//管理員預設密碼
Borrower borrower = new Borrower("admin", "admin", "cc");
CreatBorrower(borrower);
}
/// <summary>
/// 是否存在賬號
/// </summary>
/// <param name="account">賬號名</param>
/// <returns></returns>
public bool IsExistAccount(string account)
{
return AccountBorrowerDict.ContainsKey(account);
}
/// <summary>
/// 根據使用者名稱獲取使用者資訊
/// </summary>
/// <param name="account">使用者名稱</param>
/// <returns>使用者資訊</returns>
public Borrower GetBorrowerByBorrowerAccount(string account)
{
if (IsExistAccount(account))
{
return AccountBorrowerDict[account];
}
else
{
return null;
}
}
/// <summary>
/// 驗證賬號和密碼是否匹配
/// </summary>
/// <param name="account">賬號</param>
/// <param name="password">密碼</param>
/// <returns></returns>
public bool Verify(string account, string password)
{
Borrower borrower = AccountBorrowerDict[account];
return borrower.Password == password;
}
/// <summary>
/// 新增一個使用者(借閱人)
/// </summary>
/// <param name="borrower">使用者</param>
public void CreatBorrower(Borrower borrower)
{
AccountBorrowerDict.Add(borrower.Account, borrower);
IdBorrowerDict.Add(borrower.ID, borrower);
}
/// <summary>
/// 根據借閱人Id得到借閱人
/// </summary>
/// <param name="borrowerId">借閱人Id</param>
/// <returns>借閱人</returns>
public Borrower GetBorrowerByBorrowerId(string borrowerId)
{
return IdBorrowerDict[borrowerId];
}
}
#endregion
#region 工具類
class Tools
{
public static string CreatStringId()
{
return System.Guid.NewGuid().ToString("N").Substring(0, 6);
}
/// <summary>
/// 得到控制檯輸入的數字
/// </summary>
/// <returns>數字</returns>
public static int GetNumberByConsole()
{
int num = 0;
while (true)
{
string strNum = Console.ReadLine();
try
{
num = Int32.Parse(strNum);
}
catch (Exception e)
{
Console.Write("輸入無效,請重新輸入:");
continue;
}
break;
}
return num;
}
/// <summary>
/// 從控制檯得到限定字元陣列中的字元
/// </summary>
/// <param name="regionChars">限定的字元陣列</param>
public static Char GetCharByConsole(char[] regionChars)
{
char c;
while (true)
{
string strChar = Console.ReadLine();
bool isNotGetChar = true;
if (strChar.Trim().Length != 1)
{
Console.Write("輸入無效,請重新輸入:");
continue;
}
c = strChar[0];
foreach (var item in regionChars)
{
if (c == item)
{
isNotGetChar = false;
break;
}
}
if (isNotGetChar)
{
Console.Write("輸入無效,請重新輸入:");
continue;
}
break;
}
return c;
}
public static string GetStringByConsole()
{
string str;
while (true)
{
str = Console.ReadLine().Trim();
if (str.Length == 0)
{
Console.Write("輸入不能為空,請重新輸入:");
continue;
}
break;
}
return str;
}
}
#endregion
}