-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComprehensiveSalaryAnalyzer.java
More file actions
191 lines (162 loc) · 8.32 KB
/
ComprehensiveSalaryAnalyzer.java
File metadata and controls
191 lines (162 loc) · 8.32 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import java.text.NumberFormat;
import java.util.InputMismatchException;
import java.util.Locale;
import java.util.Scanner;
/**
* Enhanced Salary Calculator
* Calculates annual salary with comprehensive breakdown including
* gross salary, allowances, deductions, and net pay
*
* @author Muhammad Yamman Hammad
* @version 2.0
*/
public class ComprehensiveSalaryAnalyzer {
// Constants for calculations
private static final int MONTHS_IN_YEAR = 12;
private static final double DEFAULT_TAX_RATE = 0.15; // 15% tax rate
private static final double INSURANCE_RATE = 0.05; // 5% insurance
/**
* Calculates and displays comprehensive annual salary breakdown
* @param monthlySalary The base monthly salary
* @param monthlyAllowance The monthly allowance amount
* @param taxRate The tax rate to apply (as decimal, e.g., 0.15 for 15%)
*/
public void calculateAnnualSalary(double monthlySalary, double monthlyAllowance, double taxRate) {
// Input validation
if (monthlySalary < 0 || monthlyAllowance < 0) {
System.err.println("Error: Salary and allowance values cannot be negative!");
return;
}
if (taxRate < 0 || taxRate > 1) {
System.err.println("Error: Tax rate must be between 0 and 1 (0% to 100%)!");
return;
}
// Calculations
double annualBaseSalary = monthlySalary * MONTHS_IN_YEAR;
double annualAllowances = monthlyAllowance * MONTHS_IN_YEAR;
double grossAnnualSalary = annualBaseSalary + annualAllowances;
// Calculate deductions
double taxDeduction = grossAnnualSalary * taxRate;
double insuranceDeduction = annualBaseSalary * INSURANCE_RATE;
double totalDeductions = taxDeduction + insuranceDeduction;
double netAnnualSalary = grossAnnualSalary - totalDeductions;
// Format currency for display
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(Locale.US);
// Display comprehensive breakdown
displaySalaryBreakdown(monthlySalary, monthlyAllowance, annualBaseSalary,
annualAllowances, grossAnnualSalary, taxDeduction,
insuranceDeduction, totalDeductions, netAnnualSalary,
currencyFormat, taxRate);
}
/**
* Displays a formatted salary breakdown
*/
private void displaySalaryBreakdown(double monthlySalary, double monthlyAllowance,
double annualBaseSalary, double annualAllowances,
double grossAnnualSalary, double taxDeduction,
double insuranceDeduction, double totalDeductions,
double netAnnualSalary, NumberFormat currencyFormat,
double taxRate) {
System.out.println("\n" + "=".repeat(60));
System.out.println(" ANNUAL SALARY BREAKDOWN");
System.out.println("=".repeat(60));
System.out.printf("Monthly Base Salary: %s%n", currencyFormat.format(monthlySalary));
System.out.printf("Monthly Allowances: %s%n", currencyFormat.format(monthlyAllowance));
System.out.println("-".repeat(40));
System.out.printf("Annual Base Salary: %s%n", currencyFormat.format(annualBaseSalary));
System.out.printf("Annual Allowances: %s%n", currencyFormat.format(annualAllowances));
System.out.printf("Gross Annual Salary: %s%n", currencyFormat.format(grossAnnualSalary));
System.out.println("\nDEDUCTIONS:");
System.out.printf("Tax (%.1f%%): %s%n", taxRate * 100, currencyFormat.format(taxDeduction));
System.out.printf("Insurance (%.1f%%): %s%n", INSURANCE_RATE * 100, currencyFormat.format(insuranceDeduction));
System.out.printf("Total Deductions: %s%n", currencyFormat.format(totalDeductions));
System.out.println("-".repeat(40));
System.out.printf("NET ANNUAL SALARY: %s%n", currencyFormat.format(netAnnualSalary));
System.out.printf("NET MONTHLY SALARY: %s%n", currencyFormat.format(netAnnualSalary / MONTHS_IN_YEAR));
System.out.println("=".repeat(60));
}
/**
* Gets valid numeric input from user with error handling
* @param scanner The Scanner object for input
* @param prompt The prompt message to display
* @param fieldName The name of the field being input (for error messages)
* @return Valid double value
*/
private double getValidInput(Scanner scanner, String prompt, String fieldName) {
double value;
while (true) {
try {
System.out.print(prompt);
value = scanner.nextDouble();
if (value < 0) {
System.out.println("Error: " + fieldName + " cannot be negative. Please enter a positive value.");
continue;
}
break;
} catch (InputMismatchException e) {
System.out.println("Error: Please enter a valid number for " + fieldName.toLowerCase() + ".");
scanner.next(); // Clear invalid input
}
}
return value;
}
/**
* Gets valid tax rate input from user
* @param scanner The Scanner object for input
* @return Valid tax rate as decimal
*/
private double getValidTaxRate(Scanner scanner) {
double taxRate;
while (true) {
try {
System.out.print("Enter tax rate percentage (0-100, default 15): ");
String input = scanner.next().trim();
if (input.isEmpty()) {
taxRate = DEFAULT_TAX_RATE;
break;
}
taxRate = Double.parseDouble(input) / 100.0; // Convert percentage to decimal
if (taxRate < 0 || taxRate > 1) {
System.out.println("Error: Tax rate must be between 0 and 100 percent.");
continue;
}
break;
} catch (NumberFormatException e) {
System.out.println("Error: Please enter a valid tax rate percentage.");
}
}
return taxRate;
}
/**
* Main method - Entry point of the application
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ComprehensiveSalaryAnalyzer salaryCalculator = new ComprehensiveSalaryAnalyzer();
try {
System.out.println("=".repeat(60));
System.out.println(" WELCOME TO ANNUAL SALARY CALCULATOR");
System.out.println("=".repeat(60));
// Get user inputs with validation
double monthlySalary = salaryCalculator.getValidInput(input,
"Enter your monthly base salary: $", "Monthly salary");
double monthlyAllowances = salaryCalculator.getValidInput(input,
"Enter your monthly allowances: $", "Monthly allowances");
double taxRate = salaryCalculator.getValidTaxRate(input);
// Calculate and display results
salaryCalculator.calculateAnnualSalary(monthlySalary, monthlyAllowances, taxRate);
// Ask if user wants to calculate again
System.out.print("\nWould you like to calculate another salary? (y/n): ");
String response = input.next().trim().toLowerCase();
if (response.equals("y") || response.equals("yes")) {
main(args); // Recursive call for another calculation
} else {
System.out.println("\nThank you for using the Annual Salary Calculator!");
}
} catch (Exception e) {
System.err.println("An unexpected error occurred: " + e.getMessage());
} finally {
input.close();
}
}
}