Last active
December 20, 2022 11:41
-
-
Save sezRR/df30117a05754e16e3f9ac6d5f1d9244 to your computer and use it in GitHub Desktop.
Codes for users' update history
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Date; | |
import java.util.List; | |
/* | |
{ | |
"_id": "507f1f77bcf86cd799439011", | |
"user_id": "507f191e810c19729de860ea", | |
"changes": [ | |
{ | |
"field": "name", | |
"old_value": "Sezer", | |
"new_value": "szrr" | |
}, | |
{ | |
"field": "is_confirmed", | |
"old_value": false, | |
"new_value": true | |
} | |
], | |
"updated_at": "2020-05-18T14:10:30.000Z" | |
} | |
*/ | |
public class Main { | |
public static void main(String[] args) { | |
Change change1 = new Change("is_confirmed", false, true); | |
Change change2 = new Change("name", "Sezer", "szRR"); | |
List<Change> updates = new ArrayList<>(); | |
updates.add(change1); | |
updates.add(change2); | |
Changes changes = new Changes("ID", "USERID", updates, new Date()); | |
for (var change : changes.getChanges()) { | |
System.out.println("Field: " + change.getField().getClass().getName()); | |
System.out.println("Old value: " + change.getOldValue().getClass().getName()); | |
if (change.getOldValue() instanceof Boolean) { | |
System.out.println(" Boolean detected!"); | |
boolean b = (Boolean) change.getOldValue(); | |
System.out.println(" " + b); | |
} | |
System.out.println("New value: " + change.getNewValue().getClass().getName()); | |
} | |
} | |
} | |
class Change { | |
private String field; | |
private Object oldValue; | |
private Object newValue; | |
public Change(String field, Object oldValue, Object newValue) { | |
this.field = field; | |
this.oldValue = oldValue; | |
this.newValue = newValue; | |
} | |
public String getField() { | |
return field; | |
} | |
public void setField(String field) { | |
this.field = field; | |
} | |
public Object getOldValue() { | |
return oldValue; | |
} | |
public void setOldValue(Object oldValue) { | |
this.oldValue = oldValue; | |
} | |
public Object getNewValue() { | |
return newValue; | |
} | |
public void setNewValue(Object newValue) { | |
this.newValue = newValue; | |
} | |
} | |
class Changes { | |
private String id; | |
private String userId; | |
private List<Change> changes; | |
private Date updatedAt; | |
public Changes(String id, String userId, List<Change> changes, Date updatedAt) { | |
this.id = id; | |
this.userId = userId; | |
this.changes = changes; | |
this.updatedAt = updatedAt; | |
} | |
public String getId() { | |
return id; | |
} | |
public void setId(String id) { | |
this.id = id; | |
} | |
public String getUserId() { | |
return userId; | |
} | |
public void setUserId(String userId) { | |
this.userId = userId; | |
} | |
public List<Change> getChanges() { | |
return changes; | |
} | |
public void setChanges(List<Change> changes) { | |
this.changes = changes; | |
} | |
public Date getUpdatedAt() { | |
return updatedAt; | |
} | |
public void setUpdatedAt(Date updatedAt) { | |
this.updatedAt = updatedAt; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment