Subscription
Class ApplicationLogic

java.lang.Object
  extended by Subscription.ApplicationLogic
All Implemented Interfaces:
java.io.Serializable

public class ApplicationLogic
extends java.lang.Object
implements java.io.Serializable

This class implements the application/business logic of our subscription system. Typically this logic layer is not used alone. We will have some form of (user) interface layer on top of it. This interface layer should be placed in a different package.

This class applies the Singleton design pattern to make sure that the user interface layer can only access one instance of the application logic (at least, within the same JVM).

The role of this business logic is to maintain the business data of our subscription system; e.g. the customers, the subscriptions, etc. It furthermore provides a bunch of operations to manipulate these data or to inspect them.

The maintained business data is persisted. That is, they will be saved. In a production system you will use a database system; here we'll just use a file to save the data. Each operation that manipulates the business data will save the data before it finishes.

See Also:
Serialized Form

Field Summary
protected  java.util.Set<Customer> customers
          The set of registered customers.
(package private)  java.util.Set<DiscountStrategy> discountStrategies
          Active discount strategies.
protected static ApplicationLogic instance
          The static variable holding the singleton instance.
protected  int nextFreeCustId
          The next free customer ID number.
(package private)  int nextFreeServId
          The next free service ID number.
(package private)  int nextFreeSubscrId
          The next free subscription ID number.
protected  java.util.Set<Service> services
          The set of registered services.
protected  java.util.Set<Subscription> subscriptions
          The set of active subscriptions.
 
Constructor Summary
private ApplicationLogic()
          This constructor will create an empty application logic (with 0 customers, 0 subscriptions, etc).
 
Method Summary
 int addCustomer(java.lang.String name, java.lang.String email)
          Add a new customer with the given name, if the customer is not already 'in the system'.
 int addService(java.lang.String name, int price)
          Add a new service with the given name and price (in Euro-cent).
 int addSubscription(int custID, int servID)
          Add a new subscription for the given customer for the given service.
 boolean custExists(int id)
          Check if the a customer with the given id exists.
 int discount(int custID)
          Return the total weekly discount of the customer.
 Customer findCustomer(int custID)
          Find a customer with the given ID in the customers set.
 Service findService(int servID)
          Find a service with the given ID in the services set.
 Subscription findSubscription(int scrID)
          Find a subscription with the given ID in the subscriptions set.
 java.util.Set<Customer> getCustomers()
          Does as the name suggests.
static ApplicationLogic getInstance()
          Will return the singleton instance.
 java.util.Set<Service> getServices()
          Does as the name suggests.
 int netPrice(int custID)
          Return the net weekly total price of all subscriptions of the given customer.
 int rawPrice(int custID)
          Return the raw weekly total price of all subscriptions of the given customer.
 void removeCustomer(int custID)
          Remove the customer of the given ID.
 void removeService(int servID)
          Remove the service with the given ID.
 void removeSubscription(int scrID)
          Will remove the given subscription.
(package private) static void reset()
          Will clear the persisted data.
private  double roundUp(double x)
           
private  void save()
          Helper.
 boolean serviceExists(int id)
          Check if a service with the given id exists.
 java.util.Set<Service> subscribedServices(Customer c)
          Return the set of all services subscribed by the given customer.
 boolean subscriptionExists(int id)
          Check if a subscription with the given id exists.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

customers

protected java.util.Set<Customer> customers
The set of registered customers.


discountStrategies

java.util.Set<DiscountStrategy> discountStrategies
Active discount strategies.


instance

protected static ApplicationLogic instance
The static variable holding the singleton instance.


nextFreeCustId

protected int nextFreeCustId
The next free customer ID number. To be given to the next new customer.


nextFreeServId

int nextFreeServId
The next free service ID number. To be given to the next new service.


nextFreeSubscrId

int nextFreeSubscrId
The next free subscription ID number. To be given to the next new subscription.


services

protected java.util.Set<Service> services
The set of registered services.


subscriptions

protected java.util.Set<Subscription> subscriptions
The set of active subscriptions.

Constructor Detail

ApplicationLogic

private ApplicationLogic()
This constructor will create an empty application logic (with 0 customers, 0 subscriptions, etc). Two discount strategies are immediately loaded.

Note that the constructor is private; so the only way to get an instance of this class (from outside the package) is actually through the getInstance method.

Method Detail

addCustomer

public int addCustomer(java.lang.String name,
                       java.lang.String email)
Add a new customer with the given name, if the customer is not already 'in the system'. A customer is 'in the system' if there is already a customer in the variable customers with the same combination of name and email.

If the given customer is 'new' then it is added to the system, and this method returns the ID of the new customer. If the customer already exists, -1 will be returned.


addService

public int addService(java.lang.String name,
                      int price)
Add a new service with the given name and price (in Euro-cent). It returns the ID of the new service. If the service already exists in the system, -1 is returned.

Pre-condition: price is non-negative.


addSubscription

public int addSubscription(int custID,
                           int servID)
Add a new subscription for the given customer for the given service. If a new subscription can be created, its ID will be returned. Else -1.

Pre-condition: duration is non-negative.


custExists

public boolean custExists(int id)
Check if the a customer with the given id exists.


discount

public int discount(int custID)
Return the total weekly discount of the customer. The price is given in euro-cent.


findCustomer

public Customer findCustomer(int custID)
Find a customer with the given ID in the customers set. It returns the customer, or null if it can't be found.


findService

public Service findService(int servID)
Find a service with the given ID in the services set. It returns the service, or null if it can't be found.


findSubscription

public Subscription findSubscription(int scrID)
Find a subscription with the given ID in the subscriptions set. It returns the subscription, or null if it can't be found.


getCustomers

public java.util.Set<Customer> getCustomers()
Does as the name suggests.


getInstance

public static ApplicationLogic getInstance()
Will return the singleton instance. If it does not exists yet, then this method will reload it from a save-file. If this fails, then a new (and empty) application logic will be created.


getServices

public java.util.Set<Service> getServices()
Does as the name suggests.


netPrice

public int netPrice(int custID)
Return the net weekly total price of all subscriptions of the given customer. Net price is the price after discount. The price is given in euro-cent.


rawPrice

public int rawPrice(int custID)
Return the raw weekly total price of all subscriptions of the given customer. Raw price is the price before discount. The price is given in euro-cent.


removeCustomer

public void removeCustomer(int custID)
Remove the customer of the given ID. All his subscriptions will also be removed (cascade removal).


removeService

public void removeService(int servID)
Remove the service with the given ID.


removeSubscription

public void removeSubscription(int scrID)
Will remove the given subscription.


reset

static void reset()
Will clear the persisted data. Note that this method is package private.


roundUp

private double roundUp(double x)

save

private void save()
Helper. Will persist/save the state of the application.


serviceExists

public boolean serviceExists(int id)
Check if a service with the given id exists.


subscribedServices

public java.util.Set<Service> subscribedServices(Customer c)
Return the set of all services subscribed by the given customer.


subscriptionExists

public boolean subscriptionExists(int id)
Check if a subscription with the given id exists.