-1

First of all, I am sorry if it's hard to understand me as I am not native English speaker my point might be hard to understand.

I am trying to make a very basic library booking system, but I have a problem with getting user input to adjust book details.

What I am trying to do is to get the book details from user input, but I don't know how to do this properly. I tried the same style that I use to change status but it doesn't seem to work.

I am also having some difficulties with checking and changing the books status from available to booked.

This is the main program:

public static void main(String[] args) {

    System.out.println("Welcome to the basic library program! ");
    System.out.println("Insert author, name, genre, ISNB, page number ");

    // this one is wrong.
    Book booking = new Book("Mary","Random Book","Horror",69,69);

    System.out.println("Print book information? 1 = yes 2 = no?");
    int answer = input.nextInt();
    if (answer == 1) {
        booking.printBook();
    }
    else {

    }

    System.out.println("Check books status? 1= yes 2= no");
    int status = input.nextInt();
    if (status == 1) {
        booking.checkStatus(); 
    }
    else {

    }

    System.out.println("Change books status: 1 = Avaible 2 = Booked 3 = Lost ");
    int availability = input.nextInt();
    booking.changeAvailability(availability);
}

And this is the book class:

public class Book {

    private String author;
    private String name;
    private String genre;
    private int isbn;
    private int pageNumber;
    private int availability;
    private String setAvailability;

    public Book(String authorInput, String nameInput, String genreInput, int isbnInput, int pageInput) {

        this.author = authorInput;
        this.name = nameInput;
        this.genre = genreInput;
        this.isbn = isbnInput;
        this.pageNumber = pageInput;

    }



    public void changeAvailability(int availability) {  
        if (availability == 1) {
            setAvailability = "Avaible";
            System.out.println("Avaible.");
        }
        else if (availability == 2) {
            setAvailability = "Not avaible";
            System.out.println("Not avaible.");         
            }
        else if (availability == 3) {
            setAvailability = "Lost";
            System.out.println("Book missing.");
        }
        else {
               System.out.println("Unknown input: Program closing..");
               System.exit(1);
           }

    }

    public String checkStatus() {
        return this.setAvailability;
    }

    public void printBook() {

        System.out.print("Author: " + this.author + "\nName: " + this.name + "\nGenre: " + this.genre + "\nISBN: " + this.isbn + "\nPages: " + this.pageNumber + "\nAvailability:" + " ");

        }
    }
TRMN
  • 1
  • 1
  • 3
  • How could you change it to booked when you do not even have that condition (availability == 3) in your changeAvailibility method. – Nick Div Jul 21 '16 at 17:17
  • It is really unclear what your problem is. You should probably try to write the code to ask the book details, and then set them on the book, and then tell us, what error (build error or wrong output for some input) you are getting. – hyde Jul 21 '16 at 17:58
  • The problem is I want the details to be asked from the user. Currently the details are delivered with ''Book booking = new Book("Mary","Random Book","Horror",69,69);'' instead of reading input line, but I don't know how to properly do that. – TRMN Jul 21 '16 at 20:49

2 Answers2

1

You need to use a Scanner in your main() class. For example in your code:

Scanner input = new Scanner (System.in);

needs to be entered before you try to get the user's input. This will allow you to call input.nextInt(); successfully.

See a previous question here: How can I get the user input in Java?

Community
  • 1
  • 1
wshutt
  • 61
  • 4
1

As the first answer said you needed to use a Scanner. All you had to do was pass the scanner variables into the book created object directly. Your logic was a slightly off on a couple things and I have had way too much time on my hands tonight so I just redid your problem my way.

import java.util.Scanner;

public class bookTest {

public static void main(String[] args) {


    Scanner input = new Scanner(System.in);


    System.out.println("Welcome to the basic library program! ");
    System.out.println("Insert author, name, genre, ISNB, page number ");
    String author = input.next();
    String name = input.next();
    String genre = input.next();
    int isbn = input.nextInt();
    int pageNumber = input.nextInt();


    Book booking = new Book(author,name ,genre,isbn,pageNumber);


    System.out.println("Change books status: 1 = Avaible 2 = Booked 3 = Lost ");
    int availability = input.nextInt();
    booking.changeAvailability(availability);


    System.out.println("Print book information? 1 = yes 2 = no?");
    int answer = input.nextInt();
    if (answer == 1) {
        booking.printBook();
    }


    System.out.println("Check book status? 1= yes 2 = no");
    int status = input.nextInt();
    if (status == 1) {
       booking.checkStatus(); 
    }




}



}

Next is the Book class redid

public class Book {

private String author;
private String name;
private String genre;
private int isbn;
private int pageNumber;
private int availability;


       public Book(String authorInput, String nameInput, String genreInput, int       isbnInput, int pageInput) {
       this.author = authorInput;
       this.name = nameInput;
       this.genre = genreInput;
       this.isbn = isbnInput;
       this.pageNumber = pageInput;

   }

public void changeAvailability(int setAvailability) {  
       if (setAvailability == 1) {
           availability = 1;
       }
       else if (setAvailability == 2) {
           availability = 2;
           }
       else if (setAvailability == 3) {
           availability = 3;
       }
       else {
              System.out.println("Unknown input: Program closing..");
              System.exit(1);
          }

   }

    //Instead of set use get
   public void checkStatus() {
       if (availability == 1) {
           System.out.println("Avaible.");
       }
       else if (availability == 2) {
           System.out.println("Not avaible.");         
           }
       else if (availability == 3) {
           System.out.println("Book missing.");
       }
       else {
              System.out.println("Unknown input: Program closing..");
              System.exit(1);
          }    
   }

   public void printBook() {

       System.out.println("Author: " + this.author + "\nName: " + this.name + "\nGenre: " + this.genre + "\nISBN: " + this.isbn + "\nPages: " + this.pageNumber);

       }
}
CJMobileApps
  • 177
  • 1
  • 12