I'm trying to create this payroll system which has a login class and a salary-calculator class. I'm trying to use the username string entered by the user in the login class in the salary calculator class to determine the basic salary, but the username string is treated as null in the salary-calculator class hence I'm getting a lot of errors. I'm quite new to java programming so I would highly appreciate your help. Here is my code.
public class loginpage {
Scanner x; // declares scanner
static String name;
static String username;
public void username(){
String filepath = "/home/ogweno/NetBeansProjects/Payroll System/src/payroll/system/employees.txt"; // Declaring the filepath
// Declaring a string for the username
String password; // Declaring a string for the password
String username;
username = JOptionPane.showInputDialog("Please enter your username: ", "username");
password=JOptionPane.showInputDialog("Please enter your Password: ");
VerifyLoginA(username, password, filepath);
}
public void VerifyLoginA(String username, String password, String filepath) {
boolean flag = false; // boolean variable set to false for later use
String tempUsername = ""; // variable used for extracting the username from txt file and storing in this
String tempPassword = ""; // variable used for extracting the password from txt file and storing in this
try {
x = new Scanner(new File(filepath)); //opens the file "/home/ogweno/Students.txt"
x.useDelimiter("[,\n]"); // use of delimiter to specify the seperation of username & password by ","
boolean passed = false; // boolean variable set to false and later use
while (x.hasNext()&& !flag){ // use of while to check if the file has any contents
tempUsername = x.next(); // extracts the username and stores it
tempPassword = x.next();// extracts the password and stores it
if (tempUsername.trim().equals(username.trim()) && tempPassword.trim().equals(password.trim())) {
passed = true; // use of trim to remove any unwanted space in user input or in txt file
break; // boolean passedA becomes true if the username and passwords entered by user match the ones in file
}
}
if(passed) { // if passedA is true then prints Welcome + the username
// System.out.println("Welcome " + username);
//System.out.println(""); // space
JOptionPane.showMessageDialog(null,"Welcome " + username);
boolean check = true;
setName(username);
}
else { // if passedA is false then prints a error message
// System.out.println("********WARNING**Incorrect Username or Password*********");
JOptionPane.showMessageDialog(null,"********WARNING**Incorrect Username or Password*********");
boolean check = false;
System.exit(0); // Terminates program if username and/or password is incorrect
}
}
catch (FileNotFoundException e){ // prints error if there is an error in opening the file
System.out.println("Error");
}
}
public static void setName(String usernamename) {
name=username;
//To change body of generated methods, choose Tools | Templates.
}
public static String getName(){
return name;
}
}
Here's the salary-calculator class
public class Salary_calculator extends loginpage {
public void calculator(){
int HS=10000;
int TV=5000;
int NSSF=2000;
int NHIF=500;
loginpage login=new loginpage();
int grossPay=0;
String post="";
float netSalary;
if (name=="Jane"){
grossPay=20000;
post="Finance Officer";
}
else if (name=="Michael"){
grossPay=30000;
post="IT technician";
}
else if (name=="Josphine"){
grossPay=40000;
post="Marketing Officer";
}
else if (name=="Darshan"){
grossPay=50000;
post="Public Relations Officer";
}
else if (name=="Ketan"){
grossPay=60000;
post="Operations Manager";
}
int salary=grossPay + HS + TV -(NSSF+NHIF);
netSalary=(float) (0.7*salary);
JOptionPane.showMessageDialog(null, name + ": " + post + "\n" + "Your net salary is: " + netSalary + "\n");
}
}