Problem implementation of two methods in program

0

I have a parent class called User and two child classes that inherit from it called Organizer and Platform as follows.

User

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




    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 checkPassword(String password) {

        if (password.equals(this.password)) {

            return true;
        } else {
            return false;
        }
    }

    public List < Message > getMessages() {
        return null;
    }
    public Organizer asOrganizer() {
        Organizer ObjetoO = new Organizer(username, password, fullName);
        return ObjetoO;
    }



    public Participant asParticipant() {
        Participant ObjetoP = new Participant(username, password, fullName);
        return ObjetoP;

    }



    public Message sendMessage(String to, String Subject, String message) {
        return null;
    }

    public String toString() {
        return null;
    }

    @Override
    public boolean equals(Object o) {
        boolean prueba = false;

        if ((o != null) && (o instanceof User)) {
            User p = (User) o;
            if (this.username == null && this.password == null && this.fullName == null) {
                prueba = (p.username == null);
                prueba = (p.password == null);
                prueba = (p.fullName == null);
            } else {
                prueba = this.username.equals(p.username);
                prueba = this.password.equals(p.password);
                prueba = this.fullName.equals(p.fullName);
            }
        }

        return prueba;
    }

    public List < Competition > myCompetitions() {
        return null;
    }


    User(User u1) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}

Organizer

/**
 *
 * @author Oviedin33
 */
public class Organizer extends User {
    private List < Competition > competitions;

    public Organizer(String username, String password, String fullName) {
        super(username, password, fullName);
        competitions = new ArrayList < Competition > ();

    }
    public boolean removeSubmission(Submission submission) {
        return true;

    }
    public boolean sendMessage(Competition competition, String subject, String message) {
        return true;

    }
    public Competition newCompetition(String tittle, float target) {
        return null;

    }
    public void closeCompetition(Competition competition) {

    }

}

Participant

package edu.uoc.dpoo;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Oviedin33
 */
public class Participant extends User {

    private List < Competition > competitions;
    private List < Submission > submission;



    public Participant(String username, String password, String fullName) {
        super(username, password, fullName);
        competitions = new ArrayList < Competition > ();
        submission = new ArrayList < Submission > ();
    }
    public Submission submitPrediction(Competition competition, float prediction) {
        return null;
    }
    public List < Submission > getSubmissions() {
        return null;
    }

    void asParticipant(Submission aThis) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}

I must implement the methods asOrganizer and asParticipant of the class User , these methods must create an object of the specific class, and initialize the parent class with the data of the object User . To perform this task, it is requested to implement the copy constructor of the class User

I really do not know how to implement this, in both methods create an object of the class Organizer and Participant but I do not understand how to initialize the parent class with the data of the user object or how to implement the copy constructor of the User class.

I have a test class in which a user is created with a method that belongs to a class called Platform, it does not really interfere.

   Platform platform = new Platform();
    User u1=platform.registerUser(username1, password1, fullName1);

     Participant p1 = u1.asParticipant();
    assertNotNull(p1);
    assertEquals(u1.getFullName(), p1.getFullName());   
    assertTrue(p1 instanceof Participant);

    // Get a organizer object from user object
    Organizer o1 = u1.asOrganizer();
    assertNotNull(o1);
    assertEquals(u1.getFullName(), o1.getFullName());
    assertTrue(o1 instanceof Organizer);

This is the first part of the test, a user object is created and then based on that object an Organizer object is created and another participant ..

Then I'm asked to do this

    public void userCopyConstructor() {        
    Platform platform = new Platform();
    User u1=platform.registerUser(username1, password1, fullName1);

    // User is not NULL
    assertNotNull(u1);

    // Get a participant object from user object
    User u2 = new User(u1);        
    assertNotNull(u2);
    assertEquals(u1, u2);        
}

My question is how do I copy the user object from all its attributes and thus be able to create the Organizer and Participant objects well since they ask me to be equal to the User u1 object that I create in the platformUser method of Platform

What I am being asked to do, Each user can adopt different roles depending on how they want to interact with the platform:  Participant: With the role of participant, the user can send predictions to competitions of other users who are open.  Organizer: With the role of organizer, the user can create and manage competitions. If you look at the UML diagram, you will see that the roles have been defined as specializations of the User class. Therefore, changing roles implies obtaining an object of a more specific kind. To perform this task you will need:  Implement the asOrganizer and asParticipant methods of the User class, which allow this role change.  These methods must create an object of the specific class, and initialize the parent class with the data of the User object. To perform this task, it is requested to implement the copy constructor of the User class.

    
asked by Roman345 14.11.2016 в 19:54
source

1 answer

0

I do not know if I have reading problems but I do not understand what you want ...

"My question is how do I copy the user object all its attributes and thus be able to create Organizer and Participant objects well since they ask me to be equal to the User u1 object"

If this is what you want, just do this ...

    Organizer org = u1.asOrganizer();
    Participant part = u1.asParticipant();
    
answered by 14.11.2016 / 23:32
source