简体   繁体   English

用户和管理员Java项目

[英]User and Administrator Java Project

I am creating a Java Project for my exam. 我正在为考试创建一个Java项目。 The project asks me to do a groupon-like program using Object-oriented programming. 该项目要求我使用面向对象的程序编写类似groupon的程序。 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? 即使创建了Catalog类,并使用ArrayList管理产品,如何为每个元素添加标识符? 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! 谢谢!

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. 我假设您有针对您的产品和用户的对象,您只需要为唯一ID添加另一个字段,并为特定用户是否是管理员添加一个字段。 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: 您可以将产品存储在ArrayList并可以通过访问该字段来分辨每个唯一ID是什么:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM