0
\$\begingroup\$

First of all, I'd want to represent, in my application, two different users (admin and client) which can access to two different classes, through two different graphic interfaces...

My idea is to represent faithfully what is in the e/r and I'd want to generalize shared information (like nick, pw, email) using an interface and represent specific information into the two sub-classes

To store information, I use a database and a piece of the e/r is:

database e/r

Now, in my code, I've implemented it in this way:

Utente entity - represented using Interface:

package users;

/**
 * Interface which represent a general user (both admin either client)
 * 
 * @author Federico Cuozzo
 *
 */
public interface User {
    public String getNick();
    public String getPw();
    public String getEmail();
}

Admin entity - represented using Class with Override:

package users;

import global.Globals;
import java.util.ArrayList;

/**
 * Represents the admin user instance and his all information.
 * 
 * @author Federico Cuozzo
 *
 */
public class Admin implements User {
    private String nick;
    private String pw;
    private String email;
    private ArrayList<Integer> authorizations = new ArrayList<Integer>();   // List of authorizations rwx in integer (r-- 4, rwx 7)

    public Admin(String aNick, String aPw, String anEmail) {
        this.nick = aNick;
        this.pw = aPw;
        this.email = anEmail;
        this.authorizations.add((int) Globals.r.nextInt((7-4) + 1) + 4);
        this.authorizations.add((int) Globals.r.nextInt((7-4) + 1) + 4);
        this.authorizations.add((int) Globals.r.nextInt((7-4) + 1) + 4);
    }

    // Accessor methods for private fields
    @Override
    public String getNick() { return this.nick; }
    @Override
    public String getPw() { return this.pw; }
    @Override
    public String getEmail() { return this.email; }
    public ArrayList<Integer> getAuthorizations() { return this.authorizations; }
}

Client entity - represented using Class with Override:

package users;

/**
 * Represents the client user instance and his all information.
 * 
 * @author Federico Cuozzo
 *
 */
public class Client implements User {
    private String nick;
    private String pw;
    private String name;
    private String surname;
    private String email;
    private String street;
    private String city;
    private String cap;
    private String phoneNumber;

    public Client(String aNick, String aPw, String anEmail, String aName, String aSurname, String aStreet, String aCity, String aCap, String aPhoneNumber) {
        this.nick = aNick;
        this.pw = aPw;
        this.name = aName;
        this.surname = aSurname;
        this.email = anEmail;
        this.street = aStreet;
        this.city = aCity;
        this.cap = aCap;
        this.phoneNumber = aPhoneNumber;
    }

    // Accessor methods for private fields
    @Override
    public String getNick() { return this.nick; }
    @Override
    public String getPw() { return this.pw; }
    @Override
    public String getEmail() { return this.email; }
    public String getName() { return this.name; }
    public String getSurname() { return this.surname; }
    public String getStreet() { return this.street; }
    public String getCity() { return this.city; }
    public String getCap() { return this.cap; }
    public String getPhoneNumber() { return this.phoneNumber; }
}

Is this correct? Using an interface to represent main class and two concrete classes to represent sub-classes...

\$\endgroup\$
2
  • \$\begingroup\$ Looks correct. Make sure to comment your variable declarations if they are not autological. What is cap? Maybe try writing name instead of nick. \$\endgroup\$ – Ben Aug 16 '16 at 17:11
  • \$\begingroup\$ CAP is italian Postal Code, ZIP Code in USA ... Nick is nickname (atom01), not personal name (like Sam, John, ...). Thank you, anyway. \$\endgroup\$ – Federico Cuozzo Aug 16 '16 at 17:34
1
\$\begingroup\$

While the implementation looks logically correct, some remarks about code style:

  • package name should include your organization, e.g. com.your.company.users, com.your.company.global

  • there is no need to prefix arguments with a, you can use the same name as the object fields, since through this, it can be determined which one is referred.

E.g:

public Client(String nick, ...) {
    this.nick = nick;
    // ...
}
\$\endgroup\$
1
  • \$\begingroup\$ Thank you for your revision: I'll apply these changes as soon as possible. \$\endgroup\$ – Federico Cuozzo Aug 16 '16 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.