如何从不同类的 ArrayList 中删除对象?
我这里有两节课要布置作业。我试图从图书馆目录中删除 Item 对象并将其添加到当前读者的 checkOutItems 中。当我运行代码时,它说我没有从目录中删除该对象。这里有什么我失踪的吗?我主要关心 CheckIn 和 CheckOut 方法。
下面的 Library.java
import java.io.File;
import java.io.FileNotFoundException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Scanner;
public class Library {
private boolean isOpen;
private Patron currPatron;
public ArrayList<Patron> patrons;
public ArrayList<Item> catalog;
private LocalDate currDate;
public ArrayList<Item> lastSearchResults = new ArrayList<>();
public Library(ArrayList<Item> catalog, Scanner input, int year, int month, int day) {
this.currDate = LocalDate.of(year, month, day);
this.catalog = catalog;
this.patrons = new ArrayList<Patron>();
}
public ArrayList<Item> getCatalog() {
return catalog;
}
public LocalDate getCurrDate() {
return currDate;
}
public void start() {
Scanner s = new Scanner(System.in);
System.out.println("Enter o to open the library or x to exit the program.");
String input = s.next();
if (input.contains("x")) {
return;
}
if (!input.contains("o")) {
System.out.println("Invalid input");
}
System.out.println("Today's date: " + currDate + "\n");
System.out.println("Overdue items: \n");
System.out.println("Choose an existing patron by number, enter n to add a new patron, or enter c to close.");
input = s.next();
s.nextLine();
switch (input) {
case "c":
break;
case "n":
System.out.println("Enter patron name.");
String name = s.nextLine();
System.out.println("Enter patron card number.");
int cn = s.nextInt();
Patron p = new Patron(name, cn);
break;
}
}
public void open() {
this.isOpen = true;
}
public void close() {
this.isOpen = false;
this.currDate = currDate.plusDays(1);
}
public boolean isOpen() {
return isOpen;
}
public ArrayList<Patron> getPatrons() {
return patrons;
}
public void addPatron(String n, int cn) {
Patron p = new Patron(n, cn);
currPatron = p;
patrons.add(p);
}
public void setPatron(int p) {
for (Patron patron : patrons) {
if (patron.getCardNumber() == p) {
this.currPatron = patron;
}
}
}
public Patron getCurrPatron() {
return currPatron;
}
public void search(String search) {
lastSearchResults.clear();
for (Item currItem : catalog) {
if (currItem.detailedInfo().contains(search)) {
lastSearchResults.add(currItem);
}
}
}
public ArrayList<Item> getSearchResults() {
return lastSearchResults;
}
public void checkIn(int index) {
ArrayList<Item> ls = currPatron.getItems();
Item a = ls.get(index);
currPatron.checkInItem(a);
catalog.add(index, a);
}
public void checkOut(int index) {
Item i = catalog.get(index);
catalog.remove(index);
currPatron.checkOutItem(i);
}
/////////////////// DO NOT CHANGE THE CODE BELOW /////////////////////
public static ArrayList<Item> readCatalog(String fileName) {
Scanner input = null;
try {
input = new Scanner(new File(fileName));
} catch (FileNotFoundException e) {
System.out.println("File was not found.");
System.exit(-1);
}
String line = "";
ArrayList<Item> catalog = new ArrayList<Item>();
boolean stop = false;
while (!stop) {
line = input.nextLine();
switch (line) {
case "BOOK":
String title = input.nextLine();
String author = input.nextLine();
int pages = input.nextInt();
int year = input.nextInt();
catalog.add(new Book(title, author, pages, year));
input.nextLine();
break;
case "MUSIC":
title = input.nextLine();
String artist = input.nextLine();
String format = input.nextLine();
year = input.nextInt();
input.nextLine();
ArrayList<String> tracks = new ArrayList<String>();
while (true) {
line = input.nextLine();
if (line.equals("")) {
break;
}
tracks.add(line);
}
catalog.add(new Music(title, artist, format, year, tracks));
break;
case "VIDEO":
title = input.nextLine();
format = input.nextLine();
year = input.nextInt();
int runtime = input.nextInt();
catalog.add(new Video(title, format, year, runtime));
input.nextLine();
break;
case "END":
stop = true;
break;
}
}
return catalog;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter catalog file name.");
String fileName = input.nextLine();
int year;
int month;
int day;
System.out.println("Enter the current year.");
year = input.nextInt();
System.out.println("Enter the current month.");
month = input.nextInt();
System.out.println("Enter the current day.");
day = input.nextInt();
input.nextLine();
ArrayList<Item> catalog = Library.readCatalog(fileName);
Library l = new Library(catalog, input, year, month, day);
l.start();
}
}
Patron.java 文件
import java.util.ArrayList;
public class Patron implements Printable {
private String name;
private int cardNumber;
ArrayList<Item> checkedItems = new ArrayList<Item>();
public Patron(String n, int cn) {
this.name = n;
this.cardNumber = cn;
}
public String basicInfo() {
return this.name + " (" + this.cardNumber + ")";
}
public String detailedInfo() {
StringBuilder items = new StringBuilder();
for (Item item : checkedItems) {
items.append(" ").append(item.basicInfo()).append(" due on ").append(item.getDueDate()).append("\n");
}
String r = "Name: " + this.name + "\n"
+ "Card number: " + this.cardNumber + "\n"
+ items.toString();
return r.trim();
}
public ArrayList<Item> getItems() {
return this.checkedItems;
}
public void checkOutItem(Item i) {
checkedItems.add(i);
}
public void checkInItem(Item i) {
checkedItems.remove(i);
}
public int getCardNumber() {
return this.cardNumber;
}
}
I have two classes here for an assignment. I am attempting to remove the Item object from the library catalog and add it to the checkedOutItems of the current patron. When I run my code it is saying that I didn't remove the object from the catalog. Anything here that i'm missing? Im mainly concerned with the CheckIn and CheckOut methods.
Library.java below
import java.io.File;
import java.io.FileNotFoundException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Scanner;
public class Library {
private boolean isOpen;
private Patron currPatron;
public ArrayList<Patron> patrons;
public ArrayList<Item> catalog;
private LocalDate currDate;
public ArrayList<Item> lastSearchResults = new ArrayList<>();
public Library(ArrayList<Item> catalog, Scanner input, int year, int month, int day) {
this.currDate = LocalDate.of(year, month, day);
this.catalog = catalog;
this.patrons = new ArrayList<Patron>();
}
public ArrayList<Item> getCatalog() {
return catalog;
}
public LocalDate getCurrDate() {
return currDate;
}
public void start() {
Scanner s = new Scanner(System.in);
System.out.println("Enter o to open the library or x to exit the program.");
String input = s.next();
if (input.contains("x")) {
return;
}
if (!input.contains("o")) {
System.out.println("Invalid input");
}
System.out.println("Today's date: " + currDate + "\n");
System.out.println("Overdue items: \n");
System.out.println("Choose an existing patron by number, enter n to add a new patron, or enter c to close.");
input = s.next();
s.nextLine();
switch (input) {
case "c":
break;
case "n":
System.out.println("Enter patron name.");
String name = s.nextLine();
System.out.println("Enter patron card number.");
int cn = s.nextInt();
Patron p = new Patron(name, cn);
break;
}
}
public void open() {
this.isOpen = true;
}
public void close() {
this.isOpen = false;
this.currDate = currDate.plusDays(1);
}
public boolean isOpen() {
return isOpen;
}
public ArrayList<Patron> getPatrons() {
return patrons;
}
public void addPatron(String n, int cn) {
Patron p = new Patron(n, cn);
currPatron = p;
patrons.add(p);
}
public void setPatron(int p) {
for (Patron patron : patrons) {
if (patron.getCardNumber() == p) {
this.currPatron = patron;
}
}
}
public Patron getCurrPatron() {
return currPatron;
}
public void search(String search) {
lastSearchResults.clear();
for (Item currItem : catalog) {
if (currItem.detailedInfo().contains(search)) {
lastSearchResults.add(currItem);
}
}
}
public ArrayList<Item> getSearchResults() {
return lastSearchResults;
}
public void checkIn(int index) {
ArrayList<Item> ls = currPatron.getItems();
Item a = ls.get(index);
currPatron.checkInItem(a);
catalog.add(index, a);
}
public void checkOut(int index) {
Item i = catalog.get(index);
catalog.remove(index);
currPatron.checkOutItem(i);
}
/////////////////// DO NOT CHANGE THE CODE BELOW /////////////////////
public static ArrayList<Item> readCatalog(String fileName) {
Scanner input = null;
try {
input = new Scanner(new File(fileName));
} catch (FileNotFoundException e) {
System.out.println("File was not found.");
System.exit(-1);
}
String line = "";
ArrayList<Item> catalog = new ArrayList<Item>();
boolean stop = false;
while (!stop) {
line = input.nextLine();
switch (line) {
case "BOOK":
String title = input.nextLine();
String author = input.nextLine();
int pages = input.nextInt();
int year = input.nextInt();
catalog.add(new Book(title, author, pages, year));
input.nextLine();
break;
case "MUSIC":
title = input.nextLine();
String artist = input.nextLine();
String format = input.nextLine();
year = input.nextInt();
input.nextLine();
ArrayList<String> tracks = new ArrayList<String>();
while (true) {
line = input.nextLine();
if (line.equals("")) {
break;
}
tracks.add(line);
}
catalog.add(new Music(title, artist, format, year, tracks));
break;
case "VIDEO":
title = input.nextLine();
format = input.nextLine();
year = input.nextInt();
int runtime = input.nextInt();
catalog.add(new Video(title, format, year, runtime));
input.nextLine();
break;
case "END":
stop = true;
break;
}
}
return catalog;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter catalog file name.");
String fileName = input.nextLine();
int year;
int month;
int day;
System.out.println("Enter the current year.");
year = input.nextInt();
System.out.println("Enter the current month.");
month = input.nextInt();
System.out.println("Enter the current day.");
day = input.nextInt();
input.nextLine();
ArrayList<Item> catalog = Library.readCatalog(fileName);
Library l = new Library(catalog, input, year, month, day);
l.start();
}
}
Patron.java file
import java.util.ArrayList;
public class Patron implements Printable {
private String name;
private int cardNumber;
ArrayList<Item> checkedItems = new ArrayList<Item>();
public Patron(String n, int cn) {
this.name = n;
this.cardNumber = cn;
}
public String basicInfo() {
return this.name + " (" + this.cardNumber + ")";
}
public String detailedInfo() {
StringBuilder items = new StringBuilder();
for (Item item : checkedItems) {
items.append(" ").append(item.basicInfo()).append(" due on ").append(item.getDueDate()).append("\n");
}
String r = "Name: " + this.name + "\n"
+ "Card number: " + this.cardNumber + "\n"
+ items.toString();
return r.trim();
}
public ArrayList<Item> getItems() {
return this.checkedItems;
}
public void checkOutItem(Item i) {
checkedItems.add(i);
}
public void checkInItem(Item i) {
checkedItems.remove(i);
}
public int getCardNumber() {
return this.cardNumber;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论