2015-12-13 87 views
0

我是Java的初學者,所以對於混亂的ups非常抱歉。來自類的調用方法錯誤

我創建一個圖書館程序,並有4個類:圖書館,書籍,BookInterface和贊助人。

我在程序的最後幾個階段,但是我在庫類中有錯誤消息,因爲我遇到了實例問題。

我在我的所有switch語句中都收到錯誤消息「無法對非靜態方法進行靜態引用」。我試圖在情況1中創建一個Library類的實例,但仍然是一條錯誤消息。此外,我不能將這些方法更改爲靜態。任何幫助深表感謝! :)

圖書館類:

import java.awt.List; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Scanner; 
import java.util.Collections; 
public class Library 
{ 

public static void main(String[] args) 
{ 
    //create new library 
    Library newLibrary = new Library();  
} 

public Library() 
{ 
    Library library = new Library(); 
    Scanner input = new Scanner(System.in); 
    int choice = 0; 
    System.out.println("********************Welcome to the Public Library!********************"); 
    System.out.println("    Please Select From The Following Options:    "); 
    System.out.println("**********************************************************************"); 

    while(choice != 9) 
    { 
     System.out.println("1: Add new patron"); 
     System.out.println("2: Add new book"); 
     System.out.println("3: Edit patron"); 
     System.out.println("4: Edit book"); 
     System.out.println("5: Display all patrons"); 
     System.out.println("6: Display all books"); 
     System.out.println("7: Check out book"); 
     System.out.println("8: Check in book"); 
     System.out.println("9: Search book"); 
     System.out.println("10: Search Patron"); 
     System.out.println("11: Exit"); 
     choice = input.nextInt(); 


    switch(choice) 
    { 
    case 1: //Add new patron 
      //ERROR: Patron cannot be resolved or is not a field 
      library.Patron.newPatron(); 
      break; 

    case 2: //Add new book 
      //ERROR: Cannot make a static reference to the non-static method 
      Book.addBook(); 
      break; 

    case 3: //Edit patron name 
      Patron.editName(); 
      break; 

    case 4: //edit book 
      //ERROR: Cannot make a static reference to the non-static method 
      Book.editBook(); 
      break; 

    case 5: //display all patrons 
      System.out.println(Book.UserList); 
      break; 

    case 6: //display all books 
      //System.out.println(Book.OrigBookList); 
      Book.libraryInventory(); 
      //Book.bookStatus() 
      break; 

    case 7: //check out a book 
      //ERROR: Cannot make a static reference to the non-static method 
      Patron.CheckOutBook(); 
      break; 

    case 8: //check in a book 
      //ERROR: Cannot make a static reference to the non-static method 
      Patron.CheckInBook(); 
      break; 

    case 9: //search book 
      //ERROR: Cannot make a static reference to the non-static method 
      Patron.bookStatus(); 
      break; 

    case 10://search Patron 
      Patron.searchPatron(); 
      break; 
    case 11: //exit program 


     } 
    } 
} 

}

Book類:

import java.util.ArrayList; 
import java.util.Collections; 
import java.util.HashMap; 
import java.util.Map; 
import java.util.Scanner; 

