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
[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.
Type in the number of a band
1. Vampire Weekend
2. Of Montreal
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;
}
[/code]
But I'm not sure whether this is the best way to do it
and for comment and quitting option I did this
[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:
1. Vampire Weekend
2. Of Montreal
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,
location = 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,
location = 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,
location = 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] + "
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
[/code]
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.