/** * Assignment 1, COMP268 Class: BonusOnSavings.java * * @description Represents an object that can compute a bonus based on the amount an employee saves. * @author: mo khan Student ID: 3431709 * @date May 7, 2019 * @version 1.0 */ package Q2; import java.util.Scanner; public class BonusOnSavings { double annualRate = 0.0; double quarterlyRate = 0.0; /** Constructs a BonusOnSavings with a default quarterly rate of 3% and annual rate of 5%. */ public BonusOnSavings() { this(0.03, 0.05); } /** * Constructs a BonusOnSavings object for a given income and marital status, and computes the tax. * * @param quarterlyRate the quarterly rate to apply. * @param annualRate the annual rate to apply. */ public BonusOnSavings(double quarterlyRate, double annualRate) { this.quarterlyRate = quarterlyRate; this.annualRate = annualRate; } /** * Computes the bonus based on the target monthly commitment and the actual quarterly * contributions. * * @param monthlyCommitment the target monthly commitment for each month. * @param q1 the actual contribution for the first quarter. * @param q2 the actual contribution for the second quarter. * @param q3 the actual contribution for the third quarter. * @param q4 the actual contribution for the fourth quarter. * @return the calculated bonus amount for the year. */ public double computeBonus(double monthlyCommitment, double q1, double q2, double q3, double q4) { double quarterlyTarget = monthlyCommitment * 3; double annualTarget = monthlyCommitment * 12; return this.quarterlyBonus(quarterlyTarget, q1) + this.quarterlyBonus(quarterlyTarget, q2) + this.quarterlyBonus(quarterlyTarget, q3) + this.quarterlyBonus(quarterlyTarget, q4) + this.annualBonus(annualTarget, q1 + q2 + q3 + q4); } /** * Computes the quarterly bonus for a given target and actual contribution amount. * * @param target the target quarterly contribution amount for this employee. * @param actual the actual quarterly contribution amount for this employee. * @return the calculated quarterly bonus */ private double quarterlyBonus(double target, double actual) { return (actual >= target) ? actual * this.quarterlyRate : 0.0; } /** * Calculates the annual bonus amount for the target annual contribution and the actual * contribution. * * @param target the target annual contribution * @param actual the actual contribution amount for the year * @return the calculated annual bonus */ private double annualBonus(double target, double actual) { if (actual < target) return 0.0; return (actual * this.annualRate) + ((actual - target) * 0.25); } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Please enter your target monthly contribution: "); double monthlyCommitment = in.nextDouble(); System.out.print("Please enter Q1 contributions: "); double q1 = in.nextDouble(); System.out.print("Please enter Q2 contributions: "); double q2 = in.nextDouble(); System.out.print("Please enter Q3 contributions: "); double q3 = in.nextDouble(); System.out.print("Please enter Q4 contributions: "); double q4 = in.nextDouble(); double bonus = new BonusOnSavings().computeBonus(monthlyCommitment, q1, q2, q3, q4); System.out.println("The calculated bonus is " + bonus); } }