Need help on making the main class use the module for Theatre reservation program
hi guys
I need help here will I am building the application for booking ticket , my problem is that I have two class and I am having problem creating the main class with the PrintSeats module. Is there anyone here can help me in this I will be really appreciating this
Code:
import javax.swing.JOptionPane;
public class Theatre2D {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// declares an array of integers
int[][] myStage2D;
// allocates memory for 2*10 integers
myStage2D = new int[2][10];
myStage2D[0][0] = 20;
myStage2D[0][1] = 30;
myStage2D[0][2] = 40;
myStage2D[0][3] = 50;
myStage2D[0][4] = 60;
myStage2D[0][5] = 60;
myStage2D[0][6] = 50;
myStage2D[0][7] = 40;
myStage2D[0][8] = 30;
myStage2D[0][9] = 20;
//Start Second Row
myStage2D[1][0] = 20;
myStage2D[1][1] = 30;
myStage2D[1][2] = 40;
myStage2D[1][3] = 50;
myStage2D[1][4] = 60;
myStage2D[1][5] = 60;
myStage2D[1][6] = 50;
myStage2D[1][7] = 40;
myStage2D[1][8] = 30;
myStage2D[1][9] = 20;
String[][] seatAvail = new String[2][10];
seatAvail[0][0] = "Open";
seatAvail[0][1] = "Open";
seatAvail[0][2] = "Open";
seatAvail[0][3] = "Open";
seatAvail[0][4] = "Open";
seatAvail[0][5] = "Open";
seatAvail[0][6] = "Open";
seatAvail[0][7] = "Open";
seatAvail[0][8] = "Open";
seatAvail[0][9] = "Open";
//Second row
seatAvail[1][0] = "Open";
seatAvail[1][1] = "Open";
seatAvail[1][2] = "Open";
seatAvail[1][3] = "Open";
seatAvail[1][4] = "Open";
seatAvail[1][5] = "Open";
seatAvail[1][6] = "Open";
seatAvail[1][7] = "Open";
seatAvail[1][8] = "Open";
seatAvail[1][9] = "Open";
int column = 0;
int row = 0;
while (row<2){
while (column<10){
System.out.println("Row "+(row+1)+", seat " + (column+1)+" costs "
+myStage2D[row][column] +" dollars."
+ "That seat is currently " + seatAvail[row][column]);
column+=1;
}
row+=1;
column = 0;
}
for (int s=0;s<20;s++){
String crow = JOptionPane.showInputDialog("From which row do you wish to purchase? (1-2) ");
int crownum = Integer.parseInt(crow);
String cseat = JOptionPane.showInputDialog("From row " + crow+ " which seat do you wish to purchase? (1-10)");
int cseatnum = Integer.parseInt(cseat);
if (seatAvail[crownum-1][cseatnum-1]=="Open");
seatAvail[crownum-1][cseatnum-1] = "Occupied";
System.out.println("");
System.out.println("Thanks for you purchase! It has cost you $"+myStage2D[crownum-1][cseatnum-1]+".");
System.out.println("");
myTheatre.PrintSeats();
}
}
}
and
public class Seat2D {
public void PrintSeats() {
int[][] seats = new int[10][5];
int column = 0;
int row = 0;
while (row<5){
while (column<10){
System.out.println(seats[column][row]);
column+=1;
}
row+=1;
column = 0;
}
}
}
Thanks
Re: Need help on making the main class use the module for Theatre reservation program
I like to tell you that change the line if (seatAvail[crownum-1][cseatnum-1]=="Open"); with the if (seatAvail[crownum-1][cseatnum-1].equals("Open"))
And also you should put all the need that IF statement (the one I should above) to do in curly “{}” brackets. At present , on the code here, the just statement that the IF is execute is the one straight under it. So, stick every of that in a pair of brackets. Now, onto your issue. What I will have to do would be to just tack that method on the end of your main file, in place of in a different class. You would, certainly, have to put in the keyword static, as shown down
Code:
import javax.swing.JOptionPane;
public class Theatre2D {
/**
* @param args
*/
public static void main(String[] args) {
// ... main method stuff ...
}
public static void PrintSeats() {
// ... other stuff ...
}
}
After that, you just call it like PrintSeats(); and that would work. Test it and tell me if you need further help
Re: Need help on making the main class use the module for Theatre reservation program
Thanks for reply but I tried enter it on the similar class although I get the new issue . It tells me that "printSeats is undefined for type myStage2D". I did a some adjustments to the code under and now I need to know how to I get it to print a revise list each time a new seat is bought ?
Code:
import javax.swing.JOptionPane;
public class Theatre2D {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// declares an array of integers
int[][] myStage2D;
// allocates memory for 2*10 integers
myStage2D = new int[2][10];
myStage2D[0][0] = 20;
myStage2D[0][1] = 30;
myStage2D[0][2] = 40;
myStage2D[0][3] = 50;
myStage2D[0][4] = 60;
myStage2D[0][5] = 60;
myStage2D[0][6] = 50;
myStage2D[0][7] = 40;
myStage2D[0][8] = 30;
myStage2D[0][9] = 20;
//Start Second Row
myStage2D[1][0] = 20;
myStage2D[1][1] = 30;
myStage2D[1][2] = 40;
myStage2D[1][3] = 50;
myStage2D[1][4] = 60;
myStage2D[1][5] = 60;
myStage2D[1][6] = 50;
myStage2D[1][7] = 40;
myStage2D[1][8] = 30;
myStage2D[1][9] = 20;
String[][] seatAvail = new String[2][10];
seatAvail[0][0] = "Open";
seatAvail[0][1] = "Open";
seatAvail[0][2] = "Open";
seatAvail[0][3] = "Open";
seatAvail[0][4] = "Open";
seatAvail[0][5] = "Open";
seatAvail[0][6] = "Open";
seatAvail[0][7] = "Open";
seatAvail[0][8] = "Open";
seatAvail[0][9] = "Open";
//Second row
seatAvail[1][0] = "Open";
seatAvail[1][1] = "Open";
seatAvail[1][2] = "Open";
seatAvail[1][3] = "Open";
seatAvail[1][4] = "Open";
seatAvail[1][5] = "Open";
seatAvail[1][6] = "Open";
seatAvail[1][7] = "Open";
seatAvail[1][8] = "Open";
seatAvail[1][9] = "Open";
printSeats();
for (int s=0;s<20;s++){
String crow = JOptionPane.showInputDialog("From which row do you wish to purchase? (1-2) ");
int crownum = Integer.parseInt(crow);
String cseat = JOptionPane.showInputDialog("From row " + crow+ " which seat do you wish to purchase? (1-10)");
int cseatnum = Integer.parseInt(cseat);
if (seatAvail[crownum-1][cseatnum-1].equals("Open"));
seatAvail[crownum-1][cseatnum-1] = "Occupied";
System.out.println("");
System.out.println("Thanks for you purchase! It has cost you $"+myStage2D[crownum-1][cseatnum-1]+".");
System.out.println("");
printSeats();
}
}
public static void PrintSeats() {
int column = 0;
int row = 0;
while (row<2){
while (column<10){
System.out.println("Row "+(row+1)+", seat " + (column+1)+" costs "
+myStage2D[row][column] +" dollars."
+ "That seat is currently " + seatAvail[row][column]);
column+=1;
}
row+=1;
column = 0;
}
}
}
Re: Need help on making the main class use the module for Theatre reservation program
Well I saw the code and there is a basic mistake that on the if statement, it must read 'PrintSeats();', not 'printSeats();'. Java is case sensitive. Do that abs see if that help you to solve the problem.
Re: Need help on making the main class use the module for Theatre reservation program
That really silly of me but I have now fixed the error and yet I am getting the error in the System.out.println() it shows that myStage2D and seatAvail cannot be resolved. Can you tell me is there any way to make it in an way that those list are global similar as in the python and other languages. Or can you help me on setting up the public constructor as suggested to me by my teacher
Re: Need help on making the main class use the module for Theatre reservation program
Do the following method to make the myStage2D global you have to put its declaration before the main method
Code:
private int[][] myStage2D;
public static void main(String[] args){/*...code here*/}
You don’t need to Assign the room for the array prior to main, just ensure that it is declared prior to the main method to make it global. See if that works
Re: Need help on making the main class use the module for Theatre reservation program
Thanks
Well I have found the way to make the new method in the following code
Code:
import javax.swing.JOptionPane;
public class Theatre2D {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// declares an array of integers
int[][] myStage2D;
// allocates memory for 2*10 integers
myStage2D = new int[2][10];
myStage2D[0][0] = 30;
myStage2D[0][1] = 40;
myStage2D[0][2] = 50;
myStage2D[0][3] = 60;
myStage2D[0][4] = 70;
myStage2D[0][5] = 70;
myStage2D[0][6] = 60;
myStage2D[0][7] = 50;
myStage2D[0][8] = 40;
myStage2D[0][9] = 30;
//Start Second Row
myStage2D[1][0] = 20;
myStage2D[1][1] = 30;
myStage2D[1][2] = 40;
myStage2D[1][3] = 50;
myStage2D[1][4] = 60;
myStage2D[1][5] = 60;
myStage2D[1][6] = 50;
myStage2D[1][7] = 40;
myStage2D[1][8] = 30;
myStage2D[1][9] = 20;
String[][] seatAvail = new String[2][10];
seatAvail[0][0] = "Open";
seatAvail[0][1] = "Open";
seatAvail[0][2] = "Open";
seatAvail[0][3] = "Open";
seatAvail[0][4] = "Open";
seatAvail[0][5] = "Open";
seatAvail[0][6] = "Open";
seatAvail[0][7] = "Open";
seatAvail[0][8] = "Open";
seatAvail[0][9] = "Open";
//Second row
seatAvail[1][0] = "Open";
seatAvail[1][1] = "Open";
seatAvail[1][2] = "Open";
seatAvail[1][3] = "Open";
seatAvail[1][4] = "Open";
seatAvail[1][5] = "Open";
seatAvail[1][6] = "Open";
seatAvail[1][7] = "Open";
seatAvail[1][8] = "Open";
seatAvail[1][9] = "Open";
int column = 0;
int row = 0;
while (row<2){
while (column<10){
System.out.println("Row "+(row+1)+", seat " + (column+1)+" costs "
+myStage2D[row][column] +" dollars."
+ "That seat is currently " + seatAvail[row][column]);
column+=1;
}
row+=1;
column = 0;
}
int bookcounter = 0;
while (bookcounter<20){
try{
String crow = JOptionPane.showInputDialog("From which row do you wish to purchase? (1-2) ");
int crownum = Integer.parseInt(crow);
String cseat = JOptionPane.showInputDialog("From row " + crow+ " which seat do you wish to purchase? (1-10)");
int cseatnum = Integer.parseInt(cseat);
if (seatAvail[crownum-1][cseatnum-1].equals("Open")){
seatAvail[crownum-1][cseatnum-1] = "Occupied";
System.out.println("");
System.out.println("Thanks for you purchase! It has cost you $"+myStage2D[crownum-1][cseatnum-1]+".");
System.out.println("");
bookcounter+=1;
}
else if(seatAvail[crownum-1][cseatnum-1].equals("Occupied")){
System.out.println("");
System.out.println("Unfortunately that seat has been taken. Please chose another seat.");
System.out.println("");
}
else{
System.out.println("");
System.out.println("That was an invalid selection. Please make another.");
System.out.println("");
}
column = 0;
row = 0;
while (row<2){
while (column<10){
System.out.println("Row "+(row+1)+", seat " + (column+1)+" costs "
+myStage2D[row][column] +" dollars."
+ "That seat is currently " + seatAvail[row][column]);
column+=1;
}
row+=1;
column = 0;
}
}catch (NumberFormatException e){
System.out.println("");
System.out.println("Sorry that is not a valid selection");
System.out.println("");
}
}
}
}
But the only issue was I require to accept an input of null in order to close the program
Re: Need help on making the main class use the module for Theatre reservation program
Well you can add the final statement on the if else loop for quit the program if the transaction is undone
The code for that will be else { exit(0); } that it will repair all the accepting a null value. And that will quite automatically the application if the selection is undone
Re: Need help on making the main class use the module for Theatre reservation program
I am really sorry as this is very confusing and I am not certain that I actually understand what you exactly saying . I might place it inside the "else if" statement like you have said however that will close the application in case they put in the value that is not an integer. Do you mean to enter into the catch statement? It would be really nice if you tell me teh line you are talking about?
Re: Need help on making the main class use the module for Theatre reservation program
In case you are looking for the code that quit the application if the user put in the a specific value, after that add an IF statement to look for that value. For example from line String crow = JOptionPane.showInputDialog("From which row do you wish to purchase? (1-2) ");
Code:
String crow = JOptionPane.showInputDialog("From which row do you wish to purchase? (1-2) ");
int crownum = Integer.parseInt(crow);
if (crownum == 0){
return;
}
The return statement will quit the program. Well you can put in whatever you need in the if statement like a good bye message
Re: Need help on making the main class use the module for Theatre reservation program
Really appreciate and now the code is working brilliantly although I would like to divide this into two classes. This is the current code:
Code:
import javax.swing.JOptionPane; //imports the library required to use GUI
public class Theatre2D { //Start of class
/**
* @param args
*/
public static void main(String[] args) { //Start of method
// TODO Auto-generated method stub
int[][] myStage2D; // declares an array of integers
myStage2D = new int[2][10];// allocates memory for 2*10 integers
myStage2D[0][0] = 30; //sets a data value to the array position
myStage2D[0][1] = 40; //sets a data value to the array position
myStage2D[0][2] = 50; //sets a data value to the array position
myStage2D[0][3] = 60; //sets a data value to the array position
myStage2D[0][4] = 70; //sets a data value to the array position
myStage2D[0][5] = 70; //sets a data value to the array position
myStage2D[0][6] = 60; //sets a data value to the array position
myStage2D[0][7] = 50; //sets a data value to the array position
myStage2D[0][8] = 40; //sets a data value to the array position
myStage2D[0][9] = 30; //sets a data value to the array position
myStage2D[1][0] = 20; //sets a data value to the array position
myStage2D[1][1] = 30; //sets a data value to the array position
myStage2D[1][2] = 40; //sets a data value to the array position
myStage2D[1][3] = 50; //sets a data value to the array position
myStage2D[1][4] = 60; //sets a data value to the array position
myStage2D[1][5] = 60; //sets a data value to the array position
myStage2D[1][6] = 50; //sets a data value to the array position
myStage2D[1][7] = 40; //sets a data value to the array position
myStage2D[1][8] = 30; //sets a data value to the array position
myStage2D[1][9] = 20; //sets a data value to the array position
//creates the array of integers and allocates the memory for 2*10 integers
String[][] seatAvail = new String[2][10];
seatAvail[0][0] = "Open"; //sets the initial data value to the array position
seatAvail[0][1] = "Open"; //sets the initial data value to the array position
seatAvail[0][2] = "Open"; //sets the initial data value to the array position
seatAvail[0][3] = "Open"; //sets the initial data value to the array position
seatAvail[0][4] = "Open"; //sets the initial data value to the array position
seatAvail[0][5] = "Open"; //sets the initial data value to the array position
seatAvail[0][6] = "Open"; //sets the initial data value to the array position
seatAvail[0][7] = "Open"; //sets the initial data value to the array position
seatAvail[0][8] = "Open"; //sets the initial data value to the array position
seatAvail[0][9] = "Open"; //sets the initial data value to the array position
seatAvail[1][0] = "Open"; //sets the initial data value to the array position
seatAvail[1][1] = "Open"; //sets the initial data value to the array position
seatAvail[1][2] = "Open"; //sets the initial data value to the array position
seatAvail[1][3] = "Open"; //sets the initial data value to the array position
seatAvail[1][4] = "Open"; //sets the initial data value to the array position
seatAvail[1][5] = "Open"; //sets the initial data value to the array position
seatAvail[1][6] = "Open"; //sets the initial data value to the array position
seatAvail[1][7] = "Open"; //sets the initial data value to the array position
seatAvail[1][8] = "Open"; //sets the initial data value to the array position
seatAvail[1][9] = "Open"; //sets the initial data value to the array position
int column = 0; //creates the column counter and sets it to a value of 0
int row = 0; //creates the row counter and sets it to a value of 0
while (row<2){ //loops until it reaches the third row(which non-existent) so it quits
while (column<10){ //loops until it reaches the 11th column (which non-existent) so it quits
//Prints output to the user allowing the user to see which seats are available and the cost of each
System.out.println("Row "+(row+1)+", seat " + (column+1)+" costs "
+myStage2D[row][column] +" dollars."
+ "That seat is currently " + seatAvail[row][column]);
column+=1; //Adds to the column counter
} //end the while column loop
row+=1; //adds to the row counter
column = 0; //resets column to a value of 0 to restart the loop for the second row
} //end the while row loop
int bookcounter = 0; //creates the bookcounter variable to count the number of seats that have been booked
while (bookcounter<20){ //loops while the number of seats booked is less than 20
try{ //trys to prompt the user for input
String crow = JOptionPane.showInputDialog("From which row do you wish to purchase? (1-2) <0 to quit>");
int crownum = Integer.parseInt(crow); //changes input to an integer
if (crownum==0){//breaks if 0 is inputed
System.out.println("Goodbye!"); //prints output
break;//breaks the loop
}else{//continues if a different number is inputed
String cseat = JOptionPane.showInputDialog("From row " + crow+ " which seat do you wish to purchase? (1-10) <0 to quit>");
int cseatnum = Integer.parseInt(cseat);//changes the input into an integer
if(cseatnum==0){//breaks if 0 is inputed
System.out.println("Goodbye!"); //prints output
break;//breaks the loop
}else{//continues with the operations if the user does not wish to quit
if (seatAvail[crownum-1][cseatnum-1].equals("Open")){//if the seat is open
seatAvail[crownum-1][cseatnum-1] = "Occupied";//changes the value to occupied
System.out.println(""); //prints a blank line
System.out.println("Thanks for you purchase! It has cost you $"+myStage2D[crownum-1][cseatnum-1]+".");//provides feedback
System.out.println(""); //prints a blank line
bookcounter+=1;//adds to the number of seats booked
}//end if statement
else if(seatAvail[crownum-1][cseatnum-1].equals("Occupied")){//if the seat has already been booked
System.out.println(""); //prints a blank line
System.out.println("Unfortunately that seat has been taken. Please chose another seat.");//provides feedback
System.out.println(""); //prints a blank line
}//end else
else{ //if the user inputs a non integer
System.out.println(""); //prints a blank line
System.out.println("That was an invalid selection. Please make another.");//provides feedback
System.out.println(""); //prints a blank line
}//end else
column = 0; //sets column to a value of 0
row = 0; //sets row to a value of 0
while (row<2){ //loops until it reaches the third row(which non-existent) so it quits
while (column<10){ //loops until it reaches the 11th column (which non-existent) so it quits
//Prints output to the user allowing the user to see which seats are available and the cost of each
System.out.println("Row "+(row+1)+", seat " + (column+1)+" costs "
+myStage2D[row][column] +" dollars."
+ "That seat is currently " + seatAvail[row][column]);
column+=1; //Adds to the column counter
} //end the while column loop
row+=1; //adds to the row counter
column = 0; //resets column to a value of 0 to restart the loop for the second row
} //end the while row loop
}//end else from a non zero cseatnum
}//end else from a non zero crownum
}catch (NumberFormatException e){//catches a non integer value and ends the try
System.out.println(""); //prints a blank line
System.out.println("Sorry that is not a valid selection");//provides feedback
System.out.println(""); //prints a blank line
}//end catch
}//end book counter while loop
}//end method
}//end class
Can you help me on separate the section that I have enclosed by astrixes into its own class that might be called Initialize. I don’t know where to start and if someone can hle me on how to access array data among classes (read and write) that would good
Thanks
Re: Need help on making the main class use the module for Theatre reservation program
That actually doesn’t make sense for split this into two classes like that. , however two (or more) technique would surely facilitate create the code simpler to understand. Like You can make two arrays in the main method, then pass them as parameters to an "initialise" method that fills them with values.