Yes, the approach you explained above will work, just be careful about whether you are checking for true or false.
kramerd 29 Posting Pro in Training
Tankadin 0 Junior Poster
Ugh that is what always seems to hang me up is the boolean because sometimes you can end up with conditions that are double negatives and such and then BAM instant confusion right there.
Tankadin 0 Junior Poster
So is this correct?
And how do I get it to tell the user to take another flight if ALL of the Classes are full?
import java.util.*;
public class airlineReservation
{
public static final int MAX_COLUMN = 4;
public static final int MAX_ROW = 20;
public static void main(String[] args)
{
int[][] totalSeats = new int[MAX_ROW][MAX_COLUMN];
Scanner input = new Scanner(System.in);
displayMenu();
int menuChoice;
menuChoice = input.nextInt();
while (menuChoice < 6)
{
switch(menuChoice)
{
case 1:
System.out.println("You have Chosen First Class!");
if (isFirstClassFull(totalSeats))
{
makeFirstReservation(totalSeats,input);
}
else
{
System.out.println("This Class is Full Please Choose Another Class");
}
displayMenu();
menuChoice = input.nextInt();
break;
case 2:
System.out.println("You have Chosen Business Class!");
if (isBusinessClassFull(totalSeats))
{
makeBusinessReservation(totalSeats,input);
}
else
{
System.out.println("Business Class is Full Please Choose Another Class");
}
displayMenu();
menuChoice = input.nextInt();
break;
case 3:
System.out.println("You have Chosen Economy Class!");
if (isEconomyClassFull(totalSeats))
{
makeEconomyReservation(totalSeats,input);
}
else
{
System.out.println("Economy Class is Full Please Choose Another Class");
}
displayMenu();
menuChoice = input.nextInt();
break;
case 4:
System.out.println("You have Chosen to Display All Available Seating!");
displayArray(totalSeats);
displayMenu();
menuChoice = input.nextInt();
break;
case 5:
System.out.println("You have Chosen to Quit the Program!");
break;
}
}
}
public static void makeFirstReservation(int[][] totalSeats, Scanner input)
{
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 4; col++)
{
if (totalSeats[row][col] == 0)
{
totalSeats[row][col] = 1;
System.out.println("You have been assigned a First class seat " + col + " in row " + row);
break;
}
}
}
}
public static void makeBusinessReservation(int[][] totalSeats, Scanner input)
{
for (int row = 3; row < 7; row++)
{
for (int col = 0; col < 4; col++)
{
if (totalSeats[row][col] == 0)
{
totalSeats[row][col] = 1;
System.out.println("You have been assigned a Business class seat " + col + " in row " + row);
break;
}
}
}
}
public static void makeEconomyReservation(int[][] totalSeats, Scanner input)
{
for (int row = 7; row < 20; row++)
{
for (int col = 0; col < 4; col++)
{
if (totalSeats[row][col] == 0)
{
totalSeats[row][col] = 1;
System.out.println("You have been assigned an Economy class seat " + col + " in row" + row);
break;
}
}
}
}
public static boolean isFirstClassFull(int[][] totalSeats)
{
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 4; col++)
{
if (totalSeats[row][col] == 0)
{
totalSeats[row][col] = 1;
return true;
}
}
}
return false;
}
public static boolean isBusinessClassFull(int[][] totalSeats)
{
for (int row = 4; row < 7; row++)
{
for (int col = 0; col < 4; col++)
{
if (totalSeats[row][col] == 0)
{
totalSeats[row][col] = 1;
return true;
}
}
}
return false;
}
public static boolean isEconomyClassFull(int[][] totalSeats)
{
for (int row = 8; row < 20; row++)
{
for (int col = 0; col < 4; col++)
{
if (totalSeats[row][col] == 0)
{
totalSeats[row][col] = 1;
return true;
}
}
}
return false;
}
public static void displayArray(int[][] totalSeats)
{
for (int row = 0; row < totalSeats.length; row++)
{
for (int col = 0; col < totalSeats[row].length; col++)
{
System.out.print(totalSeats[row][col]);
}
System.out.println();
}
}
public static void displayMenu()
{
System.out.println("Choose and Option 1-5!");
System.out.println("1. First Class");
System.out.println("2. Business Class");
System.out.println("3. Economy Class");
System.out.println("4. Display All Available Seats!");
System.out.println("5. Quit!");
}
}
apines 116 Practically a Master Poster Featured Poster
One option is to keep a boolean array with a cell for each class, such that if a class is full, the representing cell in the array will become true. If all the array is true - tell the user that everything is full. Every time the user asks for a reservation, if the cell is true you don't need to check via the proper method if the class is full. If the cell is false - you need to run the method to make sure that it is still indeed false, and if all the array is true - game over, no seats.
Tankadin 0 Junior Poster
You totally lost me on that explanation LOL
But there is something going wrong with the program and you will be able to see it from this output:
> run airlineReservation
Choose and Option 1-5!
1. First Class
2. Business Class
3. Economy Class
4. Display All Available Seats!
5. Quit!
[DrJava Input Box]
You have Chosen First Class!
You have been assigned a First class seat 1 in row 0
You have been assigned a First class seat 0 in row 1
You have been assigned a First class seat 0 in row 2
Choose and Option 1-5!
1. First Class
2. Business Class
3. Economy Class
4. Display All Available Seats!
5. Quit!
[DrJava Input Box]
IT seems to be taking up more than one seat at a time or something....weird.
Tankadin 0 Junior Poster
Wow correction......something is HORRIBLY wrong with this program....I chose one of each option in my menu and it gives me this:
> run airlineReservation
Choose and Option 1-5!
1. First Class
2. Business Class
3. Economy Class
4. Display All Available Seats!
5. Quit!
[DrJava Input Box]
You have Chosen First Class!
You have been assigned a First class seat 1 in row 0
You have been assigned a First class seat 0 in row 1
You have been assigned a First class seat 0 in row 2
Choose and Option 1-5!
1. First Class
2. Business Class
3. Economy Class
4. Display All Available Seats!
5. Quit!
[DrJava Input Box]
You have Chosen Business Class!
You have been assigned a Business class seat 0 in row 3
You have been assigned a Business class seat 1 in row 4
You have been assigned a Business class seat 0 in row 5
You have been assigned a Business class seat 0 in row 6
Choose and Option 1-5!
1. First Class
2. Business Class
3. Economy Class
4. Display All Available Seats!
5. Quit!
[DrJava Input Box]
You have Chosen Economy Class!
You have been assigned an Economy class seat 0 in row7
You have been assigned an Economy class seat 1 in row8
You have been assigned an Economy class seat 0 in row9
You have been assigned an Economy class seat 0 in row10
You have been assigned an Economy class seat 0 in row11
You have been assigned an Economy class seat 0 in row12
You have been assigned an Economy class seat 0 in row13
You have been assigned an Economy class seat 0 in row14
You have been assigned an Economy class seat 0 in row15
You have been assigned an Economy class seat 0 in row16
You have been assigned an Economy class seat 0 in row17
You have been assigned an Economy class seat 0 in row18
You have been assigned an Economy class seat 0 in row19
Choose and Option 1-5!
1. First Class
2. Business Class
3. Economy Class
4. Display All Available Seats!
5. Quit!
[DrJava Input Box]
You have Chosen to Display All Available Seating!
1100
1000
1000
1000
1100
1000
1000
1000
1100
1000
1000
1000
1000
1000
1000
1000
1000
1000
1000
1000
Choose and Option 1-5!
1. First Class
2. Business Class
3. Economy Class
4. Display All Available Seats!
5. Quit!
[DrJava Input Box]
apines 116 Practically a Master Poster Featured Poster
You need to return true in your make reservation methods as you did in the is class full methods - by using break instead of return you are breaking the inner loop only.
Edited by apines because: n/a
Tankadin 0 Junior Poster
my make reservation methods are not boolean and are not supposed to be boolean so how do I do that?
I can't have true inside of a non boolean method right? and also shouldn't there be another way to do that besides using true?
kramerd 29 Posting Pro in Training
You can simply return without returning anything.
Also, your is full methods are wrong. You are assigning the value 1 to the array - you should only be doing that in your make reservation methods. Also the true & false return values are the opposite of what they should be.
Edited by kramerd because: n/a
apines 116 Practically a Master Poster Featured Poster
You can just type return;
- it will exit the method;
Edited by apines because: n/a
Tankadin 0 Junior Poster
so should I replace the break; in my make methods to return instead? or should I add it after break? I would think the first thing because break and return inside the same method doesn't make sense to me.
Tankadin 0 Junior Poster
And for my boolean methods.....huh? I thought one of you told me to do it this way or someone did.....I am pretty sure anyway. ugh
apines 116 Practically a Master Poster Featured Poster
return;
simply exits the entire method, you don't need break;
Edited by apines because: n/a
Tankadin 0 Junior Poster
I changed the returns but I am still having weird output....it skips the 2nd seat in every row it looks like unless I am reading the output wrong.
> run airlineReservation
Choose and Option 1-5!
1. First Class
2. Business Class
3. Economy Class
4. Display All Available Seats!
5. Quit!
[DrJava Input Box]
You have Chosen First Class!
You have been assigned a First class seat 1 in row 0
Choose and Option 1-5!
1. First Class
2. Business Class
3. Economy Class
4. Display All Available Seats!
5. Quit!
[DrJava Input Box]
You have Chosen First Class!
You have been assigned a First class seat 3 in row 0
Choose and Option 1-5!
1. First Class
2. Business Class
3. Economy Class
4. Display All Available Seats!
5. Quit!
[DrJava Input Box]
You have Chosen First Class!
You have been assigned a First class seat 1 in row 1
Choose and Option 1-5!
1. First Class
2. Business Class
3. Economy Class
4. Display All Available Seats!
5. Quit!
[DrJava Input Box]
You have Chosen First Class!
You have been assigned a First class seat 3 in row 1
Choose and Option 1-5!
1. First Class
2. Business Class
3. Economy Class
4. Display All Available Seats!
5. Quit!
[DrJava Input Box]
You have Chosen First Class!
You have been assigned a First class seat 1 in row 2
Choose and Option 1-5!
1. First Class
2. Business Class
3. Economy Class
4. Display All Available Seats!
5. Quit!
kramerd 29 Posting Pro in Training
That is because your is full methods are assigning a 1 to the totalSeats array when they should not.
Tankadin 0 Junior Poster
i thought u had to attach a 1 to totalseats to show that it was taken?
apines 116 Practically a Master Poster Featured Poster
Yes - when you make the reservation, not when you check whether it is full or not.
Tankadin 0 Junior Poster
so i need to change the boolean method then?
apines 116 Practically a Master Poster Featured Poster
You want your boolean method to simply check whether the class is full or not, and not change any of the sittings (meaning not to change the values of the array).
Tankadin 0 Junior Poster
so how do i fix this? change the 1 to a 0?
apines 116 Practically a Master Poster Featured Poster
You don't want your boolean method to change any of the sittings. How changing the sitting to 0 instead of 1 is not changing the sittings?
Tankadin 0 Junior Poster
so get rid of that line that sets it to 1 completely? is that able to be done and continue to allow the method to function properly?
apines 116 Practically a Master Poster Featured Poster
You know - the best way is to try it yourself. What makes you think that the method will not function properly afterwards?
kramerd 29 Posting Pro in Training
Towards the bottom of page 3 in this thread I gave you the code exactly how it should look. I think you don't read our advice very well.
Tankadin 0 Junior Poster
ah don't say that lol I DO I really do I am just insanely stressed about this so I jump around change the code re change the code.....delete the code....add the code...really really sorry I value your opinions and advice greatly.
Tankadin 0 Junior Poster
Ok I did it exactly like you said :
You just took the make reservation code and put it in the is full method. That's not right. If you just want to check whether a section is full, then you shouldn't be assigning anything to 1.
Here's all you need to check if first class is full or not.
Java Syntax (Toggle Plain Text)
1.
public static boolean isFirstclassFull(int[][] totalSeats) {
2.
for (int row = 0; row < 3; row++) {
3.
for (int col = 0; col < 4; col++) {
4.
if (totalSeats[row][col] == 0) {
5.
return false; // you found an empty seat
6.
}
7.
}
8.
}
9.
return true; // no empty seats found
10.
}
as you can see by my now updated code and it tells me all of my classes are full so shouldn't I switch the true and false in each boolean method?
import java.util.*;
public class airlineReservation
{
public static final int MAX_COLUMN = 4;
public static final int MAX_ROW = 20;
public static void main(String[] args)
{
int[][] totalSeats = new int[MAX_ROW][MAX_COLUMN];
Scanner input = new Scanner(System.in);
displayMenu();
int menuChoice;
menuChoice = input.nextInt();
while (menuChoice < 6)
{
switch(menuChoice)
{
case 1:
System.out.println("You have Chosen First Class!");
if (isFirstClassFull(totalSeats))
{
makeFirstReservation(totalSeats,input);
}
else
{
System.out.println("This Class is Full Please Choose Another Class");
}
displayMenu();
menuChoice = input.nextInt();
break;
case 2:
System.out.println("You have Chosen Business Class!");
if (isBusinessClassFull(totalSeats))
{
makeBusinessReservation(totalSeats,input);
}
else
{
System.out.println("Business Class is Full Please Choose Another Class");
}
displayMenu();
menuChoice = input.nextInt();
break;
case 3:
System.out.println("You have Chosen Economy Class!");
if (isEconomyClassFull(totalSeats))
{
makeEconomyReservation(totalSeats,input);
}
else
{
System.out.println("Economy Class is Full Please Choose Another Class");
}
displayMenu();
menuChoice = input.nextInt();
break;
case 4:
System.out.println("You have Chosen to Display All Available Seating!");
displayArray(totalSeats);
displayMenu();
menuChoice = input.nextInt();
break;
case 5:
System.out.println("You have Chosen to Quit the Program!");
break;
}
}
}
public static void makeFirstReservation(int[][] totalSeats, Scanner input)
{
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 4; col++)
{
if (totalSeats[row][col] == 0)
{
totalSeats[row][col] = 1;
System.out.println("You have been assigned a First class seat " + col + " in row " + row);
return;
}
}
}
}
public static void makeBusinessReservation(int[][] totalSeats, Scanner input)
{
for (int row = 3; row < 7; row++)
{
for (int col = 0; col < 4; col++)
{
if (totalSeats[row][col] == 0)
{
totalSeats[row][col] = 1;
System.out.println("You have been assigned a Business class seat " + col + " in row " + row);
return;
}
}
}
}
public static void makeEconomyReservation(int[][] totalSeats, Scanner input)
{
for (int row = 7; row < 20; row++)
{
for (int col = 0; col < 4; col++)
{
if (totalSeats[row][col] == 0)
{
totalSeats[row][col] = 1;
System.out.println("You have been assigned an Economy class seat " + col + " in row" + row);
return;
}
}
}
}
public static boolean isFirstClassFull(int[][] totalSeats)
{
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 4; col++)
{
if (totalSeats[row][col] == 0)
return false;
}
}
return true;
}
public static boolean isBusinessClassFull(int[][] totalSeats)
{
for (int row = 4; row < 7; row++)
{
for (int col = 0; col < 4; col++)
{
if (totalSeats[row][col] == 0)
return false;
}
}
return true;
}
public static boolean isEconomyClassFull(int[][] totalSeats)
{
for (int row = 8; row < 20; row++)
{
for (int col = 0; col < 4; col++)
{
if (totalSeats[row][col] == 0)
return false;
}
}
return true;
}
public static void displayArray(int[][] totalSeats)
{
for (int row = 0; row < totalSeats.length; row++)
{
for (int col = 0; col < totalSeats[row].length; col++)
{
System.out.print(totalSeats[row][col]);
}
System.out.println();
}
}
public static void displayMenu()
{
System.out.println("Choose and Option 1-5!");
System.out.println("1. First Class");
System.out.println("2. Business Class");
System.out.println("3. Economy Class");
System.out.println("4. Display All Available Seats!");
System.out.println("5. Quit!");
}
}
kramerd 29 Posting Pro in Training
Think about this: on lines 23-30 you are saying "if first class is full, then make a reservation. Otherwise tell the user there are no more seats in first class." Does this make sense to you?
I believe that you are changing the code without really thinking about what you are doing. You need to engage your brain more. :)
Tankadin 0 Junior Poster
it's that double negative stuff again! LOL
ugh sorry again.....I can't stand double negatives it always hangs me up worse than the whole parameter issue and argument issue I had before.
Tankadin 0 Junior Poster
Ok changed the code but now it gives me seats for row 0 1 2 and 3....before it did it as 1 2 3 I am sure of that.....when I choose first class i mean....I did not change any numbers.....only thing I did was change my if/else in the menu system. At least I am pretty sure that I didn't.
import java.util.*;
public class airlineReservation
{
public static final int MAX_COLUMN = 4;
public static final int MAX_ROW = 20;
public static void main(String[] args)
{
int[][] totalSeats = new int[MAX_ROW][MAX_COLUMN];
Scanner input = new Scanner(System.in);
displayMenu();
int menuChoice;
menuChoice = input.nextInt();
while (menuChoice < 6)
{
switch(menuChoice)
{
case 1:
System.out.println("You have Chosen First Class!");
if (!isFirstClassFull(totalSeats))
{
makeFirstReservation(totalSeats,input);
}
else
{
System.out.println("This Class is Full Please Choose Another Class");
}
displayMenu();
menuChoice = input.nextInt();
break;
case 2:
System.out.println("You have Chosen Business Class!");
if (!isBusinessClassFull(totalSeats))
{
makeBusinessReservation(totalSeats,input);
}
else
{
System.out.println("Business Class is Full Please Choose Another Class");
}
displayMenu();
menuChoice = input.nextInt();
break;
case 3:
System.out.println("You have Chosen Economy Class!");
if (!isEconomyClassFull(totalSeats))
{
makeEconomyReservation(totalSeats,input);
}
else
{
System.out.println("Economy Class is Full Please Choose Another Class");
}
displayMenu();
menuChoice = input.nextInt();
break;
case 4:
System.out.println("You have Chosen to Display All Available Seating!");
displayArray(totalSeats);
displayMenu();
menuChoice = input.nextInt();
break;
case 5:
System.out.println("You have Chosen to Quit the Program!");
break;
}
}
}
public static void makeFirstReservation(int[][] totalSeats, Scanner input)
{
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 4; col++)
{
if (totalSeats[row][col] == 0)
{
totalSeats[row][col] = 1;
System.out.println("You have been assigned a First class seat " + col + " in row " + row);
return;
}
}
}
}
public static void makeBusinessReservation(int[][] totalSeats, Scanner input)
{
for (int row = 3; row < 7; row++)
{
for (int col = 0; col < 4; col++)
{
if (totalSeats[row][col] == 0)
{
totalSeats[row][col] = 1;
System.out.println("You have been assigned a Business class seat " + col + " in row " + row);
return;
}
}
}
}
public static void makeEconomyReservation(int[][] totalSeats, Scanner input)
{
for (int row = 7; row < 20; row++)
{
for (int col = 0; col < 4; col++)
{
if (totalSeats[row][col] == 0)
{
totalSeats[row][col] = 1;
System.out.println("You have been assigned an Economy class seat " + col + " in row" + row);
return;
}
}
}
}
public static boolean isFirstClassFull(int[][] totalSeats)
{
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 4; col++)
{
if (totalSeats[row][col] == 0)
return false;
}
}
return true;
}
public static boolean isBusinessClassFull(int[][] totalSeats)
{
for (int row = 4; row < 7; row++)
{
for (int col = 0; col < 4; col++)
{
if (totalSeats[row][col] == 0)
return false;
}
}
return true;
}
public static boolean isEconomyClassFull(int[][] totalSeats)
{
for (int row = 8; row < 20; row++)
{
for (int col = 0; col < 4; col++)
{
if (totalSeats[row][col] == 0)
return false;
}
}
return true;
}
public static void displayArray(int[][] totalSeats)
{
for (int row = 0; row < totalSeats.length; row++)
{
for (int col = 0; col < totalSeats[row].length; col++)
{
System.out.print(totalSeats[row][col]);
}
System.out.println();
}
}
public static void displayMenu()
{
System.out.println("Choose and Option 1-5!");
System.out.println("1. First Class");
System.out.println("2. Business Class");
System.out.println("3. Economy Class");
System.out.println("4. Display All Available Seats!");
System.out.println("5. Quit!");
}
}
kramerd 29 Posting Pro in Training
If it gave 1 and 3 before that's because it was incorrectly skipping 0 and 2. It should have always given 0, 1, 2, 3 because arrays in Java start with 0. You can tell your teacher this seating plan was invented by a Java programmer! :)
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.