// Online Java Compiler // Use this editor to write, compile and run your Java code…
- User
// Online Java Compiler // Use this editor to write, compile and run your Java code online import java.util.Scanner; import java.util.Arrays;
public class Library {
public static String[][] addBook(String[][] db ){ // initiate scanner object Scanner sc = new Scanner(System.in); // create a new larger database String[][] newDB = new String[db.length + 1][db[0].length]; for (int i=0; i<db.length; i++){ for (int j=0; j < db[i].length; j++){ newDB[i][j] = db[i][j]; } } // ask user input for last row System.out.println("What is the book title?"); String newTitle = sc.nextLine().toLowerCase(); System.out.println("Who is the Author?"); String newAuthor = sc.nextLine().toLowerCase(); System.out.println("How many books are you adding?"); String newAvailability = sc.nextLine(); String[] newRow = {newTitle, newAuthor, newAvailability}; newDB[db.length] = newRow; // find if book already exist for (int i = 0; i < db.length; i++) { if (newTitle.equals(db[i][0]) && newAuthor.equals(db[i][1])) { db[i][2] = Integer.toString(Integer.parseInt(db[i][2]) + Integer.parseInt(newAvailability)); newDB = db; //break; } } return newDB; } public static String[][] borrowBook(String[][] db ) { // initiate scanner object Scanner sc = new Scanner(System.in); // ask user input for last row System.out.println("What is the book title you want to borrow?"); String borrowTitle = sc.nextLine().toLowerCase(); System.out.println("Who is the Author of the boow you want to borrow?"); String borrowAuthor = sc.nextLine().toLowerCase(); System.out.println("How many books are you borrowing?"); String borrowAvailability = sc.nextLine(); // find if book already exist // check out a book try{ for (int i = 0; i < db.length; i++) { if (borrowTitle.equals(db[i][0]) && borrowAuthor.equals(db[i][1])) { if (Integer.parseInt(db[i][2]) - Integer.parseInt(borrowAvailability) > 0 ) { db[i][2] = Integer.toString(Integer.parseInt(db[i][2]) - Integer.parseInt(borrowAvailability)); break; }else{ System.out.println("Not enough books in stock"); break; } }else{ System.out.println("No such book in stock"); break; } } }catch (Exception e) { // Default exception handler System.out.println(“An unexpected exception occurred! Only number inputs are accepted in availibity”); } return db; } public static String[][] returnBook(String[][] db ) { // initiate scanner object Scanner sc = new Scanner(System.in); // ask user input for last row System.out.println("What is the book title you want to return?"); String returnTitle = sc.nextLine().toLowerCase(); System.out.println("Who is the Author of the boow you want to return?"); String returnAuthor = sc.nextLine().toLowerCase(); System.out.println("How many books are you returning?"); String returnAvailability = sc.nextLine(); // find if book already exist // check out a book for (int i = 0; i < db.length; i++) { if (returnTitle.equals(db[i][0]) && returnAuthor.equals(db[i][1]) ) { db[i][2] = Integer.toString(Integer.parseInt(db[i][2]) + Integer.parseInt(returnAvailability)); break; }else{ System.out.println("No such book in stock or availibity not a number"); break; } } return db; } public static void main(String[] args) { // initiate scanner object Scanner sc = new Scanner(System.in); // database of books String[][] db = { {"title", "author", "quantity" }, {"harry potter", "jk rowling", "5"} }; System.out.println("Welcome to the Library System!"); System.out.println("\n"); System.out.println("What would you wish to do?"); System.out.println("\n"); System.out.println("A. Add books?"); System.out.println("B. Borrow books?"); System.out.println("C. Return books?"); System.out.println("\n"); // type in an answer String answer = sc.next().toLowerCase(); try{ if(answer.equals("a")){ db = addBook(db); }else if(answer.equals("b")){ db = borrowBook(db); }else if(answer.equals("c")){ db = returnBook(db); }else{ System.out.println("Please choose a correct letter from the choices provided."); } }catch(Exception e){ // Default exception handler System.out.println("An unexpected exception occurred!"); } System.out.println("\n"); } - Assistant
The main method is responsible for interacting with the user and calling the appropriate methods based on the user's input. It prompts the user to choose an action - add books, borrow books, or return books. Then, it uses a switch statement to call the corresponding method based on the user's choice. The addBook, borrowBook, and returnBook methods are responsible for adding books to the database, borrowing books from the database, and returning books to the database, respectively. These methods prompt the user for the necessary information and make the required updates to the database. The database is represented as a 2D array, with each row representing a book and each column representing a different attribute of the book (title, author, and quantity).
- User
Use the program above as a basis to complete the following assignment. Use 2D arrays to save the data. Add globl variables to use in the methods and the control accesses that are asked. The assignment goes like this For this assignment, you are tasked with developing a Student Record Management System in Java for a university. The system should provide administrators with the ability to manage student records effectively. It should allow administrators to add new students, update student information, and view student details. The project should emphasize the use of appropriate access modifiers (public, private, protected) to control access to class members. Requirements:
- Assistant
- User
add comments explaining the code
- Assistant