fork download
  1. import java.util.Scanner;
  2.  
  3. public class LibraryManagementSystem {
  4. // Classes for Book, Student, and Library Management
  5. static class Book {
  6. private int sNo;
  7. private String bookName;
  8. private String authorName;
  9. private int bookQty;
  10. private int bookQtyCopy;
  11.  
  12. public Book() {
  13. Scanner input = new Scanner(System.in);
  14. System.out.print("Enter Serial No of Book: ");
  15. this.sNo = input.nextInt();
  16. input.nextLine(); // Clear buffer
  17.  
  18. System.out.print("Enter Book Name: ");
  19. this.bookName = input.nextLine();
  20.  
  21. System.out.print("Enter Author Name: ");
  22. this.authorName = input.nextLine();
  23.  
  24. System.out.print("Enter Quantity of Books: ");
  25. this.bookQty = input.nextInt();
  26. this.bookQtyCopy = this.bookQty;
  27. }
  28.  
  29. public int getsNo() { return sNo; }
  30. public String getBookName() { return bookName; }
  31. public String getAuthorName() { return authorName; }
  32. public int getBookQty() { return bookQty; }
  33. public int getBookQtyCopy() { return bookQtyCopy; }
  34.  
  35. public void addBookQty(int qty) {
  36. this.bookQty += qty;
  37. this.bookQtyCopy += qty;
  38. }
  39. }
  40.  
  41. static class Student {
  42. private String studentName;
  43. private String regNum;
  44. private Book[] borrowedBooks = new Book[3];
  45. public int booksCount = 0;
  46.  
  47. public Student() {
  48. Scanner input = new Scanner(System.in);
  49. System.out.print("Enter Student Name: ");
  50. this.studentName = input.nextLine();
  51.  
  52. System.out.print("Enter Registration Number: ");
  53. this.regNum = input.nextLine();
  54. }
  55.  
  56. public String getRegNum() { return regNum; }
  57. public String getStudentName() { return studentName; }
  58. }
  59.  
  60. static class Library {
  61. private Book[] books = new Book[50];
  62. private Student[] students = new Student[50];
  63. private static int bookCount = 0;
  64. private static int studentCount = 0;
  65.  
  66. public void addBook(Book book) {
  67. for (int i = 0; i < bookCount; i++) {
  68. if (book.getsNo() == books[i].getsNo() || book.getBookName().equalsIgnoreCase(books[i].getBookName())) {
  69. System.out.println("Book already exists.");
  70. return;
  71. }
  72. }
  73. if (bookCount < 50) {
  74. books[bookCount++] = book;
  75. System.out.println("Book added successfully.");
  76. } else {
  77. System.out.println("No space to add more books.");
  78. }
  79. }
  80.  
  81. public void showAllBooks() {
  82. System.out.println("S.No\tName\tAuthor\tAvailable Qty\tTotal Qty");
  83. for (int i = 0; i < bookCount; i++) {
  84. System.out.printf("%d\t%s\t%s\t%d\t%d%n",
  85. books[i].getsNo(), books[i].getBookName(), books[i].getAuthorName(),
  86. books[i].getBookQtyCopy(), books[i].getBookQty());
  87. }
  88. }
  89.  
  90. public void searchBySerialNumber(int sNo) {
  91. for (int i = 0; i < bookCount; i++) {
  92. if (books[i].getsNo() == sNo) {
  93. System.out.printf("Found: %d\t%s\t%s\t%d\t%d%n",
  94. books[i].getsNo(), books[i].getBookName(), books[i].getAuthorName(),
  95. books[i].getBookQtyCopy(), books[i].getBookQty());
  96. return;
  97. }
  98. }
  99. System.out.println("No Book found with Serial No " + sNo);
  100. }
  101.  
  102. public void addStudent(Student student) {
  103. for (int i = 0; i < studentCount; i++) {
  104. if (student.getRegNum().equalsIgnoreCase(students[i].getRegNum())) {
  105. System.out.println("Student is already registered.");
  106. return;
  107. }
  108. }
  109. if (studentCount < 50) {
  110. students[studentCount++] = student;
  111. System.out.println("Student registered successfully.");
  112. } else {
  113. System.out.println("No space to add more students.");
  114. }
  115. }
  116.  
  117. public void showAllStudents() {
  118. System.out.println("Student Name\tReg Number");
  119. for (int i = 0; i < studentCount; i++) {
  120. System.out.printf("%s\t%s%n", students[i].getStudentName(), students[i].getRegNum());
  121. }
  122. }
  123. }
  124.  
  125. public static void main(String[] args) {
  126. Scanner input = new Scanner(System.in);
  127. Library library = new Library();
  128. int choice;
  129.  
  130. do {
  131. displayMenu();
  132. choice = input.nextInt();
  133.  
  134. switch (choice) {
  135. case 1:
  136. library.addBook(new Book());
  137. break;
  138. case 2:
  139. library.showAllBooks();
  140. break;
  141. case 3:
  142. System.out.print("Enter Serial No of Book to search: ");
  143. int sNo = input.nextInt();
  144. library.searchBySerialNumber(sNo);
  145. break;
  146. case 4:
  147. library.addStudent(new Student());
  148. break;
  149. case 5:
  150. library.showAllStudents();
  151. break;
  152. case 0:
  153. System.out.println("Exiting the application.");
  154. break;
  155. default:
  156. System.out.println("Invalid choice. Please select between 0 to 5.");
  157. }
  158. } while (choice != 0);
  159. input.close();
  160. }
  161.  
  162. private static void displayMenu() {
  163. System.out.println("******************** Welcome to the Library! ********************");
  164. System.out.println("1. Add new Book");
  165. System.out.println("2. Show All Books");
  166. System.out.println("3. Search a Book by Serial No");
  167. System.out.println("4. Register Student");
  168. System.out.println("5. Show All Registered Students");
  169. System.out.println("0. Exit");
  170. System.out.println("********************************************************************");
  171. }
  172. }
  173. import java.util.Scanner;
  174.  
  175. public class LibraryManagementSystem {
  176. // Classes for Book, Student, and Library Management
  177. static class Book {
  178. private int sNo;
  179. private String bookName;
  180. private String authorName;
  181. private int bookQty;
  182. private int bookQtyCopy;
  183.  
  184. public Book() {
  185. Scanner input = new Scanner(System.in);
  186. System.out.print("Enter Serial No of Book: ");
  187. this.sNo = input.nextInt();
  188. input.nextLine(); // Clear buffer
  189.  
  190. System.out.print("Enter Book Name: ");
  191. this.bookName = input.nextLine();
  192.  
  193. System.out.print("Enter Author Name: ");
  194. this.authorName = input.nextLine();
  195.  
  196. System.out.print("Enter Quantity of Books: ");
  197. this.bookQty = input.nextInt();
  198. this.bookQtyCopy = this.bookQty;
  199. }
  200.  
  201. public int getsNo() { return sNo; }
  202. public String getBookName() { return bookName; }
  203. public String getAuthorName() { return authorName; }
  204. public int getBookQty() { return bookQty; }
  205. public int getBookQtyCopy() { return bookQtyCopy; }
  206.  
  207. public void addBookQty(int qty) {
  208. this.bookQty += qty;
  209. this.bookQtyCopy += qty;
  210. }
  211. }
  212.  
  213. static class Student {
  214. private String studentName;
  215. private String regNum;
  216. private Book[] borrowedBooks = new Book[3];
  217. public int booksCount = 0;
  218.  
  219. public Student() {
  220. Scanner input = new Scanner(System.in);
  221. System.out.print("Enter Student Name: ");
  222. this.studentName = input.nextLine();
  223.  
  224. System.out.print("Enter Registration Number: ");
  225. this.regNum = input.nextLine();
  226. }
  227.  
  228. public String getRegNum() { return regNum; }
  229. public String getStudentName() { return studentName; }
  230. }
  231.  
  232. static class Library {
  233. private Book[] books = new Book[50];
  234. private Student[] students = new Student[50];
  235. private static int bookCount = 0;
  236. private static int studentCount = 0;
  237.  
  238. public void addBook(Book book) {
  239. for (int i = 0; i < bookCount; i++) {
  240. if (book.getsNo() == books[i].getsNo() || book.getBookName().equalsIgnoreCase(books[i].getBookName())) {
  241. System.out.println("Book already exists.");
  242. return;
  243. }
  244. }
  245. if (bookCount < 50) {
  246. books[bookCount++] = book;
  247. System.out.println("Book added successfully.");
  248. } else {
  249. System.out.println("No space to add more books.");
  250. }
  251. }
  252.  
  253. public void showAllBooks() {
  254. System.out.println("S.No\tName\tAuthor\tAvailable Qty\tTotal Qty");
  255. for (int i = 0; i < bookCount; i++) {
  256. System.out.printf("%d\t%s\t%s\t%d\t%d%n",
  257. books[i].getsNo(), books[i].getBookName(), books[i].getAuthorName(),
  258. books[i].getBookQtyCopy(), books[i].getBookQty());
  259. }
  260. }
  261.  
  262. public void searchBySerialNumber(int sNo) {
  263. for (int i = 0; i < bookCount; i++) {
  264. if (books[i].getsNo() == sNo) {
  265. System.out.printf("Found: %d\t%s\t%s\t%d\t%d%n",
  266. books[i].getsNo(), books[i].getBookName(), books[i].getAuthorName(),
  267. books[i].getBookQtyCopy(), books[i].getBookQty());
  268. return;
  269. }
  270. }
  271. System.out.println("No Book found with Serial No " + sNo);
  272. }
  273.  
  274. public void addStudent(Student student) {
  275. for (int i = 0; i < studentCount; i++) {
  276. if (student.getRegNum().equalsIgnoreCase(students[i].getRegNum())) {
  277. System.out.println("Student is already registered.");
  278. return;
  279. }
  280. }
  281. if (studentCount < 50) {
  282. students[studentCount++] = student;
  283. System.out.println("Student registered successfully.");
  284. } else {
  285. System.out.println("No space to add more students.");
  286. }
  287. }
  288.  
  289. public void showAllStudents() {
  290. System.out.println("Student Name\tReg Number");
  291. for (int i = 0; i < studentCount; i++) {
  292. System.out.printf("%s\t%s%n", students[i].getStudentName(), students[i].getRegNum());
  293. }
  294. }
  295. }
  296.  
  297. public static void main(String[] args) {
  298. Scanner input = new Scanner(System.in);
  299. Library library = new Library();
  300. int choice;
  301.  
  302. do {
  303. displayMenu();
  304. choice = input.nextInt();
  305.  
  306. switch (choice) {
  307. case 1:
  308. library.addBook(new Book());
  309. break;
  310. case 2:
  311. library.showAllBooks();
  312. break;
  313. case 3:
  314. System.out.print("Enter Serial No of Book to search: ");
  315. int sNo = input.nextInt();
  316. library.searchBySerialNumber(sNo);
  317. break;
  318. case 4:
  319. library.addStudent(new Student());
  320. break;
  321. case 5:
  322. library.showAllStudents();
  323. break;
  324. case 0:
  325. System.out.println("Exiting the application.");
  326. break;
  327. default:
  328. System.out.println("Invalid choice. Please select between 0 to 5.");
  329. }
  330. } while (choice != 0);
  331. input.close();
  332. }
  333.  
  334. private static void displayMenu() {
  335. System.out.println("******************** Welcome to the Library! ********************");
  336. System.out.println("1. Add new Book");
  337. System.out.println("2. Show All Books");
  338. System.out.println("3. Search a Book by Serial No");
  339. System.out.println("4. Register Student");
  340. System.out.println("5. Show All Registered Students");
  341. System.out.println("0. Exit");
  342. System.out.println("********************************************************************");
  343. }
  344. }
