Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2012
    Posts
    28
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Confused with this problem. Need Feedback and Guidance.adding and viewing information

    Below is the problem I face :
    View Places– The user selects a destination and the system displays all available Places at that destination. If there are no Places for a destination it should display a message to say so.

    Add Place– Allows the user to add Place(s) for a given destination. This should add to the list of existing Placesfor that destination (if there are any). For example, the system might ask how many Places I wish to add; I might enter 2; and then I might add "tesco" and "asda". I do not know how to put the added Places into the view Places method :S

    Im really confused on how to solve this problem. What i think i should do is to create two different methods for both problems. Whether i should use arrays and user can be asked to input Places which are stored in arrays.
    below is what i have tryed. Would appreciate some help. UPDATE this is what ive tryed but i think its the wrong way to go about it. :

    Code:
    import java.util.*;
    
    public class placeMethods
    {
    public static void main(String[] args) 
    	{
    	addplace();
    	
    	}
    
    	public static void viewplace() 
    	{
    		String[] destination = new String[5];
    				destination[0] = "1: New York";
    				destination[1] = "2: Dahab";
    				destination[2] = "3: Rome";
    				destination[3] = "4: Sydney";
    				destination[4] = "5: Tokyo";
    				
    				int j;
    				for (j=0; j <destination.length; j++) //loop below is used to display the string of array
    				{ 
    				System.out.println(destination[j]);
    				}
    				System.out.println("Please select a Destination!");
    				
    				
    	}
    	
    	public static void addplace()
    	{	
    		
    		String[] destination = new String[5];
    				destination[0] = "1: New York";
    				destination[1] = "2: Dahab";
    				destination[2] = "3: Rome";
    				destination[3] = "4: Sydney";
    				destination[4] = "5: Tokyo";
    				
    				int j;
    				for (j=0; j <destination.length; j++) //loop below is used to display the string of array
    				{ 
    					System.out.println(destination[j]);
    				}
    				System.out.println("Please select a Destination!");
    				
    				
    				 
    				Scanner selectDestination = new Scanner(System.in);
    		        String inputDestination = selectDestination.nextLine();
    				String month;
    				if (inputDestination.equals("1"))
    				{
    				System.out.println("How many places do you wish to add?");
    				Scanner selectplace = new Scanner(System.in);
    				String inputNumber = selectplace.nextLine();
    					if (inputNumber.equals("1"))
    					{
    					System.out.println("Please enter the name of the place.");
    					addPlace = selectplace.next();
    					List list = new ArrayList();
    					list.add(addPlace);
    					System.out.println(list);
    					}	
    	
    				}
    		}		
    		
    	
    	}
    Last edited by nervousprogramm; 03-25-2013 at 11:31 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    One step back.
    Do you track places as well as hotels within places separately? Or just one, such as in Tokyo?
    Arrays or collections are fine. Adding is more useful if you use a collection such as an ArrayList though. Arrays are not scalable, so if you add say two locations, then decide to add another two, you'll need to resize the array by creating a new array and then copying the existing data before inserting new data. Collections and Vectors excel at this since you can scale them both up and down very easily, and are typically iterated using the foreach style syntax.
    You'll need to be handling a single (or multiple, not sure yet) arrays (or collection objects) for this. You can do so either at the class level by declaring a static array as a member property, OR you can create an array and pass it around from method to method to modify. Array pointer values are passed by reference in Java, but not the pointer itself (so you can modify an array within a function but you cannot modify what the array points at). So to keep it simple, I'd suggest either a collection OR a static class member that contains these.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    nervousprogramm (03-25-2013)

  • #3
    New Coder
    Join Date
    Oct 2012
    Posts
    28
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    One step back.
    Do you track places as well as hotels within places separately? Or just one, such as in Tokyo?
    Arrays or collections are fine. Adding is more useful if you use a collection such as an ArrayList though. Arrays are not scalable, so if you add say two locations, then decide to add another two, you'll need to resize the array by creating a new array and then copying the existing data before inserting new data. Collections and Vectors excel at this since you can scale them both up and down very easily, and are typically iterated using the foreach style syntax.
    You'll need to be handling a single (or multiple, not sure yet) arrays (or collection objects) for this. You can do so either at the class level by declaring a static array as a member property, OR you can create an array and pass it around from method to method to modify. Array pointer values are passed by reference in Java, but not the pointer itself (so you can modify an array within a function but you cannot modify what the array points at). So to keep it simple, I'd suggest either a collection OR a static class member that contains these.

    Im thinking of creating an array list for each destination. But is it possible to pass on the array list to another method?

    So when user selects the option Add places, the user can enter the places and then to view them the user then returns to menu and selects view hotels.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Yes, you can pass an ArrayList<T> object through a method parameter, or contain it as a static member. Both would be fine and you can alter it within each method without the mess of an array.
    If you are tracking places within places (which I'm not sure if you are, but you may be. . .) such as a place within a city, than a good option may be to use a Map<String, ArrayList<String>> type. This would let you put on a city name, and have a list of places that can be added or iterated within it.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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