rfid图书借阅系统C#
时间: 2023-11-25 08:08:46 浏览: 87
以下是一个简单的RFID图书借阅系统的C#代码示例:
```csharp
using System;
using System.Collections.Generic;
namespace RFIDLibrarySystem
{
class Program
{
static void Main(string[] args)
{
// 初始化图书列表和用户列表
List<Book> books = new List<Book>();
List<User> users = new List<User>();
// 添加图书和用户
books.Add(new Book("001", "C# Programming", "John Smith"));
books.Add(new Book("002", "Java Programming", "Jane Doe"));
users.Add(new User("001", "Alice"));
users.Add(new User("002", "Bob"));
// 初始化RFID卡
RFIDCard userCard = new RFIDCard("001");
RFIDCard bookCard = new RFIDCard("002");
// 用户借书
User user = users[0];
Book book = books[0];
user.BorrowBook(book, userCard, bookCard);
// 用户还书
user.ReturnBook(book, userCard, bookCard);
}
}
// 图书类
class Book
{
public string Id { get; set; }
public string Name { get; set; }
public string Author { get; set; }
public Book(string id, string name, string author)
{
Id = id;
Name = name;
Author = author;
}
}
// 用户类
class User
{
public string Id { get; set; }
public string Name { get; set; }
public List<Book> BorrowedBooks { get; set; }
public User(string id, string name)
{
Id = id;
Name = name;
BorrowedBooks = new List<Book>();
}
// 借书
public void BorrowBook(Book book, RFIDCard userCard, RFIDCard bookCard)
{
if (book != null && userCard != null && bookCard != null)
{
// 检查图书是否已被借出
if (bookCard.IsAvailable())
{
// 借书
BorrowedBooks.Add(book);
bookCard.SetUser(userCard);
Console.WriteLine(Name + " borrowed " + book.Name);
}
else
{
Console.WriteLine("The book is not available.");
}
}
}
// 还书
public void ReturnBook(Book book, RFIDCard userCard, RFIDCard bookCard)
{
if (book != null && userCard != null && bookCard != null)
{
// 检查用户是否借了这本书
if (BorrowedBooks.Contains(book))
{
// 还书
BorrowedBooks.Remove(book);
bookCard.ClearUser();
Console.WriteLine(Name + " returned " + book.Name);
}
else
{
Console.WriteLine("You did not borrow this book.");
}
}
}
}
// RFID卡类
class RFIDCard
{
public string Id { get; set; }
public User User { get; set; }
public RFIDCard(string id)
{
Id = id;
}
// 设置用户
public void SetUser(RFIDCard userCard)
{
if (userCard != null)
{
User = userCard.User;
}
}
// 清除用户
public void ClearUser()
{
User = null;
}
// 检查卡是否可用
public bool IsAvailable()
{
return User == null;
}
}
}
```
阅读全文