I am having trouble understanding and getting this function of my program to work. I am creating a library where I first input multiple objects into an array-list. The objects consist of title and format of the media. I need to be able to search through the objects in my array-list to find a specific object and mark that object as "checked out". I have been researching iterators and trying to understand how to make them find a specified title within an object but I am having trouble. I am getting MediaItem@3c4568f8 when I attempt to println(it.next()); so I know there is a problem with returning the correct format of information. Any help on how to search through the objects in my items array-list would be greatly appreciated.
import java.util.Iterator;
import java.util.Scanner;
import java.util.ArrayList;
public class Library {
static ArrayList<MediaItem> items = new ArrayList<MediaItem>();
static int menuOption;
static Scanner scan = new Scanner(System.in);
public static void main(String args[]) {
String title, format, loanedTo, dateLoaned;
boolean right = false;
do {
displayMenu();
if (menuOption == 1) {
System.out.println("Enter Title: ");
title = scan.next();
System.out.println("Enter format: ");
format = scan.next();
addNewItem(title, format);
} else if (menuOption == 2) {
System.out.println("Enter the item title");
title = scan.next();
System.out.println("Who are you loaning it to?");
loanedTo = scan.next();
System.out.println("When did you loan it to them?");
dateLoaned = scan.next();
markItemOnLoan(title, loanedTo, dateLoaned);
} else if (menuOption == 3) {
for (MediaItem mi : items) {
System.out.println(mi.getTitle() + ", " + mi.getFormat());
}
} else if (menuOption == 4) {
} else {
System.exit(1);
}
} while (!right);
}
static int displayMenu() {
boolean right = false;
do {
System.out.println("Menu: ");
System.out.println("1. Add New Item");
System.out.println("2. Mark an item as on loan");
System.out.println("3. List all items");
System.out.println("4. Mark an item as returned");
System.out.println("5. Quit");
menuOption = scan.nextInt();
if (menuOption < 1 || menuOption > 5) {
System.out.println("Invalid Number!");
}
return menuOption;
} while (!right);
}
static void addNewItem(String title, String format) {
MediaItem b = new MediaItem();
b.setTitle(title);
b.setFormat(format);
items.add(b);
}
static void markItemOnLoan(String title, String name, String date) {
Iterator<MediaItem> it = items.iterator();
System.out.println(it.next());
}
}
public class MediaItem {
String title;
String format;
boolean onLoan;
String loanedTo;
String dateLoaned;
MediaItem() {
title = null;
format = null;
onLoan = false;
loanedTo = null;
dateLoaned = null;
}
MediaItem(String title, String format) {
title = new String();
format = new String();
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
public boolean isOnLoan() {
return onLoan;
}
public void setOnLoan(boolean onLoan) {
this.onLoan = onLoan;
}
public String getLoanedTo() {
return loanedTo;
}
public void setLoanedTo(String loanedTo) {
this.loanedTo = loanedTo;
}
public String getDateLoaned() {
return dateLoaned;
}
public void setDateLoaned(String dateLoaned) {
this.dateLoaned = dateLoaned;
}
void markOnLoan(String name, String date) {
onLoan = true;
}
void markReturned() {
onLoan = false;
}
}