user1868607
user1868607

Reputation: 357

User and Administrator Java Project

I am creating a Java Project for my exam. The project asks me to do a groupon-like program using Object-oriented programming. It asks me to realize a program to sell discounted products. I have holidays, dinners and normal objects. I decided to create a super-class Product and then to extend it creating the other three classes, dinners, holidays, and objects. But it also asks me to manage a catalog of the products making have to every product an unique identifier, how can I? Even if I create a Catalog class, managing the product with an ArrayList, how can I add an identifier to each element? I also have to distinguish between users and administrator.Each of them can do different things, the administrator can add and remove products from the catalog, see the active offers and order them, and can see the no longer affordable offers. The User can register to the system, buy credits, see the avaible offers, buy something and can see everything that he bought, sorting them by the price or by the date of purchase.

My question is, how can i distinguish the user and allow them to do different things ? I have to allow a log-in operation at the beginning and show different button for users and administrator. Could help me? Give me an idea of how to manage the situation? (Should they be a superclass(user) with an administrator that extends the user?)

Thanks!

Upvotes: 2

Views: 2830

Answers (1)

Jon Lin
Jon Lin

Reputation: 143906

I assume you've got objects for your products and users, you just need to add another field for the unique ID and a field for whether a specific user is an administrator. Something like:

public class Product
{
    public int uniqueID;
    public String productName;

    // etc...
}

public class User
{
    public boolean isAdministrator;
    public String username;

    // etc...
}

You can store your products in an ArrayList and be able to tell what each unique ID is by accessing that field:

catalog.elementAt(x).uniqueID;

and check if a user is an admin by accessing the field:

if(user.isAdministrator) 

Should they be a superclass(user) with an administrator that extends the user?

You could also do it that way, you'd then check whether a user is an admin by checking the type:

if(user instanceof Administrator)

Upvotes: 0

Related Questions