Sample Problem: DS for balance in Java

Designing an e-Wallet using HashMap in Java

In this digital age, e-Wallets have become an integral part of our lives, allowing us to store, manage, and transfer funds electronically. A well-designed e-Wallet can provide users with convenience, security, and accessibility to their funds. In this article, we will explore how to design an e-Wallet using a HashMap data structure in Java.

Problem Statement

Suppose we are given with user IDs and their respective balances, and we need to design a data structure to maintain these balances with user IDs. We will perform two kinds of operations on the designed data structure:

  1. set(user id, balance): If the user ID given doesn't exist in the data structure, this method will insert this user ID along with the balance provided. If the user ID already exists, then this method will update the previous balance corresponding to this user ID.

  2. get(user id): This method will return the balance corresponding to the given user ID.

Solution

The idea to efficiently implement such a data structure is to use a HashMap in Java because it allows us to search for an item in O(1) time and also insert/update in O(1) time.

We can simply create a HashMap in Java with user IDs as keys and their corresponding balance as values. Whenever we call the get() method, it will simply return the corresponding value for the user ID, and whenever we call the set() method, it will insert/update the value corresponding to the given key.

java
import java.util.*; class UserList { private HashMap<Integer, Integer> userBalances; UserList() { userBalances = new HashMap<>(); } int get(int id) { return userBalances.getOrDefault(id, 0); } void set(int id, int balance) { userBalances.put(id, balance); } } class EWallet { public static void main(String args[]) { UserList userList = new UserList(); userList.set(1, 100); userList.set(2, 2000); System.out.println(userList.get(1)); userList.set(1, 5000); System.out.println(userList.get(1)); System.out.println(userList.get(2)); } }

Let's go through the code above. We first create a class UserList that contains a HashMap of user IDs and their corresponding balances. In the get() method, we retrieve the balance corresponding to the given user ID using the getOrDefault() method. If the user ID does not exist, it returns a default value of 0. In the set() method, we simply put the user ID and balance into the HashMap.

In the EWallet class, we create an instance of UserList and perform some basic operations on it to demonstrate how to use the data structure.

Example

Let's consider an example to demonstrate the usage of the e-Wallet data structure. Suppose we have the following operations:

java
public class Main { public static void main(String[] args) { UserList userList = new UserList(); userList.set(1, 100); userList.set(2, 2000); userList.set(3, 300); userList.set(4, 4000); System.out.println(userList.get(2)); // Output: 2000 userList.set(1, 5000); userList.set(3, 200); System.out.println(userList.get(1)); // Output: 5000 System.out.println(userList.get(3)); // Output: 200 } }

In this program, we first create a new UserList object called userList. We then use the set() method to set the balances for four different users: user 1 has a balance of 100, user 2 has a balance of 2000, user 3 has a balance of 300, and user 4 has a balance of 4000.

Next, we use the get() method to retrieve the balance for user 2 and print it out. The output should be 2000, since that is the balance we set for user 2 earlier.

We then use the set() method again to update the balances for users 1 and 3, and use the get() method to retrieve the updated balances and print them out. The output should be 5000 for user 1 and 200 for user 3, since those are the updated balances we set.

With this program, we have demonstrated how to use the UserList class to maintain balances for a list of users using a HashMap in Java. This data structure is efficient and allows us to easily retrieve and update balances for each user.

let's further explore the capabilities of our UserList class. As mentioned earlier, we can easily update the balance of an existing user using the set() method, and the corresponding balance will be updated in O(1) time.

userList.set(1, 5000); userList.set(3, 200);

After executing the above code, the balance for user with id 1 will be updated to 5000, and the balance for user with id 3 will be updated to 200.

Now, let's see how we can retrieve the updated balances using the get() method.

System.out.println(userList.get(1)); // Output: 5000 System.out.println(userList.get(3)); // Output: 200

As expected, the output of the above code will be 5000 and 200, respectively. The get() method is able to retrieve the updated balances in O(1) time.

We can also add new users to our UserList by calling the set() method with a new user id and balance. Let's add a new user with id 5 and balance 50000.

userList.set(5, 50000);

Now, if we call the get() method for user id 5, we will get the balance of 50000.

System.out.println(userList.get(5)); // Output: 50000

In addition to adding new users, we can also remove users from our UserList by using the remove() method. The remove() method takes a user id as an argument and removes the user with the given id from our UserList.

userList.remove(4);

After executing the above code, the user with id 4 will be removed from our UserList.

We can also get a list of all the user ids in our UserList by using the keySet() method. The keySet() method returns a set of all the keys (user ids) in our HashMap.

System.out.println(userList.keySet()); // Output: [1, 2, 3, 5]

As expected, the output of the above code will be a set containing the user ids 1, 2, 3, and 5.

import java.util.*; class UserList { HashMap<Integer, Integer> balances; public UserList() { balances = new HashMap<Integer, Integer>(); } public void set(int userId, int balance) { balances.put(userId, balance); } public int get(int userId) { return balances.getOrDefault(userId, 0); } public void remove(int userId) { balances.remove(userId); } public Set<Integer> keySet() { return balances.keySet(); } }

In this code, we define a class called UserList, which has a HashMap called balances to store the balances for each user. The constructor initializes the HashMap, and the set() and get() methods are used to set and retrieve the balance for a given user id.

The remove() method removes a user from the list, and the keySet() method returns a set of all the user ids in the list.

Note that in the get() method, we use the getOrDefault() method of the HashMap class to return a default value of 0 if the user id is not present in the map. This is because if a user id is not present in the map, it means that the user has a balance of 0.

With this implementation, we can easily maintain balances for a large number of users in O(1) time using a HashMap in Java.

Overall, the UserList class provides an efficient way to maintain balances for a large number of users using a HashMap in Java. The get() and set() methods allow us to retrieve and update balances for any user in O(1) time, while the remove() method and keySet() method allow us to manipulate the user data as needed.

Previous Post Next Post