-1

I wish to be able to update information about objects entered by a user. I would like the user to enter a book name and user name to take out the book, as well as being able to return it with the same information. The objects have to be inserted into the right place in my sortedarraylists. Each user is assumed to be unique and each book can only be taken out by one user (who can take out up to 3 books).

When I try to compile my code, I get this error message:

java: method issueBook in class Driver cannot be applied to given types;
  required: Book,User
  found:    no arguments
  reason: actual and formal argument lists differ in length

This corresponds to issueBook(); in case i of my menu.

I also have User and Book classes that implement Comparable<Book> etc. I tried to omit as much irrelevant code as I could, such as the filereader to scan in new user and book objects. I included the book and user classes in case they need to be looked at (I think they are mostly fine). Please let me know if you need any more details however.

import java.io.*;
import java.util.Scanner;

public class Driver {

    static Scanner sc = new Scanner(System.in);

    private static void mainMenu() {
        System.out.println("------------------------------\n"+
                "f: Finish running the program\n" +
                "b -Display on the screen the information about all the books in the library\n" +
                "u -Display on the screen the information about all the users.\n" +
                "i -Update the stored data when a book is issued to a user\n" +
                "r -Update the stored data when a user returns a book to the library\n" +
                "------------------------------\n" +
                "Type a letter and press Enter\n");
    }

 private static User readNames() throws User.InvalidBookLimitException {
        System.out.println("Enter the user's firstname: ");
        String firstName = sc.next();
        System.out.println("Enter the user's surname: ");
        String surName = sc.next();
        sc.nextLine();                                    //TODO check this
        return new User(firstName, surName, 0);
    }

    private static User readUserData(User user) throws User.InvalidBookLimitException {
        User u = readNames();
        System.out.println("Enter " + user + "'s age, and press Enter");
        int numberOfBooks = sc.nextInt();
        sc.nextLine();
        return new User(u.getFirstName(), u.getSurName(),u.getNumberOfBooks());
    }

    private static Book readBookName (){
        System.out.println("Type in the name of the book");
        String bookName = sc.nextLine();
        return new Book(bookName);
    }

/* public SortedArrayList<User> sortedUsers = new SortedArrayList<>();    public SortedArrayList<Book> sortedBooks = new SortedArrayList<>();*/ 
//If this part is commented out, I get errors since I can't seem to access the arraylists in the
// main method for the issueBook/returnBook methods.

    public void issueBook(Book book, User user){
        for (Book b : sortedBooks){
            if(b.equals(book)){
                b.loanStatus(true);
                /*b.setLoanerNames(user);*/ b.setLoanerNames("John", "Doe");
                break;
            }
        }
        for (User u: sortedUsers){
            if(u.equals(user)){
                u.setNumberOfBooks(u.getNumberOfBooks()+1);

            }
        }
    }

    public void returnBook(Book book, User user){
        for (Book b : sortedBooks){
            if(b.equals(book)){
                 b.loanStatus(false);
                 b.setLoanerNames(null, null);
                break;

            } for (User u: sortedUsers){
                if(u.equals(user)){
                    u.setNumberOfBooks(u.getNumberOfBooks()-1);
                }
            }
        }
    }

    public static <E> void main(String[] args) throws FileNotFoundException, User.InvalidBookLimitException {
        //These SortedArrayLists have been derived from the sorted arraylist class
        SortedArrayList<User> sortedUsers = new SortedArrayList<>();
        SortedArrayList<Book> sortedBooks = new SortedArrayList<>();

mainMenu(); //main menu printing method
        char ch = sc.next().charAt(0);
        sc.nextLine();
        while (ch !='f') //the program ends as desired if f is pressed


        { switch(ch){

            case 'b':
                System.out.println("Displaying information about all books in the library: ");
                /*for (Object item : sortedBooks) {
                    System.out.println(sortedBooks.toString());
                }*/
                System.out.println(sortedBooks/*.toString()*/);

                break;
            case 'u':
                System.out.println("Displaying information about all users");
                System.out.println(sortedUsers/*.toString()*/);
                break;

            case 'i':
                System.out.println("Enter the loaning out data. ");
                System.out.println("Enter the user's first name and surname: ");
                readNames();
                System.out.println("Enter the name of the book: ");
                readBookName();
                issueBook();
              
                or maybe  if(u1.compareTo(u2) == 0)*/

                break;
            case 'r':
                System.out.println("Please the details of the book to be returned: ");
                /*Book b = new Book("test1", "test2", true, "lol","lol2");*/
//this was just a test and didn't work 
                /*returnBook(b);*/

                break;

            default:
                System.out.println("Invalid input, please enter f, b, i or r");

        }
        mainMenu();
        ch = sc.next().charAt(0);
            sc.nextLine();
        }
    }
}