public class Book implements BookInterface 
{ 
Scanner input = new Scanner(System.in); 

static ArrayList <String> UserList = new ArrayList<String>(); 
static ArrayList <String> BookList = new ArrayList <String>(); //display just titles// use when checking out books 
static ArrayList <String> OrigBookList = new ArrayList <String>(); //keep track of all titles ever entered 


public String title; 
public String author; 
public String book; 
public boolean checkIn; 

private String status; 
private String borrower; 

public Book(String t, String a) 
{ 
    title = t; 
    author = a; 
} 

//constructor create new book 
public Book(String newTitle) 
{ 
    title = newTitle; 
} 


public String toString() 
{ 
    return title + " " + author; 
} 

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 void addBook() 
{ 
    Scanner input = new Scanner(System.in); 
    Scanner inputread = new Scanner(System.in); 
    System.out.print("Enter book title: "); 

    String title1 = inputread.nextLine(); 

    Scanner input1 = new Scanner(System.in); 
    System.out.print("Enter book author: "); 
    String author1 = inputread.next(); 

    Book fullBook = new Book(title1, author1); //create constructor w/ title & author 
    Book book1 = new Book(title1);    //constructor w/ just title to be used to display all books 
    //BookList.add(title1); 
    OrigBookList.add(title1); 
    setStatus("IN"); //false = checked in 
    System.out.println("-----------------------------------------------"); 
    System.out.println("-----" + title1 + " is now in the library!-----"); 
    System.out.println("-----------------------------------------------"); 
} 

public void editBook() 
{ 
    Scanner inputread = new Scanner(System.in); 
    System.out.println("Enter original book title: "); 
    String origTitle = inputread.nextLine(); 
    System.out.println("Enter edited book title: "); 
    String editedTitle = inputread.nextLine(); 
    Collections.replaceAll(Book.UserList, origTitle, editedTitle); 
    System.out.println("------------------------------------------------------"); 
    System.out.println(origTitle + " has been changed to " + editedTitle + "!"); 
    System.out.println("------------------------------------------------------"); 

} 

public String getStatus(String book) 
{ 
    return status; 
} 

public void setStatus(String status)  
{ 
    this.status = status; 
} 

public void setBorrower(String borrower) 
{ 
    this.borrower = borrower; 
} 

public String getBorrower(String checkPatron) 
{ 
    return borrower; 
} 

public String getBook(String checkPatron) 
{ 
    return book; 
} 

public void setBook(String bookCheckOut) 
{ 
    this.book = bookCheckOut; 
} 

public void libraryInventory() 
{ 
    System.out.println("------------------ Library Inventory: ---------------"); 
    for(int i =0; i<= OrigBookList.size()-1; i++) 
    { 
     //Book Title: checked in/out 
     System.out.println(OrigBookList.get(i) + ":" + getStatus(OrigBookList.get(i)));   
    } 
    System.out.println("-----------------------------------------------------"); 
} 

}

贊助類:

import java.text.DateFormat; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Date; 
import java.util.GregorianCalendar; 
import java.util.Scanner; 

