Results 1 to 2 of 2
  1. #1
    behedwin is offline Member
    Join Date
    Oct 2012
    Posts
    26
    Rep Power
    0

    Default call an array list in another class?

    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!!
    Java Code:
    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

    Java Code:
    import java.util.ArrayList;
    import java.util.List;
    
    
    public class ReaderCollection {
    	
    	private List<Reader> readers = new ArrayList<Reader>();
    	
    	
    	
    
    
    }
    READER CLASS


    Java Code:
    
    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!!!

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: call an array list in another class?

    When you say you can't get it to work in that class, what do you mean? Do you get errors?

Similar Threads

  1. Replies: 3
    Last Post: 06-22-2012, 06:54 PM
  2. Replies: 2
    Last Post: 05-03-2012, 07:12 PM
  3. read or call an array from other class an method
    By ki_ha1984 in forum New To Java
    Replies: 5
    Last Post: 01-23-2012, 10:42 PM
  4. Linked List, Array List time complexity
    By Rick99771977 in forum New To Java
    Replies: 4
    Last Post: 08-18-2011, 05:37 AM
  5. Replies: 0
    Last Post: 03-27-2011, 05:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •