DEV Community

Cover image for Building a Console-Based Maintenance Helpdesk System in Java
Thomas
Thomas

Posted on

Building a Console-Based Maintenance Helpdesk System in Java

Introduction

As part of my Digital and Technology Solution Apprenticeship Course module on Introduction to Programming, I have developed a console-based maintenance helpdesk system using Java. The applications aim is to simulate a simple estates maintenance tracker, allowing users to create and view maintenance requests, while administrators can manage technicians, assign technicians to jobs and update the status of the request.

This blog post will document the development process that I undertook to develop the programme; describing the features of the programme with in-depth description of the code and report on issues i encountered while coding.

Describing the Features

Below are 3 features from my programme where i will provide an in-depth description of the code that i have written linking the practical implementation to my theoretical understanding of the code.

User Input

Maintenance Request Tracker
===========================
1 - Add a request
2 - View a request
3 - Adminstraitor
4 - Exit
Please choose an option 1-4:
a
Invalid input, please choose a number between 1 and 4
Please choose an option 1-4:
Enter fullscreen mode Exit fullscreen mode

User input is a core feature of the maintenance helpdesk programme, as all interactions with the application takes place through the console. Java's Scanner class is used to read inputs from the users, allowing them to navigate menu options, create maintenance requests, and enter reference number when they wish to update or close jobs.

int choice = 0; // Stores the menu option chosen by the user.
boolean validChoice = false; // Used to make sure only valid input are accepted.
while (!validChoice) { // Loop keeps asking until user enter a valid number.
    try { // Try/catch is used because nextInt will crash if user enters letter.
        System.out.println("Please choose an option 1-4:");
        choice = input.nextInt(); // Reads user menu choice as a number.
        input.nextLine(); // Clears scanner buffer.
        validChoice = true; // If a valid choice will leave the loop.
    }
    catch (Exception e) { // If incorrect character entered displays error message.
        System.out.println("Invalid input, please choose a number between 1 and 4");
        input.nextLine(); // Clears scanner buffer.
    }
}
Enter fullscreen mode Exit fullscreen mode

During development, I learnt that Scanner handles different types of inputs when reading different data types. Methods such as nextInt() read only the numerical values entered and leave the newline characters in the input buffer. To make sure this behaviour is handled correctly, I included additional calls to nextLine() to clear the buffer before accepting more text inputs.

To improve the reliability of my code further, I introduced input validation with exceptional handling. This made sure that the programme continues to run smoothly even when the user inputs unexpected characters/ or numbers.

while (!validRef) {
    try {
        System.out.println("Please enter your request reference number:");
        refNumber = input.nextInt();
        input.nextLine();
        validRef = true;
    }
    catch (Exception e) {
        System.out.println("Invalid input, please enter the correct number");
        input.nextLine();
    }
}

Enter fullscreen mode Exit fullscreen mode

This approach reflects the principles of defensive programming, where the programme is designed to handle incorrect user inputs. By validating input and displaying error messages to instruct help users for correct entries therefore the application becomes more robust and user friendly with reduces runtime errors. This links to content which was covered in the bootcamp lecture 3, where we explored handling user input in Java using the Scanner class.

Selection (If, If-Else)

I used selection statements throughout the Maintenance Helpdesk programme to help control how the program responds to user input. In a menu based, console-based application, selection is essential because it determines which part of the program should run next based on which option is chosen by the user.

In the main menu, user input is determined using a number of if and else if statements. Each condition checks the value entered by the user and directs the programme to the associated features such as adding a request, viewing an existing request or accessing the administrator menu.

if (choice == 1) {
    addRequest();
}
else if (choice == 2) {
    viewRequest();
}
else if (choice == 3) {
    adminMenu();
}
else if (choice == 4) {
    System.out.println("Exiting Programme");
    running = false;
}
else {
    System.out.println("Invalid Option Choosen");
}

Enter fullscreen mode Exit fullscreen mode

From a theoretical perspective, this demonstrates selection and control flow, which is a core concept in structured programming. Condition statements allow the program to follow different path's depending on the logical conditions. I chose if and else if statements because each condition shows what will happen when a value is entered, which improves the programmes readability and maintainability while the programme grows.

String buildingName = ""; // Will hold the building name once selected.
            boolean validBuilding = false; // Used to keep the looping until user select a valid building.

            while (!validBuilding) { // Loops continues until valid building chosen.

                try { // Try/catch used because it reads a number with nextInt.

                    System.out.println("Building Name (Choose 1-5):");
                    System.out.println("1 - Simon");
                    System.out.println("2 - Beyer");
                    System.out.println("3 - Michael Smith");
                    System.out.println("4 - Stopford");
                    System.out.println("5 - Alan Turing");

                    int buildingChoice = input.nextInt(); // Stores the building option as an integer.
                    input.nextLine(); // Clears scanner buffer.
Enter fullscreen mode Exit fullscreen mode

I also used selection elsewhere in the programme to validate choices, such as selecting a building, choosing a priority level or confirming a Y/N action. By continually checking conditions before proceeding, it made sure that only valid options were chosen, again reducing the risk of unexpected behaviours. This links to content which was covered in the bootcamp lecture 4 and Week 1 lecture, where we focused on selection statements.

Arrays

The maintenance helpdesk system stores and manages data using Java's Arraylist class. Arraylist are used to hold data on maintenance requests and technicians, allowing the programme to store multiply objects and manage them dynamically.

private static ArrayList<Request> request = new ArrayList<>();
private static ArrayList<Technician> technician = new ArrayList<>();

Enter fullscreen mode Exit fullscreen mode

From a theoretical perspective, Arraylist is part of the Java's framework and represents a dynamic data structure which means the size of the data can grow or shrink. This makes it more suitable than a fixed sized array when it's unsure how much data would be stored in advance. Within the programme the user can continuously enter more requests and even more technicians, so I needed to make sure it has a dynamic structure.

Request newRequest = new Request(refNumber, requester, telephone, buildingName, description, priority);
request.add(newRequest);

Enter fullscreen mode Exit fullscreen mode

New Objects are added to these lists as they are created. When a new maintenance request is added, the Request object is initiated and stored in the request list.

private static int findRequest(int refNumber) {

    for (int i = 0; i < request.size(); i++) {
        if (request.get(i).getRefNumber() == refNumber) {
            return i;
        }
    }
    return -1;
}

Enter fullscreen mode Exit fullscreen mode

To retrieve the data, the programme iterates through the ArrayList using a loop. When a user enters a reference number to view a request, the system search through the list to find a matching object. If no match is found it returns a -1 which acts as a failure indicator. This links to content which was covered in the week 5 lecture, where we explored Arrays and Arraylists.

Issues Encountered

Issue 1: Missing Clearing of the Scanner Buffer

One issued I encountered during development was related to handling user input on Y/N Confirmation menus, as when asking the user whether they wanted to add another request or continue with an action. The programme would repeatedly display the same prompt not allowing the user to progress resulting in an infinity loop.

This happened because i missed a call to input.nextLine() after reading the numerical input using nextInt(). The newline left in the scanner buffer was then picked up by the next nextLine() call, meaning the programme never waited for the users Y or N entry. To fix this, I made sure the scanner buffer was cleared after every numerical input by adding input.nextLine() before reading a Y/N response.

choice = input.nextInt();
input.nextLine();
Enter fullscreen mode Exit fullscreen mode

This resolved the issue and allowed the Y/N Menu to work correct. It helped me understand the importance of handling user input carefully when mixing number and text in a console-based programme.

Issue 2: Validating Menu Choices

Another issue I encountered was ensuring that user could only select valid menu options. I quickly learnt in earlier versions that the programme allowed users to enter numbers outside the expected range, which could lead to unexpected behaviours.

This happened because user input was not always checked to make sure if it matched the available menu options. Entering number outside the menu range would still be accepted by the programme.

if (choice >= 1 && choice <= 4) {
    validChoice = true;
} else {
    System.out.println("Invalid option, please choose between 1 and 4");
}
Enter fullscreen mode Exit fullscreen mode

To resolve this, I added additional validation checks to ensure that user could only chose valid menu options within the range. If an invalid option was chosen an error message would appear asking the user to re-enter the correct option.

This improved the overall usability of the programme by preventing invalid selections and directing the user towards to correct path. It was helped me reinforce the importance of validation user input in interactive programmes.

Issue 3: Managing reference numbers correctly

Another issue I encountered during the development was managing reference number for maintenance requests and technicians. Each request and technician in the programme needs a unique reference number so that it can be found and retrieved correctly later on in the programme.

// These are used to auto generate reference number for request / technicians
private static int nextRequestRef = 1001;
private static int nextTechNumber = 1;
Enter fullscreen mode Exit fullscreen mode

The issue kept occurring because reference numbers were automatically generated using counts such as nextRequestRef and nextTechNumber. When I added pre-populated test data at the start of the programme, I forgot to update these counters. This meant that newly created requests or technicians could be given a reference number that already existed in the programme, which could cause confusion and incorrect behaviour when searching for records.

int refNumber = nextRequestRef;
nextRequestRef++;

Request newRequest = new Request(refNumber, requester, telephone, buildingName, description, priority);
request.add(newRequest);
Enter fullscreen mode Exit fullscreen mode

To resolve this issue, I made sure that the counters were updated after the test data was created, so that any request or technician would continue from the correct reference number. This made sure that the reference number remained unique throughout the programme.

Ensuring I managed this issue helped me understand the importance of keeping track of the programme's state, especially when the data is generated automatically. It also highlighted to me how such a small issue can lead to larger issues when working with identifiers in the programme that rely on reference numbers for searching and updating the records.

Conclusion

In Conclusion, developing the maintenance helpdesk system helped me improve my understanding of Java programming, particularly around handling user input, using selection statements, and managing data within ArrayLists. As the programme grey in size, I also realised the importance of breaking the code down into separate classes. Doing this helpdesk me partially tidy up my code, making it easier to read, although I would have like to further improve this as I felt I could have made more improvements to the code.

If I were to develop my code further, I would have like to add the read and write functionality so that the data could be saved and loaded between sessions rather than being reset once the programme closed. Overall though this project helped me understand the importance of good structure, input validation and the need to plan and structure code carefully.

Top comments (0)