I am making a library application in Java that will allow me to search, check ou
ID: 3778334 • Letter: I
Question
I am making a library application in Java that will allow me to search, check out, and return a book. After that I also need to put it in a file. I completed the search option, but am struggling to figure out how to build the check out method. Here is my code so far. I have two classes. Library is my main class.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Library { public static void main(String[] args)
{ // TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int a;
boolean good;
System.out.println("Welcome to Dropout library");
delay();
System.out.println("Please enter your name");
String name = input.next();
do{ System.out.println(name + ", Please select from the following options: Press 1 to search a book Press 2 to checkout a book press 3 to return a book press 4 to exit");
a = input.nextInt();
if( a==1){
good = true;
System.out.println(" What book would you like to search? Type in the title of the book.");
String books = input.next();
System.out.print( " The book " + books + Book.titleSearch(books)); }
else if (a == 2){
good = true;
System.out.println("Enter the book you want to check out " ); }
else if(a == 3){
good = true;
System.out.print("What book are you returning today?"); }
else if(a == 4){
good = true;
System.out.print("See you agian soon"); }
else{
System.out.print("Sorry invalid input, try again ");
good = false; }
} while(!(good) || a==1 || a==2 || a==3); }
public static void delay(){
try{ Thread.sleep(1000); }catch(java.lang.InterruptedException ex)
{ System.out.print(ex); } } }
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Book implements Serializable{
private String title;
private String author;
private String genre;
private int isbnNumber;
private int quantity;
public boolean available = true;
public Book(String title, String author, String genre, int isbnNumber, int quantity) {
this.title = title;
this.author = author;
this.genre = genre;
this.isbnNumber = isbnNumber;
this.quantity = quantity; }
public int getQuantity() {
return quantity; }
public void
setQuantity(int quantity) {
this.quantity = quantity; }
public String getTitle() {
return title; }
public void setTitle(String title) {
this.title = title; }
public String
getAuthor() {
return author; }
public void setAuthor(String author) {
this.author = author; }
public String getGenre() {
return genre; }
public void setGenre(String genre) { t
his.genre = genre; }
public int getIsbnNumber() {
return isbnNumber; }
public void setIsbnNumber
(int isbnNumber) {
this.isbnNumber = isbnNumber; }
public static String titleSearch(String title){
Book book1 = new Book("Java" ,"hd" , "Horror", 192837647, 2);
Book book2 = new Book("Constructors", "John","ScienceFiction", 839284823, 3);
Book book3 = new Book("History_of_Java", "Albert", "History", 928374651, 4);
List books = new ArrayList();
books.add(book1);
books.add(book2);
books.add(book3);
for(int i = 0; i
Explanation / Answer
import java.util.Scanner;
public class Library
{
private String libraryName;
private Borrower[] libraryUsers = new Borrower[20];
private BookInformation [] libraryItems = new BookInformation[2];
Scanner input = new Scanner(System.in);
int x=0;
int i=0;
public Library(String libraryName)
{
this.libraryName=libraryName;
}
private String inputString(String prompt) {
System.out.println(prompt);
return input.next();
}
private int inputInt(String prompt) {
int val = -1;
for(;;)/>
{
System.out.println(prompt);
try {
val = Integer.parseInt(input.next());
break;
}
catch (Exception ex) {
System.out.println("Invalid value, please try again.");
}
}
return val;
}
private double inputDouble(String prompt) {
double val = -1.00;
for(;;)/>
{
System.out.println(prompt);
try {
val = Double.parseDouble(input.next());
break;
}
catch (Exception ex) {
System.out.println("Invalid value, please try again.");
}
}
return val;
}
public void addUser(int choice)
{
if(choice==1)
{
String firstName = inputString("Please enter your first name ");
String lastName = inputString("Please enter your last name ");
String title = inputString("Please enter your title ");
String dateOfBirth = inputString("Please enter your date of birth ");
String homeAddress = inputString("Please enter your home address ");
String phoneNumber = inputString("Please enter your phone number ");
int barCode = inputInt("Please enter your library card barcode");
String studentId = inputString("Please enter your Student ID ");
StudentBorrower studentBor = new StudentBorrower(firstName, lastName, title, dateOfBirth, homeAddress, phoneNumber, barCode, studentId);
libraryUsers[x]=studentBor;
x++;
}
else if(choice==2)
{
String firstName = inputString("Please enter your first name ");
String lastName = inputString("Please enter your last name ");
String title = inputString("Please enter your title ");
String dateOfBirth = inputString("Please enter your date of birth ");
String homeAddress = inputString("Please enter your home address ");
String phoneNumber = inputString("Please enter your phone number ");
int barCode = inputInt("Please enter your library card barcode");
String studentId = inputString("Please enter your Student ID ");
StaffBorrower staffBor = new StaffBorrower(firstName, lastName, title, dateOfBirth, homeAddress, phoneNumber, barCode, studentId);
libraryUsers[x]=staffBor;
x++;
}
}
public void addItem(int choice)
{
if(choice==1)
{
String title = inputString("Please enter your item code ");
String author = inputString("Please enter the items due date ");
String year = inputString("Please enter your items title ");
String price = inputString("Please enter your books title ");
BookInformation book = new BookInformation(title,author,year,price);
libraryItems[i] = book;
i++;
}
else if(choice==2)
{
String title = inputString("Please enter your item code ");
String author = inputString("Please enter the items due date ");
String pages = inputString("Please enter your items title ");
String refNumber = inputString("Please enter your journals title ");
Journal jnal = new Journal(title,author,pages,refNumber);
libraryItems[x] = dvd;
i++;
}
}
public void getBorrowers()
{
for(int x=0; x<libraryUsers.length; x++)
{
if (libraryUsers[x] == null)
{
break;
}
else
{
System.out.println(libraryUsers[x].toString());
}
}
}
public void getItems()
{
for(int x=0; x<libraryItems.length; x++)
{
System.out.println(libraryItems[x].toString());
}
}
public Person[] searchUsers()
{
System.out.println("Please enter the surname of the user you wish to find");
return null;
}
public BookInformation[] searchItems()
{
return null;
}
public int textMenu()
{
System.out.println("WELCOME TO " + libraryName + " LIBRARY" + " ");
System.out.println("To add a User, press 1" + " ");
System.out.println("To add an Item, press 2" + " ");
System.out.println("To list the users of this library, press 3" + " ");
System.out.println("To list the items in this library, press 4" + " ");
System.out.println("To search for a user, press 5" + " ");
System.out.println("To search for an Item, press 6" + " ");
System.out.println("To exit the system, press 7" + " ");
int choice = input.nextInt();
return choice;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.