Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C# Write a console application to meet the following requirements. Create a syst

ID: 3878619 • Letter: C

Question

C#

Write a console application to meet the following requirements. Create a system for a simple library. The library has a name and a list of books. Each book has a title, author and an int as the id number. Define classes for both the library and the book. The library should have methods for adding a book to the library, to search for a book by title, to sort and display information about all of the books and to delete a book (by title) from the library. You will need to write 3 sort methods. One for sorting by title, one for sorting by author and one for sorting by idnum. Each sort must use a different process, i.e. one uses a bubble sort, one uses a selection sort and one uses an insertion sort.

The following conditions apply to the books in the library:

Each book's name is unique. There are no duplicate titles. When adding a book, ensure that the title doesn't already exist. If it does exist, don't let the new book be added.

There is only one copy of each book in the library.

An author may have more than one book in the library.

Complete the code in this incomplete client designed to test the classes and their functionality. You should not change any of the other code in main.

class MainClass

          {

                   public static void Main (string[] args)

                   {

                             Library lib = new Library ();

                             lib.AddBook ("C# programming", "Gesick", 4);

                             lib.AddBook ("java programming", "Roth", 2);

                             lib.AddBook ("C++ programming", "Franklin", 1);

                             lib.AddBook ("unity programming", "Preston", 3);

                             lib.AddBook ("graphics & multimedia", "Chastine", 5);

                             printMenu ();

                             string p = Console.ReadLine ();

                             p = p.ToUpper ();

                             char pick = p [0];

                            

                             while (pick!='Q') {

                                      switch (pick) {

                                      case 'A':

                                                //insert code here for adding a book

                                                break;

                                      case 'S':

                                                //insert code here for finding a book and

                                                //printing its details

                                                break;

                                      case 'D':

                                           //insert code here for displaying all of the books in the library        

                                          // alphabetically by title

                                          break;

                                       case 'E':

                                           //insert code here for displaying all of the books in the library        

                                         // alphabetically by author

                                          break;

                                        case 'F':

                                           //insert code here for displaying all of the books in the library        

                                          // in ascending order by id num

                                          break;

                                      case 'R':

                                                //insert code here for removing a book    

                                              break;

                                      default:

                                                Console.WriteLine (" Invalid choice, please re-enter");

                                                break;

                                      }

                                      printMenu ();

                                      p = Console.ReadLine ();

                                      p = p.ToUpper ();

                                      pick = p [0];

                             }

                            

                             Console.WriteLine ("good bye");

                            

                   }

                   public static void printMenu ()

                   {

                   Console.WriteLine (" Select one of the following: " +

                   " A to add a book to the library " +

                   " S to search for a book by title " +

                   " D to display the contents of the library, alphabetically by title " +

                     " E to display the contents of the library, alphabetically by author " +

                     " F to display the contents of the library, in ascending order by id num " +

                   " R to remove a book from the library " +

                   " Q to quit this program ");

                   Console.Write ("enter choice here: ");

                                              

                   }

          }

Explanation / Answer

using System;
using System.Collections.Generic;

namespace LibraryManagement
{
//Defining a class Book
class Book
{
public int bookId;
public string bookName;
public int bookPrice;
public int bookCount;
public int x;
}
//Defining a class Borrow
class BorrowDetails
{
public int userId;
public string userName;
public string userAddress;
public int borrowBookId;
public DateTime borrowDate;
public int borrowCount;
}

class Program
{
static List<Book> bookList = new List<Book>();
static List<BorrowDetails> borrowList = new List<BorrowDetails>();
static Book book = new Book();
static BorrowDetails borrow = new BorrowDetails();

//Password verfication and Menu
static void Main(string[] args)
{
Console.Write("Welcome !!! Enter your password :");
string password = Console.ReadLine();

if (password == "sync")
{
bool close = true;
while (close)
{
Console.WriteLine(" Menu " +
"1)Add book " +
"2)Delete book " +
"3)Search book " +
"4)Borrow book " +
"5)Return book " +
"6)Close ");
Console.Write("Choose your option from menu :");

int option = int.Parse(Console.ReadLine());

if (option == 1)
{
GetBook();
}
else if (option == 2)
{
RemoveBook();
}
else if (option == 3)
{
SearchBook();
}
else if (option == 4)
{
Borrow();
}
else if (option == 5)
{
ReturnBook();
}
else if (option == 6)
{
Console.WriteLine("Thank you");
close = false;
break;
}
else
{
Console.WriteLine("Invalid option Retry !!!");
}
}
}
else
{
Console.WriteLine("Invalid password");
}
Console.ReadLine();
}

//To add book details to the Library database
public static void GetBook()
{
Book book = new Book();
Console.WriteLine("Book Id:{0}", book.bookId = bookList.Count + 1);
Console.Write("Book Name:");
book.bookName = Console.ReadLine();
Console.Write("Book Price:");
book.bookPrice = int.Parse(Console.ReadLine());
Console.Write("Number of Books:");
book.x = book.bookCount = int.Parse(Console.ReadLine());
bookList.Add(book);
}

//To delete book details from the Library database
public static void RemoveBook()
{
Book book = new Book();
Console.Write("Enter Book id to be deleted : ");

int Del = int.Parse(Console.ReadLine());

if (bookList.Exists(x => x.bookId == Del))
{
bookList.RemoveAt(Del - 1);
Console.WriteLine("Book id - {0} has been deleted", Del);
}
else
{
Console.WriteLine("Invalid Book id");
}

bookList.Add(book);
}

//To search book details from the Library database using Book id
public static void SearchBook()
{
Book book = new Book();
Console.Write("Search by BOOK id :");
int find = int.Parse(Console.ReadLine());

if (bookList.Exists(x => x.bookId == find))
{
foreach (Book searchId in bookList)
{
if (searchId.bookId == find)
{
Console.WriteLine("Book id :{0} " +
"Book name :{1} " +
"Book price :{2} " +
"Book Count :{3}", searchId.bookId, searchId.bookName, searchId.bookPrice, searchId.bookCount);
}
}
}
else
{
Console.WriteLine("Book id {0} not found", find);
}
}

//To borrow book details from the Library
public static void Borrow()
{
Book book = new Book();
BorrowDetails borrow = new BorrowDetails();
Console.WriteLine("User id : {0}", (borrow.userId = borrowList.Count + 1));
Console.Write("Name :");

borrow.userName = Console.ReadLine();

Console.Write("Book id :");
borrow.borrowBookId = int.Parse(Console.ReadLine());
Console.Write("Number of Books : ");
borrow.borrowCount= int.Parse(Console.ReadLine());
Console.Write("Address :");
borrow.userAddress = Console.ReadLine();
borrow.borrowDate = DateTime.Now;
Console.WriteLine("Date - {0} and Time - {1}", borrow.borrowDate.ToShortDateString(), borrow.borrowDate.ToShortTimeString());

if (bookList.Exists(x => x.bookId == borrow.borrowBookId))
{
foreach (Book searchId in bookList)
{
if (searchId.bookCount >= searchId.bookCount - borrow.borrowCount && searchId.bookCount - borrow.borrowCount >= 0)
{
if (searchId.bookId == borrow.borrowBookId)
{
searchId.bookCount = searchId.bookCount - borrow.borrowCount;
break;
}
}
else
{
Console.WriteLine("Only {0} books are found", searchId.bookCount);
break;
}
}
}
else
{
Console.WriteLine("Book id {0} not found", borrow.borrowBookId);
}
borrowList.Add(borrow);
}

//To return borrowed book to the library
public static void ReturnBook()
{
Book book = new Book();
Console.WriteLine("Enter following details :");

Console.Write("Book id : ");
int returnId = int.Parse(Console.ReadLine());

Console.Write("Number of Books:");
int returnCount = int.Parse(Console.ReadLine());

if (bookList.Exists(y => y.bookId == returnId))
{
foreach (Book addReturnBookCount in bookList)
{
if (addReturnBookCount.x >= returnCount + addReturnBookCount.bookCount)
{
if (addReturnBookCount.bookId == returnId)
{
addReturnBookCount.bookCount = addReturnBookCount.bookCount + returnCount;
break;
}
}
else
{
Console.WriteLine("Count exists the actual count");
break;
}
}
}
else
{
Console.WriteLine("Book id {0} not found", returnId);
}
}
}
}