I'm making a reservation system using Java, and I want to create random ID numbers. (The code I'm going to show isn't all the code in my class, its just the stuff that's relevant.)
I have named a field randomGenerator, which is a Random object and declared it in my constructor:
public class ReservationSystem {
private Random randomGenerator;
public ReservationSystem() {
randomGenerator = new Random();
}
And then this is the method that I am having difficulty with:
public void generateCustomerID(String id, int numbersInID) {
//incNumber stands for incremented number
int incNumber = 1;
//create a new array of numbers as big as the user has defined
int[] numbers = new int[numbersInID];
while(numbersInID < (incNumber +1)) {
//add a new number to the array between 0 and 10
numbers[incNumber] = randomGenerator.nextInt(10);
incNumber++;
}
System.out.print(id);
for(int number: numbers)
System.out.print(number);
}
In my application I have given these values:
public class Application {
public static void main(String[] args) {
reserve.generateCustomerID("AB-", 3);
}
}
And I have been receiving the output AB-000.
Aucun commentaire:
Enregistrer un commentaire