Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring Suggestion - Addressing Long Method #49

Open
lborja04 opened this issue Jan 9, 2024 · 0 comments
Open

Refactoring Suggestion - Addressing Long Method #49

lborja04 opened this issue Jan 9, 2024 · 0 comments

Comments

@lborja04
Copy link

lborja04 commented Jan 9, 2024

Hi shouryaj98,

I trust this message finds you well. During my recent code review, I noticed a potential improvement opportunity in the bookroom method. It exhibits characteristics of a "Long Method," which can impact code readability and maintainability. I'd like to propose a refactoring using the "Preserve Whole Object" technique.

Why Refactor?

Readability: Long methods can be challenging to read and understand. Breaking them into smaller, focused methods enhances code readability.
Maintainability: Smaller methods are easier to maintain and update, promoting a more agile development process.
Refactoring Proposal:

public class RoomBooking {
    private static Scanner sc = new Scanner(System.in);

    public static void bookroom(int roomType) {
        int roomNumber = displayAvailableRooms(roomType);

        if (roomNumber != -1) {
            try {
                CustDetails(roomType, roomNumber);
                System.out.println("Room Booked");
            } catch (Exception e) {
                System.out.println("Invalid Option");
            }
        } else {
            System.out.println("Enter valid option");
        }
    }

    private static int displayAvailableRooms(int roomType) {
        int roomNumberOffset = 0;
        Room[] rooms;

        switch (roomType) {
            case 1:
                rooms = hotel_ob.luxury_doublerrom;
                break;
            case 2:
                rooms = hotel_ob.deluxe_doublerrom;
                roomNumberOffset = 10;
                break;
            case 3:
                rooms = hotel_ob.luxury_singleerrom;
                roomNumberOffset = 30;
                break;
            case 4:
                rooms = hotel_ob.deluxe_singleerrom;
                roomNumberOffset = 40;
                break;
            default:
                return -1;
        }

        System.out.println("\nChoose room number from : ");
        for (int j = 0; j < rooms.length; j++) {
            if (rooms[j] == null) {
                System.out.print(j + roomNumberOffset + ",");
            }
        }

        System.out.print("\nEnter room number: ");
        return sc.nextInt() - roomNumberOffset;
    }
}

By breaking down the bookroom method into smaller, focused methods, we improve the overall readability and maintainability of the code. Each method now has a clear and specific responsibility.

Feel free to incorporate this suggestion, and if you have any questions or need further assistance, please don't hesitate to reach out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant