views:

35

answers:

1

Hi guys, I have a gross looking function and am wanting to clean it up. All it contains is some maps, a for loop, and if statements.

Basically I have a map that I would like to grab only certain information from but one of the keys that I need from it changes by one number in each map.

I was thinking maybe a simple switch statement or something should fix it, but I often get confused with simplifying such things.

Here is what the function looks like:

public void separateBooksFromList() {

book1 = [:]                     //map for book1
book2 = [:]                     //map for book2
book3 = [:]                     //map for book3
book4 = [:]                     //map for book4
book5 = [:]                     //map for book5
book6 = [:]                     //map for book6
book7 = [:]                     //map for book7
book8 = [:]                     //map for book8
book9 = [:]                     //map for book9
book10 = [:]                    //map for book10

lastModified = new Date(dataFile.lastModified())         //last time the file was scanned
readDate = new Date()                                    //current date the text file was read

for(int i = 0; i < bookList.size(); i++) {


    if(i==0) {
        book1['lastScan'] = lastModified
        book1['readDate'] = readDate
        book1['bookNumber'] = bookList['Book Number 0'][i]      // <- only part of the map that changes
        book1['bookTitle'] = bookList['Book Title'][i]

    }

    if(i==1) {
        book2['lastScan'] = lastModified
        book2['readDate'] = readDate
        book2['bookNumber'] = bookList['Book Number 1'][i]      // <- only part of the map that changes
        book2['bookTitle'] = bookList['Book Title'][i]
    }

    if(i==2) {
        book3['lastScan'] = lastModified
        book3['readDate'] = readDate
        book3['bookNumber'] = bookList['Book Number 2'][i]      // <- only part of the map that changes
        book3['bookTitle'] = bookList['Book Title'][i]
    }   

    if(i==3) {
        book4['lastScan'] = lastModified
        book4['readDate'] = readDate
        book4['bookNumber'] = bookList['Book Number 3'][i]      // <- only part of the map that changes
        book4['bookTitle'] = bookList['Book Title'][i]      
    }   

    if(i==4) {
        book5['lastScan'] = lastModified
        book5['readDate'] = readDate
        book5['bookNumber'] = bookList['Book Number 4'][i]      // <- only part of the map that changes
        book5['bookTitle'] = bookList['Book Title'][i]      
    }   

    if(i==5) {
        book6['lastScan'] = lastModified
        book6['readDate'] = readDate
        book6['bookNumber'] = bookList['Book Number 5'][i]      // <- only part of the map that changes
        book6['bookTitle'] = bookList['Book Title'][i]
    }   

    if(i==6) {
        book7['lastScan'] = lastModified
        book7['readDate'] = readDate
        book7['bookNumber'] = bookList['Book Number 6'][i]      // <- only part of the map that changes
        book7['bookTitle'] = bookList['Book Title'][i]
    }

    if(i==7) {
        book8['lastScan'] = lastModified
        book8['readDate'] = readDate
        book8['bookNumber'] = bookList['Book Number 7'][i]      // <- only part of the map that changes
        book8['bookTitle'] = bookList['Book Title'][i]  
    }

    if(i==8) {
        book9['lastScan'] = lastModified
        book9['readDate'] = readDate
        book9['bookNumber'] = bookList['Book Number 8'][i]      // <- only part of the map that changes
        book9['bookTitle'] = bookList['Book Title'][i]  
    }

    if(i==9) {
        book10['lastScan'] = lastModified
        book10['readDate'] = readDate
        book10['bookNumber'] = bookList['Book Number 9'][i]     // <- only part of the map that changes
        book10['bookTitle'] = bookList['Book Title'][i]
    }

  }

}

As you can see, it's quite an ugly function.

Am I able to do a simple switch statement or something to cut down the code and make it look more professional?

+2  A: 

There are a few things you can do here. You can use a list of books rather than book1, book2, etc... This will save repeating yourself so much.

This is almost the same but with that change. It will create a list of size 10 (assuming 10 books) with a map entry like what you had before.

public void separateBooksFromList() {
    lastModified = new Date(dataFile.lastModified())         //last time the file was scanned
    readDate = new Date()                                    //current date the text file was read

    // Use a list of Maps instead of separate variables
    int numberOfBooks =  bookList.size()
    def books = []
    numberOfBooks.times {
        books[it] = [
            lastScan: lastModified, 
            readDate: readDate, 
            bookNumber: booklist["Book Number $it"][it], 
            bookTitle: booklist["BookTitle"][it]
        ]
    }
}
Chris Dail
Wow that's pretty cool. I had to look up 'int.times' to see what it did. Thank you for showing me this Chris. It is very helpful and looks very clean. So if I wanted grails to generate a page from the list of books, I could just have it iterate through and add the values based on the keys correct?Thank you again
StartingGroovy
.times is a fun function, but you should use eachWithIndex instead. Even better, the collect method lets you build up a list as you go, but won't work in this situation since you are using the indices inside the final list.
Blacktiger
Thank you for more tips Blacktiger. I ended up using eachWithIndex to print them out and make sure everything is in working order. I will look a bit more into the collect method as well. It's unfortunate groovy is slow compared to java, I enjoy how it cuts down so much code though.
StartingGroovy