3

こんにちは、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);
    }


}

}

4

1 に答える 1

4

クラスを使用するループでは、毎回Patron新しい (空白) を作成しています。Patron

パトロンを切り替えたい場合はMap<Integer, Patron> patrons、メイン関数に a などを含めることをお勧めします。new Patron(patronID)を毎回作成するのではなく、から取得しpatrons、そこにまだ作成されていない場合にのみ作成 (およびマップに保存) します。

(ただし、クラスに関しては、本を削除するときに例外をスローすることが多い Patronそのクラスの実際のテストを行うとわかる場合があります。時間です。注意してください。)PatronReturnConcurrentModificationException

于 2013-03-02T23:16:54.813 に答える