Write a console application to meet the following requirements. Create a system
ID: 3884014 • 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 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 books name is unique. There are no duplicate titles. When adding a book, ensure that the title doesnt already exist. If it does exist, dont 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: ");
}
}
Your submission should include hard copies of all 3 classes and the output from running the program using the following options: D, A ( add a book of your own), e, s for a book in the library, s for a book not in the library, r for a book in the library, r for a book not in the library, f, and q.
Explanation / Answer
Below is your program. Let me know if you have any issue in comments
using System;
using System.Collections.Generic;
using System.Linq;
namespace MainClass
{
class MainClass
{
public static void Main (string[] args)
{
Library lib = new Library("Library 1");
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':
Console.WriteLine ("Please insert a title for the new book.");
string t = Console.ReadLine();
int ab = Library.Search(t);
if(ab == -1)
{
Console.WriteLine ("Please insert an author for the new book.");
string a = Console.ReadLine();
int numb = Library.GetBookCount();
lib.AddBook (t, a, numb + 1);
Console.WriteLine("Book successfully added!");
}
else
{
Console.WriteLine ("Book title already exists!");
break;
}
break;
case 'S':
Console.WriteLine ("Please type a book title to search: ");
string s = Console.ReadLine ();
int result = Library.Search(s);
if(result == -1)
{
Console.WriteLine("Book not found.");
}
break;
case 'D':
Console.WriteLine(lib.ToStringTitle());
break;
case 'E':
Console.WriteLine(lib.ToStringAuthor());
break;
case 'F':
Console.WriteLine(lib.ToStringID());
break;
case 'R':
Console.WriteLine ("Please type a book title to remove: ");
string y = Console.ReadLine ();
Library.RemoveBook(y);
Console.WriteLine ("Book successfully removed!");
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: ");
}
}
public class Library
{
private string libName;
static List<Book> bookTitles = new List<Book>();
public Library(string x)
{
libName = x;
}
public void AddBook(string t, string a, int ident)
{
Book book = new Book (t, a, ident);
bookTitles.Add (book);
}
public override string ToString()
{
string h = "";
foreach (Book z in bookTitles) {
h += z.ToString ();
h += " ";
}
return h;
}
public string ToStringTitle()
{
string h = "";
var sortedList = bookTitles.OrderBy(x => x.getTitle()).ToList();
foreach (Book z in sortedList) {
h += z.ToString ();
h += " ";
}
return h;
}
public string ToStringAuthor()
{
string h = "";
var sortedList = bookTitles.OrderBy(x => x.getAuthor()).ToList();
foreach (Book z in sortedList) {
h += z.ToString2 ();
h += " ";
}
return h;
}
public string ToStringID()
{
string h = "";
var sortedList = bookTitles.OrderBy(x => x.getID()).ToList();
foreach (Book z in sortedList) {
h += z.ToString2 ();
h += " ";
}
return h;
}
public static int Search(string title)
{
for (int i=0; i<= bookTitles.Count - 1; i++)
{
if(title == bookTitles[i].ToString())
{
Console.WriteLine (bookTitles[i].ToString2());
return i;
}
}
return -1;
}
public static void RemoveBook(string title)
{
for (int i=0; i<= bookTitles.Count - 1; i++)
{
if(title == bookTitles[i].ToString())
{
bookTitles.RemoveAt (i);
}
}
}
public static int GetBookCount()
{
return bookTitles.Count;
}
}
public class Book
{
private string title;
private string author;
private int ID;
public Book(string a, string b, int c)
{
title = a;
author = b;
ID = c;
}
public string getTitle() {
return title;
}
public string getAuthor() {
return author;
}
public int getID() {
return ID;
}
public override string ToString()
{
return title;
}
public string ToString2()
{
return title + " by " + author + " with ID " + ID;
}
}
}
Sample Run
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.