Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 4 123 ... LastLast
Results 1 to 25 of 96

Thread: Creating Random unused ID number for every library member.

  1. #1
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Thumbs up Creating Random unused ID number for every library member.

    /**
         * Constructor for objects of class Member
         */
        public Member(String firstname,String lastname,String phone, int id)
        {
            //constructor
           if(id > 0)
            {
                if(id == this.memberID)
                {
                    System.out.println("Error: Member ID '" + id + "' already exists");
                }
                else
                {
                 this.firstname = firstname;
                 this.lastname = lastname;
                 this.phone = phone;
                 this.memberID = id;
                }
            }
            else
            {
                System.out.println("Member ID MUST be a positive integer");
            }
        }

    Hi guys. This is suppose to be a lending Library. with every member issued a unique Id. but before issuing this ID I need to do a test that the issued ID is not already in use. I wrote this if statement but it doesnt work because I ve just issued this same ID to 3 different members and it was not detected.. Help! I would like to create a system where they are issued with a RAMDOM number after a test has been carried out and they not already in use. cheers for all inputs


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating Random unused ID number for every library member.

    How are you keeping track of previously issued id numbers?
    How that is done would depend on the max id value.
    One way would be to use a Set to save ids that have been issued.
    Then test if the Set contained the generated id before issuing it.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    Quote Originally Posted by Norm View Post
    How are you keeping track of previously issued id numbers?
    How that is done would depend on the max id value.
    One way would be to use a Set to save ids that have been issued.
    Then test if the Set contained the generated id before issuing it.
    Thats a good idea mate but daunting as i ve never used "SET" before. can you write basics for us and we can do the rest..im very new to java. Im going to read up on set now...cheers..

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating Random unused ID number for every library member.

    Write a small test program that creates a Set and adds values to it and see how it works. For example define an array of values with some repeats and add the contents of the array to the Set.
    Don't forget to test the values returned by the methods.
    Print out the Set when done to see what is in it.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    Quote Originally Posted by Norm View Post
    Write a small test program that creates a Set and adds values to it and see how it works. For example define an array of values with some repeats and add the contents of the array to the Set.
    Don't forget to test the values returned by the methods.
    Print out the Set when done to see what is in it.
    Struggling big time have to submit this project next week...

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating Random unused ID number for every library member.

    Post the small testing program if you are having trouble with it and ask some questions.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating Random unused ID number for every library member.

    What is the posted code supposed to do when it's executed?

    Did it do what you expected? If not, please explain.

    If you get errors, please post the full text of the error messages.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    Quote Originally Posted by clarky2006 View Post
     
      public void  hashset(int[]) 
      {
        Set s = new HashSet();
        int[] values = new int[] 
        for (int i = 0; i < values.length; i++)
          if (!s.add(values[i]))
            System.out.println("Duplicate detected: " + values[i]);
     
        System.out.println(s.size() + " distinct numbers detected: " + s);
      }

    here it is.

    --- Update ---



    dont know wot im doing...
    I was trying to create a set method to hold all the given ID numbers in use, so before an object is created at constructor, the new ID would be tested against the existing ones to ensure no duplicates. I got lost in the middle of writing the code.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating Random unused ID number for every library member.

    Did you compile and execute the posted code? What happened?
    If there were errors, copy and paste here the full text of the error messages.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating Random unused ID number for every library member.

    Can you explain what you did to guarantee no duplication?

    Does the code work?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    Quote Originally Posted by Norm View Post
    Can you explain what you did to guarantee no duplication?

    Does the code work?
    Ye it works fine......I ve initialised the memberID 3000 and bookId to 200, and to be incremented by 1 hence memberId++, and bookId++, every time a new object is created. Not very sophisticated but works well.....

  12. #12
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    Quote Originally Posted by Norm View Post
    Can you explain what you did to guarantee no duplication?

    Does the code work?
    Ye it works fine......I ve initialised the memberID 3000 and bookId to 200, and to be incremented by 1 hence memberId++, and bookId++, every time a new object is created. Not very sophisticated but works well.....

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating Random unused ID number for every library member.

    That doesn't look like you are using random numbers. Incrementing by 1 means that the numbers are in sequence: 3000 3001 3002 etc
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    Quote Originally Posted by Norm View Post
    That doesn't look like you are using random numbers. Incrementing by 1 means that the numbers are in sequence: 3000 3001 3002 etc
    Yep, easily done but yet does the job..I struggled with Random numbers. could not stop it duplicating numbers.

    --- Update ---

    Now Im onto the Loan Class....borrowing 3 books is regarded as 3 loans. I need time and date when the book was loaned-out and time and date when its returned (21 days) Maximum) using Gregorian Calendar..You got any helpful suggestions? would be appreciated...

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating Random unused ID number for every library member.

    Look at the Calendar class for methods to get and set the date. Write a small testing program and experiment with different methods and print out the results for them.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    Quote Originally Posted by Norm View Post
    Look at the Calendar class for methods to get and set the date. Write a small testing program and experiment with different methods and print out the results for them.
    ok Im on it now...just looking at the methods on the Java API.....and reading up on it...looks complicated tho..

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating Random unused ID number for every library member.

    It's different. The trick is defining the field (one of the args to the methods) to refer to what field in the calendar object you want to work with. From the API doc:
    set(Calendar.MONTH, Calendar.SEPTEMBER) //sets the date's month to September
    The field here is: Calendar.MONTH
    There is a list of different field definitions that can be used in the API doc
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    Cheers for that...That would be really helpful..just tut I do upload the loan Class to show you where I am at....


    import java.text.DateFormat;
    import java.util.Date;
    import java.text.SimpleDateFormat;
    import java.util.GregorianCalendar;
    import java.util.Calendar;
     
    /**
     * This Class would hold all loan details.
     
     
        }   
     
        /**
         * Set date when the loan was taken out.
         * 
         * @param date the day of the month 1 to 28, 30 or 31 depending on month
         * @param month 1 (January) to 12 December
         * @param year the year including century e.g. 2010
         */
        public void setLoanDtae(int date, int month, int year)
        {
     
        }
     
         /**
         * Get the date of the loan.
         * @return
         */
        public GregorianCalendar getLoandDate()
        {
            return (GregorianCalendar) loanDate.clone();
        }
     
     
        /**
         * Set date when loan is due back.
         * 
         * @param date the day of the month 1 to 28, 30 or 31 depending on month
         * @param month 1 (January) to 12 December
         * @param year the year including century e.g. 2010
         */
        public void setDateDueBack(int date, int month, int year)
        {
            dateDueBack.set(date, month - 1, year);
        }
     
         /**
         * Get date when loan is due back.
         * @return
         */
        public GregorianCalendar getDateDueBack()
        {
            return (GregorianCalendar) dateDueBack.clone();
        }
     
        /**
         * Get the loan date as a string
         * @return the date of loan as a string.
         */
        public String getLoanDateString()
        {
            SimpleDateFormat df = new SimpleDateFormat("d MMM yyyy");
            return df.format(loanDate.getTime());
        }
     
        /**
    	 * Get loan date as a Sring.
    	 * @return
    	 */
    	public String getloanDateSt
    	}
     
     
    	/**
    	 * Return date when loan is due back as a String.
    	 */
    	public String toS
    	}
     
     
    	/**
    	 * 
    	 * 
    	 */
        public void printLoanDetails()
        {
             System.out.println("The Book ID is: "  +  bookId +  " The loan Date is: " + loanDate +   " The date due back: " + dateDueBack);  
     
    }
    }

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating Random unused ID number for every library member.

    Does the code compile?
    If no errors compiling, have you tested it? Make a small driver to call the various methods with some values and print out the results to see if the methods are working as expected.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    Quote Originally Posted by Norm View Post
    Does the code compile?
    If no errors compiling, have you tested it? Make a small driver to call the various methods with some values and print out the results to see if the methods are working as expected.
    It does compile but when I print loan details, it only prints the loan object instead of the details and it does not print time or date...Im pulling my hair out here trying to sort it. please have a look at it in detail, I think I might have made a few mistakes there, even tho it compiles..what do you think about the print method?..

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating Random unused ID number for every library member.

    Post the program's output and add some comments to it saying what is wrong with it and show what the output should be.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #22
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    3001 201 Loan java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=false,le nient=true,zone=sun.util.calendar.ZoneInfo[id="Europe/London",offset=0,dstSavings=3600000,useDaylight=tr ue,transitions=242,lastRule=java.util.SimpleTimeZo ne[id=Europe/London,offset=0,dstSavings=3600000,useDaylight=tru e,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode =2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=?,Y EAR=2008,MONTH=4,WEEK_OF_YEAR=?,WEEK_OF_MONTH=?,DA Y_OF_MONTH=10,DAY_OF_YEAR=?,DAY_OF_WEEK=?,DAY_OF_W EEK_IN_MONTH=?,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE =0,SECOND=0,MILLISECOND=?,ZONE_OFFSET=?,DST_OFFSET =?]

    This is the output when i invoke the ListAllLoans method. At the top "3001" is the memberId and "201" is the book Id..Those are printed fine, but time and date are not..

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating Random unused ID number for every library member.

    The part that begins with: java.util.GregorianCalendar... is from when you print an instance of that class. You'll want to format that for printing. Look at the DateFormat class.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #24
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    Quote Originally Posted by Norm View Post
    The part that begins with: java.util.GregorianCalendar... is from when you print an instance of that class. You'll want to format that for printing. Look at the DateFormat class.
    ok i ll have a look...

    --- Update ---

    Quote Originally Posted by Norm View Post
    The part that begins with: java.util.GregorianCalendar... is from when you print an instance of that class. You'll want to format that for printing. Look at the DateFormat class.
    Not really seen any thing that would help me @ date format..

  25. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating Random unused ID number for every library member.

    Sorry, I should have said to look at the SimpleDateFormat class.
    If you don't understand my answer, don't ignore it, ask a question.

Page 1 of 4 123 ... LastLast

Similar Threads

  1. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  2. Creating a new class and dont know how to generate a random number math code
    By beatlebaby70 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 15th, 2011, 03:03 PM
  3. How to returned random number to original number?
    By i4ba1 in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 19th, 2011, 04:35 AM
  4. creating a shared library
    By navinbecse in forum Threads
    Replies: 1
    Last Post: April 9th, 2010, 07:54 AM
  5. Generation of random number using random class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 16th, 2009, 06:10 AM