简体   繁体   English

ArrayList不存储对象

[英]ArrayList not storing objects

Hello I am having trouble printing out items in my ArrayList. 您好我在我的ArrayList中打印出项目时遇到问题。 I can print it out in my PatronBorrow method but in PatronList and PatronReturn it does not print anything. 我可以在我的PatronBorrow方法中打印出来,但在PatronList和PatronReturn中它不会打印任何内容。 Can anyone tell me what is wrong with the code? 谁能告诉我代码有什么问题? Thank you all very much 非常感谢你们

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);
    }


}

} }

In the loop that uses the Patron class, you're creating a new (blank) Patron each time. 在使用Patron类的循环中,您每次都会创建一个新的(空白) Patron

What you'll want to do, if you want to switch between patrons, is have a Map<Integer, Patron> patrons or the like in your main function. 如果你想在顾客之间切换,你想要做的是在你的主要功能中有一个Map<Integer, Patron> patrons等。 Instead of creating a new Patron(patronID) each time, retrieve it from patrons , and only create one (and store it in the map) if there's not already one in there. 而不是每次都创建一个new Patron(patronID) ,从patrons检索它,只创建一个(并将其存储在地图中),如果那里还没有。

(As far as your Patron class, though, you might find if you do some real testing of that class that PatronReturn often throws an exception when you remove a book. ConcurrentModificationException , namely, cause you're removing from a list you're iterating over at the time. Just a heads up.) (至于你的Patron类,不过,你可能会如果你这样做类的一些实际测试发现PatronReturn当您删除一本书,往往会抛出异常。 ConcurrentModificationException ,即,因为你是从列表中删除你迭代在那个时候。只是抬头。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM