Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9. //Library Class///////////////////////////////////////////////////////////
  10. class Library
  11. {
  12.  
  13. ////Potential Variables\
  14. Book[] library = new Book[1000];
  15.  
  16. public Library()
  17. {
  18.  
  19.  
  20.  
  21. // List<Book> lib = new List<Book>(library);
  22. }
  23.  
  24. public void AddBook(String t, String a, int id)
  25. {
  26. if (this.searchBook(t) == -1)
  27. {
  28. Book b = new Book(a, t, id);
  29. library[id] = b;
  30. }
  31. }
  32.  
  33. public int searchBook(String t)
  34. {
  35. int index = -1;
  36. for (int j = 0; j < library.Length; j++)
  37. {
  38. if (library[j] != null)
  39. {
  40. if (library[j].getTitle().Equals(t))
  41. {
  42. index = j;
  43. break;
  44. }
  45. }
  46. }
  47. return index;
  48. }
  49.  
  50. public void removeBook(String t)
  51. {
  52. if (this.searchBook(t) > -1)
  53. {
  54. library[this.searchBook(t)] = null;
  55. }
  56.  
  57. }
  58.  
  59. public String displayBook(String t)
  60. {
  61. int index = this.searchBook(t);
  62. return library[index].getTitle() + "'s " +
  63. library[index].getAuthor() + " (" +
  64. library[index].getId() + ")";
  65.  
  66. }
  67.  
  68. public String displayLibrary()
  69. {
  70. String output = "Library Contents:";
  71.  
  72. for (int i = 0; i < library.Length; i++)
  73. {
  74. if(library[i] != null)
  75. {
  76. output += " \n" + displayBook(library[i].getTitle());
  77. }
  78.  
  79.  
  80. }
  81. return output;
  82.  
  83. }
  84.  
  85.  
  86.  
  87. ///////////////////////Book Class/////////////////////////////
  88.  
  89. class Book
  90. {
  91. private string title;
  92. private string author;
  93. private int id;
  94.  
  95. public Book()
  96. {
  97. title = "Title";
  98. author = "Author"; //Def const.
  99. id = 0;
  100. }
  101.  
  102. public Book(string t, string a, int i) //par. const.
  103. {
  104. title = t;
  105. author = a;
  106. id = i;
  107. }
  108.  
  109. public String getTitle()
  110. {
  111. return title;
  112. }
  113.  
  114. public String getAuthor()
  115. {
  116. return author;
  117. }
  118.  
  119. public int getId()
  120. {
  121. return id;
  122. }
  123. }
  124. //////////////////////////////////Main Class//////////////////////////////////////////////////////
  125. class MainProject
  126. {
  127. static void Main(string[] args)
  128. {
  129. Library lib = new Library();
  130. lib.AddBook("C# programming", "Gesick", 4);
  131. lib.AddBook("java programming", "Roth", 2);
  132. lib.AddBook("C++ programming", "Franklin", 1);
  133. lib.AddBook("unity programming", "Preston", 3);
  134. lib.AddBook("graphics & multimedia", "Chastine", 5);
  135. printMenu();
  136. string p = Console.ReadLine();
  137. p = p.ToUpper();
  138. char pick = p[0];
  139.  
  140. while (pick != 'Q')
  141. {
  142. switch (pick)
  143. {
  144. case 'A':
  145. Console.WriteLine("Input book title.");
  146. string t = Console.ReadLine();
  147. Console.WriteLine("Input book’s Author.");
  148. string a = Console.ReadLine();
  149.  
  150. Console.WriteLine("Input ID number.");
  151. int id = Int32.Parse(Console.ReadLine());
  152. lib.AddBook(t, a, id);
  153. Console.WriteLine( t + " By " + a + " has been added to the library");
  154. break;
  155.  
  156. case 'S':
  157. Console.WriteLine("Please provide the title of the book.");
  158. string title = Console.ReadLine();
  159. if (lib.searchBook(title) == -1)
  160. {
  161. Console.WriteLine("Book does not exist within the library.");
  162. }
  163. else
  164. {
  165. lib.displayBook(title);
  166. }
  167. break;
  168. case 'D':
  169. Console.WriteLine(lib.displayLibrary());
  170. break;
  171. case 'R':
  172. Console.WriteLine("Please enter the book title that needs removal.");
  173. string tit = Console.ReadLine();
  174. if (lib.searchBook(tit) == -1)
  175. {
  176. Console.WriteLine("Book does not currently exist within the library.");
  177. }
  178. else
  179. {
  180. lib.removeBook(tit);
  181. Console.WriteLine("Book successfully removed.");
  182. }
  183. break;
  184. default:
  185. Console.WriteLine("\nInvalid choice, please re-enter");
  186. break;
  187. }
  188. printMenu();
  189. p = Console.ReadLine();
  190. p = p.ToUpper();
  191. pick = p[0];
  192. }
  193.  
  194. Console.WriteLine("good bye");
  195.  
  196. }
  197.  
  198. public static void printMenu()
  199. {
  200. Console.WriteLine("\nSelect one of the following:\n\n" +
  201. " A to add a book to the library\n" +
  202. " S to search for a book by title\n" +
  203. " D to display the contents of the library\n" +
  204. " R to remove a book from the library\n" +
  205. " Q to quit this program\n\n");
  206. Console.Write("enter choice here: ");
  207.  
  208. }
  209. }
  210. }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement