如何确保Java中不同Staff子类的staffNo及牙医GDCRegNo唯一性?
Hey there! Let's tackle this problem step by step. You want to ensure unique staffNo across all Staff subclasses (Dentist, Nurse, Receptionist) and unique GDCRegNo specifically for Dentist instances. Here's how to implement this properly:
Step 1: Ensure Unique staffNo for All Staff Subclasses
First, we need a way to track all generated staffNo values across every Staff instance. A static Set<Integer> in the Staff class is ideal here—sets automatically reject duplicate values, so we can use that to enforce uniqueness. We'll modify the number generation logic to keep creating numbers until we find one that hasn't been used yet.
Here's the updated Staff class:
import java.util.HashSet; import java.util.Random; import java.util.Set; public class Staff { protected int staffNo; protected String name; // Static set to track all used staff numbers (shared across all Staff instances) private static final Set<Integer> usedStaffNumbers = new HashSet<>(); private static final Random rand = new Random(); public Staff(String name) { this.name = name; this.staffNo = generateUniqueStaffNumber(); } private int generateUniqueStaffNumber() { int newStaffNo; do { // Generate number between 1000 and 9999 (inclusive) newStaffNo = rand.nextInt(9000) + 1000; } while (!usedStaffNumbers.add(newStaffNo)); // Set.add() returns false if the number already exists, so loop until we get a unique one return newStaffNo; } // Make these methods protected so subclasses can access them if needed protected int getStaffNo() { return this.staffNo; } protected String getName() { return this.name; } }
Step 2: Ensure Unique GDCRegNo for Dentist Instances
For Dentist-specific GDCRegNo, we'll use the exact same pattern: a static Set<Integer> in the Dentist class to track used registration numbers. We'll also clean up redundant code from your original example (like the unnecessary List<Dentist> declarations in the random number method) and properly initialize the patients list.
Here's the updated Dentist class:
import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Random; import java.util.Set; public class Dentist extends Staff { private final int GDCRegNo; private List<Patient> patients; // Static set to track all used GDC registration numbers private static final Set<Integer> usedGDCRegNumbers = new HashSet<>(); private static final Random rand = new Random(); public Dentist(String name) { super(name); // Call parent constructor to set name and generate unique staffNo this.GDCRegNo = generateUniqueGDCRegNumber(); this.patients = new ArrayList<>(); // Initialize the patients list } private int generateUniqueGDCRegNumber() { int newGDCRegNo; do { // Generate number between 1000000 and 9999999 (inclusive) newGDCRegNo = rand.nextInt(9000000) + 1000000; } while (!usedGDCRegNumbers.add(newGDCRegNo)); return newGDCRegNo; } // Getter for GDCRegNo since it's private final public int getGDCRegNo() { return GDCRegNo; } // Example method to manage patients public void addPatient(Patient patient) { this.patients.add(patient); } }
Key Details to Note
- Static Sets: The
usedStaffNumbersandusedGDCRegNumbersare static, meaning they belong to the class itself (not individual instances). This lets us track every generated number across all instances of the class. - Unique Generation Logic: The
do-whileloops keep generating numbers untilSet.add()returnstrue—this only happens when the number isn't already in the set, guaranteeing uniqueness. - No Redundant Code: We removed the unnecessary
List<Dentist>declarations from your original random number methods and properly initialized thepatientslist in the Dentist constructor. - Inheritance: Nurse and Receptionist subclasses will automatically get the unique
staffNologic by callingsuper(name)in their constructors. No extra code is needed for their staff numbers.
Example Nurse class for reference:
public class Nurse extends Staff { // Add Nurse-specific fields or methods here public Nurse(String name) { super(name); // Staff constructor handles unique staffNo generation } }
内容的提问来源于stack exchange,提问作者Just a human

