java - array list, hur anropar jag den i en annan klass?
Hej
Klistrar in en fråga (den är på engelska) som jag försökt få svar på ett tag.
Kanske någon här kan hjälpa mig
Hello
I am trying to figure out how to manage my array list.
I know im doing things a bit strnage, but that is mainly becouse of a school assignment.
I have 3 classes.
1. Main class
2. Reader Collection - here i want my array list to be.
3. Reader - here all the reader information is specified.
Everything works fine. But i cant get the array list to work in its own class. I can only get it to work if i put it in the main class.
I understand that i need to call the ReaderCollection class somehow, but i cant understand how to do that.
Here follows my code for all classes.
MAIN CLASS!!
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class MainLibraryProgram {
private Scanner input = new Scanner(System.in);
private List<Reader> readers = new ArrayList<Reader>();
public static void main(String[] args) {
MainLibraryProgram library = new MainLibraryProgram();
library.start();
}
private void start() {
System.out.println("---------------Welcome to The Library"
+ "---------------\n---------Please select your "
+ "preferd option.--------\n");
while(true){
printMenu();
handleMenuCommand();
}
}
private void printMenu() {
System.out.println("A - View Reader List");
System.out.println("B - View Media Item List");
System.out.println("C - Add Reader");
System.out.println("D - Delete Reader");
System.out.println("E - Add Media Item");
System.out.println("F - Delete Media Item");
System.out.println("G - Credits");
System.out.println("H - Exit");
System.out.print("--------------------\n>");
}
private char readCommand() {
String commandLine;
do {
commandLine = input.nextLine();
} while (commandLine.length() == 0);
return commandLine.charAt(0);
}
private void handleMenuCommand() {
char commandLine = readCommand();
switch (commandLine) {
case 'a':
case 'A':
System.out.println("\n");
return;
case 'b':
case 'B':
System.out.println("test");
System.out.println("\n");
return;
case 'c':
case 'C':
createReader();
System.out.println("\n");
return;
case 'd':
case 'D':
deleteReader();
System.out.println("\n");
return;
case 'e':
case 'E':
System.out.println("\n");
return;
case 'f':
case 'F':
System.out.println("\n");
return;
case 'g':
case 'G':
System.out.println("\n");
return;
case 'h':
case 'H':
System.out.println("Going Offline");
System.exit(0);
return;
default:
wrongCommand();
}
}
private String readString(String prompt) {
System.out.print(prompt);
return input.nextLine();
}
private int readInt(String prompt) {
System.out.print(prompt);
int value = input.nextInt();
input.nextLine();
return value;
}
/*
* private void printReaderCollection() {
* System.out.println("Name \t ID \n----------------------");
* for (Reader a: readers) {
* System.out.println(a.getName() + "\t" + a.getReaderID()); }
*
* }
*/
/*
* private void printMediaCollection() {
* System.out.println("Name \t ID \n----------------------");
* for (MediaItem a: mediaItems) { System.out.println(a.get....() + "\t" + a.get....()); }
*/
private void wrongCommand(){
System.out.println("You are WRONG, do it right!!!");
}
private void createReader() {
String name = readString("Name: ");
int readerID = readInt("Reader ID: ");
int birthYear = readInt("Birth Year: ");
//Address address = readAddress("Address: ");
//PhoneNo phoneNo = readPhoneNo("Phone Number: ");
//Email email = readEmail("Email: ");
Reader theNewCustomer = new Reader(name, readerID, birthYear);
readers.add(theNewCustomer);
System.out.println("Reader " + name + " added");
}
private void deleteReader() {
String name = readString("Name: ");
int readerID = readInt("Reader ID: ");
int birthYear = readInt("Birth Year: ");
//Address address = readAddress("Address: ");
//PhoneNo phoneNo = readPhoneNo("Phone Number: ");
//Email email = readEmail("Email: ");
Reader theCustomer = new Reader(name, readerID, birthYear);
readers.remove(theCustomer);
System.out.println("Reader " + name + " removed");
}
}
READER COLLECTION CLASS
import java.util.ArrayList;
import java.util.List;
public class ReaderCollection {
private List<Reader> readers = new ArrayList<Reader>();
}
READER CLASS
public class Reader {
private String name;
private int readerID;
private int birthYear;
//private Address address;
//private PhoneNo phoneNo;
//private Email email;
public Reader(String name, int readerID, int birthYear){
this.name = name;
this.readerID = readerID;
this.birthYear = birthYear;
//this.address = address;
//this.phoneNo = phoneNo;
//this.email = email;
}
public String getName() {
return name;
}
public int getReaderID(){
return readerID;
}
public int getbirthYear(){
return birthYear;
}
/*
public Address getAddress(){
return address;
}
public PhoneNo getPhoneNo(){
return phoneNo;
}
public Email getEmail(){
return email;
}
*/
public String toString(){
return name + readerID + birthYear;
}
}
The methods that need to use the array list so fare are those that are in the main class and are commented out.
Plus the create reader and delete reader methods.
Im sure everyone have comments on my code everywhere. But first id like to solve my issue
Thanks in advance!!!