-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.cs
More file actions
62 lines (48 loc) · 1.74 KB
/
User.cs
File metadata and controls
62 lines (48 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.Collections.Generic;
namespace LibrarySystem
{
public class User
{
private static int _nextId = 1; // shared counter
public int UserId { get; protected set; }
public string UserName { get; private set; }
private readonly List<Book> _borrowedBooks;
public User(string userName)
{
UserId = _nextId++;
UserName = userName;
_borrowedBooks = new List<Book>();
}
public List<Book> GetBorrowedBooks()
{
return _borrowedBooks;
}
public string BorrowBook(Book book)
{
if (book == null)
return "Error: Invalid book.";
if (_borrowedBooks.Contains(book))
return $"You already borrowed \"{book.Title}\".";
if (!book.CheckOut(UserName))
return $"Error: \"{book.Title}\" is already checked out.";
_borrowedBooks.Add(book);
return $"You have successfully borrowed \"{book.Title}\".";
}
public string ReturnBook(Book book)
{
if (book == null)
return "Error: Invalid book.";
if (!_borrowedBooks.Contains(book))
return $"You don't have \"{book.Title}\".";
if (!book.Return())
return $"Error: \"{book.Title}\" was not checked out.";
_borrowedBooks.Remove(book);
return $"You have successfully returned \"{book.Title}\".";
}
public string DisplayInfo()
{
return $"UserID: {UserId} \tUser Name: {UserName} \tBorrowed Books: {_borrowedBooks.Count}";
}
}
}