Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication1
- {
- //Library Class///////////////////////////////////////////////////////////
- class Library
- {
- ////Potential Variables\
- Book[] library = new Book[1000];
- public Library()
- {
- // List<Book> lib = new List<Book>(library);
- }
- public void AddBook(String t, String a, int id)
- {
- if (this.searchBook(t) == -1)
- {
- Book b = new Book(a, t, id);
- library[id] = b;
- }
- }
- public int searchBook(String t)
- {
- int index = -1;
- for (int j = 0; j < library.Length; j++)
- {
- if (library[j] != null)
- {
- if (library[j].getTitle().Equals(t))
- {
- index = j;
- break;
- }
- }
- }
- return index;
- }
- public void removeBook(String t)
- {
- if (this.searchBook(t) > -1)
- {
- library[this.searchBook(t)] = null;
- }
- }
- public String displayBook(String t)
- {
- int index = this.searchBook(t);
- return library[index].getTitle() + "'s " +
- library[index].getAuthor() + " (" +
- library[index].getId() + ")";
- }
- public String displayLibrary()
- {
- String output = "Library Contents:";
- for (int i = 0; i < library.Length; i++)
- {
- if(library[i] != null)
- {
- output += " \n" + displayBook(library[i].getTitle());
- }
- }
- return output;
- }
- ///////////////////////Book Class/////////////////////////////
- class Book
- {
- private string title;
- private string author;
- private int id;
- public Book()
- {
- title = "Title";
- author = "Author"; //Def const.
- id = 0;
- }
- public Book(string t, string a, int i) //par. const.
- {
- title = t;
- author = a;
- id = i;
- }
- public String getTitle()
- {
- return title;
- }
- public String getAuthor()
- {
- return author;
- }
- public int getId()
- {
- return id;
- }
- }
- //////////////////////////////////Main Class//////////////////////////////////////////////////////
- class MainProject
- {
- 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':
- Console.WriteLine("Input book title.");
- string t = Console.ReadLine();
- Console.WriteLine("Input book’s Author.");
- string a = Console.ReadLine();
- Console.WriteLine("Input ID number.");
- int id = Int32.Parse(Console.ReadLine());
- lib.AddBook(t, a, id);
- Console.WriteLine( t + " By " + a + " has been added to the library");
- break;
- case 'S':
- Console.WriteLine("Please provide the title of the book.");
- string title = Console.ReadLine();
- if (lib.searchBook(title) == -1)
- {
- Console.WriteLine("Book does not exist within the library.");
- }
- else
- {
- lib.displayBook(title);
- }
- break;
- case 'D':
- Console.WriteLine(lib.displayLibrary());
- break;
- case 'R':
- Console.WriteLine("Please enter the book title that needs removal.");
- string tit = Console.ReadLine();
- if (lib.searchBook(tit) == -1)
- {
- Console.WriteLine("Book does not currently exist within the library.");
- }
- else
- {
- lib.removeBook(tit);
- Console.WriteLine("Book successfully removed.");
- }
- break;
- default:
- Console.WriteLine("\nInvalid 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("\nSelect one of the following:\n\n" +
- " A to add a book to the library\n" +
- " S to search for a book by title\n" +
- " D to display the contents of the library\n" +
- " R to remove a book from the library\n" +
- " Q to quit this program\n\n");
- Console.Write("enter choice here: ");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement