Ver a proveniência

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 há 8 anos
ascendente
cometimento
4ab4761225
2 ficheiros alterados com 77 adições e 2 eliminações
  1. +7
    -2
      User.java
  2. +70
    -0
      UserFactory.java

+ 7
- 2
User.java Ver ficheiro

@@ -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 Ver ficheiro

@@ -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);
}
}
}
}

Carregando…
Cancelar
Guardar