HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpModerate

Removing code smell from Room Booking code

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
bookingremovingcodefromsmellroom

Problem

I am new with coding and I recently read an article about code refactoring, so I have made a console application for booking rooms on a ship. I need help with code refactoring. As far as I understand the refactoring, I think in my project there are only 2 parts where I need refactoring, which are as follows.

One is a block of for statements:

ship1 = new Ship("Olympic Countess");

    ArrayList groupA = new ArrayList();
    for (int i = 0; i < 10; i++)
    {
        groupA.Add(new room(5000, "A" + (i + 1)));
    }

    ArrayList groupB = new ArrayList();
    for (int i = 0; i < 10; i++)
    {
        groupB.Add(new room(4000, "B" + (i + 1)));
    }

    ArrayList groupC = new ArrayList();
    for (int i = 0; i < 30; i++)
    {
        groupC.Add(new room(3500, "C" + (i + 1)));
    }

    ArrayList groupD = new ArrayList();
    for (int i = 0; i < 36; i++)
    {
        groupD.Add(new room(3400, "D" + (i + 1)));
    }

    ArrayList groupE = new ArrayList();
    for (int i = 0; i < 40; i++)
    {
        groupE.Add(new room(3300, "E" + (i + 1)));
    }

    ArrayList groupF = new ArrayList();
    for (int i = 0; i < 30; i++)
    {
        groupF.Add(new room(3400, "F" + (i + 1)));
    }

    ArrayList groupG = new ArrayList();
    for (int i = 0; i < 36; i++)
    {
        groupG.Add(new room(3300, "G" + (i + 1)));
    }

    ArrayList groupH = new ArrayList();
    for (int i = 0; i < 40; i++)
    {
        groupH.Add(new room(3200, "H" + (i + 1)));
    }

    ship1.addDeck("Balcony Suite", groupA);
    ship1.addDeck("Suite", groupB);
    ship1.addDeck("Deck 3 - Outside Twin", groupC);
    ship1.addDeck("Deck 2 - Outside Twin", groupD);
    ship1.addDeck("Deck 1 - Outside Twin", groupE);
    ship1.addDeck("Deck 3 - Inside Twin", groupF);
    ship1.addDeck("Deck 2 - Inside Twin", groupG);
    ship1.addDeck("Deck 1 - Inside Twin", groupH);      
}


and the other one is the if else statement as follows:

```
public Reservation bookPassage(String cabinclass,

Solution

None of this information should be written into your program's source code. Rather, it should be stored in a configuration file, perhaps in XML, JSON, or YAML format — or maybe stored in a database instead. Not only would your code be less repetitive, it would also be more flexible, in that you wouldn't have to rebuild the application just to change trivial things such as product offerings and prices.

Context

StackExchange Code Review Q#61170, answer score: 15

Revisions (0)

No revisions yet.