简体   繁体   中英

How to generate 10 random numbers in java and assign them to fields?

I have one project in school where i need to make a simulation of keeping records for vehicles.

And Vehicle is abstract class which extends two classes (Automobile class and Motor class). There is also one Interface (NewRegistration) which contains one method register() and these two classes implements that interface. It should register a new registration for automobile and motor, but i should previously check if registration which i have insert is correct in one method checkNumber() .

All in all, i have solved every task i have gotten, but only one i can't solve and it is to generate 10 random numbers..

Registration should have 15 characters, for first and second character UpperCase letter, for third character symbol "-", and for the the last two characters lowerCase letters.. other characters should be numbers and those numbers should be randomly generated.

Here is what i have done so far:

Automobile.java:

public class Automobil extends Vozilo implements NovaRegistracija{

private int brojVrata, brojSedista;

public Automobil(String marka, String model, String registracija, int kubikaza, int brojVrata, int brojSedista) {
    super(marka, model, registracija, kubikaza);
    this.brojVrata = brojVrata;
    this.brojSedista = brojSedista;
}

public Automobil() {

}

// checkNumber() method
@Override
public void proveriBroj() {
    boolean provera = true;
    boolean proveraKubikaze = true;
    String parsedInteger = String.valueOf(getKubikaza());

    for (int j = 0; j < parsedInteger.length(); j++) {
        if (Character.isLetter(parsedInteger.charAt(j))) {
            proveraKubikaze = false;
        }
    }

    // Checking registration
    if (this.getRegistracija().length() == 15) {
        if (Character.isUpperCase(getRegistracija().charAt(0)) && Character.isUpperCase(getRegistracija().charAt(1))) {
            if (getRegistracija().charAt(2) == '-') {
                if (Character.isLowerCase(getRegistracija().charAt(getRegistracija().length()-1)) && Character.isLowerCase(getRegistracija().charAt(getRegistracija().length()-2))) {
                    for (int i = 3; i < getRegistracija().length()-2; i++ ) {
                        if (!Character.isDigit(getRegistracija().charAt(i))) {
                            provera = false;
                            System.out.println("11111111111111111");
                            break;
                        }
                    }
                } else {
                    System.out.println("222222222222");
                    provera = false;
                }
            } else {
                System.out.println("333333333333");
                provera = false;
            }
        } else {
            System.out.println("44444444444444444");
            provera = false;
        }
    } else {
        System.out.println("555555555555555");
        provera = false;
    }

    if (proveraKubikaze) {
        System.out.println("Kubikaza je dobra!");
    } else {
        System.out.println("Kubikaza nije dobra!");
    }

    if (provera) {
        System.out.println("Broj registracije je ispravan!");
    } else {
        System.out.println("Broj registracije je neispravan!");
    }

}

// register() method from interface NewRegistration
@Override
public void registruj() {
    this.setRegistracija("AA-" + "aa");
    proveriBroj();
    System.out.println("- - - - - - - - - - - - - - - - - - - -");
}

public int getBrojVrata() {
    return brojVrata;
}

public void setBrojVrata(int brojVrata) {
    this.brojVrata = brojVrata;
}

public int getBrojSedista() {
    return brojSedista;
}

public void setBrojSedista(int brojSedista) {
    this.brojSedista = brojSedista;
}

}

So how would i now assign randomly 10 generate numbers for the whole registration of vehicle?

Thank you in advance.

使用org.apache.commons.lang.RandomStringUtils

System.out.println(RandomStringUtils.randomNumeric(10));

Math.random() so this is the method i would go for, you can simply generate simple numbers by for ex. multiplying 10 and (or without) rounding.

public static double random()

Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.

Here is a function I wrote:

import java.util.Random

private String generateRegistration() {
    String selectFrom1 = "ABCDEFGHIJKLMNOPRSTUVWXYZ";
    String selectFrom2 = "0123456789";
    String selectFrom3 = selectFrom1.toLowerCase();

    return randomFromSelection(selectFrom1,2)+"-"+randomFromSelection(selectFrom2,10)+randomFromSelection(selectFrom3,2)
}
private String randomFromSelection(String selection, int times) {
    Random random = new Random();
    String toReturn = "";
    for (int useless = 0; useless < times; useless++) {
        toReturn += selection.charAt(random.nextInt(selection.length()));
    }
    return toReturn;
}

This function is very flexible and can be used with any input for selection or times .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM