Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * PROBLEM *
- A Java developer wants to save a few user settings such as
- volume_level = 3, theme_color = green, username = John.
- Now, a database would be too heavy to save just these few values.
- Assess this situation and suggest an alternative approach to save these values.
- Provide code for your suggestion.
- **/
- /**
- * SOLUTION *
- A user defined data type like 'class' may could be a good choice for saving user settings values.
- As each users' data is well structured. So each user's data can be identified easier than saving in a list.
- Here's a simple demonstration...
- **/
- class UserSettings {
- public Integer volumeLevel;
- public String themeColor;
- public String username;
- // Default Constructor
- public UserSettings () {}
- // Parameterized Constructor
- public UserSettings(Integer volumeLevel, String themeColor, String username) {
- this.volumeLevel = volumeLevel;
- this.themeColor = themeColor;
- this.username = username;
- }
- public void printAttributes() {
- System.out.println("volume_level = " + this.volumeLevel);
- System.out.println("theme_color = " + this.themeColor);
- System.out.println("username = " + this.username);
- System.out.println("\n");
- }
- }
- public class Problem2 {
- public static void main(String[] args) {
- UserSettings user1 = new UserSettings(3, "green", "John");
- System.out.println("User settings for user 1:");
- user1.printAttributes();
- UserSettings user2 = new UserSettings();
- user2.username = "Alex";
- user2.themeColor = "red";
- user2.volumeLevel = 5;
- System.out.println("User settings for user 2:");
- user2.printAttributes();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement