Преглед изворни кода

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<User>
- Added update(User, User) to update/replace an item in the List
master
BinHong Lee пре 8 година
родитељ
комит
4ab4761225
2 измењених фајлова са 77 додато и 2 уклоњено
  1. +7
    -2
      User.java
  2. +70
    -0
      UserFactory.java

+ 7
- 2
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;


+ 70
- 0
UserFactory.java Прегледај датотеку

@@ -0,0 +1,70 @@
/*
* Written by : Bin Hong Lee
* Last edited : 6/6/2016
*/

import java.util.*;

class UserFactory
{
private List<User> users = new List<User>;
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);
}
}
}
}

Loading…
Откажи
Сачувај