From 4ab47612252dda0ac419a3c6870c4e90a4869f51 Mon Sep 17 00:00:00 2001 From: BinHong Lee Date: Tue, 7 Jun 2016 00:11:28 -0700 Subject: [PATCH] getId(), UserFactory User.java - Added getId() UserFactory - Added simple constructor (to be modified further) - Added newUser(), getUser(String), getUser(int) for interaction with the List - Added update(User, User) to update/replace an item in the List --- User.java | 9 +++++-- UserFactory.java | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 UserFactory.java diff --git a/User.java b/User.java index 8e73f6c..314e137 100644 --- a/User.java +++ b/User.java @@ -1,9 +1,9 @@ /* * Written by : Bin Hong Lee - * Last edited : 6/4/2016 + * Last edited : 6/6/2016 */ - import java.util.*; +import java.util.*; class User { @@ -24,6 +24,11 @@ class User return name; } + public int getId() + { + return id; + } + public void setName() { this.name = name; diff --git a/UserFactory.java b/UserFactory.java new file mode 100644 index 0000000..e45afe3 --- /dev/null +++ b/UserFactory.java @@ -0,0 +1,70 @@ +/* + * Written by : Bin Hong Lee + * Last edited : 6/6/2016 + */ + +import java.util.*; + +class UserFactory +{ + private List users = new List; + private int id; + + public UserFactory() + { + id = 0; + } + + public User newUser(String name; int limit) + { + User temp = new User(name, id, limit); + users.add(temp); + + id++; + + return temp; + } + + public User getUser(String name) + { + for (int i = 0; i < users.size(); i++) + { + User temp = users.get(i); + + if(temp.getName() == name) + { + return temp; + } + } + + throw new NullPointerException(); + } + + public User getUser(int index) + { + for (int i = 0; i < users.size(); i++) + { + User temp = users.get(i); + + if(temp.getId() == index) + { + return temp; + } + } + + throw new NullPointerException(); + } + + public void update(User old, User new) + { + for (int i = 0; i < users.size(); i++) + { + User temp = users.get(i); + + if(temp.getId() == old.getId()) + { + users.set(i, new); + } + } + } +}