Success #stdin #stdout 0.04s 26048KB
stdin
Standard input is empty
stdout
import java.util.Scanner;

public class LibraryManagementSystem {
    // Classes for Book, Student, and Library Management
    static class Book {
        private int sNo;
        private String bookName;
        private String authorName;
        private int bookQty;
        private int bookQtyCopy;

        public Book() {
            Scanner input = new Scanner(System.in);
            System.out.print("Enter Serial No of Book: ");
            this.sNo = input.nextInt();
            input.nextLine(); // Clear buffer

            System.out.print("Enter Book Name: ");
            this.bookName = input.nextLine();

            System.out.print("Enter Author Name: ");
            this.authorName = input.nextLine();

            System.out.print("Enter Quantity of Books: ");
            this.bookQty = input.nextInt();
            this.bookQtyCopy = this.bookQty;
        }

        public int getsNo() { return sNo; }
        public String getBookName() { return bookName; }
        public String getAuthorName() { return authorName; }
        public int getBookQty() { return bookQty; }
        public int getBookQtyCopy() { return bookQtyCopy; }

        public void addBookQty(int qty) {
            this.bookQty += qty;
            this.bookQtyCopy += qty;
        }
    }

    static class Student {
        private String studentName;
        private String regNum;
        private Book[] borrowedBooks = new Book[3];
        public int booksCount = 0;

        public Student() {
            Scanner input = new Scanner(System.in);
            System.out.print("Enter Student Name: ");
            this.studentName = input.nextLine();

            System.out.print("Enter Registration Number: ");
            this.regNum = input.nextLine();
        }

        public String getRegNum() { return regNum; }
        public String getStudentName() { return studentName; }
    }

    static class Library {
        private Book[] books = new Book[50];
        private Student[] students = new Student[50];
        private static int bookCount = 0;
        private static int studentCount = 0;

        public void addBook(Book book) {
            for (int i = 0; i < bookCount; i++) {
                if (book.getsNo() == books[i].getsNo() || book.getBookName().equalsIgnoreCase(books[i].getBookName())) {
                    System.out.println("Book already exists.");
                    return;
                }
            }
            if (bookCount < 50) {
                books[bookCount++] = book;
                System.out.println("Book added successfully.");
            } else {
                System.out.println("No space to add more books.");
            }
        }

        public void showAllBooks() {
            System.out.println("S.No\tName\tAuthor\tAvailable Qty\tTotal Qty");
            for (int i = 0; i < bookCount; i++) {
                System.out.printf("%d\t%s\t%s\t%d\t%d%n", 
                    books[i].getsNo(), books[i].getBookName(), books[i].getAuthorName(), 
                    books[i].getBookQtyCopy(), books[i].getBookQty());
            }
        }

