Coding
Java Script
The Java Script project below was an assignment that I completed where we were tasked with creating a simple library check in and check out system. Part of the challenge was creating multiple arrays for the program to cycle through in order to determine if the student was eligible to check out a book as well as if there was a copy of the book for them to check out.
/* Robert Curtis Library Driver */ import java.util.Scanner; class LibraryDriver { public static void main(String[] args) { Scanner cin = new Scanner(System.in); String name; String author; int ISBN; int quantity; int id; System.out.println("Welcome to my Program"); System.out.println("Please make a selection from the following menu by entering the corresponding number"); int userChoice = 100; Library library = new Library(); library.generateArrays(); while(userChoice != 0) { System.out.println("1.Add a book \n2.Update the number of book \n3.Search for book by ISBN \n4.Print list of all books \n5.Register a new student \n6.Print a list of all registered students \n7.Is a student registered \n8.How many books a student has checked out \n9.Check out a book \n10.Check in a book \n0.To Exit"); userChoice = Integer.parseInt(cin.nextLine()); switch(userChoice) { case 1: { System.out.println("Please enter the name of the book:"); name = cin.nextLine(); System.out.println("Please enter the author's name"); author = cin.nextLine(); System.out.println("Please enter the initial quantity of the book"); quantity = Integer.parseInt(cin.nextLine()); library.addBook(name, author, quantity); break; } case 2: { System.out.println("Please enter the ISBN that you wish to update"); ISBN = Integer.parseInt(cin.nextLine()); System.out.println("Enter the new quantity"); quantity = Integer.parseInt(cin.nextLine()); library.updateBookQuantity(ISBN, quantity); break; } case 3: { System.out.println("Please enter the ISBN of the book"); ISBN = Integer.parseInt(cin.nextLine()); library.searchBookByISBN(ISBN); break; } case 4: { System.out.println("The books listed in the array are"); library.printAllBooks(); break; } case 5: { System.out.println("Please enter the students name"); name = cin.nextLine(); library.registerStudent(name); break; } case 6: { System.out.println("The students listed in the array are"); library.printAllStudents(); break; } case 7: { System.out.println("Please enter the students ID number"); id = Integer.parseInt(cin.nextLine()); boolean found = library.isStudentRegistered(id); if(found == true) { System.out.println("Student is already registered"); } else { System.out.println("Student is not registered"); } System.out.println(); break; } case 8: { System.out.println("Please enter the students ID: "); id = Integer.parseInt(cin.nextLine()); int numberOfBooks = library.numOfBooksCheckedOutByStudent(id); System.out.println("The student has: " + numberOfBooks + " currently checked out"); break; } case 9: { System.out.println("Please enter the students id "); id = Integer.parseInt(cin.nextLine()); System.out.println("Please enter the ISBN number for the book "); ISBN = Integer.parseInt(cin.nextLine()); library.checkOutBook(id, ISBN); break; } case 10: { System.out.println("Please enter the students id "); id = Integer.parseInt(cin.nextLine()); System.out.println("Please enter the ISBN of the book"); ISBN = Integer.parseInt(cin.nextLine()); library.checkInBook(id, ISBN); break; } } } System.out.println("Goodbye!"); } }
/* Robert Curtis Class Library */ class Library { private Book[] books = new Book[50]; private Student[] students = new Student[30]; int countAddBook = 0; int countAddStudent = 0; public void generateArrays() { for(int i = 0; i <= books.length -1; i++) { books[i] = new Book(); } for(int i = 0; i <= students.length -1; i++) { students[i] = new Student(); } } public void addBook(String name, String author, int quantity) { String tempName = books[countAddBook].getName(); if(tempName.equalsIgnoreCase("Invalid Name!")) { books[countAddBook] = new Book(name, author, quantity); System.out.println("The book " + books[countAddBook].getName() + " by " + books[countAddBook].getAuthor() + " whose ISBN is: " + books[countAddBook].getISBN() + " was succesfully added to the array at index: " + countAddBook); } else { System.out.println("An error occurred please try again"); } countAddBook = countAddBook + 1; } public void updateBookQuantity(int ISBN, int quantity) { boolean bookFound = false; int bookFoundAt = -1; for(int i =0; i <= books.length -1;i++) { //int searchedISBN = books[i].getISBN; if(ISBN == books[i].getISBN()) { bookFound = true; bookFoundAt = i; } } if(bookFound == true) { books[bookFoundAt].setQuantity(quantity); System.out.println(books[bookFoundAt].getName() + " quantity has been updated to: " + books[bookFoundAt].getQuantity()); } else { System.out.println("Sorry that ISBN is not recognized"); } } public Book searchBookByISBN(int ISBN) { boolean bookFound = false; int bookFoundAt = -1; for(int i = 0; i <= books.length - 1; i ++) { if(ISBN == books[i].getISBN()) { bookFound = true; bookFoundAt = i; } } if(bookFound == true) { System.out.println("Book Found"); System.out.println("Name: " + books[bookFoundAt].getName()); System.out.println("Author: " + books[bookFoundAt].getAuthor()); System.out.println("ISBN: " + books[bookFoundAt].getISBN()); System.out.println("Quantity: " + books[bookFoundAt].getQuantity()); return books[bookFoundAt]; } else { return null; } } public void printAllBooks() { for(int i = 0; i <= books.length - 1; i++) { if(!books[i].getName().equalsIgnoreCase("Invalid Name!")) { System.out.println("Book: " + books[i].getName()); System.out.println("Author: " + books[i].getAuthor()); System.out.println("ISBN: " + books[i].getISBN()); System.out.println("Quantity: " + books[i].getQuantity()); System.out.println(); } } } public void registerStudent(String name) { String tempName = students[countAddStudent].getName(); if(tempName.equalsIgnoreCase("Invalid Name!")) { students[countAddStudent] = new Student(name); System.out.println("The student " + students[countAddStudent].getName() + " whose ID is: " + students[countAddStudent].getId() + " has " + students[countAddStudent].getCheckedOutBooks() + " books currently checked out; has been added to the system"); } countAddStudent = countAddStudent + 1; } public void printAllStudents() { for(int i = 0; i<= students.length - 1; i++) { if(!students[i].getName().equalsIgnoreCase("Invalid Name!")) { System.out.println("Student's name: " + students[i].getName()); System.out.println("Student's ID: " + students[i].getId()); System.out.println("Number of checked out books: " + students[i].getCheckedOutBooks()); System.out.println(); } } } public boolean isStudentRegistered(int id) { boolean studentFound = false; for(int i = 0; i <= students.length -1; i++) { if(id == students[i].getId()) { studentFound = true; } } return studentFound; } public int numOfBooksCheckedOutByStudent(int id) { int result = -1; for(int i = 0; i <= students.length -1; i++) { if(id == students[i].getId()) { result = students[i].getCheckedOutBooks(); } } return result; } public void checkOutBook(int id, int ISBN) { int isbnFound = 0; for(int i = 0; i <= students.length - 1; i++) { if(id == students[i].getId()) { if(students[i].getCheckedOutBooks() != 3) { for(int j = 0; j <= books.length -1; j++) { if(ISBN == books[j].getISBN()) { isbnFound = j; if(books[isbnFound].getQuantity() != 0) { int totalCheckedOut = students[i].getCheckedOutBooks() +1; students[i].setCheckedOutBooks(totalCheckedOut); int totalQuantity = books[j].getQuantity() - 1; books[j].setQuantity(totalQuantity); System.out.println("Book successfully checked out"); break; } else { System.out.println("Their are no more books by that ISBN that can be checked out"); break; } } else { System.out.println("Their is no book with that ISBN"); break; } } } else { System.out.println("The student already has 3 books checked out"); break; } break; } else { System.out.println("Their is no student with that ID number please register the student and then try again"); break; } } } public void checkInBook(int id, int ISBN) { int isbnFound = 0; for(int i = 0; i <= students.length -1; i++) { if(id == students[i].getId()) { if(students[i].getCheckedOutBooks() != 0) { for(int j = 0; j
/* Robert Curtis Class Student */ class Student { private int id; private String name; private int checkedOutBooks; public Student() { name = "Invalid Name!"; id = -1; checkedOutBooks = -1; } public Student(String name) { this.name = name; id = generateId(); checkedOutBooks = 0; } private int generateId() { int id = (int) (Math.random() * 100000000) + 1; return id; } public int getId() { return id; } public String getName() { return name; } public int getCheckedOutBooks() { return checkedOutBooks; } public void setName(String name) { this.name = name; } public void setId() { if(id == -1) { id = generateId(); } } public void setCheckedOutBooks(int checkedOutBooks) { this.checkedOutBooks = checkedOutBooks; } }
/* Robert Curtis Class Book */ class Book { private String name; private String author; private int ISBN; private int quantity; public Book() { name = "Invalid Name!"; author = "Invalid Name!"; ISBN = -1; quantity = -1; } public Book(String name, String author, int quantity) { this.name = name; this.author = author; this.quantity = quantity; ISBN = generateISBN(); } private int generateISBN() { int ISBN = (int) (Math.random() * 100000000) +1; return ISBN; } public String getName() { return name; } public String getAuthor() { return author; } public int getISBN() { return ISBN; } public int getQuantity() { return quantity; } public void setName(String name) { this.name = name; } public void setAuthor(String author) { this.author = author; } public void setISBN(int ISBN) { this.ISBN = ISBN; } public void setQuantity(int quantity) { this.quantity = quantity; } }