connect array list elements with tester class statements

to make user input add an element to an array list

Page 1 of 1

5 Replies - 3209 Views - Last Post: 01 May 2010 - 06:00 PM Rate Topic: -----

#1 Guest_Kyle*


Reputation:

connect array list elements with tester class statements

Posted 01 May 2010 - 01:59 PM

hello all,

Im very new at java, and have been having difficulty in class since day one.

Nearing the end of the year, we were asked to design a program that simulates a library. The program consists of 4 classes: library, book, patron, and library main(the tester class).

I'm having trouble allowing input by the user to be added to an array list. For example, i want the user to be able to add a book into the list simply by typing it in the main console. Im aware i must use a method for this, but i'm not sure which one, or how to implement it.

This is what i have in the tester class so far:

import java.util.Scanner;
import java.util.ArrayList;

public class LibraryMain {
	
	public static void main(String args[]){
		

		
		
		Scanner in = new Scanner(System.in);
		
		//summary of options and instructions
		System.out.println("-Options-");
		System.out.println("Add Book");
		System.out.println("Remove Book");
		System.out.println("Check-In Book");
		System.out.println("Check-Out Book");
		System.out.println("Search Library");
		System.out.println("--------------------------");
		System.out.println("Type (show all books) to display all books available.");
		System.out.println("Type (show all patrons) to display all patrons on record.");
		System.out.println("Type (show all of type(genre here)), to display books of specific subject.");
		System.out.println("--------------------------");
		
		String input;
		
		System.out.print("Enter a selection: ");
		
		input = in.nextLine();

		// add/remove books control
		if (input.equalsIgnoreCase("add book"))
		{
			System.out.println("Enter book to add: ");	
			
			input = in.nextLine();
			
		
		
			
		}		
		else if (input.equalsIgnoreCase("remove book"))
		{	
			System.out.println("Enter book to remove: ");
		}
			
		// add/remove patrons control
		else if (input.equalsIgnoreCase("add patron"))
		{
			System.out.println("Enter patron to add: ");
		}
		else if (input.equalsIgnoreCase("remove patron"))
		{
			System.out.println("Enter patron to remove: ");
		}
		
		// show entire library
		else if (input.equalsIgnoreCase("show all books"))
		{
			System.out.println("Displaying all books available in library: ");
		}
		else
		{
			System.out.println("Invalid Entry");
		}/code]



and the library class: ..

[code]
import java.util.Scanner;
import java.util.ArrayList;


public class Library {
	
	public static void main(String args[]) { 
		
	Scanner in = new Scanner(System.in);
			
	ArrayList<String> books = new ArrayList<String>(); 
	books.add("Hitchhikers Guide to The Galaxy");
	
	ArrayList<String> patrons = new ArrayList<String>();
	patrons.add("Douglas Adams");
	



If anybody could help me out with this i would greatly appreciate it.
Thank You

Is This A Good Question/Topic? 0

Replies To: connect array list elements with tester class statements

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: connect array list elements with tester class statements

Posted 01 May 2010 - 02:22 PM

Why do you have two main methods? Assuming you will be using both of the classes you listed in conjunction, you can only have one. The main method is the point where the JVM begins execution of your program.

This post has been edited by erik.price: 01 May 2010 - 02:22 PM

Was This Post Helpful? 0
  • +
  • -

#3 Guest_Kyle*


Reputation:

Re: connect array list elements with tester class statements

Posted 01 May 2010 - 02:50 PM

like i said.. very new with java. thank you. what do you suggest i change the other method to?
Was This Post Helpful? 0

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10807
  • View blog
  • Posts: 40,287
  • Joined: 27-December 08

Re: connect array list elements with tester class statements

Posted 01 May 2010 - 03:03 PM

You may find my tutorial Moving Away From Parallel Arrays helpful to learn basic class design, as I use a simple inventory program case study. Your assignment is asking for a little more complex inventory program, but it still uses the same basic concepts.

Some things to consider when designing your classes:
-What are some attributes of a Book? A patron? A library? I'll get you started for book- a Book has author, copyright date, numPages. These attributes should be represented by instance variables in the respective classes.

-You are on the right track with the Lists for Patrons and Books, though maybe you should use ArrayList<Book> and ArrayList<Patron> instead of ArrayList<String> for both.

-A class is a blueprint for an Object. Therefore, it should outline attributes for each Object (ie., each Patron has a name), but should not be runnable/have a main() method. Your LibraryMain class should handle running the program; and therefore, it should be the only class with a main() method.

Beyond these points, you should also check out the Java tutorials section, as we have a few tutorials (such as Locke's) on class design, which I think you will really find helpful.
Was This Post Helpful? 0
  • +
  • -

#5 Guest_Kyle*


Reputation:

Re: connect array list elements with tester class statements

Posted 01 May 2010 - 03:13 PM

Will do, thanks a lot macosxnerd101.


View Postmacosxnerd101, on 01 May 2010 - 02:03 PM, said:

You may find my tutorial Moving Away From Parallel Arrays helpful to learn basic class design, as I use a simple inventory program case study. Your assignment is asking for a little more complex inventory program, but it still uses the same basic concepts.

Some things to consider when designing your classes:
-What are some attributes of a Book? A patron? A library? I'll get you started for book- a Book has author, copyright date, numPages. These attributes should be represented by instance variables in the respective classes.

-You are on the right track with the Lists for Patrons and Books, though maybe you should use ArrayList<Book> and ArrayList<Patron> instead of ArrayList<String> for both.

-A class is a blueprint for an Object. Therefore, it should outline attributes for each Object (ie., each Patron has a name), but should not be runnable/have a main() method. Your LibraryMain class should handle running the program; and therefore, it should be the only class with a main() method.

Beyond these points, you should also check out the Java tutorials section, as we have a few tutorials (such as Locke's) on class design, which I think you will really find helpful.

Was This Post Helpful? 0

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10807
  • View blog
  • Posts: 40,287
  • Joined: 27-December 08

Re: connect array list elements with tester class statements

Posted 01 May 2010 - 06:00 PM

Not a problem. Glad I could help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1