1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
/**
* 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 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();
BonusOnSavings savings = new BonusOnSavings();
double bonus = savings.computeBonus(monthlyCommitment, q1, q2, q3, q4);
System.out.println("The calculated bonus is " + bonus);
}
}
|