How Enum Classes Address a Fundamental Problem in Java’s OOP

Android programming has been an enjoyable and transformative part of my coding journey. Java, the language powering billions of software applications worldwide, is the one I’ve worked with the most, and I plan to continue using it professionally.

Starting in July, I will officially become an Android developer, as I previously mentioned. To prepare, I have resumed coding in Java with one of my original projects. This project, initially named Offline Budget Tracker, is designed to monitor my budget flow using its internal database, specifically the Android Room Database.

However, the app is no longer “offline” since my latest addition connects it to my home network server using LAMP architecture. It now interacts with the MySQL database on a Linux server, a feature I have been experimenting with.

During development, I encountered several challenges that were new to me. One significant issue arose from using the MVVM architecture and attempting to pass data through DTO (data transfer object) classes instead of passing the data directly. This approach led to difficulties in querying when the data types were identical, but the data itself was not.

For example, consider a scenario where I want to execute a query using three elements: store name, date-from, and date-to. The DTO class recognizes this query through its constructor that accepts three arguments.

DtoClass storeDto = new DtoClass ("Amazon", "2024-06-01", "2024-06-14");

The constructor:

public DtoClass(String storeName, String dateFrom, String dateTo) {
        this.storeName = storeName;
        this.dateFrom = dateFrom;
        this.dateTo = dateTo;
}

However, what would you do if you wanted to execute a query for a product name instead of a store name? In this scenario, the data types for both cases are the same: String. As I mentioned earlier, the constructor only recognizes the first argument as a store name.

This is where an enum steps in.

Let’s create an enum class SpendingType.

public enum SpendingType {
    STORE,
    PRODUCT_NAME
}

Here, you can create a new constructor that branches out based on the SpendingType it receives as its first argument. This approach allows the constructor to differentiate between a store name and a product name, even when both are represented as strings.

public BudgetTrackerMysqlSpendingDto(SpendingType type, String name, String dateFrom, String dateTo) {
        if (type == SpendingType.STORE) {
            this.storeName = name;
        } else if (type == SpendingType.PRODUCT_NAME) {
            this.productName = name;
        }
        this.dateFrom = dateFrom;
        this.dateTo = dateTo;
}

And this is how you can initiate the dto class. isn’t it a brilliant solution?

DtoClass storeDto = new DtoClass(SpendingType.STORE, "Amazon", "2024-06-01", "2024-06-14");
DtoClass productDto = new DtoClass(SpendingType.PRODUCT, "Laptop", "2024-06-01", "2024-06-14");

Conclusion:

Through Android programming, I have learned a great deal about Java. This knowledge is transferable to various Java-related frameworks and software development formats. Most importantly, I am passionate about Android and cherish being part of its open-source community, including Custom ROMs. I truly love Android!

Leave a Reply