samedi 29 avril 2017

Static variables that are auto-generated and must be passed in constructor for object instantation

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:

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 alphabet= "abcdefghijklmnopqrstuvwxyz";      
           String   onoma = "";
              Random random = new Random();
              int randomLen = 1+random.nextInt(9);
             for(int i = 0; i < randomLen; i++) {
              char c = alphabet.charAt(random.nextInt(26));
               onoma+=c;     
           //it produces a random alphabetic string for the onoma variable
        }
      //String onoma = RandomStringUtils.randomAlphabetic(10);
  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.




Aucun commentaire:

Enregistrer un commentaire