Somnath Kayal Somnath Kayal - 2 months ago 18
Java Question

How to implement usage dependency, Enumeration data type in Java? Confusion to implement Aggregation, Composition

I have to implement the following class diagram to the java code. This diagram is very complicated and some parts creates confusion. This question definitely going to help me a lot as well as any reader because it contains several important aspects of UML diagram.enter image description here

class Book{
String isbn;
String publisher;
String publishDate;
int pages;
}
class BookItem extends Book{
String barcode;
boolean isReferenceOnly;
}
class Author{
String name;
String biography;
Collection<Book> book;
}
class Account{
String number;
List<History> history;
String openDate;
AccountState state;
public Account(AccountState state){
this.state = state;
}
}
enum AccountState{
Active,
Frozen,
Closed
}
class Catalog implements Search, Manage{
List<BookItem> bookItem;
/* Implement the methods of Manage interface */
void add(BookItem item){ }
void remove(BookItem item){ }
/* Implement the methods of Search interface */
int search(BookItem item){ }
}
class Account{
String number;
List<History> history;
Student student = new Student();

void setStudent(Student student){
this.student = student;
}
}
interface Search{
int search(BookItem item);
}
interface Manage{
void add(BookItem item);
void remove(BookItem item);
}
class Student{
String name;
String address;
Search searchBook = new Catalog();
}
class Librarian{
String name;
String address;
String position;
Search searchBook = new Catalog();
Manage manage = new Catalog();
Account account = new Account();

void setAccount(Account account){
this.account = account;
}
class Library{
String name;
String Address;
List<BookItem> bookItem = new ArrayList<BookItem>();
Catalog catalog = new catalog();
List<Account> accounts = new ArrayList<Account>();

Library(Catalog catalog){
this.catalog = catalog;
}
void setBookItem(List<BookItem> bookItem){
this.bookItem = bookItem;
}
void setAccounts(List<Account> accounts){
this.accounts = accounts;
}
}


I implemented in the following way but confusion arise in various cases:


  1. How to implement Class Student use the interface Search.

  2. How to implement Class Librarian use the interfaces Search and Manage.

  3. Why we are not use association instead of usage dependency.

  4. How to implement that Enumeration data type in this case with usage dependency [I have just considered AccountState as a class, i the it is a wrong implementation].

  5. How to use AccountState in the Account [I have just created a object of AccountState].

  6. After read many blogs still unable to implement Aggregation and Composition confidently. Note: In this diagram 3 Aggregations and 1 Composition Exist. Those are:

    (a) Library consists of many Account. {Aggregation}

    (b) Many Book Item is the part of Library. {Aggregation}

    (c) An Account is the part of a Student. {Aggregation}

    (d) Library must have a Catalog. {Composition}

    Please give your valuable advice so i can learn it well. Thanking you.


Answer

Since this question is homework for learning purposes, I will post only examples of how to implement the things you need to review and won't give a direct answer about how to apply them to your current design.

  • Enumeration in Java is implemented by using enum.

    enum WeekDays {
        MONDAY,
        TUESDAY,
        WEDNESDAY,
        THURSDAY,
        FRIDAY,
        SATURDAY,
        SUNDAY;
    }
    
  • Aggregation/Composition means to have a field of the other class. If it's a weak association (aggregation), it should be initialized by the setter or another method. If it's a strong association, it should be initialized in the class constructor since it is needed for the class to live/work.

    class WeakAssociation { }
    class StrongAssociation { }
    class NeedWeekAndStrongAssociation {
        private WeakAssociation weakAssociation;
        private StrongAssociation strongAssociation;
        public NeedWeekAndStrongAssociation(StrongAssociation strongAssociation) {
            this.strongAssociation = strongAssociation;
        }
        public void setWeakAssociation(WeakAssociation weakAssociation) {
            this.weakAssociation = weakAssociation;
        }
    }
    
  • Usage dependency means that the class/interface will use the other class/interface within one or more of its methods:

    class WantToBeUsed {
        public void methodToBeUsed(String data) {
            //fancy implementation
        }
    }
    class CannotDoThisAlone {
        public void cannotDoItAlone(String data) {
            WantToBeUsed wantToBeUsed = new WantToBeUsed();
            wantToBeUsed.methodToBeUsed(data);
        }
    }
    
Comments