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.

Results 1 to 4 of 4

Thread: Placement algorithm assistance

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Location
    Manchester, UK
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Placement algorithm assistance

    First of all thank you to everyone who has posted in threads that I have read throughout my time. I'm a big lurker on these forums and generally surf over and read threads. Until today, I have a little problem that I was hoping someone could provide a little guidance.

    The algorithm focuses on placement based on user requirements - I'll try to explain below.

    Users: 10 users - Simple user id (userid) and user allowed (userallowed = 1 or 0)

    Houses: 5 houses that are stored in a Map - all the same and nothing unique about each one.

    The aim is to place the 10 users into each of the 5 houses, however when a user is not allowed (userallowed = 0) they are placed in a house thats "secured" and only other disallowed users (userallowed = 0 ) are able to placed. All other users are shared amongst the other 4 houses (for example).

    My approach so far has been to use a for loop which an if statement, however this has been unsuccessful and I'm looking for some guidance as to how best to proceed.

    Regards,

    Dan


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Placement algorithm assistance

    Also posted at Placement algorithm assistance - Dev Shed

    Can you post code showing what you have tried so far?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Location
    Manchester, UK
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Placement algorithm assistance

    Quote Originally Posted by Norm View Post
    Also posted at Placement algorithm assistance - Dev Shed

    Can you post code showing what you have tried so far?
    Posted in an attempt to get wider readership - I'll bring together a smaller example to demonstrate and reply back. As stated previously, its part of a larger project, however I do acknowledge I do need to provide an example, for which I will do soon.

    Regards

    Dan

  4. #4
    Junior Member
    Join Date
    Dec 2011
    Location
    Manchester, UK
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Placement algorithm assistance

    I've put together a very basic and raw example. Below are a few scenarios:

    Example 1: If user 1/8 is not allowed, the first house in the foor loop to allocated will be house 1, hereafter only disallowed users (1/8) would be allowed to be placed in this house. All other users would be distributed to other houses.

    Example 2: if user 5/6 are disallowed users, all houses have already been allocated to allowed users then they will simply be rejected as no secure house can be offered.

    The code is very basic and provides a starting point, thank you all who have so far taken time to answer my questions.

     
    public class placement {
     
     
    	/** Linking the user to a house **/
    	private static Map<Integer, Integer> housesUsed;
     
    	/** Hold user data **/
    	private static Map<Integer, Integer> userData;
     
    	/** Hold houses **/
    	private static Map<Integer, Integer> houses;
     
    	public static void main(String[] args) {
    		setUsers();
    		setHouses();
    		housesUsed = new HashMap<Integer, Integer>();
     
    		int housePosition = 0;
     
    		for (int a = 0; a < userData.size(); a++) {
     
    			for (int i = 0; i < houses.size(); i++) {
     
    				if (!housesUsed.containsKey(a)) {
    					housePosition = i;
    					housesUsed.put(housePosition, a);
    				}
     
    			}
    			System.out.println("House id: " + housePosition + " User id: " + a);
     
    		}
     
    	}
     
    	public static void setUsers() {
    		userData = new HashMap<Integer, Integer>();
    		// User id followed by allowed (o not / 1 allowed)
    		userData.put(0, 0);
    		userData.put(1, 1);
    		userData.put(2, 1);
    		userData.put(3, 1);
    		userData.put(4, 0);
    		userData.put(5, 1);
    		userData.put(6, 1);
    		userData.put(7, 0);
    		userData.put(8, 1);
    		userData.put(9, 1);
    		Collection c = userData.keySet();
    		Iterator itr = c.iterator();
    		while (itr.hasNext())
    			System.out.println("user id " + itr.next());
    	}
     
    	public static void setHouses() {
    		houses = new HashMap<Integer, Integer>();
    		houses.put(0, 0);
    		houses.put(1, 1);
    		houses.put(2, 2);
    		houses.put(3, 3);
    		houses.put(4, 4);
     
    		Collection c = houses.values();
     
    		Iterator itr = c.iterator();
     
    		while (itr.hasNext())
    			System.out.println("houses " + itr.next());
    	}
     
    }

    Thanks,

    Dan

Similar Threads

  1. Need some assistance please
    By JavaPhish in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 25th, 2012, 10:00 AM
  2. [SOLVED] Bracket placement issues
    By fride360 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 14th, 2011, 05:08 AM
  3. JLabel placement issues
    By fride360 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 11th, 2011, 01:20 PM
  4. Variable declaration placement
    By 2nickpick in forum Java Theory & Questions
    Replies: 2
    Last Post: January 22nd, 2011, 11:34 AM
  5. Placement of code within an application
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 12th, 2010, 08:26 PM