public class Patron 
{ 

Scanner input = new Scanner(System.in); 
private String fullName; 
int bookCount = 0; //amount books user has in pocket 
int books = 0; 


//constructor to "add new patron" by entering their name. 
public Patron(String newName) 
{ 
    fullName = newName; 
} 

public String toString() 
{ 
    return fullName; 
} 

public String getName() 
{ 
    return fullName; 
} 

public void CheckOutBook() 
{ 
    //create and format due date (7days) 
    GregorianCalendar returnDate = new GregorianCalendar(); 
    returnDate.add(GregorianCalendar.DATE, 7); 
    Date d = returnDate.getTime(); 
    DateFormat df = DateFormat.getDateInstance(); 
    String dueDate = df.format(d); 

    Scanner inputread = new Scanner(System.in); 
    Scanner input = new Scanner(System.in); 
    System.out.println("Enter full patron name: "); 
    String borrower = inputread.nextLine(); 
    System.out.println("Enter book title to check out: "); 
    String bookCheckOut = inputread.nextLine(); 
    Book checkOut = new Book(bookCheckOut); 

    if(Book.BookList.contains(bookCheckOut)) 
    { 
     Book.BookList.remove(bookCheckOut); 
     checkOut.setStatus("OUT");  
     checkOut.setBorrower(borrower); 
     Book.BookList.remove(bookCheckOut); 

     System.out.println("----------" + bookCheckOut + " has been checked out!----------"); 
     System.out.println("-------------------BOOK STATUS:---------------------"); 
     System.out.println("Book Title: " + bookCheckOut); 
     System.out.println("Book Status: Checked out"); 
     System.out.println("Borrower: " + borrower); 
     System.out.println("Due Date: " + dueDate); 
     System.out.println("----------------------------------------------------"); 
    } 

    else if (!(Book.UserList.contains(borrower))) 
    { 
     System.out.println("Patron is not a part of library system. Please enter patron in system."); 
    } 
    else if (!(Book.BookList.contains(bookCheckOut))) 
    { 
     System.out.println(bookCheckOut + " is not in the library. Please enter " 
     + "a different book to be checked out"); 
    } 


} 

public void CheckInBook() 
{ 
    Scanner inputread = new Scanner(System.in); 
    System.out.println("Enter book title to be checked in: "); 
    String bookCheckIn = inputread.nextLine(); 
    Book checkOut = new Book(bookCheckIn); 

    if((!Book.BookList.contains(bookCheckIn)) && Book.OrigBookList.contains(bookCheckIn)) 
    { 
     Book.BookList.add(bookCheckIn); 
     checkOut.setStatus("IN"); 
     System.out.println("------------------------------------------------------"); 
     System.out.println("-----" + bookCheckIn + " has been checked in!-----"); 
     System.out.println("------------------------------------------------------"); 

    } 
    else 
     System.out.println(bookCheckIn + " is not part of the library system. Please enter " 
     + "a different book to be checked in"); 
} 

public boolean canBorrow() 
{ 
    if(bookCount <= 3) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

public static void editName() 
{ 
    Scanner inputread = new Scanner(System.in); 
    System.out.println("Enter original patron full name: "); 
    String origName = inputread.nextLine(); 
    System.out.println("Enter edited patron full name: "); 
    String editedName = inputread.nextLine(); 
    Collections.replaceAll(Book.UserList, origName, editedName); 
    System.out.println("------------------------------------------------------"); 
    System.out.println(origName + " has been changed to " + editedName + "!"); 
    System.out.println("------------------------------------------------------");  
} 

public void newPatron() 
{ 
    Scanner inputread = new Scanner(System.in); 
    System.out.println("Enter patron full name: "); 
    String name = inputread.nextLine(); 
    Book.UserList.add(name);  //add name to userList 
    Patron patron1 = new Patron(name); 


    System.out.println("-----------------------------------------------------------------"); 
    System.out.println("-----" + name + " has been added to the library system" + "-----"); 
    System.out.println("-----------------------------------------------------------------"); 


} 

public void bookStatus()  //STOPS 
{ 
    Scanner input = new Scanner(System.in); 
    Scanner inputread = new Scanner(System.in); 
    System.out.println("Enter book title: "); 
    String checkStatus = inputread.nextLine(); 
    Book status = new Book(checkStatus); 

    if(Book.OrigBookList.contains(checkStatus) && status.getStatus(checkStatus) == "OUT") 
    { 
     System.out.println("Status: checked out"); 
     System.out.println("Borrower: " + status.getBorrower(checkStatus)); 
    } 
    else if(Book.OrigBookList.contains(checkStatus) && status.getStatus(checkStatus) == "IN")  
    { 
     System.out.println("Status: checked in"); 
     System.out.println("Borrower: none"); 
    } 
    else if(!(Book.OrigBookList.contains(checkStatus))) 
     System.out.print("Book is not in library system. Please add the book first."); 

} 

public void searchPatron()  //WORKS!!! 
{ 
    Scanner inputread = new Scanner(System.in); 
    System.out.println("Enter patron full name: "); 
    String checkPatron = inputread.nextLine(); 
    Book search = new Book(checkPatron); 

    if(Book.UserList.contains(checkPatron)) 
    { 
     System.out.println("-------------------------------------------------"); 
     System.out.println("Books checked out: " + search.getBook(checkPatron)); 
     System.out.println("-------------------------------------------------"); 

    } 
    else 
     System.out.println("Patron is not part of the library system. Please create new patron first."); 
} 

}

+1

除此之外,你必須在構造函數中你的'Library'類遞歸調用。每當你創建一個你創建另一個等等。通常不應該將所有的類邏輯放在構造函數中。 – ChiefTwoPencils

+1

你的班級'書'和'贊助人'在哪裏?明顯的解決方法只是將靜態關鍵字添加到方法中,但Java是面向對象的,所以這是不好的做法。 while循環應該是一個while,顯然'9'不應該退出。由你的'switch''11'判斷應該退出。 – WalterM

+0

你可以放入'Book'和'Patron'的代碼嗎? –

回答

1

由於您正在調用Patron和Book的非靜態方法,因此您必須讓它們的對象調用這些方法。只能在Class上直接調用靜態方法。檢查下面的鏈接爲靜態成員

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html細節
https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

的事情要記住夫婦