My sorted arraylist class with a (working) insert method:

import java.util.ArrayList;

public class SortedArrayList<E extends Comparable<E>> extends ArrayList<E> {

    //no need for a generic in the insert method as this has been declared in the class
    public void insert(E value) {
        if (this.size() == 0) {
            this.add(value);
            return; }
        for (int i = 0; i < this.size(); i++) {
            int comparison = value.compareTo((E) this.get(i));
            if (comparison < 0) {
                this.add(i, value);
                return; }
            if (comparison == 0) {
                return; }
        }

        this.add(value);
    }

User class:

import java.io.PrintWriter;

public class User implements Comparable<User> {
    private String firstName;
    private String surName;
    private int numberOfBooks;

    public User(){
        firstName = "";
        surName = "";
        numberOfBooks = 0;
    }

    public class InvalidBookLimitException extends Exception{
        public InvalidBookLimitException(){
            super("Invalid number of books");
        }

    }

    public User(String name1, String name2, int books) throws InvalidBookLimitException {

        this.firstName = name1;
        this.surName = name2;
        if (books>3) throw new InvalidBookLimitException ();
        this.numberOfBooks = books;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getSurName(){
        return surName;
    }

    public int getNumberOfBooks(){
        return numberOfBooks;
    }

    public boolean borrowMoreBooks(){
        return numberOfBooks < 3;
    }

    public void setNumberOfBooks(int numberOfBooks){
        this.numberOfBooks=numberOfBooks;
    }

    public void setUser(String name1, String name2, int books){
        firstName = name1;
        surName = name2;
        numberOfBooks = books;

    }


    public boolean userNameTest (User otherUser){
        return (firstName.equals(otherUser.firstName) && surName.equals(otherUser.surName));
    }


  /*  public boolean loanAnotherBook(){
        return !(numberOfBooks<3);
        numberOfBooks++;
    }*/
    public void printName(PrintWriter f){f.println(firstName+ " " + surName);}


/*
    public void setUser(User user) {
        if (loanStatus == false){
            loanStatus = Driver.readNameInput();
            loanStatus = true;
        }
    }*/
    @Override
    public String toString(){
        return "Name: " + firstName + " " + surName + " | Number of books: " + numberOfBooks;
    }



    public int compareTo(User u) {
       /* int snCmp = surName.compareTo(u.surName);
        if (snCmp != 0)
            return snCmp;
        else{
            int fnCmp = firstName.compareTo(u.firstName);
            if (fnCmp !=0)
                return fnCmp;
        }*/

        int fnCmp = firstName.compareTo(u.firstName);
        if (fnCmp != 0) return fnCmp;
        int snCmp= surName.compareTo(u.surName);
        if (snCmp !=0) return snCmp;
        else return numberOfBooks -u.numberOfBooks;



    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }
}

Book class:

public class Book implements Comparable<Book>{
    public String bookName;
    public String authorName;
    public boolean loanStatus;
    public String loanerFirstName;
    public String loanerSurName;
    //if boolean loanStatus == true private string loaner name

    public Book(){
        bookName = "";
        authorName = "";
        loanStatus = false;
        loanerFirstName = null;
        loanerSurName = null;
    }

    Book(String book, String author, boolean loanStatus, String loanerFirstName, String loanerSurName) {

        this.bookName = book;
        this.authorName = author;
        this.loanStatus = loanStatus;
        this.loanerFirstName = loanerFirstName;
        this.loanerSurName = loanerSurName;
    }
    Book(String book){
        this.bookName=book;
    }



    public String getBookName(){
        return bookName;
    }
    public String getAuthorName(){
        return bookName;
    }
    public boolean getLoanStatus(){
        return loanStatus;
    }

    public String getLoanerFirstName(){
        return loanerFirstName;
    }
    public String getLoanerSurName(){
        return loanerSurName;
    }


    public void setBook(String book, String author, boolean loanStatus, String loanerFirstName, String loanerSurName){
        bookName = book;
        authorName = author;
        loanStatus = loanStatus;
        loanerFirstName = loanerFirstName;
        loanerSurName = loanerSurName;


    }

    public void setBookName(String bookName){
        this.bookName=bookName;
    }

    public void loanStatus(boolean loanStatus){
        this.loanStatus=loanStatus;
    }
    public void setLoanerNames(String loanerFirstName, String loanerSurName){
        this.loanerFirstName=loanerFirstName;
        this.loanerSurName=loanerSurName;
    }

 /*   public boolean nameTest (User otherUser){
        return (loanerFirstName.equals(otherUser.loanerFirstName)&& loanerSurName.equals(otherUser.loanerSurName));
    }
*/

    public String toString(){

        return "Book: " + bookName + " | Author: " + authorName + " | Loan status: " + loanStatus + " | Loaned to: " + loanerFirstName + " " + loanerSurName ;
    }


    //this may be redundant TODO
  /*  public void setLoanStatus(User user){
        loanStatus = false;

    }*/

    //This compare method allows new user objects to be compared to ones in the
 @Override       //sortedArrayList, enabling the insertion method.
    public int compareTo(Book b) {
        int bnCmp = bookName.compareTo(b.bookName);
        if (bnCmp != 0) return bnCmp;
        int anCmp= authorName.compareTo(b.authorName);
        if (anCmp !=0) return anCmp;
//        int lsCmp= loanStatus.(b.loanStatus);
//        if (lsCmp !=0) return lsCmp;
        int lrfnCmp =loanerFirstName.compareTo(b.loanerFirstName);
        if (lrfnCmp !=0) return lrfnCmp;
        int lrlnCmp =loanerSurName.compareTo(b.loanerSurName);
        if (lrlnCmp !=0) return lrlnCmp;
        else return 0;

    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • In the Driver class switch statement, for the case i, you are calling the method `issueBook()` without passing any argument. But the `issueBook` is expecting Book and User as arguments as per its definition `issueBook(Book book, User user)`. Please pass the appropriate parameters. Hope this helps! – Sruthi Dec 13 '20 at 16:00
  • Hi, thank you but priot to calling those methods, I have created book and user objects so I don't understand why that still happens – Steven Schmidt Dec 13 '20 at 16:07
  • You have created the object but not passed those in the issueBook method. – Sruthi Dec 13 '20 at 16:10

1 Answers1

0
User user = readNames();
Book book = readBookName();
issueBook(book, user);

You have to pass the user and book which you have created while calling the issueBook method.

Sruthi
  • 119
  • 8
  • Hi, thank you. I tried that but got 186:17 java: non-static method issueBook(Book,User) cannot be referenced from a static context – Steven Schmidt Dec 13 '20 at 16:18
  • This might help you: https://stackoverflow.com/questions/290884/what-is-the-reason-behind-non-static-method-cannot-be-referenced-from-a-static. You can make your issueBook static similar to other methods `readNames()` and `readBookName()` – Sruthi Dec 13 '20 at 16:29
  • Hi, thank you. I tried making the issueBook method public void static but then I get this error correcsponding to the arraylists sortedBooks and sortedUsers: Non-static field 'sortedBooks' cannot be referenced from a static context. It seems to create a cascade of problems. – Steven Schmidt Dec 13 '20 at 16:58
  • Either you can make the fields and methods static so that you can access them directly, or you can create an object of class and access with the object. This might help you to understand the concepts https://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context – Sruthi Dec 13 '20 at 20:11