Results 1 to 15 of 15
  1. #1
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default HELP - Creating OPTIONS in program and LOOPS

    Hi I'm writing a program that offers several options including booking, quitting and posting a comment about a concert.
    The comment is then printed the next time someone chooses that group and replacing any earlier comment. This program will run continuously until the user clicks quit option.

    So far this is what I have written

    for booking option I did this

    Java Code:
    public static void main (String[] param)
    	{
    	
    	    
    	    //System.out.println(JOptionPane.showConfirmDialog(null, "yes", "title", JOptionPane.YES_NO_OPTION ));
    		orderTickets();
    		System.exit(0);
    	}
    	
    	public static void orderTickets()
    	{	
    		
    		String ordertickets = getText("Order tickets now.\nType in the number of a band\n\n 1. Vampire Weekend \n 2. Of Montreal \n 3. Someone Still Loves You Boris Yeltsin");
    		int ticketnum = Integer.parseInt(ordertickets);
    		
    		String[] bands = {"Vampire Weekend","Of Montreal","Someone Still Loves You Boris Yeltsin"};
    		int[] prices = {10,8,6};
    		
    		int tickets = Integer.parseInt(getText("Please type in the number of tickets."));
    	
    		if (ticketnum <= bands.length) 
    		{
    			int price = prices[ticketnum-1]*tickets;
    			
              	JOptionPane.showMessageDialog(null, price + " pounds - General Admission");
    		} 
    		else 
    		{
       			JOptionPane.showMessageDialog(null,"Error. Please type in the number of the group listed.");
    		}
    		
    		int accounts = accountNumber();
    	 }	
    	 
    	 // asking the user to input their account number and returning 
    	 public static int accountNumber() 
    	 {	int accountNumber = -1;
    	 	int tocontinue = -1;
    		do
    		{
    		String accountNumString = getText("Please provide your account number.");
    		accountNumber = Integer.parseInt(accountNumString);
      
    		int accountIndex = validateAccount(accountNumber);
    	
    			if (accountIndex >= 0) 
    			{
    			JOptionPane.showMessageDialog(null, "BOOKED");
    			} 
    			else 
    			{
    				tocontinue = wantToCarryOn();
    				
    			}
    		  }
    		  while(tocontinue == 0);
    		return accountNumber;
    	  } 
    
    	  public static int validateAccount(int accountNumber)
    	  {		int[] arrayOfAccounts = {1000,1234,1337,1066};
    			for (int i = 0; i<arrayOfAccounts.length; i++) 
    			{
     				if (arrayOfAccounts[i] == accountNumber) 
    				{
    					return i;
    				}
    				else
    				{
    				//Not a valid account
    					   return -1;
    				}
    	  		}
    	    return -1;
    	  } 
    	  
    	  
    	  
    	  public static int wantToCarryOn()
    	  {
    	  	return JOptionPane.showConfirmDialog(null, "Invalid account number. Do you want to try a different account number ?", "", JOptionPane.YES_NO_OPTION );
    	  
    	  }
    	  
    	  public static String getText(String msg)
    		{
    			String txt = null;
    			while (txt == null || txt.trim().equals(""))
    			{
    				txt = JOptionPane.showInputDialog(msg);
    			}
    			return txt;
    		}
    But I'm not sure whether this is the best way to do it

    and for comment and quitting option I did this

    Java Code:
    class testing
    {
         boolean quit    = false;
         int SlotsFilled = 0;
         String ListOfBands[]   = new String [1000];
    	 String ListOfComment[] = new String [1000];
         String ListOfMessages[]= new String [1000];
    	
        public static void main (String[] param)
        {
           testing launch = new testing();
           launch.goToBands();
           System.exit(0);
           
        } // END main
    
    	public void goToBands()
    	{
    		while (!quit)
    		{
             popGroups();
          	}
    	}
    
    	
        public void popGroups()
        {
     		String band; 
           	int band1;
           
           band = getText("Please type the number of group according the list below: \n 1. Vampire Weekend \n 2. Of Montreal \n 3. Someone Still Loves You Boris Yeltsin");
           band1 = Integer.parseInt(band);
           
           String CurrentComment;
           String NamesOfBand; 
           int tocontinue = -1;
           do
           {
           	switch (band1)
           	{
           		case 1: JOptionPane.showMessageDialog(null, "date = 10 Nov 2010,\nlocation = O2 Academy Brixton");      
                    NamesOfBand ="Vampire Weekend";
                    CurrentComment = getText("Comment");
                    //System.out.println(getComment(NamesOfBand,CurrentComment));
                    JOptionPane.showConfirmDialog(null, getComment(NamesOfBand,CurrentComment), "", JOptionPane.OK_CANCEL_OPTION);
                    break;
           
           	    case 2: JOptionPane.showMessageDialog(null, "date = 3 Dec 2010,\nlocation = Alexandra Palace");
                    NamesOfBand ="Of Montreal";
                    CurrentComment = getText("Comment");
                    //System.out.println(getComment(NamesOfBand,CurrentComment));
                    JOptionPane.showConfirmDialog(null, getComment(NamesOfBand,CurrentComment), "", JOptionPane.OK_CANCEL_OPTION);
                    break;
           
           	    case 3: JOptionPane.showMessageDialog(null, "date = 11 Nov 2010,\nlocation = O2 Academy Brixton");
                    NamesOfBand ="Someone Still Loves You Boris Yeltsin";
                    CurrentComment = getText("Comment");
                    //System.out.println(getComment(NamesOfBand,CurrentComment));
                    JOptionPane.showConfirmDialog(null, getComment(NamesOfBand,CurrentComment), "", JOptionPane.OK_CANCEL_OPTION);
                    break;
           
           	    default: JOptionPane.showMessageDialog(null, " Error. Please type in the number of group listed.");
                    NamesOfBand ="";
                    break;
           }
    		tocontinue = wantToCarryOn();
    	  } while (tocontinue == 0);
    	}
    	
    	
    	public String getComment(String namesofbands,String usercomment)
        {
          
         	this.ListOfBands[SlotsFilled] =  namesofbands;
         	this.ListOfComment[SlotsFilled] = usercomment;
          	ListOfMessages[SlotsFilled] = "The Band you Chose is :" + ListOfBands[SlotsFilled] + "\n Your Comment = " + ListOfComment[SlotsFilled];
          	String PreviousReview = ListOfMessages[SlotsFilled];
          	//this.SlotsFilled++;  
          
          return PreviousReview;
        }
       
       public static int wantToCarryOn()
    	{
    	  	return JOptionPane.showConfirmDialog(null, "Do yo want to continue","", JOptionPane.YES_NO_OPTION);
    	  
    	}
        public String getText(String msg)
    	{
    			String txt = null;
    			while (txt == null || txt.trim().equals(""))
    			{
    				txt = JOptionPane.showInputDialog(msg);
    			}
    			return txt;
    	}
    } // END
    For this second program, I'm not quite sure How to make the comment to appear after someone new comments on a band, and I tried to make it quit when the user clicks no, but it is still running. How do I make it quit when the user clicks No.


    And one more thing, How Do I make the program to run continuously, I know I have to use a loop, probably while loop. But where should I put it?


    Any help would greatly appreciate.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default

    Okay so basically, the first program is for the user to book concert tickets.
    It will first ask the user which band he would like to see, once he chooses the band he likes, the program will then ask for how many he would like to buy, once he types in the number of tickets, it will calculate the total and, then it will ask the user to input his account number, which will be checked against a series of accounts.
    if it is a valid account, a box(a receipt) will pop up "BOOKED", otherwise he will have to input again, or he can choose to stop.
    Last edited by whateverme; 12-07-2010 at 03:01 AM.

  4. #4
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default

    For the second program I did, was supposed to give a several options to the user including booking, quitting and posting a comment about the concert.
    The comment is then printed the next time someone chooses that group replacing any earlier comment.
    It will run continuously until the quit option is chosen.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes, I've looked into your code. Seems fine, but only for a single instance. So basically you've to continue this from,

    Java Code:
    public static int wantToCarryOn()
    	  {
    	  	return JOptionPane.showConfirmDialog(null, "Invalid account number. Do you want to try a different account number ?", "", JOptionPane.YES_NO_OPTION );
    	  
    	  }
    isn't it?

    So what you can do is, use a status field and set it here. Then in a continuous loop (while, etc..) you can invoke the same procedure.

  6. #6
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default

    I'm really sorry I don't really get it.
    could you be more detailed.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by whateverme View Post
    For the second program I did, was supposed to give a several options to the user including booking, quitting and posting a comment about the concert.
    The comment is then printed the next time someone chooses that group replacing any earlier comment.
    It will run continuously until the quit option is chosen.
    So where you keep those comments? Since you want to display the previous messages you have to keep them in a permanent storage.

  8. #8
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default

    so basically I have to do an arraylist for the comments?

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by whateverme View Post
    I'm really sorry I don't really get it.
    could you be more detailed.
    Okay, I think you've done the same in the second program. What I'm saying is that you have to maintain a global status to track that user wants to exit the program of continue. And that you can set in the code segment that I've pointed in previous post. Is that clear?

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by whateverme View Post
    so basically I have to do an arraylist for the comments?
    For the temporary storing, yes, ArrayList is the most suitable. Since you are not worried about them after the program exit, then it's fine. Otherwise you may write them to a database or a text file.

  11. #11
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default

    I just read about textfields,
    is it suitable to use it for comments?
    if yes how would I use it?
    Last edited by whateverme; 12-07-2010 at 03:32 AM.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  13. #13
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default

    how do I apply this for comments on the concert the user has chosen?

  14. #14
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default

    I'm basically stuck, I need a specific solution, so basically,
    all I want to do is to make my second program runs continuously and as for the comments part, I need the user to write a comment about the concert, and once a new user chooses the same group and comments on the concert it will replace any earlier comment.

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by whateverme View Post
    I'm basically stuck, I need a specific solution, so basically
    If you clearly explain, then we can help you.

    Quote Originally Posted by whateverme View Post
    all I want to do is to make my second program runs continuously
    So invoke your process in an infinite loop

    Java Code:
    while(true) {
         // rest of the stuff
    }


    Quote Originally Posted by whateverme View Post
    I need the user to write a comment about the concert, and once a new user chooses the same group and comments on the concert it will replace any earlier comment.
    Your commenting is not bind with the user, but with the convert. So each concert you've an ID, all what you've to do is maintain a map with the ID and the comment.

    Or else, seems to me you have three bands, which are treated as convert. So simply maintain three global status. But personally I'm not encourage you to do that, maintain lots of global status (variables) not a good programming practice.

Similar Threads

  1. I need help creating this program
    By LostinJavaLand in forum New To Java
    Replies: 14
    Last Post: 04-29-2010, 11:41 AM
  2. Replies: 5
    Last Post: 04-01-2010, 04:57 AM
  3. Replies: 2
    Last Post: 02-23-2009, 03:18 AM
  4. Creating Program Stubs
    By Bascotie in forum New To Java
    Replies: 2
    Last Post: 01-18-2009, 07:27 AM
  5. Help combining loops into 1 program.
    By kewlgeye in forum New To Java
    Replies: 5
    Last Post: 04-22-2008, 10:58 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
  •