ElDorado-Travel-and-Tour-Management-System icon indicating copy to clipboard operation
ElDorado-Travel-and-Tour-Management-System copied to clipboard

Bad Coding Smells concerns: Data Clumps

Open Jazarumag opened this issue 1 year ago • 0 comments

THIS ISSUE IS POSTED AS A COLLEGE ASSIGNMENT ON BAD CODING SMELLS AND REFACTORING TECHNIQUES, PLEASE CLOSE THIS ISSUE IF IT'S OF NO USE.

I found some bad smells in your coding, so i tried to fix them by refactoring some parts of it.

To eliminate the bad smell of "data clumps" in the class MongoDB.java we can apply "Extract Class" and "Replace Temp with Query", you can create a class that represents the information of the documents (for example, a Bus or Train), and then Use this class instead of passing multiple parameters in this way the vehicle data is better organized.

public class TransportInfo {
    private String from;
    private String to;
    private String name;
    private String type;
    private String time;
    private int fare;
    private int ticketAvailability;
}

Next, we adjust the mongoDBBusFinder and mongoDBTrainFinder methods to use this new class and apply "Replace Temp with Query" to improve code readability:

public class MongoDBFinder {
    ArrayList<Integer> mongoDBBusFinder(String from, String to) {
        int sum = 1;
        ArrayList<Integer> array = new ArrayList<>();
        for (Document doc : iterdocbus) {
            if (doc.get("To").equals(to)) {
                TransportInfo busInfo = createBusInfo(doc, from);
                array.add(busInfo.getFare());
                displayTransportInfo(sum, busInfo);
                sum++;
            }
        }
        return array;
    }

    ArrayList<Integer> mongoDBTrainFinder(String from, String to) {
        int sum = 1;
        ArrayList<Integer> array2 = new ArrayList<>();
        for (Document doc : iterdoctrain) {
            if (doc.get("To").equals(to)) {
                TransportInfo trainInfo = createTrainInfo(doc, from);
                array2.add(trainInfo.getFare());
                displayTransportInfo(sum, trainInfo);
                sum++;
            }
        }
        return array2;
    }

    private TransportInfo createBusInfo(Document doc, String from) {
        return new TransportInfo(from, doc.get("To").toString(), doc.get("Bus").toString(),
                doc.get("Time").toString(), Integer.parseInt(doc.get("Fare").toString()),
                Integer.parseInt(doc.get("TicketAvail").toString()));
    }

    private TransportInfo createTrainInfo(Document doc, String from) {
        return new TransportInfo(from, doc.get("To").toString(), doc.get("Train").toString(),
                doc.get("Class").toString(), doc.get("Time").toString(),
                Integer.parseInt(doc.get("Fare").toString()), Integer.parseInt(doc.get("TicketAvail").toString()));
    }

    private void displayTransportInfo(int sum, TransportInfo transportInfo) {
        System.out.print("\n\t\t\t\t|");
        System.out.format("%5s%10s%10s%20s%20s%6s%4s%4s", sum + "\t|", transportInfo.getFrom(),
                "-" + transportInfo.getTo() + "\t|", transportInfo.getName() + "\t|", transportInfo.getType() + "\t|",
                transportInfo.getTime() + "\t|", transportInfo.getFare() + "\t|\t", transportInfo.getTicketAvailability() + "\t|");
    }
}

Jazarumag avatar Jan 09 '24 01:01 Jazarumag