Reputation: 11
I have included in a Login function for my eDepot system (using Eclipse) but I'm not sure how to do a logout function. I've tried several after researching it but it doesn't seem to work.
This is my code:
package uk.ac.livjm.cms; //Name of package
import java.util.Scanner; //Imports scanner
import java.io.IOException; //Imports IOException
public class Depot { //Name of Program
static Scanner console = new Scanner(System.in);
@SuppressWarnings("unused")
public static void main(String[] args) throws IOException {
String User = "SFoster"; //Username of Manager 1
String Pass = "001"; //Password of Manager 1
String Username = "BSamuel"; //Username of Manager 2
String Password = "002"; //Password of Manager 2
String User1 = "1"; //Username of Driver 1
String Pass1 = "111"; //Password of Driver 1
String Username1 = "2"; //Username of Driver 2
String Password1 = "222"; //Password of Driver 2
String User2 = "3"; //Username of Driver 3
String Pass2 = "333"; //Password of Driver 3
String inputuser; // Input when user is typing their username in
String inputpass; // Input when user is typing their password in
int menu = 0; //Displays menu
int count = 0;
int input;
System.out.println(
"Are you a Manager or a Driver or are you Creating a new account?"
+ "\n"
+ "1. Manager"
+ "\n"
+ "2. Driver"
+ "\n"
+ "3. Creating a new account"); //Displays message
input = console.nextInt();
if (input == 3) {
DepotSystem.Account();
}
if (input == 1) { //Login for Manager
System.out.println("Managers Login"); //Displays message
System.out.println(
"Please Note: You will only be allowed to re-enter your Username and Password three times."); //Displays message
while (count < 3) // 3 menu options
{
System.out.println("Enter your Username:"); //Displays message
inputuser = console.next();
System.out.println("Enter your Password:"); //Displays message
inputpass = console.next();
if (inputuser.equals(User) && inputpass.equals(Pass)
|| (inputuser.equals(Username) && inputpass.equals(Password))) {
System.out.println(
"You have successfully logged in."
+ "\n"
+ "Welcome to the Managers Section!"
+ "\n"
+ "Enter the Number that corresponds to the option you wish to choose:");
System.out.println(
"1. Transfer available depot to another vehicle."
+ "\n"
+ "2. Set up new work schedule."
+ "\n"
+ "3. Search for available driver and vehicle."
+ "\n"
+ "4. Log out.");
menu = console.nextInt();
if (menu == 1) {
System.out.println("Transfer available depot to another vehicle.");
Transfer.transfer();
count = 0;
break;
} else if (menu == 2) {
System.out.println("Set up new work schedule.");
WorkSchedule WorkSchedule = new WorkSchedule();
uk.ac.livjm.cms.WorkSchedule.work();
count = 0;
break;
} else if (menu == 3) {
System.out.println("Search for available driver and vehicle.");
Vehicle Vehicle = new Vehicle();
uk.ac.livjm.cms.Vehicle.vehi();
count = 0;
break;
}
if (menu == 4) {
System.out.println("Log out.");
count = 0;
break;
} else {
System.out.println(menu + ": Invalid option.");
count++;
}
} else {
System.out.println("Incorrect Username or Password");
count++;
}
}
}
if (input == 2) {
System.out.println("Drivers Login");
System.out.println(
"Please Note: You will only be allowed to re-enter your Username and Password three times.");
while (count < 3) {
System.out.println("Enter your Username: ");
inputuser = console.next();
System.out.println("Enter your Password: ");
inputpass = console.next();
if (inputuser.equals(User1) && inputpass.equals(Pass1)
|| (inputuser.equals(Username1) && inputpass.equals(Password1))
|| (inputuser.equals(User2) && inputpass.equals(Pass2))) {
System.out.println(
"You have successfully logged in."
+ "\n"
+ "Welcome to the Drivers Section!"
+ "\n"
+ "Enter the Number that corresponds to the option you wish to choose: ");
System.out.println("1. View Work Schedule." + "\n" + "2. Log Out.");
menu = console.nextInt();
if (menu == 1) {
System.out.println("View Work Schedule.");
WorkScheduleDriver.workdriver();
count = 0;
break;
} else if (menu == 2) {
System.out.println("Log out.");
count = 0;
break;
} else {
System.out.println("Incorrect Username or Password");
count++;
}
}
}
}
}
}
Upvotes: 1
Views: 552
Reputation:
Your code is in a mess, this is not the right way to do this, but as per your code you have a while loop that runs while count is < 3 and in the logout section rather than setting count to zero , set it to 3 or any other number so that it breaks from the while loop, that would simulate logout. that is :-
else if (menu == 2)
{
System.out.println("Log out.");
count = 3; // set to 3
}
more over you have a lot of unneccessary break statements in your code such as count = 0; break;
, you can either set again count to 3 or use break alone instead of count = 0
which is unnecessary there, and also you could create a seperate login method and compare with usrename and password there there by reducing a few lines of code.
Upvotes: 1