  1. 構造函數應該用於創建對象只有你的庫()構造做太多工作,嘗試把它放在構造函數之外。
  2. 您的情況11沒有退出循環,我在那裏添加了System.exit(0)。
  3. 使用input.close()關閉掃描儀對象
  4. 在代碼中嘗試一些異常處理以確保安全。
  5. 在Java中,慣例是在命名非靜態字段和方法時保留第一個字母很小。檢查此鏈接https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

下面是更正後的代碼

import java.awt.List; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Scanner; 
import java.util.Collections; 

public class Library { 

    public static void main(String[] args) { 
     // create new library 
     Library newLibrary = new Library(); 
    } 

    public Library() { 
     Scanner input = new Scanner(System.in); 
     int choice = 0; 
     System.out 
       .println("********************Welcome to the Public Library!********************"); 
     System.out 
       .println("    Please Select From The Following Options:    "); 
     System.out 
       .println("**********************************************************************"); 

     while (choice != 9) { 
      System.out.println("1: Add new patron"); 
      System.out.println("2: Add new book"); 
      System.out.println("3: Edit patron"); 
      System.out.println("4: Edit book"); 
      System.out.println("5: Display all patrons"); 
      System.out.println("6: Display all books"); 
      System.out.println("7: Check out book"); 
      System.out.println("8: Check in book"); 
      System.out.println("9: Search book"); 
      System.out.println("10: Search Patron"); 
      System.out.println("11: Exit"); 
      choice = input.nextInt(); 

      //creating Patron and Book objects 
      Patron patron = new Patron(); 
      Book book = new Book(); 
      switch (choice) { 
      case 1: // Add new patron 
        // ERROR: Patron cannot be resolved or is not a field 
       patron.newPatron(); 
       break; 

      case 2: // Add new book 
        // ERROR: Cannot make a static reference to the non-static 
        // method 
       book.addBook(); 
       break; 

      case 3: // Edit patron name 
       patron.editName(); 
       break; 

      case 4: // edit book 
        // ERROR: Cannot make a static reference to the non-static 
        // method 
       book.editBook(); 
       break; 

      case 5: // display all patrons 
       System.out.println(Book.UserList); 
       break; 

      case 6: // display all books 
        // System.out.println(Book.OrigBookList); 
       Book.libraryInventory(); 
       // Book.bookStatus() 
       break; 

      case 7: // check out a book 
        // ERROR: Cannot make a static reference to the non-static 
        // method 
       patron.CheckOutBook(); 
       break; 

      case 8: // check in a book 
        // ERROR: Cannot make a static reference to the non-static 
        // method 
       patron.CheckInBook(); 
       break; 

      case 9: // search book 
        // ERROR: Cannot make a static reference to the non-static 
        // method 
       patron.bookStatus(); 
       break; 

      case 10:// search Patron 
       patron.searchPatron(); 
       break; 
      case 11: // exit program 
       //added exit(0) 
       System.exit(0); 
      } 
     } 
    } 
}