0

Hi guys I am currently learning Java and I'm trying to make a program that you can use too book motel rooms. I'm trying to create a method to search the amount of days a guest needs and then check the days and units that are free. I'm using a 2D array of an array of an array.

    public void doSearch() {
    this.redisplay();
    int daysWanted = UI.askInt("Number of days required"); 
    int days = 0;
    for(int i = 0; i < NUM_UNITS; i++){
        for(int j = 0; j < NUM_DAYS; j++){
            if(bookings[i][j] == null){
                days++;
                if(daysWanted >= days && this.bookings[i] == this.bookings[i]){
                    this.displayCell (i, j, Color.red);
                }
            }
        }
    }
}

Image of motel program

here is what the program looks like currently, and what i have shown above is the method i am trying to do. What my problem is, when i type in how many days, it checks if the available unit is free for that many days, but i want it to be consequtive. How would i go about this? any help is appreciated, thanks :)

1
  • 2
    if(daysWanted >= days && this.bookings[i] == this.bookings[i]){ this line is incorrect because the second condition is always true. Commented May 26, 2015 at 8:16

2 Answers 2

1

change this :

for(int j = 0; j < NUM_DAYS; j++){
    if(bookings[i][j] == null){
        days++;
        if(daysWanted >= days && this.bookings[i] == this.bookings[i]){
            this.displayCell (i, j, Color.red);
        }
    }
}

to something like this:

days = 0;
int[] daysWeWant = new int[daysWanted];
for(int j = 0; j < NUM_DAYS; j++){
    if(bookings[i][j] == null){
       daysWeWant[days] = j;
       days++;
       if(daysWanted == days){
           break;
       }
    }
    else{
        days = 0;
        int[] daysWeWant = new int[daysWanted];
    }
}
if(days== daysWanted ){
    for(int j = 0 ; j< daysWanted; j++){
        int day = daysWeWant [j];
        this.displayCell (i, day , Color.red);
    }
}

If you do this it makes days zero if one day is not avaiable and continue to search again.

Sign up to request clarification or add additional context in comments.

11 Comments

that doesn't work sorry, that just finds a whole bunch of different days @Lrrr
@NandaArdianto check out my update and change your code, this must work
This is always true && this.bookings[i] == this.bookings[i]
this.bookings[i] = i; this.bookings[j] = j; @Lrrr is this what you meant by //add i, j to that array??
@NandaArdianto have an array or a list, and every time a day is free add that day to your list, that array or lists elements count became your desired number is, and if you see a day which date is registered but empty your array and try with next day and so on ...
|
0

I suspect Lrrr was on the right path - you need to reset your days counter for each unit that you are searching. Likely you can change:

int days = 0;
for(int i = 0; i < NUM_UNITS; i++){
    for(int j = 0; j < NUM_DAYS; j++){

to

for(int i = 0; i < NUM_UNITS; i++){
    int days = 0;
    for(int j = 0; j < NUM_DAYS; j++){

But without sample data, it would be a bit of work to check for certain.

You might find that writing some unit tests would help you a lot in working on this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.