Results 1 to 15 of 15
- 12-06-2010, 07:31 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 51
- Rep Power
- 0
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
But I'm not sure whether this is the best way to do itJava 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; }
and for comment and quitting option I did this
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.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
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.
- 12-07-2010, 01:53 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Ok, lets take the fist scenario.
What's the procedure you have design when booking a ticket?
- 12-07-2010, 01:59 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 51
- Rep Power
- 0
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 02:01 AM.
- 12-07-2010, 02:05 AM #4
Member
- Join Date
- Oct 2010
- Posts
- 51
- Rep Power
- 0
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.
- 12-07-2010, 02:07 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes, I've looked into your code. Seems fine, but only for a single instance. So basically you've to continue this from,
isn't it?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 ); }
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.
- 12-07-2010, 02:12 AM #6
Member
- Join Date
- Oct 2010
- Posts
- 51
- Rep Power
- 0
I'm really sorry I don't really get it.
could you be more detailed.
- 12-07-2010, 02:13 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 12-07-2010, 02:15 AM #8
Member
- Join Date
- Oct 2010
- Posts
- 51
- Rep Power
- 0
so basically I have to do an arraylist for the comments?
- 12-07-2010, 02:21 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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?
- 12-07-2010, 02:24 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 12-07-2010, 02:27 AM #11
Member
- Join Date
- Oct 2010
- Posts
- 51
- Rep Power
- 0
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 02:32 AM.
- 12-07-2010, 02:33 AM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
JTextArea is easy, mainly you can provide enough space, while the user can type on it freely. Try a sample first of all.
How to Use Text Areas (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
- 12-07-2010, 02:41 AM #13
Member
- Join Date
- Oct 2010
- Posts
- 51
- Rep Power
- 0
how do I apply this for comments on the concert the user has chosen?
- 12-07-2010, 02:49 AM #14
Member
- Join Date
- Oct 2010
- Posts
- 51
- Rep Power
- 0
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.
- 12-07-2010, 05:55 AM #15
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
If you clearly explain, then we can help you.
So invoke your process in an infinite loop
Java Code:while(true) { // rest of the stuff }
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
-
I need help creating this program
By LostinJavaLand in forum New To JavaReplies: 14Last Post: 04-29-2010, 10:41 AM -
Writing a program to crack a PIN number using nested for-loops
By rcastillo106 in forum New To JavaReplies: 5Last Post: 04-01-2010, 03:57 AM -
trouble creating program using loops for multiplication table
By cuse17 in forum New To JavaReplies: 2Last Post: 02-23-2009, 02:18 AM -
Creating Program Stubs
By Bascotie in forum New To JavaReplies: 2Last Post: 01-18-2009, 06:27 AM -
Help combining loops into 1 program.
By kewlgeye in forum New To JavaReplies: 5Last Post: 04-22-2008, 09:58 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks