如何在另一个类中访问ArrayList

时间:2015-01-09 01:23:25

标签: java arraylist import

我目前有这个代码,但它不会让我运行它,因为有一个错误,我知道它与我如何导入Library类有关,但我不知道解决这个问题的适当方法。我正在尝试导入ArrayList以在加载保存文件时使用。我希望从文本文件中更新此ArrayList。

这是我的代码:

package library;

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

import javax.swing.JOptionPane;

public class Manual {

    String serial;
    String title;
    String author;
    String publicationYear;
    String status;
    String borrower;
    String borrowDate;
    String returnDate;  

Scanner userInput = new Scanner(System.in); 

final String displayManual(){

    String ManualInfo ="\nSerial Number: ........... "+serial+ 
                    "\nTitle: ................... "+title+
                    "\nAuthor: .................. "+author+
                    "\nPublication Year: ........ "+publicationYear+
                    "\nStatus: .................. "+status+
                    "\nBorrower: ................ "+borrower+
                    "\nDate Borrowed: ........... "+borrowDate+
                    "\nReturn date: ............. "+returnDate+
                    "\n";
    return ManualInfo;  
}   

final void createManual(){

    serial = Console.readString(Messages.enterSerialMessage, Messages.tooShortMessage, 3);
    title = Console.readString(Messages.enterTitleMessage, Messages.tooShortMessage, 2);
    author = Console.readString(Messages.enterAuthorMessage, Messages.tooShortMessage, 3);
    publicationYear = Console.readString(Messages.enterPublicationYearMessage, Messages.tooShortMessage, 4);

    borrower = "N/A";
    borrowDate = "N/A";
    returnDate = "N/A";

    status = "Available";
}   
...

            if(Menu.menuChoice == 7){
                boolean loadYesNo = Console.readYesNo("\n\nThe manualKeeper app is able to load and display any 'Library.txt' files \nfound in your home folder directory.\n\nWould you like to load and display library? (Y/N):\n");
                String fileName = "Library.txt";
                String line;
                if(loadYesNo==true){
                try {
                    BufferedReader input = new BufferedReader (new FileReader (fileName));
                    if (!input.ready()) {
                        throw new IOException();
                    }
                    while ((line = input.readLine()) != null) {
                        Library.ManualList.add(line);
                    }
                    input.close();
                } catch (IOException e) {
                    System.out.println(e);
                }

                Menu.displayMenu();

                } else if(loadYesNo==false){
                    System.out.println("\n\n--------------------------------------------------------------------------");
                    System.out.println("\n                             Library not loaded!\n");
                    System.out.println("--------------------------------------------------------------------------\n");
                    Menu.displayMenu();
                }
            }

...

            if(Menu.menuChoice == 0){
                if(Menu.menuChoice == 0){
                    if(Library.ManualList.size() > 0){
                        boolean saveYesNo = Console.readYesNo("\nThe manualKeeper app is able to save your current library to a '.txt' \nfile in your home folder directory (C:\\Users\\ 'YOUR NAME').\n\nWould you like to save the current library? (Y/N):\n");
                        File fileName = new File ("Library.txt");
                        if(saveYesNo==true){
                            try {
                                FileWriter fw = new FileWriter(fileName);
                                Writer output = new BufferedWriter(fw);
                                for (int i = 0; i < Library.ManualList.size(); i++){
                                    output.write(Library.ManualList.get(i).displayManual() + "\n");
                                }
                                output.close();
                            } catch (Exception e) {
                                JOptionPane.showMessageDialog(null, "I cannot create that file!");
                                }
                        }
                            else if(saveYesNo==false){
                                System.out.println("\n\n--------------------------------------------------------------------------");
                                System.out.println("\n                              Library not saved!\n");
                                System.out.println("--------------------------------------------------------------------------\n");
                                break exit;
                        }
                        Menu.displayMenu();
                    }else if(Library.ManualList.isEmpty()){ 
                        Menu.displayMenu();
                    }
                }
            }               

        }
    System.out.println("\n              ~   You have exited the manualKeeper app!   ~                  ");
    System.out.println("\n                  Developed by Oscar Moore - 2014 - UWL\n");
    System.out.println("\n                                   <3\n");

}
}

如果需要,这是我的库类:

package library;

import java.util.ArrayList;

public class Library {  

public static int ManualChoice;

static String returnManualTitle;

static String status1 = "Available";

static String status2 = "Borrowed"; 

public static ArrayList<Manual> ManualList = new ArrayList<Manual>();
static ArrayList<Manual> borrowedManuals = new ArrayList<Manual>();

static void addManual(){
    Manual newManual = new Manual(); 
    newManual.createManual();
    ManualList.add(newManual);
    System.out.println("\n\n--------------------------------------------------------------------------");
    System.out.println("\n                          Manual added to library!\n");
    System.out.println("--------------------------------------------------------------------------\n");
}

static void displayManualList(){
    if (ManualList.isEmpty()){
        System.out.println("-------------------------------------------------------------");
        System.out.println(Messages.empltyLibraryMessage + Messages.tryAgainMessage);
        System.out.println("-------------------------------------------------------------");
        Menu.menuChoice = 8;

    } else {    
        System.out.printf("\n\nHere are the Manual/s currently stored in the library:\n\n\n");
        for (int i = 0; i < ManualList.size(); i++){
            System.out.printf("-------------------- Index Number: %s --------------------\n",i);
            System.out.println(ManualList.get(i).displayManual());  
            System.out.println("---------------------------------------------------------\n");
            }
        }
    }

static void displayBorrowedManuals(){
    if (ManualList.isEmpty()){
        System.out.println("-------------------------------------------------------------");
        System.out.println(Messages.empltyLibraryMessage + Messages.tryAgainMessage);
        System.out.println("-------------------------------------------------------------");
        Menu.menuChoice = 8;

    } else {                    
        for (int i = 0; i < borrowedManuals.size(); i++){
            System.out.printf("-------------------- Index Number: %s --------------------\n",i);
            System.out.println(borrowedManuals.get(i).displayManual()); 
            System.out.println("---------------------------------------------------------");
        }
    }
}

public static void borrowManual(){

    displayManualList();
    ManualChoice = (Console.readInteger(Messages.enterManualIndexMessage, Messages.ManualIndexNotInListMessage, 0, Library.ManualList.size() - 1));

    borrowLoop:
    while(Menu.menuChoice == 3){

        if ((ManualList.get(ManualChoice).status.equalsIgnoreCase(status1)) && (ManualList.size() >= ManualChoice)){
            ManualList.get(ManualChoice).status = "Borrowed";
            ManualList.get(ManualChoice).borrower = User.userName;
            ManualList.get(ManualChoice).borrowDate = "Today.";
            ManualList.get(ManualChoice).returnDate = "In two weeks.";
            borrowedManuals.add(ManualList.get(ManualChoice));
            System.out.println("\n--------------------------------------------------------------------------");
            System.out.println("\n                             Manual borrowed!\n");
            System.out.println("--------------------------------------------------------------------------\n");
            break borrowLoop;

        }else if(ManualList.get(ManualChoice).status.equalsIgnoreCase(status2) && ManualList.size() >= ManualChoice){
            System.out.println("\n--------------------------------------------------------------------------");
            System.out.println("\n            "
                    + " The Manual you wish to borrow is already on loan.");
            System.out.println("\n--------------------------------------------------------------------------\n");
            break borrowLoop;

        }else if(ManualChoice > ManualList.size()-1){
            System.out.println(Messages.noSuchManualMessage);
            break borrowLoop;
        }
    if(ManualList.size() > 1){
        displayManualList();
    }
    else if(ManualList.size() == 1){
        ManualList.get(ManualChoice).status = "Borrowed";
        ManualList.get(ManualChoice).borrower = User.userName;
        ManualList.get(ManualChoice).borrowDate = "Today.";
        ManualList.get(ManualChoice).returnDate = "In two weeks.";
        borrowedManuals.add(ManualList.get(ManualChoice));
        System.out.printf("\n\n %s\n\n", ManualList.get(ManualChoice).displayManual());
        System.out.println("Please return the Manual within two weeks!\n");
        displayManualList();
        }
    }
    Menu.displayMenu();
}

static void returnManual(){
    System.out.printf("\n\nHere are the Manual/s currently out on loan:\n\n");

    if(borrowedManuals.size() > 0){
    for (int i = 0; i < borrowedManuals.size(); i++)
        System.out.println(borrowedManuals.get(i).displayManual());
        returnManualTitle = Console.readString(Messages.enterManualSerial, Messages.tooShortMessage, 3);
    }

    int x = 0;
    boolean serialExistance = false;
    while (x < ManualList.size()){

        if (ManualList.get(x).serial.equalsIgnoreCase(returnManualTitle)){

            ManualList.get(x).status = "Available";
            ManualList.get(x).borrower = "N/A";
            ManualList.get(x).borrowDate = "N/A";
            ManualList.get(x).returnDate = "N/A";

            int p = 0;
                while (p < borrowedManuals.size()) {
                    Manual borrowed = borrowedManuals.get(p);
                    if (borrowed.serial.equalsIgnoreCase(returnManualTitle)) {
                        borrowedManuals.remove(p);
                        break;
                    }
                    p++;
                }               
            System.out.println(Messages.successReturnMessage);
            serialExistance = true;

            break;
        }
        x = x+1;
    }
    if(serialExistance == false){
        boolean repeatReturnManual = Console.readYesNo("\n--------------------------------------------------------------------------" + "\n\nThe Manual with the serial "+"\""+returnManualTitle +"\""+ " wasn't found!"
                                                        +"\n\nDo you want to try again? (Y/N):\n");
        System.out.println("\n--------------------------------------------------------------------------");
        if(repeatReturnManual){
            returnManual();
        }
    }else if(serialExistance){
        Menu.menuChoice = 8;
    }               
}

public static void removeManual(){

    if(ManualList.size() >0){
        displayManualList();
        ManualChoice = Console.readInteger(Messages.enterRemoveManualIndex ,Messages.ManualIndexNotInListMessage, 0, ManualList.size());        
        int p = 0;
        while (p < borrowedManuals.size()){

            if (borrowedManuals.get(p).title.equalsIgnoreCase(returnManualTitle)){

                borrowedManuals.remove(p);
            }
        }
        ManualList.remove(ManualChoice);
        System.out.print(Messages.successRemovedManualMessages);
        Menu.menuChoice = 8;
    }               
}

static void emptyLibrary(){
    System.out.println("\n                                 WARNING!");
    System.out.println("\n           You have chosen to delete all Manuals in the library.\n"); 
    System.out.println("--------------------------------------------------------------------------");
    boolean emptyLibraryChoice = Console.readYesNo("\nAre you sure you wish to destroy the library? (Y/N): \n");
    System.out.println("\n--------------------------------------------------------------------------\n");
    if(emptyLibraryChoice){
        Library.ManualList.clear();
        System.out.println(Messages.successEmptyLibraryMesssage);
        System.out.println("--------------------------------------------------------------------------\n");
        Menu.menuChoice = 8;
        }

}

}

错误发生在&#34; Library.ManualList ...&#34;线。我是Java的新手,所以任何人都可以帮我解决这个问题吗? :(

1 个答案:

答案 0 :(得分:0)

我知道这个代码段的方法签名是什么样的,或者Library类看起来是什么样的,但我相信问题可能在于ManualList中的Library的声明。 ManualList课程。 public应该是staticLibrary.ManualList.add(line);,这应该可以让您在另一个类中访问它

编辑: 我在这一行中找到:Menu您要将一个字符串添加到包含自定义类型的数组中。否则,我自己编译代码没有发现任何问题。这当然是假设类在同一个包中,我不得不注释掉我没有的代码(例如所有final引用)。但它为我编译。我没有运行它

Edit2:我想到的是,{{1}}修饰符可能会导致一些问题?但是当我测试它时它对我有用