Java Code Examples: User Name Generation and Repair Scheduling

Classified in Computers

Written on in English with a size of 6.34 KB

This document presents Java code snippets demonstrating two distinct functionalities: user name generation and management, and a car repair scheduling system. Each section includes method implementations with explanations of their purpose and logic.

User Name Management System

The UserName class is designed to generate and manage potential user names based on a user's first and last names. It also provides functionality to filter out names that are already in use.

UserName Class Constructor

This constructor initializes a UserName object, populating a list of possible user names. It assumes that firstName and lastName are valid strings containing only letters and have a length greater than zero.

import java.util.ArrayList;

public class UserName { private ArrayList<String> possibleNames; // Assumed field declaration

/**
 * Constructs a UserName object.
 * Precondition: firstName and lastName have length greater than 0
 * and contain only uppercase and lowercase letters.
 */
public UserName(String firstName, String lastName) {
    possibleNames = new ArrayList&lt;String&gt;();
    // Loop through the length of firstName
    for (int i = 1; i &lt;= firstName.length(); i++) {
        // Create substrings starting from the first character up to the ith character
        String sub = firstName.substring(0, i);
        // Concatenate lastName with the current substring and add to possibleNames
        possibleNames.add(lastName + sub);
    }
}

Setting Available User Names

The setAvailableUserNames method removes any generated user names that are found within a provided list of already used names. This ensures that only unique and available names remain in the possibleNames list.

    /**
     * Removes strings from possibleNames that are found in usedNames.
     * This method iterates backwards to avoid skipping elements when removing.
     */
    public void setAvailableUserNames(String[] usedNames) {
        for (int i = possibleNames.size() - 1; i >= 0; i--) {
            // Check if the current possible name is used
            if (isUsed(possibleNames.get(i), usedNames)) { // Assumes an 'isUsed' helper method exists
                // Remove the used name from possibleNames
                possibleNames.remove(i);
            }
        }
    }
    // The 'isUsed' method is assumed to be defined elsewhere within the UserName class.
}

Car Repair Scheduling System

The RepairSchedule class manages the scheduling of car repairs, tracking which mechanics are busy and which bays are occupied. It ensures that no mechanic or bay is double-booked.

Class Structure and Constructor

The RepairSchedule class maintains a list of current repairs and the total number of mechanics available. The constructor initializes the schedule and sets the number of mechanics.

import java.util.ArrayList;

public class RepairSchedule { /* Each element represents a repair by an individual mechanic in a bay. / private ArrayList<CarRepair> schedule; // Assumes CarRepair class exists /* Number of mechanics available in this schedule. / private int numberOfMechanics;

/**
 * Constructs a RepairSchedule object.
 * Precondition: n &gt;= 0
 */
public RepairSchedule(int n) {
    schedule = new ArrayList&lt;CarRepair&gt;();
    numberOfMechanics = n;
}

Adding a Repair to the Schedule

The addRepair method attempts to schedule a new repair. A repair can only be added if both the specified mechanic and bay are currently free. If successful, it returns true; otherwise, false.

    /**
     * Attempts to schedule a repair by a given mechanic in a given bay.
     * Precondition: 0 <= m < numberOfMechanics and b >= 0
     */
    public boolean addRepair(int m, int b) {
        // Check for existing repairs by the same mechanic or in the same bay
        for (CarRepair cr : schedule) {
            if (cr.getMechanicNum() == m || cr.getBayNum() == b) {
                return false; // Mechanic or bay is already occupied
            }
        }
        CarRepair rep = new CarRepair(m, b); // Assumes CarRepair constructor
        schedule.add(rep); // Corrected spelling from 'scheudle' to 'schedule'
        return true; // Repair successfully added
    }

Identifying Available Mechanics

The availableMechanics method returns an ArrayList containing the identifiers of all mechanics who are currently not assigned to any repair in the schedule.

    /**
     * Returns an ArrayList containing the mechanic identifiers of all available mechanics.
     */
    public ArrayList<Integer> availableMechanics() {
        ArrayList<Integer> mechs = new ArrayList<Integer>();
        for (int i = 0; i < numberOfMechanics; i++) {
            boolean free = true;
            for (CarRepair cr : schedule) {
                if (cr.getMechanicNum() == i) {
                    free = false; // Mechanic is busy
                    break; // No need to check further for this mechanic
                }
            }
            if (free) {
                mechs.add(i); // Add mechanic if free
            }
        }
        return mechs;
    }

Completing a Repair (Car Out)

The carOut method removes a completed repair from the schedule based on the bay number. This frees up the bay and the mechanic for new assignments.

    /**
     * Removes an element from schedule when a repair is complete.
     * This method iterates backwards to avoid skipping elements when removing.
     */
    public void carOut(int b) {
        for (int i = schedule.size() - 1; i >= 0; i--) {
            if (schedule.get(i).getBayNum() == b) {
                schedule.remove(i);
                // Assuming only one repair per bay at a time, we can break after removal
                break;
            }
        }
    }
}

Related entries: