OneCompiler

COIT20245 Introduction to Programming (Term 3 2023) Assessment One – Java Console Program

1709

import java.util.Scanner;

/**

  • The main class for the Employee Wage Recording System.
    */
    public class EmployeeWageRecordingSystem {

    public static void main(String[] args) {
    System.out.println("Welcome to m2ES Employee Wage Recording System");
    System.out.println("My ID is z65112233zy, 6 is the highest digit");
    System.out.println("6 employees' details will be captured below");

     Scanner scanner = new Scanner(System.in);
    
     // Calculate N based on the highest digit in the student ID
     int N = calculateN("z65112233zy");
    
     double totalSalary = 0;
     double minSalary = Double.MAX_VALUE;
     double maxSalary = Double.MIN_VALUE;
     String minSalaryEmployee = "";
     String maxSalaryEmployee = "";
    
     int lessThan10 = 0;
     int between10And20 = 0;
     int above20 = 0;
    
     // Create StringBuilder to accumulate dataset
     StringBuilder datasetBuilder = new StringBuilder();
    
     // Print header for the dataset
     datasetBuilder.append("Employee Name        Skill Level    Hours Worked    Over Time (OT) Applicable\n");
     datasetBuilder.append("------------------------------------------------------------\n");
    
     // Collect and process employee data
     for (int i = 0; i < N; i++) {
         Wage employee = new Wage();
    
         // Get employee name
         System.out.println("Enter the name of employee " + (i + 1) + ":");
         String enteredName = readEmployeeName(scanner);
         employee.setName(enteredName);
    
         // Get skill level
         System.out.print("Enter the skill level (1, 2, 3) of the employee, " + employee.getName() + ": ");
         int skillLevel = readSkillLevel(scanner);
         employee.setSkillLevel(skillLevel);
    
         // Get hours worked
         System.out.print("Enter the hours (integer) worked by the employee, " + employee.getName() + ": ");
         int hoursWorked = readHoursWorked(scanner);
         employee.setHoursWorked(hoursWorked);
    
         double wage = employee.calculateWage();
         System.out.printf("The wage of the employee, %s <Level %d> for %d hours is $%.2f%n",
                 employee.getName(), employee.getSkillLevel(), employee.getHoursWorked(), wage);
    
         // Update statistics
         if (wage < minSalary) {
             minSalary = wage;
             minSalaryEmployee = employee.getName();
         }
         if (wage > maxSalary) {
             maxSalary = wage;
             maxSalaryEmployee = employee.getName();
         }
    
         totalSalary += wage;
    
         if (employee.getHoursWorked() < 10) {
             lessThan10++;
         } else if (employee.getHoursWorked() <= 20) {
             between10And20++;
         } else {
             above20++;
         }
    
         // Append individual employee data to StringBuilder
         System.out.println("\nBelow is the Data Set");
         datasetBuilder.append(String.format("%-20s%-15d%-15d%-25s%n", employee.getName(), employee.getSkillLevel(),
                 employee.getHoursWorked(), (employee.getHoursWorked() > Wage.OVERTIME_THRESHOLD ? "Yes!" : "No!")));
     }
    
     double averageSalary = totalSalary / N;
    
     // Print the accumulated dataset
     System.out.println(datasetBuilder.toString());
    
     // Print statistical information and bar chart
     System.out.println("\nStatistical Information & Bar Chart");
     System.out.println("-------------------------------------------------");
     System.out.printf("Minimum of the salaries: $%.2f  (earned by)  %s%n", minSalary, minSalaryEmployee);
     System.out.printf("Maximum of the salaries: $%.2f  (earned by)  %s%n", maxSalary, maxSalaryEmployee);
     System.out.printf("Average salary: $%.2f%n", averageSalary);
    
     System.out.println("Bar chart of the length of worked hours:");
     System.out.printf("Number of employees who worked less than 10 hours: %d%n", lessThan10);
     System.out.printf("Number of employees who worked 10-20 hours: %d%n", between10And20);
     System.out.printf("Number of employees who worked hours above 20: %d%n", above20);
    
     scanner.close();
    
     System.out.println("\nThank you for using the Employee Wage Recording System");
     System.out.println("Program written by z65112233zy");
    

    }

    private static int calculateN(String studentId) {
    int maxDigit = 0;
    for (char digitChar : studentId.toCharArray()) {
    if (Character.isDigit(digitChar)) {
    int digit = Character.getNumericValue(digitChar);
    maxDigit = Math.max(maxDigit, digit);
    }
    }
    return Math.max(3, maxDigit);
    }

    private static String readEmployeeName(Scanner scanner) {
    String name;
    while (true) {
    name = scanner.nextLine();
    if (!name.isEmpty()) {
    break;
    } else {
    System.out.println("ERROR! Employee Name Can't be Blank");
    System.out.print("Enter the name of the employee: ");
    }
    }
    return name;
    }

    private static int readSkillLevel(Scanner scanner) {
    while (true) {
    if (scanner.hasNextInt()) {
    int skillLevel = scanner.nextInt();
    if (skillLevel >= 1 && skillLevel <= 3) {
    scanner.nextLine(); // consume the newline character
    return skillLevel;
    } else {
    System.out.printf("ERROR! Skill level should not be %d%n", skillLevel);
    }
    } else {
    System.out.println("ERROR! Skill level should be an integer. Please enter a valid skill level (1, 2, 3).");
    scanner.next(); // consume invalid input
    }
    System.out.print("Enter the skill level (1, 2, 3) of the employee: ");
    }
    }

    private static int readHoursWorked(Scanner scanner) {
    while (true) {
    if (scanner.hasNextInt()) {
    int hoursWorked = scanner.nextInt();
    if (hoursWorked >= 0) {
    scanner.nextLine(); // consume the newline character
    return hoursWorked;
    } else {
    System.out.printf("ERROR! Hours worked should not be %d%n", hoursWorked);
    }
    } else {
    System.out.println("ERROR! Hours worked should be an integer. Please enter a valid number (0 or greater).");
    scanner.next(); // consume invalid input
    }
    System.out.print("Enter the hours (integer) worked: ");
    }
    }
    }

YOU CAN BE SEPERATE THESE CODE INTO TWO FILES AND RUN

/**

  • Represents an employee's wage information.
    */
    class Wage {

    // Constants
    public static final int LEVEL_1_RATE = 50;
    public static final int LEVEL_2_RATE = 70;
    public static final int LEVEL_3_RATE = 100;
    public static final int OVERTIME_THRESHOLD = 40;

    // Variables
    private String name;
    private int skillLevel;
    private int hoursWorked;

    // Default constructor
    public Wage() {
    this.name = "";
    this.skillLevel = 1; // Default to valid level
    this.hoursWorked = 0;
    }

    /**

    • Calculates the wage for the employee based on skill level and hours worked.

    • @return The calculated wage.
      */
      public double calculateWage() {
      double hourlyRate;

      switch (skillLevel) {
      case 1:
      hourlyRate = LEVEL_1_RATE;
      break;
      case 2:
      hourlyRate = LEVEL_2_RATE;
      break;
      case 3:
      hourlyRate = LEVEL_3_RATE;
      break;
      default:
      // Handle invalid skill level gracefully
      throw new IllegalArgumentException("Invalid skill level: " + skillLevel);
      }

      double regularPay = Math.min(hoursWorked, OVERTIME_THRESHOLD) * hourlyRate;
      double overtimePay = Math.max(0, hoursWorked - OVERTIME_THRESHOLD) * (hourlyRate * 1.5);

      return regularPay + overtimePay;
      }

    /**

    • Gets the name of the employee.
    • @return The name of the employee.
      */
      public String getName() {
      return name;
      }

    /**

    • Sets the name of the employee.
    • @param name The name of the employee.
      */
      public void setName(String name) {
      this.name = name;
      }

    /**

    • Gets the skill level of the employee.
    • @return The skill level of the employee.
      */
      public int getSkillLevel() {
      return skillLevel;
      }

    /**

    • Sets the skill level of the employee.
    • @param skillLevel The skill level of the employee.
      */
      public void setSkillLevel(int skillLevel) {
      this.skillLevel = skillLevel;
      }

    /**

    • Gets the hours worked by the employee.
    • @return The hours worked by the employee.
      */
      public int getHoursWorked() {
      return hoursWorked;
      }

    /**

    • Sets the hours worked by the employee.
    • @param hoursWorked The hours worked by the employee.
      */
      public void setHoursWorked(int hoursWorked) {
      this.hoursWorked = hoursWorked;
      }
      }