Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I need to create a code which randomly generates 1 million different vehicle registration plates.
The plates need:
- to consist of 3 numbers
- to consist of 3 letters excluding vowels
-the letters and numbers need to be separated by a dash
- a randomly generated country code needs to be added to the end of the plate, it needs to be one of the following ( CA, ZN, MP, EC, L, GP, NC, FS, NW)
- everything needs to be displayed in a GUI and when you press the generate button it generates everything

What I have tried:

I have tried creating arrays and for loops and my code is not working
Posted
Updated 7-May-18 22:29pm
Comments
Patrice T 7-May-18 15:59pm    
Show your code and describe problem you have.

Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)[^], Random (Java Platform SE 8 )[^].

Although, showing a million items on the GUI in one go will really annoy your users.
 
Share this answer
 
v4
Comments
Maciej Los 8-May-18 5:34am    
5ed!
Assuming five vowels (that is considering 'Y' a consonant), you could have
10 ^ 3 * 21 ^ 3 * 9 = 83349000
different plates.

So you can choose a random number in the [0..83348999] range and generate the plate string using it. I am going to show you the code:
Java
import java.util.Random;

class Plate
{
  static final int PLATES = 83349000;
  static String plate(int n)
  {
    char [] dgt = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    char [] letter = { 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z' };
    String [] cc = { "CA", "ZN", "MP", "EC", "L", "GP", "NC", "FS", "NW" };

    String p = "";

    if ( n<0 || n >= PLATES) return p;
    int rem;
    for ( int i = 0; i<3; i++)
    {
      rem = n % 10;
      p += dgt[rem];
      n = (n - rem) / 10;
    }
    p += "-";

    for ( int i = 0; i<3; i++)
    {
      rem = n % 21;
      p += letter[rem];
      n = (n - rem) / 21;
    }
    p += "-";
    rem = n % 9;
    p += cc[rem];
    return p;
  }

  public static void main(String arg[])
  {
    int n;
    Random rand = new Random();
    // generate and show 10 random plates (repetition is allowed)
    for (n=0; n<10; ++n)
    {
      int r = rand.nextInt(PLATES);
      System.out.printf( "%3d %s\n", (n+1), plate( r) );
    }

  }
}

Now you have to generate 1000000 of such plates (and I assume) avoiding repetitions.
You could use (if your programming box has enough memory) the algorithm shown here: Random extraction of 5 cards from a deck[^] to make the trick.

Finally you have to create a GUI for it, possibly using, as suggested the Swing Framework. This artistic step is very up to you.
 
Share this answer
 
Comments
Maciej Los 8-May-18 5:35am    
5ed!
CPallini 8-May-18 5:43am    
Thank you very much, Maciej.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900