0

Hi i'm new in Java and i have an assignment where for days i couldn't figure out how to have a unique for each object attribute that is random generated.In my class reservation i want kratisid to be unique and random generated for every object but if i do that in the constructor in the main() method i need to pass a arithmetic value and not the auto generated one.Here is the code:

import java.utils.*;
import static org.apache.commons.lang3.RandomStringUtils.*;

public class reservation {

    String onoma;
    int afixi;
    int mdiam;
    int atoma;
    Domatio domat;//domat prosorinos deiktis sto object Domatio tha balo d meta
    Domatio d;
    Random rand = new Random();//this.kratisid=kratisid; kai stin main bazo random
    //random kratisi id
    static int kratisid = rand.nextInt(500) + 100;//It produces a random kratisid between 100 and 500

    String onoma = RandomStringUtils.randomAlphabetic(10);  //it produces a random alphabetic string for the onoma variable

    public reservation(String onoma, int kratisid, int afixi, int mdiam, int atoma, Domatio d)//boolean s)
    { //info for customer of a hotel reservation,d is reference to Domatio objects an another class
        d = null;

        this.onoma = onoma;   //System.out.println("Enter your name please");
        //Scanner scanner1= new Scanner(System.in);
        //onoma=scanner1.nextLine()         

        this.afixi = afixi;
        //System.out.println("Enter your day of arrival please ,it must be from 1 to 30 max");
        //Scanner scanner2=new Scanner(System.in);
        // afixi=scanner2.nextInt();
        // if(afixi<1 && afixi>30){
        //   afixi=0;
        //  System.out.println("Please re-enter your arrival within the month boundaries");
        //}
        this.mdiam = mdiam;
        //System.out.println("Please enter the number of days you will be staying");
        // Scanner scanner3=new Scanner(System.in);
        // mdiam=scanner3.nextInt();
        this.atoma = atoma;//

    }

Now in the main method when i create an object eg r i need to pass the static kratisid as an argument but how i make the object have the static random kratisid without needing to pass it in the object creation in main? In case i didn't tell i try to make a unique random kratisid for each object and an onoma which its random too but if the user wants so he can input his own.

George J
  • 11
  • 5
  • 1
    Please take the time to provide a [mcve], properly formatted, without randomly commented-out code, following normal naming conventions. – Jon Skeet Apr 29 '17 at 08:52
  • ok but for some reason i cant understand how always the first line slips out sorry – George J Apr 29 '17 at 09:21
  • 2
    Format your code @GeorgeJ! Use proper indentation. Almost all IDEs can do that: NetBeans, Eclipse, IDEA, etc. – Behrang Apr 29 '17 at 09:32
  • Is this even compiling? You have a for loop where you can't have one! – Behrang Apr 29 '17 at 09:33
  • if anyone could provide advice would be usefull the project's deadline runs out tommorow. – George J Apr 29 '17 at 09:36
  • i know the for loop is stupid to be there it is a mistake but i want to create a random Sting onoma and the Apache RandomStringUtils.RandomAlphabetic dont work out i will edit it out immediately thx for your observation(initially the for loop and the Random() where inside the constructor but due to my question i couldnt leave them there) – George J Apr 29 '17 at 09:39
  • sorry for my bad editing but that code you see was copy paste fom my bluej editor – George J Apr 29 '17 at 09:45

1 Answers1

1

If you want to create objects with different randomly initialized field, then you should consider something like this:

public class Reservation {
    private static final Random r = new Random(0);
    private int kratisid = r.nextInt(500) + 100;
    // ...
}

Please note that:

  • Random numbers and unique identifiers are related concepts, but being random is not the same as being unique
  • This solution suffers of critical race condition, as objects might interfere on r

As such, consider this solution:

public class Reservation {
    private static final Random r = new Random(0);
    private static Semaphore semaphore = new Semaphore(1);
    private static int getRandomInt() {
        final int res;
        semaphore.acquireUninterruptibly();
        res = r.nextInt(500) + 100;
        semaphore.release();
        return res;
    }
    private int kratisid = getRandomInt();
    // ...
}
w4bo
  • 855
  • 7
  • 14
  • with Semaphore what you want to accomplish?Your code is great but i don't understand, making kratisid final it will have forever the same value for a specific instance right?Also about r my reservation object i use it to universally handle reservation type objects because i need that in other classes is it wrong?thx for everything! – George J Apr 29 '17 at 09:49
  • also i cant have kratisid be static the exercise says to use a static variable so as to each object to have a unique and random id? – George J Apr 29 '17 at 10:09
  • `kratisid` is private, not final, so you can update its value. Being `r` a static field, if multiple objects invoke `r.nextInt` at the same time, then you might end up having concurrency issues (http://stackoverflow.com/questions/23396033/random-over-threadlocalrandom). Generally speaking, global variables should be organized properly (https://en.wikipedia.org/wiki/Global_variable#Java) – w4bo Apr 29 '17 at 10:11
  • `kratisid` is neither static, nor final. It is private – w4bo Apr 29 '17 at 10:15