Advertisement
farhansadaf

Problem 2

Jul 20th, 2020
1,533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.63 KB | None | 0 0
  1. /**
  2.  * PROBLEM *
  3.  A Java developer wants to save a few user settings such as
  4.  volume_level = 3, theme_color = green, username = John.
  5.  Now, a database would be too heavy to save just these few values.
  6.  Assess this situation and suggest an alternative approach to save these values.
  7.  Provide code for your suggestion.
  8.  **/
  9.  
  10. /**
  11.  * SOLUTION *
  12.  A user defined data type like 'class' may could be a good choice for saving user settings values.
  13.  As each users' data is well structured. So each user's data can be identified easier than saving in a list.
  14.  
  15.  Here's a simple demonstration...
  16.  **/
  17. class UserSettings {
  18.     public Integer volumeLevel;
  19.     public String themeColor;
  20.     public String username;
  21.    
  22.     // Default Constructor
  23.     public UserSettings () {}
  24.    
  25.     // Parameterized Constructor
  26.     public UserSettings(Integer volumeLevel, String themeColor, String username) {
  27.         this.volumeLevel = volumeLevel;
  28.         this.themeColor = themeColor;
  29.         this.username = username;
  30.     }
  31.    
  32.     public void printAttributes() {
  33.         System.out.println("volume_level = " + this.volumeLevel);
  34.         System.out.println("theme_color  = " + this.themeColor);
  35.         System.out.println("username = " + this.username);
  36.         System.out.println("\n");
  37.     }
  38.    
  39. }
  40.  
  41. public class Problem2 {
  42.  
  43.     public static void main(String[] args) {
  44.         UserSettings user1 = new UserSettings(3, "green", "John");
  45.         System.out.println("User settings for user 1:");
  46.         user1.printAttributes();
  47.        
  48.         UserSettings user2 = new UserSettings();
  49.         user2.username = "Alex";
  50.         user2.themeColor = "red";
  51.         user2.volumeLevel = 5;
  52.         System.out.println("User settings for user 2:");
  53.         user2.printAttributes();
  54.     }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement