Skip to content

Instantly share code, notes, and snippets.

@elhardoum
Created September 30, 2021 05:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elhardoum/57f481462bdc55836b66e5c6221b16b9 to your computer and use it in GitHub Desktop.
Save elhardoum/57f481462bdc55836b66e5c6221b16b9 to your computer and use it in GitHub Desktop.
C# - Create a set of classes designed to help libraries track which books they own and whether they are available or checked out
using System;
using System.Collections.Generic;
using System.Linq;
namespace LearningActivity1
{
public class Book
{
public string Name { get; set; }
public string Author { get; set; }
public int ISBN { get; set; }
public bool Rented { get; private set; }
public Book( string Name, string Author, int ISBN )
{
this.Name = Name;
this.Author = Author;
this.ISBN = ISBN;
}
public override string ToString()
{
String status = Rented ? "UNAVAILABLE" : "AVAILABLE";
return $"{ISBN} - {Name} by {Author} - {status}";
}
public void Rent()
{
if (Rented)
{
Console.WriteLine("This book is already rented.");
return;
}
Rented = true;
Console.WriteLine($"Book {Name} successfully rented to new reader.");
}
public void Return()
{
if (!Rented)
{
Console.WriteLine("This book is already returned.");
return;
}
Rented = false;
Console.WriteLine($"Book {Name} successfully rented to new reader.");
}
}
public class Library
{
private List<Book> Books = new List<Book>();
public void AddBook(Book book)
{
Books.Add(book);
}
public Book FindABookByISBN( int ISBN )
{
return Books.Find(book => book.ISBN == ISBN);
}
public void SearchBooks( string keyword )
{
List<Book> matched = Books.FindAll(book =>
book.Name.ToLower().Contains(keyword.ToLower())
|| book.Author.ToLower().Contains(keyword.ToLower()));
foreach (Book book in matched)
{
Console.WriteLine(book);
}
}
public void ListBooks()
{
foreach (Book book in Books)
{
Console.WriteLine(book);
}
}
public void Checkout( int ISBN )
{
Book book = FindABookByISBN(ISBN);
if (null == book)
{
Console.WriteLine("This book is not available in the registery.");
return;
}
book.Rent();
}
public void Return(int ISBN)
{
Book book = FindABookByISBN(ISBN);
if (null == book) {
Console.WriteLine("This book is not available in the registery.");
}
book.Return();
}
public void HandleCommand( String[] parts )
{
String command = "", argument = "";
for ( int i=0; i<parts.Length; i++)
{
if ( 0 == i )
{
command = parts[i];
} else
{
argument += (String.IsNullOrEmpty(argument) ? "" : " ") + parts[i];
}
}
switch ( command.ToLower() )
{
case "list-all":
ListBooks();
break;
case "search":
SearchBooks(argument);
break;
case "checkout":
case "return":
int ISBN;
if ( int.TryParse(argument, out ISBN))
{
if ("return" == command.ToLower())
{
Return(ISBN);
}
else
{
Checkout(ISBN);
}
} else
{
Console.WriteLine($"Invalid ISBN supplied: {argument}");
}
break;
default:
Console.WriteLine($"c: {command}, a: {argument}");
break;
}
}
}
public class CliClient
{
public CliClient( Library library )
{
string line;
StdinHint();
while ((line = Console.ReadLine()) != null)
{
string[] parts = line.Trim().Split(' ').Where(s => ! String.IsNullOrWhiteSpace(s)).ToArray();
if (parts.Length < 2 && ! ( parts.Length == 1 && (
"list-all" == parts[0].ToLower()
|| "help" == parts[0].ToLower()
|| "exit" == parts[0].ToLower()
) ))
{
Console.WriteLine("Please type a command and at least 1 argument. For a list of commands, type: help");
StdinHint();
continue;
}
switch ( parts[0].ToLower() )
{
case "help":
Help();
break;
case "list-all":
case "search":
case "checkout":
case "return":
library.HandleCommand( parts );
break;
case "exit":
return;
default:
Console.WriteLine("Unknown command. For a list of commands, type: help");
break;
}
StdinHint();
}
}
public void Help()
{
Console.WriteLine("Available commands:\n list-all\n search keyword\n checkout ISBN\n return ISBN\n help\n exit");
}
public void StdinHint()
{
Console.Write("> ");
}
}
class Program
{
static void Main(string[] args)
{
Library library = new Library();
// add a list of books
library.AddBook(new Book("Science Of Storytelling", "Will Storr", 1));
library.AddBook(new Book("Steve Jobs", "Walter Isaacson", 2));
library.AddBook(new Book("How We Got to Now", "Steven Johnson", 3));
library.AddBook(new Book("The Big Nine", "Amy Webb", 4));
library.AddBook(new Book("Deviced", "Doreen Dodgen-Magee", 5));
library.AddBook(new Book("The Truth About Your Future", "Eric Edelman", 6));
library.AddBook(new Book("Digital", "Joshua J Omojuwa", 7));
library.AddBook(new Book("How to Be Good at Science, Technology, and Engineering", "DK", 8));
library.AddBook(new Book("A World Without Work", "Daniel Susskind", 9));
new CliClient(library);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment