Ok, so I’m trying to finish our simple project for Data Strcutures but there is always some error that just keep showing up on my program. I have fixed some of them but there is this one that makes me want to give up. Maybe you guys could help me find the solution?
Here is my code (class LogInSystem):
// NMQ
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Vector;
public class LogInSystem {
private static final int MAX_SEATS = 10;
Vector<String> username = new Vector<String>();
ArrayList<String> password = new ArrayList<String>();
int p;
String Username, Password, rUsername, rPassword;
private Scanner input = new Scanner(System.in);
public LogInSystem() {
loginScreen();
}
private void loginScreen() {
boolean done = false;
do {
printMainMenu();
int choice = getMainMenuChoice();
switch (choice) {
case 1: // Log In
logIn();
break;
case 2: // To Register
register();
break;
case 3: // Exit
done = true;
break;
}
} while (!done);
}
//Registration
private void register() {
System.out.println("Input a Username");
Username = input.next();
System.out.println("Input a Password");
Password = input.next();
System.out.println("... Registered!");
}
//Log In
private void logIn() {
p = 0;
System.out.println("Input a Username");
rUsername = input.next();
System.out.println("Input a Password");
rPassword = input.next();
// If the log in is successful it will got to the next menu
if(rUsername.equals(Username)&& rPassword.equals(Password))
{
System.out.println("... Logging In");
boolean done = false;
String[] seats = new String[MAX_SEATS]; // the item list
initializeItems(seats);
// Printing of 2nd menu
do {
printMainMenu2();
int choice = getMainMenuChoice2(); // choice off of the main menu
switch (choice) {
case 1: // Adding Seat
addSeat(seats);
break;
case 2: // Viewing Seat List
viewSeatList(seats);
break;
case 3: // Exit
done = true;
break;
}
} while (!done);
}
else
System.out.println("Invalid log in, please try again.");
}
private int getMainMenuChoice() {
int choice; // choice entered
boolean valid = false; // is choice valid?
do {
System.out.print(">>>>> ");
choice = cineplexError.readInt();
if (1 <= choice && choice <= 4) {
valid = true;
} else {
System.out.println("Invalid choice.");
}
} while (!valid);
return choice;
}
private void printMainMenu() {
System.out.println("\nMain Menu\n");
System.out.println("Press 1 to Log In");
System.out.println("Press 2 to Register");
System.out.println("Press 3 to Exit");
System.out.println();
}
public static void main(String[] args) {
new LogInSystem();
}
int getMainMenuChoice2() {
int choice; // choice entered
boolean valid = false; // is choice valid?
do {
System.out.print(">>>>> ");
choice = cineplexError.readInt();
if (1 <= choice && choice <= 4) {
valid = true;
} else {
System.out.println("Invalid choice.");
}
} while (!valid);
return choice;
}
void printMainMenu2() {
System.out.println("\nMain Menu\n");
System.out.println("Press 1 to Reserve a Seat");
System.out.println("Press 2 to View the Seat List");
System.out.println("Press 3 to Exit Buying");
System.out.println();
}
void initializeItems(String[] seats) {
for (int i = 0; i < seats.length; i++) {
seats[i] = "";
}
}
void addSeat(String[] seats) {
int seatIndex = findEmptySeatList(seats); // index of first empty item list
if (seatIndex == seats.length) {
System.out.println("Seat is already occupied. Sorry.");
}
else {
String seat = getSeatName(); // seat's name
seats[seatIndex] = seat;
System.out.println(seat + " is on seat list #"
+ (seatIndex + 1));
}
}
int findEmptySeatList(String[] seat) {
for (int i = 0; i < seat.length; i++) {
if (isEmpty(seat, i)) {
return i;
}
}
return seat.length;
}
boolean isEmpty(String[] seats, int seatIndex) {
return seats[seatIndex].equals("");
}
String getSeatName() {
System.out.print("Enter the name of reservator: ");
return cineplexError.readString();
}
void viewSeatList(String[] seats) {
System.out.println("\nSeat List\n");
for (int i = 0; i < seats.length; i++) {
System.out.println((i + 1) + ". " + seats[i]);
}
}
}
Here is the other class (cineplexError):
// NMQ
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class cineplexError {
private static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
private static String integerReprompt = "Invalid integer. Try again: ";
private static String doubleReprompt = "Invalid double. Try again: ";
private static String charReprompt = "Invalid character. Try again: ";
public static String readString() {
String s = null;
try {
s = in.readLine();
} catch (IOException ex) {}
return s;
}
public static char readChar() {
char c = 0;
String s = null;
try {
s = in.readLine();
} catch (IOException ex) {}
if (s.length() == 1) {
c = s.charAt(0);
// valid = true;
} else {
System.out.print(charReprompt);
}
return c;
}
public static int readInt() {
int i = 0;
boolean valid = false;
try {
i = Integer.parseInt(in.readLine());
} catch (IOException ex) {
System.out.print(integerReprompt);
}
valid = true;
return i;
}
public static double readDouble() {
double d = 0.0;
boolean valid = true;
try {
d = Double.parseDouble(in.readLine());
} catch (IOException ex) {}
valid = true;
valid = false;
System.out.print(doubleReprompt);
return d;
}
public void pause() {
System.out.print("Press enter to continue...");
try {
in.readLine();
} catch (IOException ex) {}
}
public static void setIntegerReprompt(String prompt) {
integerReprompt = prompt;
}
public void setDoubleReprompt(String prompt) {
doubleReprompt = prompt;
}
public void setCharReprompt(String prompt) {
charReprompt = prompt;
}
}
The bug in the program is that whenever I reserve a seat and log out and log in again the name of the reservator or the name of the one that reserves a seat is being erased when I view the seat list. Should I put a control statement that makes the value stored permanently? Can you help me out?
Thanks.
Every time you call logIn you are re-initializing the list of seats to all empty strings. That initialization should be done in the constructor. So move Line 73 (declaration of String[] seats) to be an instance variable of the class, and move Line 74 to be in the constructor.