0

I am trying to create a supermarket software that allows either a customer or the owner to log-in and use my system via a swing based GUI in Java. When the Customer has logged in they can view products. When the owner has logged in they can view products and add new products.

I want a method in the Customer class: ViewProducts()
and methods in the Owner class: ViewProducts(), AddProducts().
Are these methods wrong because they're not specific to the customer/owner (they're related to the product).

My relationship would be the Customer class having a 1 to 1 relationship with ProductList and the Owner having a 1 to 1 relationship with ProductList, and the two classes can manipulate the data in their own way. Am I going about this wrong?

This way doesn't make sense because Customer and Owner can't have attributes that aren't related to them such as ProductList.

AVX-42
  • 755
  • 2
  • 13
  • 21

2 Answers2

2

You should always aim to capture what exists in reality. A Customer instance does not have a 1 to 1 relationship with a ProductList because a ProductList can be viewed by more than one Customer at a time, and the Customer in no way owns that list.

What is probably closer to reality is:

  • Every Supermarket individual manages one Inventory individual
  • Every Inventory individual:
    • is managed by one Supermarket individual
    • comprises Inventory Item individuals
  • Every Inventory Item individual
    • is comprised by one Inventory individual
    • describes Product individuals
  • Every Product individual
    • is described by one Inventory Item individual
    • is located at one Physical Location individual
  • Every User Account individual
    • identifies one Person individual
    • plays Role individuals
  • Every Role individual provides Capability individuals

In real life, people play roles. These roles might be “customer”, “doctor”, or “police officer”. Every individual Role has a set of capabilities it can perform. In an OO system, every individual Role can use operations to implement its capabilities, such as purchaseProduct(), prescribeMedication(), or writeMovingViolation().

There are multiple ways to represent these roles and capabilities in an OO system. In one approach, a customer instance of a Role might be configured to allow access to queryInventory() and purchaseProduct() operations on the Supermarket and InventoryItem classes respectively. An owner instance of a Role 1 might be configured to allow access to addInventoryItem() and removeInventoryItem() operations on the Inventory class.

Here is an example of a UML model:

enter image description here

In another approach, you might create singleton subclasses of the Role class, called CustomerRole and OwnerRole, and then have each of those subclasses invoke operations. You might put your viewProducts() and addProducts() operations into those singletons.


1 Consider calling this role “manager”, so the owner of the supermarket can hire other people to do the work.

Jim L.
  • 6,177
  • 3
  • 21
  • 47
  • If I'm not wrong the <> relations to operations are not UML compliant. If so, could you make a reference, please? – qwerty_so Jan 12 '20 at 13:51
  • On hand I have the UML 2.5 spec. See §7.7.3.2. "A Usage is a Dependency in which one NamedElement requires another NamedElement (or set of NamedElements) for its full implementation or operation." Both `InstanceSpecification` and `Operation` are kinds of `NamedElement`. Thus, what I drew is UML compliant. – Jim L. Jan 12 '20 at 15:00
  • Yes, somehow ;-) The issue is, that there is no documentation for the notation of the latter. EA invented some notation and here I have another one. – qwerty_so Jan 12 '20 at 19:44
  • Anyway, it's a good answer, but probably you should make an annotation about that notation (or find a real reference...). – qwerty_so Jan 12 '20 at 19:45
  • Huh? See §7.7.4: "A Usage is shown as a Dependency with a «use» keyword attached to it." Perhaps EA is not as standards conformant as you think. – Jim L. Jan 12 '20 at 19:50
  • No. They are not referring operations/attributes but elements. Your image uses an arrow going inside the element with a little circle. EA uses an open rectangle. Both notations are inventions of the product designers (I did not say that EA is conformant; it's at about 99% or so at a guess). – qwerty_so Jan 12 '20 at 19:53
  • A UML `Operation` _is_ a kind of `Element`. It is true that the little circle is non-standard, but why does that matter? The `InstanceSpecification` clearly depends on the `Operation`, which is exactly what you will find in the UML meta-model instances. – Jim L. Jan 12 '20 at 19:59
  • Yeah i agree with what you've there although program is really supposed to have users interacting with the supermarket. (Theres no backend, so the user interacts with the same place as the productList and userList). I think for this case i just have to simplify it until i implement it further. – Aidan Gannon Jan 12 '20 at 20:04
  • Users _do_ interact with a supermarket -- through a role they play. For an analogy, a person flies a plane while playing the role of a pilot. Pilots fly planes. Customers shop at a supermarket. Not all users are customers. – Jim L. Jan 12 '20 at 20:09
  • BTW, not separating roles properly is a _huge_ source of user dissatisfaction and maintenance problems. – Jim L. Jan 12 '20 at 20:10
  • @JimL. Well, everything matters. Even (or especially) shortcomings of a "standard". – qwerty_so Jan 12 '20 at 20:33
0

The first thing you need to consider is the IS-A vs HAS-A. If your class is a specfic thing, it should be a child of a super class. If it has something, then it is a composition. If I were designing this, I would make productList a class and then have store employee as the super class with owner and worker as sub-classes both taking the product list as a composition. Describes Logic of Designing Classes

  • That's what i've done. I probably didn't explain it properly my User super class and my productList class are going to be linked together somehow. Is this allowed though. Could you further justify why this relationship is allowed because in my opinion the User cannot have a productList because the products don't belong the user?? – Aidan Gannon Jan 12 '20 at 02:19
  • For the User super class, it could take an accessor method getProductList() to get access to the data. While there is not a strong relationship between the two classes, one could just access the data as a method for the UML. I don't think compostion/aggregration of classes would be right here. – Ryan Marinelli Jan 12 '20 at 02:25
  • So you would make the ProductList private inside of the generic User class?? Then the get function returns it which can be accessed by the Customer/Owner? Rather than making ProductList protected inside of User superclass. Still is this allowed because surley user still runs independantly from ProductList or is it different for abstract classes?? – Aidan Gannon Jan 12 '20 at 02:29
  • I would make productList its own class with a constructor and assessor methods. Then have the User class take the accessor method from productList. It should be able to access the protected data, but it should have a class relationship. – Ryan Marinelli Jan 12 '20 at 02:32
  • Right so my issue is that productList upon instantiation loads from a text file a load of data populating an array called products which contains many different products. If i don't have it as an attribute somewhere it's not going to populate my arrayList in productList and then im going to have to load from the file each time i want to access my data. Do you follow? (Probably should have mentioned this above) – Aidan Gannon Jan 12 '20 at 02:37
  • I think I follow, I would suggest then to use an abstract class to pass the information from the product list without damaging the logical relationships. You might also think about implementing an infereace from productList. https://stackoverflow.com/questions/36036913/how-to-pass-reference-to-abstract-class-java – Ryan Marinelli Jan 12 '20 at 02:46
  • So add it the User class? (Again something i didn't think to mention when i asked the question initially) the GUI connects to a class called UserList which is an abstract class that contains a bunch of users stored in an polymorphic attribute called Users of type Arraylist, so each user will generate a new array for ProductList will it not? – Aidan Gannon Jan 12 '20 at 02:55
  • If you are already using an abstract class, then you might want to consider implementing an interface within the User super class. It ideally should be an abstract class, but with limitations in Java I don't think that is permissible. The only issue with using an interface is that there would be no state, so all variables would be really constants. You would need another way to update the data. – Ryan Marinelli Jan 12 '20 at 03:09
  • `ProductList` is an association class, not a plain one. – qwerty_so Jan 12 '20 at 19:48