I am making an application and I have a question... I explain:
User registration is done directly in the class (Platform). To register, the user must indicate a username, a password and his full name (Name and Surname).
It must be taken into account that:
A user identified him by his username. There can be no users with duplicate usernames.
Two users are considered equal if their username, password and full name are the same.
As a result of the creation, a new object of type User will be generated. This object will allow you to interact with the platform class. In case a user with the same name already exists, the new user will not be created, and null will be returned to indicate the error.
To check if a user exists or not, I implement the findUser method, which allows me to search for a registered user based on their username. If it does not exist, this method will return a null value.
To test the correct operation of the developed code, I have the test class: PR1_Ex2_1_Test
The classes are defined like this:
The Platform class which is where I do all the methods
package edu.uoc.dpoo;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Guillermo Rodriguez Barcelo
*/
public class Platform {
private List<Competition> competitions;
private List<User> users;
public Platform(){
competitions = new ArrayList<Competition>();
users = new ArrayList<User>();
}
public User registerUser(String username ,String password,String fullname){
User p = findUser(username);
if (p == null) {
p = new User(username, password,fullname);
users.add(p);
}
else {
p.getUserName();
}
return (User) p;
}
public User findUser (String username) {
User user = null;
for (User p : users) {
if (username.equals(p.getUserName())) {
user = p;
break;
}
}
return user;
}
public User login(String username,String password){
return null;
}
public Integer getNumUsers(){
Integer size=users.size();
return size ;
}
public Integer getNumCompetitions(){
return null;
}
public Message sendMessage(User from,String to,String subject,String message){
return null;
}
private float evaluateAll(){
return (float) 0.0;
}
public List<Competition> getOpenCompetitions(){
return null;
}
private User User(User u) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
The User class
package edu.uoc.dpoo;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Guillermo Rodriguez Barcelo
*/
public class User {
private String username;
private String password;
private String fullName;
private Platform platform;
private List<Message> inBox;
private List<Message> outBox;
public User (String username, String password, String fullName) {
this.username = username;
this.password = password;
this.fullName = fullName;
inBox = new ArrayList<Message>();
outBox = new ArrayList<Message>();
}
User(String password, String fullname) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public String getUserName() {
return username;
}
public String getPassword() {
return password;
}
public String getFullName() {
return fullName;
}
public void setUserName(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setFullName(String fullname) {
this.fullName = fullname;
}
public boolean checkPasword(String password){
return true;
}
public List<Message> getMessages() {
return null;
}
public Organizer asOrganizer(){
return null;
}
public Participant asParticipant(){
return null;
}
public Message sendMessage(String to,String Subject,String message){
return null;
}
public String toString () {
return null;
}
@Override
public boolean equals (Object obj) {
return (boolean) obj;
}
public List<Competition> myCompetitions(){
return null;
}
}
Finally the class where I do the test and the method fails me
User u2=platform.registerUser(username1, password1, fullName1);
assertNull(u2);
assertEquals((int)platform.getNumUsers(), 1);
the class stays like this
package edu.uoc.dpoo;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import edu.uoc.dpoo.Organizer;
import edu.uoc.dpoo.User;
import edu.uoc.dpoo.Platform;
import edu.uoc.dpoo.Participant;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author xavie
*/
public class PR1_Ex2_1_Test {
private final String username1 = "username1";
private final String password1 = "password1";
private final String fullName1 = "Test User 1";
private final String username2 = "username2";
private final String password2 = "password2";
private final String fullName2 = "Test User 2";
public PR1_Ex2_1_Test() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
// Test create new user
@Test
public void createUser() {
Platform platform = new Platform();
// Check that no user is in the platform
assertEquals((int)platform.getNumUsers(), 0);
// Register new user
User u1=platform.registerUser(username1, password1, fullName1);
// User is not NULL
assertNotNull(u1);
// User full name is correct
assertEquals(u1.getFullName(), fullName1);
// Check that only 1 user is in the platform (login does not created new user)
assertEquals((int)platform.getNumUsers(), 1);
}
// Test create new user controls
@Test
public void createUserControls() {
Platform platform = new Platform();
// Check that no user is in the platform
assertEquals((int)platform.getNumUsers(), 0);
// Register new user
User u1=platform.registerUser(username1, password1, fullName1);
assertNotNull(u1);
assertEquals((int)platform.getNumUsers(), 1);
// Try to register the same user again
User u2=platform.registerUser(username1, password1, fullName1);
assertNull(u2);
assertEquals((int)platform.getNumUsers(), 1);
// Register new user
User u3=platform.registerUser(username2, password2, fullName2);
assertNotNull(u3);
assertEquals((int)platform.getNumUsers(), 2);
}
}
I don't understand why it fails me in the methods
assertNull(u2);
assertEquals((int)platform.getNumUsers(), 1);
Well, I already have the answer to my question, the implementation of the methods of the plattform class to be able to pass the test looks like this:
I managed to get it out in the end.