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
}