        public void searchBySerialNumber(int sNo) {
            for (int i = 0; i < bookCount; i++) {
                if (books[i].getsNo() == sNo) {
                    System.out.printf("Found: %d\t%s\t%s\t%d\t%d%n",
                        books[i].getsNo(), books[i].getBookName(), books[i].getAuthorName(), 
                        books[i].getBookQtyCopy(), books[i].getBookQty());
                    return;
                }
            }
            System.out.println("No Book found with Serial No " + sNo);
        }

        public void addStudent(Student student) {
            for (int i = 0; i < studentCount; i++) {
                if (student.getRegNum().equalsIgnoreCase(students[i].getRegNum())) {
                    System.out.println("Student is already registered.");
                    return;
                }
            }
            if (studentCount < 50) {
                students[studentCount++] = student;
                System.out.println("Student registered successfully.");
            } else {
                System.out.println("No space to add more students.");
            }
        }

        public void showAllStudents() {
            System.out.println("Student Name\tReg Number");
            for (int i = 0; i < studentCount; i++) {
                System.out.printf("%s\t%s%n", students[i].getStudentName(), students[i].getRegNum());
            }
        }
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Library library = new Library();
        int choice;

        do {
            displayMenu();
            choice = input.nextInt();

            switch (choice) {
                case 1:
                    library.addBook(new Book());
                    break;
                case 2:
                    library.showAllBooks();
                    break;
                case 3:
                    System.out.print("Enter Serial No of Book to search: ");
                    int sNo = input.nextInt();
                    library.searchBySerialNumber(sNo);
                    break;
                case 4:
                    library.addStudent(new Student());
                    break;
                case 5:
                    library.showAllStudents();
                    break;
                case 0:
                    System.out.println("Exiting the application.");
                    break;
                default:
                    System.out.println("Invalid choice. Please select between 0 to 5.");
            }
        } while (choice != 0);
        input.close();
    }

    private static void displayMenu() {
        System.out.println("******************** Welcome to the Library! ********************");
        System.out.println("1. Add new Book");
        System.out.println("2. Show All Books");
        System.out.println("3. Search a Book by Serial No");
        System.out.println("4. Register Student");
        System.out.println("5. Show All Registered Students");
        System.out.println("0. Exit");
        System.out.println("********************************************************************");
    }
}
import java.util.Scanner;

public class LibraryManagementSystem {
    // Classes for Book, Student, and Library Management
    static class Book {
        private int sNo;
        private String bookName;
        private String authorName;
        private int bookQty;
        private int bookQtyCopy;

        public Book() {
            Scanner input = new Scanner(System.in);
            System.out.print("Enter Serial No of Book: ");
            this.sNo = input.nextInt();
            input.nextLine(); // Clear buffer

            System.out.print("Enter Book Name: ");
            this.bookName = input.nextLine();

            System.out.print("Enter Author Name: ");
            this.authorName = input.nextLine();

            System.out.print("Enter Quantity of Books: ");
            this.bookQty = input.nextInt();
            this.bookQtyCopy = this.bookQty;
        }

