/** * Updates the stored user with any not null information in the passed user * * @param u The user to update from. * @throws InvalidFieldException Thrown if anything is of the wrong format. */ public void update(User u) throws InvalidFieldException { if (u.getFirstName() != null) { setFirstName(u.getFirstName()); } if (u.getLastName() != null) { setLastName(u.getLastName()); } if (u.getID() != null) { setID(u.getID()); } if (u.getPoints() != getPoints()) { setPoints(u.getPoints()); } if (u.getWeeklyPick() != null && !u.getWeeklyPick().isNull()) { setWeeklyPick(u.getWeeklyPick()); } if (u.getUltimatePick() != null && !u.getUltimatePick().isNull()) { setUltimatePickNoSetPts(u.getUltimatePick()); } if (u.getUltimatePoints() != getUltimatePoints()) { setUltimatePoints(u.getUltimatePoints()); } }
// ====== TEST DRIVER ===== // public static void main(String[] args) { User u = null; try { u = new User("bob", "builder", "bbuilde"); } catch (InvalidFieldException e) { } Contestant c = new Contestant(); Contestant ul = new Contestant(); try { c.setID("aa"); ul.setID("ab"); u.setWeeklyPick(c); u.setUltimatePick(ul); } catch (InvalidFieldException e1) { e1.printStackTrace(); } try { System.out.println(u.toJSONObject().toString()); } catch (ParseException e) { e.printStackTrace(); } }
/** * Turns a JSON object into a User. * * @param o JSON object */ public void fromJSONObject(JSONObject o) { try { setFirstName((String) o.remove(KEY_FIRST_NAME)); setLastName((String) o.remove(KEY_LAST_NAME)); setID((String) o.remove(KEY_ID)); setPoints(Utils.numToInt(o.remove(KEY_POINTS))); String id = (String) o.remove(KEY_WEEKLY_PICK_ID); Contestant c = null; GameData g = GameData.getCurrentGame(); if (id.equals(Contestant.NULL_ID)) { c = new Contestant(); c.setNull(); } else { c = g.getContestant(id); } setWeeklyPick(c); id = (String) o.remove(KEY_ULT_PICK_ID); if (id.equals(Contestant.NULL_ID)) { c = new Contestant(); c.setNull(); } else { c = g.getContestant(id); } setUltimatePick(c); setUltimatePoints(Utils.numToInt(o.remove(KEY_WIN_PICK_POINTS))); setNumBonusAnswer(((Number) o.remove(KEY_NUM_BONUS_ANSWER)).intValue()); } catch (InvalidFieldException e) { System.out.println("Warning: InvalidFieldException in fromJSONObject"); e.printStackTrace(); } }