Write a console application to meet the following requirements. Create a system
ID: 3876211 • Letter: W
Question
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 display information about all of the books and to delete a book (by title) from the library.
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 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 " +
" R to remove a book from the library " +
" Q to quit this program ");
Console.Write ("enter choice here: ");
}
}
Explanation / Answer
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];
string bt,au,bid;
while (pick!='Q') {
switch (pick) {
case 'A':
Console.WriteLine("Enter Book Title : ");
bt = Console.ReadLine();
Console.WriteLine("Enter Book Author : ");
au = Console.ReadLine();
Console.WriteLine("Enter Book ID : ");
bid = Console.ReadLine();
lib.AddBook(bt,au,bid);
break;
case 'S':
Console.WriteLine("Enter Book Title to Search : ");
bt = Console.ReadLine();
lib.search(bt);
break;
case 'D':
lib.display();
break;
case 'R':
Console.WriteLine("Enter Book Title to Delete : ");
bt = Console.ReadLine();
remove(bt);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 " +
" R to remove a book from the library " +
" Q to quit this program ");
Console.Write ("enter choice here: ");
}
}
class Library
{
int c=0;
int id[100];//maximum 100 records can be stored
string author[100];//to store more please just increase 100
string title[100];// for title of the book
bool areEqual(string a,string b) // for comparing book titles are equal or not
{
if (a==b) return true;
else return false;
}
bool IsAlreadyExisted(string bookTitle)//checking wheather a book is already existed or not
{
int i=0;
for (i=0;title[i]!=null;i++)
{
if (areEqual(title[i],bookTitle))
{
return true;
}
}
return false;
}
void search(string bookTitle)//for searching for a book
{
int i=0;
for (i=0;title[i]!=null;i++)
{
if (title[i]==bookTitle)
{
Console.WriteLine(title[i]+" "+author[i]+" "+id[i]);
return;
}
}
Console.WriteLine("Not Found!");
return;
}
void AddBook(string t,string a,string i) // for adding a book
{
if (IsAlreadyExisted(t)!=true)
{
id[c]=i;
author[c]=a;
title[c]=t;
c++;
}
else
{
Console.WriteLine("Book Already Existed ");
Console.WriteLine("Add a New book! ");
}
}
void remove(string bookTitle) // for removing a book for the lib
{
if(IsAlreadyExisted(bookTitle)==true)
{
int i;
for (i=0;title[i]!=null;i++)
{
if(areEqual(bookTitle,title[i])
{
title[i]="_rm_";
author[i]="_rm_";
id[i]=-999;
break;
}
}
return;
}else
{
Console.WriteLine("Book Dosen't Existed! ");
}
}
void display()// display
{
int i=0;
Console.WriteLine("title author id");
for(i=0;title[i]!=null;i++)
{
if (title[i]=="_rm_")// if the book is removed not printing.
{
continue;
}
else
{
Console.WriteLine(title[i]+" "+author[i]+" "+id[i]);
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.