ArrayList不存储对象

时间:2013-03-02 23:09:26

标签: java arraylist project

您好我在打开ArrayList中的项目时遇到问题。我可以在我的PatronBorrow方法中打印出来,但在PatronList和PatronReturn中它不会打印任何内容。谁能告诉我代码有什么问题?非常感谢你们

package proj1;

import java.util.ArrayList;
import java.util.List;


public class Patron {

private int id;
private Book book;
private List<Book> books;

public Patron(int id){
    this.id = id;
    books = new ArrayList<Book>();
}

public int getID(){
    return id;
}

public List<Book> getBooks(){
    return books;
}

public void PatronBorrow(String b){
    book = new Book(b);
    books.add(book);
    System.out.println("Patron " + id + " has borrowed " + book.getTitle());
}

public void PatronReturn(String b){
    for(Book book : books){
        if(book.getTitle().equals(b)){
            books.remove(book);
            System.out.println("Patron " + id + " has borrowed " +     book.getTitle());
        }
    }
}

public void PatronList(){
    for(Book b : books){
        System.out.println("Patron " + id + " has borrowed " + books.size() + " item(s)");
        System.out.println(b);
    }
}

}

package proj1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Project1 {

public static boolean isNumeric(String str){
    for(char c : str.toCharArray()){
        if(Character.isDigit(c)){
            return true;
        }
    }
    return false;
}

public static void main(String[] args){

    String command;
    String line;
    Patron patron;
    int patronID;
    String title;
    String newTitle;
    String infile = args[0];

    if (args.length != 1){
        throw new IllegalArgumentException("Enter in file name");
    }

    try{
        Scanner file = new Scanner(new FileInputStream(infile));
        while(file.hasNext()){
            command = file.next();
            if(isNumeric(command)){
                patronID = Integer.parseInt(command);
                patron = new Patron(patronID);
                command = file.next();
                if(command.equals("borrow")){
                    title = file.nextLine();
                    newTitle = title.substring(2, title.length() - 1);
                    patron.PatronBorrow(newTitle);
                }else if(command.equals("return")){
                    title = file.nextLine();
                    newTitle = title.substring(2, title.length() - 1);
                    patron.PatronReturn(newTitle);
                }else if(command.equals("list")){
                    patron.PatronList();
                }
            }else{

            }
        }

    }catch (FileNotFoundException e) {
        System.out.println("File not found" + e.getMessage());
        System.exit(0);
    }


}

}

1 个答案:

答案 0 :(得分:4)

在使用Patron类的循环中,您每次都会创建一个新的(空白)Patron

如果你想在顾客之间切换,你想要做的是在你的主要功能中有Map<Integer, Patron> patrons之类的东西。不是每次创建new Patron(patronID),而是从patrons检索它,只创建一个(并将其存储在地图中),如果那里还没有。{/ p>

(至于您的Patron课程,您可能会发现,如果您对该课程进行了一些真正的测试,PatronReturn在您删除图书时经常会抛出异常。ConcurrentModificationException ,也就是说,导致你从当时正在迭代的列表中删除。只是抬头。)