PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, July 21, 2022

[FIXED] How to create a 'best fit' comparison of several integers and an input integer in Java?

 July 21, 2022     arrays, integer, java, loops, oop     No comments   

Issue

I have 3 (subject to change to more rooms in the future, but that's irrelevant) rooms, all with a different number of seats (suppose these are 'Room' Objects):

Room Seats
1 10
2 20
3 30

I then input a value of the number of seats I need to reserve and then my code will automatically assign the user a room based on their input with the room that has the "best fit" or closest amount capable of seats that best fits their demand. So some examples

User inputs: My Code Assigns them Room:
10 1
22 3
25 3
4 1
9 1
15 2

(Assume the inputs won't go over 30) If the inputs go over 30 I do something else, which isn't relevant to this question.

So here's my attempt:

returnValue = 0;
inputValue = 10; //can be anything, doesn't have to be 10
ArrayList<Room> rooms= new ArrayList<Room>(); //where each room is already in the array list identified by it's unique number 

//assume getRoomNumber() returns the room number of the Room object
// assume getRoomSeats() returns the number of seats of a Room object

for (Room i: rooms){

    if (i.getRoomSeats()==inputValue){

        returnValue = i.getRoomNumber();
    }
    elif(i.getRoomSeats()<inputValue){//donothing}

    elif(i.getRoomSeats()>inputValue){

        returnValue = i.getRoomNumber;

}}}

Is this the best way to do what I want?


Solution

You can do something like this

import java.util.ArrayList;
import java.util.Comparator;

public class Main {

    public static void main(String[] args) {
        new Main().test();
    }

    void test() {
        int inputValue = 22;

        ArrayList<Room> rooms = new ArrayList<Room>(); // where each room is already in the array list identified by
        rooms.add(new Room(10, 1));
        rooms.add(new Room(20, 2));
        rooms.add(new Room(30, 3));

        Integer currentDifference = null;
        Room roomWithMinimalDifference = null;

        for (Room room : rooms) {
            int difference = room.getRoomSeats() - inputValue;
            System.out.println("room "+room.getRoomNumber()+" difference "+difference);
            boolean roomFitsEnteredSeats = difference >= 0; //check if room fits the entered value

            if(roomFitsEnteredSeats) {
                if (currentDifference == null || difference < currentDifference) {
                    currentDifference = difference;
                    roomWithMinimalDifference = room;
                }
            }
        }

        if (roomWithMinimalDifference != null) {
            System.out.println("found room" + roomWithMinimalDifference.getRoomNumber() + " seats "
                    + roomWithMinimalDifference.roomSeats);
        } else {
            System.out.println("no room was found");
        }

        System.out.println("-----------------------");

        //========== or use this with java >= 8
        Room bestMatchingRoom = rooms.stream()
                .sorted(Comparator.comparingInt(Room::getRoomSeats))
                .filter(r -> r.getRoomSeats() >= inputValue)
                .findFirst()
                .orElse(null);

        if (bestMatchingRoom != null) {
            System.out.println("found room" + roomWithMinimalDifference.getRoomNumber() + " seats "
                    + roomWithMinimalDifference.roomSeats);
        } else {
            System.out.println("no room was found");
        }
    }

    class Room {
        int roomSeats;
        int roomNumber;

        public Room(int roomSeats, int roomNumber) {
            super();
            this.roomSeats = roomSeats;
            this.roomNumber = roomNumber;
        }

        public int getRoomSeats() {
            return roomSeats;
        }

        public void setRoomSeats(int roomSeats) {
            this.roomSeats = roomSeats;
        }

        public int getRoomNumber() {
            return roomNumber;
        }

        public void setRoomNumber(int roomNumber) {
            this.roomNumber = roomNumber;
        }
    }
}


Answered By - JavaMan
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing