0

I have this problem when i call the computePayroll() in the userMenu() it just keep showing the user menu instead of compute payroll, do you have any idea how can i fix this? i tried restarting my ide but still the ouput are same.

import java.sql.*;
import java.util.Scanner;

public class PayrollDatabase {
    static final String DB_URL = "jdbc:sqlserver://DESKTOP-E68IH5A\\SQLEXPRESS; Database = PayrollDatabaseGUI; IntegratedSecurity = true; Encrypt = false";

    static Scanner scanner = new Scanner(System.in);
    static Connection conn;
    static PreparedStatement stmt;
    static ResultSet rs;

    static int userId;
    static boolean isAdmin;
    
    public static void main(String[] args) {
        try {
            conn = DriverManager.getConnection(DB_URL);
            System.out.println("Database connected successfully.");

            System.out.println("Select an option:");
            System.out.println("1. Login");
            System.out.println("2. Forgot Password");
            System.out.print("Enter choice: ");
            int choice = scanner.nextInt();
            scanner.nextLine();

            switch (choice) {
                case 1:
                    login();
                    
                    if (isAdmin) {
                        adminMenu();
                    } else {
                        userMenu();
                    }
                    
                    break;
                case 2:
                    forgotPassword();
                    break;
                default:
                    System.out.println("Invalid choice.");
                    break;
            }

            conn.close();
            scanner.close();
        } catch (SQLException e) {
            System.out.println("Database connection failed.");
            e.printStackTrace();
        }
    }


    // Login method
    static void login() {
        int attempts = 0;
        while (true) {
            System.out.print("Enter username: ");
            String username = scanner.nextLine();
            System.out.print("Enter password: ");
            String password = scanner.nextLine();

            try {
                stmt = conn.prepareStatement("SELECT * FROM userID WHERE username = ? AND password = ?");
                stmt.setString(1, username);
                stmt.setString(2, password);
                rs = stmt.executeQuery();

                if (rs.next()) {
                    userId = rs.getInt("user_id");
                    isAdmin = rs.getBoolean("is_admin");

                    stmt = conn.prepareStatement("INSERT INTO login_history (user_id, login_time) VALUES (?, GETDATE())");
                    stmt.setInt(1, userId);
                    stmt.executeUpdate();

                    System.out.println("Login successful.");
                    break;
                } else {
                    attempts++;
                    System.out.println("Invalid username or password. Attempts left: " + (3 - attempts));
                    if (attempts >= 3) {
                        System.out.println("You have been blocked. Please contact the administrator.");
                        System.exit(0);
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
 // Forgot password method
    static void forgotPassword() {
        System.out.print("Enter your username: ");
        String username = scanner.nextLine();

        try {
            stmt = conn.prepareStatement("SELECT * FROM userID WHERE username = ?");
            stmt.setString(1, username);
            rs = stmt.executeQuery();

            if (rs.next()) {
                int userId = rs.getInt("user_id");

                // Additional verification steps can be added here, such as security questions

                System.out.print("Enter your new password: ");
                String newPassword = scanner.nextLine();

                // Update the password in the database
                stmt = conn.prepareStatement("UPDATE userID SET password = ? WHERE user_id = ?");
                stmt.setString(1, newPassword);
                stmt.setInt(2, userId);
                stmt.executeUpdate();

                System.out.println("Password reset successful. Your new password has been saved.");
            } else {
                System.out.println("Invalid username. Password reset failed.");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    // Admin menu method
    static void adminMenu() {
        while (true) {
            System.out.println("Admin Menu");
            System.out.println("1. Add new employee");
            System.out.println("2. Logout");
            System.out.print("Enter choice: ");

            int choice = scanner.nextInt();
            scanner.nextLine();

            switch (choice) {
                case 1:
                    addNewEmployee();
                    break;
                case 2:
                    login();
                default:
                    System.out.println("Invalid choice.");
            }
        }
    }

    static void userMenu() {
        
        while (true) {
            System.out.println("User Menu");
            System.out.println("1. Compute payroll");
            System.out.println("2. Logout");
            System.out.print("Enter choice: ");

            String choiceStr = scanner.nextLine();
            int choice = Integer.parseInt(choiceStr);

            switch (choice) {
                case 1:
                    computePayroll();
                    break;
                case 2:
                    return;
                default:
                    System.out.println("Invalid choice.");
            }
        }
    }


    static void computePayroll() {
        try {
            stmt = conn.prepareStatement("SELECT * FROM employee WHERE employee_id = ?");
            stmt.setInt(1, userId);
            rs = stmt.executeQuery();

            if (rs.next()) {
                String employeeName = rs.getString("employee_name");
                double dailyRate = rs.getDouble("daily_rate");

                System.out.print("Enter number of days worked: ");
                int daysWorked = scanner.nextInt();
                scanner.nextLine(); // consume the newline character

                double grossPay = dailyRate * daysWorked;
                double tax = calculateTax(grossPay);
                double totalDeductions = tax + rs.getDouble("sss") + rs.getDouble("pagibig") + rs.getDouble("philhealth");
                double netPay = grossPay - totalDeductions;

                System.out.println("Employee Name: " + employeeName);
                System.out.println("Daily Rate: " + dailyRate);
                System.out.println("Gross Pay: " + grossPay);
                System.out.println("Tax: " + tax);
                System.out.println("Total Deductions: " + totalDeductions);
                System.out.println("Net Pay: " + netPay);
                
                System.out.println("\n1. Go back to menu");
                System.out.print("Enter choice: ");
                int choice = scanner.nextInt();
                scanner.nextLine(); // consume the newline character

                if (choice == 1) {
                    return;
                } else {
                    System.out.println("Invalid choice. Returning to menu.");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    static double calculateTax(double grossPay) {
        if (grossPay <= 30000) {
            return grossPay * 0.05;
        } else if (grossPay <= 50000) {
            return grossPay * 0.07;
        } else {
            return grossPay * 0.1;
        }
    }
    
    static void addNewEmployee() {
        if (!isAdmin) {
            System.out.println("You are not authorized to perform this operation.");
            return;
        }

        System.out.print("Enter employee ID: ");
        int employeeId = scanner.nextInt();
        scanner.nextLine(); // consume the newline character

        System.out.print("Enter employee name: ");
        String employeeName = scanner.nextLine();

        System.out.print("Enter daily rate: ");
        double dailyRate = scanner.nextDouble();
        scanner.nextLine(); // consume the newline character

        System.out.print("Enter SSS deduction: ");
        double sss = scanner.nextDouble();
        scanner.nextLine(); // consume the newline character

        System.out.print("Enter Pag-IBIG deduction: ");
        double pagibig = scanner.nextDouble();
        scanner.nextLine(); // consume the newline character

        System.out.print("Enter PhilHealth deduction: ");
        double philhealth = scanner.nextDouble();
        scanner.nextLine(); // consume the newline character

        System.out.print("Enter username: ");
        String username = scanner.nextLine();

        System.out.print("Enter password: ");
        String password = scanner.nextLine();

        try {
            stmt = conn.prepareStatement("INSERT INTO userID (username, password, is_admin) VALUES (?, ?, 0)");
            stmt.setString(1, username);
            stmt.setString(2, password);
            stmt.executeUpdate();

            stmt = conn.prepareStatement("INSERT INTO employee (employee_id, employee_name, daily_rate, sss, pagibig, philhealth) VALUES (?, ?, ?, ?, ?, ?)");
            stmt.setInt(1, employeeId);
            stmt.setString(2, employeeName);
            stmt.setDouble(3, dailyRate);
            stmt.setDouble(4, sss);
            stmt.setDouble(5, pagibig);
            stmt.setDouble(6, philhealth);
            stmt.executeUpdate();

            System.out.println("Employee added successfully.");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
}



this the ouput:

Database connected successfully.
Select an option:
1. Login
2. Forgot Password
Enter choice: 1
Enter username: jetlee
Enter password: 098765
Login successful.
User Menu
1. Compute payroll
2. Logout
Enter choice: 1
User Menu
1. Compute payroll
2. Logout
Enter choice: 

i've tried changing the computePayroll() name, moving it near the userMenu(), use nextLine() instead of nextInt():

Albina
  • 1,901
  • 3
  • 7
  • 19
Kiid
  • 1

0 Answers0