Contract.AbstractContract.java Source code

Java tutorial

Introduction

Here is the source code for Contract.AbstractContract.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Contract;

import interfaces.JSONObjectInterface;
import interfaces.LevyInterface;
//import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import Exceptions.WrongNumberValueException;
import Payments.AbstractPayment;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

/**
 *
 * @author novyjpolzovatel
 */
public abstract class AbstractContract implements LevyInterface, JSONObjectInterface {
    static long contractUniqueId = 0;
    public List<AbstractPayment> payments;
    protected String workPlace = "";
    protected String post = "";
    protected Date validDate;
    protected boolean forever = false;

    /**
     * 
     * @param workPlace - ? 
     * @param post - ?
     */
    public AbstractContract(String workPlace, String post) {
        this.workPlace = workPlace;
        this.post = post;
        this.payments = new ArrayList<AbstractPayment>();
        contractUniqueId++;
        this.forever = true;
    }

    public AbstractContract(JSONObject obj) {
        this.payments = new ArrayList<AbstractPayment>();
        this.loadParamsFromJson(obj);
        contractUniqueId++;
    }

    /**
     * 
     * @param workPlace - ? 
     * @param post - ?
     * @param validDate -  ? 
     * @throws WrongNumberValueException 
     */
    public AbstractContract(String workPlace, String post, Date validDate) throws WrongNumberValueException {

        if (validDate.compareTo(new Date()) <= 0) {
            throw new WrongNumberValueException(
                    "? ?  ?  ? ??");
        } else if (post.length() == 0) {
            throw new WrongNumberValueException(
                    "? ?   ? ?");
        }

        this.workPlace = workPlace;
        this.post = post;
        this.payments = new ArrayList<AbstractPayment>();
        contractUniqueId++;
        this.forever = false;
    }

    abstract public long getContractUniqueId();

    public void addPayment(AbstractPayment p) throws WrongNumberValueException {
        if (p == null) {
            throw new WrongNumberValueException(
                    " ? null    ");
        }

        this.payments.add(p);
    }

    public Collection getAllPaymentsWithTimePeriod(Date start, Date end) throws WrongNumberValueException {
        ArrayList<AbstractPayment> allPayments = new ArrayList<AbstractPayment>();
        for (Iterator<AbstractPayment> i = this.payments.iterator(); i.hasNext();) {
            AbstractPayment payment = i.next();

            if (payment.validForTimePeriod(start, end)) {
                allPayments.add(payment);
            }
        }

        return allPayments;
    }

    public String getPost() {
        return this.post;
    }

    public String getWorkPlace() {
        return this.workPlace;
    }

    //========================================================
    abstract public String getType();

    @Override
    public String toString() {
        String value = "";
        if (this.getContractUniqueId() == 0) {
            value = "   ";
        } else {
            value = "  ?   " + this.getContractUniqueId();
        }

        value = " ? " + this.post + "  ? :" + this.workPlace
                + "  ? : \n";
        for (Iterator<AbstractPayment> it = this.payments.iterator(); it.hasNext();) {
            AbstractPayment ap = it.next();
            value += ap.toString() + "\n";
        }

        return value;

    }

    @Override
    public double getTotalWithDate(Date start, Date end) {
        double fullSumm = 0.;
        for (Iterator<AbstractPayment> it = this.payments.iterator(); it.hasNext();) {
            AbstractPayment ap = it.next();
            fullSumm += ap.getTotalWithDate(start, end);
        }
        return fullSumm;
    }

    @Override
    public double getLevy() {
        double fullLevySumm = 0.;

        for (Iterator<AbstractPayment> it = this.payments.iterator(); it.hasNext();) {
            AbstractPayment ap = it.next();
            fullLevySumm += ap.getRawSum() - ap.getTotalWithDate(null, new Date());
        }

        return fullLevySumm;
    }

    @Override
    public void setLevy(double newLevy) throws WrongNumberValueException {
        if (newLevy >= 100) {
            throw new WrongNumberValueException("?  99% ?!");
        } else if (newLevy < 0) {
            throw new WrongNumberValueException("?  0% ?!");
        }

        for (AbstractPayment ap : this.payments) {
            ap.setLevy(newLevy);
        }
    }

    @Override
    public double getRawSummWithDate(Date start, Date end) {
        double returnValue = 0.;
        for (Iterator<AbstractPayment> it = this.payments.iterator(); it.hasNext();) {
            AbstractPayment ap = it.next();
            returnValue += ap.getRawSummWithDate(start, end);
        }
        return returnValue;
    }

    @Override
    public JSONObject getJSONObject() {
        JSONObject obj = new JSONObject();
        JSONArray list = new JSONArray();
        for (Iterator<AbstractPayment> it = this.payments.iterator(); it.hasNext();) {
            AbstractPayment ap = it.next();
            list.add(ap.getJSONObject());
        }
        obj.put("payments", list);
        obj.put("id", this.getContractUniqueId());
        obj.put("workPlace", this.workPlace);
        obj.put("post", this.post);
        obj.put("validDate", this.validDate);
        obj.put("forever", new Boolean(this.forever));

        return obj;

    }

}