        public int getsNo() { return sNo; }
        public String getBookName() { return bookName; }
        public String getAuthorName() { return authorName; }
        public int getBookQty() { return bookQty; }
        public int getBookQtyCopy() { return bookQtyCopy; }

        public void addBookQty(int qty) {
            this.bookQty += qty;
            this.bookQtyCopy += qty;
        }
    }

    static class Student {
        private String studentName;
        private String regNum;
        private Book[] borrowedBooks = new Book[3];
        public int booksCount = 0;

        public Student() {
            Scanner input = new Scanner(System.in);
            System.out.print("Enter Student Name: ");
            this.studentName = input.nextLine();

            System.out.print("Enter Registration Number: ");
            this.regNum = input.nextLine();
        }

        public String getRegNum() { return regNum; }
        public String getStudentName() { return studentName; }
    }

    static class Library {
        private Book[] books = new Book[50];
        private Student[] students = new Student[50];
        private static int bookCount = 0;
        private static int studentCount = 0;

        public void addBook(Book book) {
            for (int i = 0; i < bookCount; i++) {
                if (book.getsNo() == books[i].getsNo() || book.getBookName().equalsIgnoreCase(books[i].getBookName())) {
                    System.out.println("Book already exists.");
                    return;
                }
            }
            if (bookCount < 50) {
                books[bookCount++] = book;
                System.out.println("Book added successfully.");
            } else {
                System.out.println("No space to add more books.");
            }
        }

        public void showAllBooks() {
            System.out.println("S.No\tName\tAuthor\tAvailable Qty\tTotal Qty");
            for (int i = 0; i < bookCount; i++) {
                System.out.printf("%d\t%s\t%s\t%d\t%d%n", 
                    books[i].getsNo(), books[i].getBookName(), books[i].getAuthorName(), 
                    books[i].getBookQtyCopy(), books[i].getBookQty());
            }
        }

        public void searchBySerialNumber(int sNo) {
            for (int i = 0; i < bookCount; i++) {
                if (books[i].getsNo() == sNo) {
                    System.out.printf("Found: %d\t%s\t%s\t%d\t%d%n",
                        books[i].getsNo(), books[i].getBookName(), books[i].getAuthorName(), 
                        books[i].getBookQtyCopy(), books[i].getBookQty());
                    return;
                }
            }
            System.out.println("No Book found with Serial No " + sNo);
        }

        public void addStudent(Student student) {
            for (int i = 0; i < studentCount; i++) {
                if (student.getRegNum().equalsIgnoreCase(students[i].getRegNum())) {
                    System.out.println("Student is already registered.");
                    return;
                }
            }
            if (studentCount < 50) {
                students[studentCount++] = student;
                System.out.println("Student registered successfully.");
            } else {
                System.out.println("No space to add more students.");
            }
        }

        public void showAllStudents() {
            System.out.println("Student Name\tReg Number");
            for (int i = 0; i < studentCount; i++) {
                System.out.printf("%s\t%s%n", students[i].getStudentName(), students[i].getRegNum());
            }
        }
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Library library = new Library();
        int choice;

        do {
            displayMenu();
            choice = input.nextInt();

            switch (choice) {
                case 1:
                    library.addBook(new Book());
                    break;
                case 2:
                    library.showAllBooks();
                    break;
                case 3:
                    System.out.print("Enter Serial No of Book to search: ");
                    int sNo = input.nextInt();
                    library.searchBySerialNumber(sNo);
                    break;
                case 4:
                    library.addStudent(new Student());
                    break;
                case 5:
                    library.showAllStudents();
                    break;
                case 0:
                    System.out.println("Exiting the application.");
                    break;
                default:
                    System.out.println("Invalid choice. Please select between 0 to 5.");
            }
        } while (choice != 0);
        input.close();
    }

    private static void displayMenu() {
        System.out.println("******************** Welcome to the Library! ********************");
        System.out.println("1. Add new Book");
        System.out.println("2. Show All Books");
        System.out.println("3. Search a Book by Serial No");
        System.out.println("4. Register Student");
        System.out.println("5. Show All Registered Students");
        System.out.println("0. Exit");
        System.out.println("********************************************************************");
    }
}