Write a Java program that will calculate tax for the employees of a company called XYZ. The main menu of the tax calculation program is as follows.
Welcome to Tax Management System of XYZ
Please select one of the following options:
1. Calculate tax
2. Search tax
3. Exit
When 1 is selected then the program will calculate the tax of an employee based on the annual income of the employee and tax rates on the income. The tax rates on the income is stored in a file called taxrates.txt. The program needs to read the taxrates.txt file and store the information in proper data structure. If the taxrates.txt file does not exist in the directory of the source code then the program should ask to provide the taxrates.txt file as an input. The format of the taxrates.txt file is as follows.
Taxable Income | Tax on Income |
---|---|
0 – $18,200 | 0 |
$18,201 – $37,000 | 19c for each $1 over $18,200 |
$37,001 – $90,000 | $3,572 plus 32.5c for each $1 over $37,000 |
$90,001 – $180,000 | $20,797 plus 37c for each $1 over $90,000 |
$180,001 and over | $54,097 plus 45c for each $1 over $180,000 |
The program will take user inputs on Employee ID (4-digit number) and the annual income of the employee (floating-point number with two decimal places). Based on the annual income of the employee the program will then calculate the tax (using the information in taxrates.txt file) of the employee. For example, if the annual income of an employee is $100000.00 then the tax of the employee=20797+(100000 - 90000)*0.37= 24497.00
After calculating the tax of an employee the program will then write the Employee Id, taxable income and tax into a file called taxreport.txt. The format of the taxreport.txt file is as follows.
Employee ID | Taxable Income | Tax |
---|---|---|
1111 | 100000.00 | 24497.00 |
2222 | 90000.00 | 20797.00 |
Once the tax calculation is done for one employee then the program will ask if XZY wants to calculate the tax for another employee, if yes then the above process will continue again. The program will calculate the tax for as many employees as XYZ wants. However, if XZY does not want to calculate the tax for another employee then main menu will be displayed.
When 2 is selected then the program will search the tax for an employee using the employee id in the taxreport.txt file. However, if the taxreport.txt file does not exist in the directory of the source code then the program should ask to provide the taxreport.txt file as an input.
If the taxreport.txt file contains the multiple tax for the same employee then the program will get the latest tax amount of that employee. If the taxreport.txt file does not contain the employee id then the program should give an warning message that the taxreport.txt file does not contain the tax of that employee.
Once searching tax (based on employee id) is done for one employee then the program will ask if XZY wants to search tax for another employee, if yes then the above process will continue again. The program will search tax for as many employees as XYZ wants. However, if XZY does not want to search tax for another employee then main menu will be displayed.
When 3 is selected then the program will
exit.
File read and write operations need to be done properly. You need
to use proper data structure. Input validation also needs to be
done.
`Hey,
Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
public class TaxManagementSystem {
public static void main(String[] args) {
while (true)
Menu.display();
}
}
class Menu {
static void display() {
int selection;
String usr_input = "";
String id = "";
double income;
Scanner input = new Scanner(System.in);
// display text based menu
System.out.println("==================================================================");
System.out.println("| MENU SELECTION |");
System.out.println("==================================================================");
System.out.println("| Options: |");
System.out.println("| 1. Calculate Tax |");
System.out.println("| 2. Search Tax |");
System.out.println("| 3. Exit |");
System.out.println("==================================================================");
System.out.println("Select an option: ");
System.out.println(">>>");
selection = input.nextInt();
switch (selection) {
case 1:
System.out.println("Enter the Employee ID: ");
System.out.println(">>>");
id = input.next();
// "Input validation also needs to be done"
if(!(Employee.isValidId(id))){
System.out.println("Please enter a valid FOUR DIGIT Employee ID.");
break;
}
System.out.println("Enter the taxable income: ");
System.out.println(">>>");
usr_input = input.next();
// "Input validation also needs to be done"
if (!isType(usr_input)){
System.out.println("Please enter a numeric value.");
break;
}
income = Double.parseDouble(usr_input);
if (income > 0) {
App.calculate_tax(income, id);
}
break;
case 2:
System.out.println("Enter the Employee ID: ");
System.out.println(">>>");
id = input.next();
// "Input validation also needs to be done"
if(!(Employee.isValidId(id))){
System.out.println("Please enter a valid FOUR DIGIT Employee ID.");
break;
}
App.search_tax(id);
break;
case 3:
System.out.println("Goodbye!");
System.exit(1);
break;
default:
System.out.println("Select 1, 2, or 3");
display();
}
}
// helper to check type of user input (income)
private static Boolean isType(String str) {
try {
Double.parseDouble(str);
return true;
} catch(Exception e) {
return false;
}
}
}
class App {
private final static String TAXRATES = "taxrates.txt";
private final static String TAXREPORT = "taxreport.txt";
private final static double[] tax_rates = readTaxRates(TAXRATES);
// data structure holding the tax bracket thresholds
private final static int tax_table[] = {18200, 37000, 87000,
180000};
static void calculate_tax(double input, String employee_id) {
double tax_due = 0;
double income = Math.round(input*100.0)/100.0;
int len = tax_table.length;
if (income <= tax_table[len - 1]) {
mainloop:
for (int i = 0; i < len; ++i)
{
// tax is applied progressively:
if (income <= tax_table[i]) {
if (i > 0) {
//get amount of income in top-most tax bracket
tax_due += (income - tax_table[i - 1]) * tax_rates[i];
for (int j = 1; j < i; j++) {
double amount = (tax_table[j] - tax_table[j - 1]) * tax_rates[j];
tax_due += amount;
}
} else {
//'0' in this case
tax_due = income * tax_rates[i];
}
break mainloop;
}
}
System.out.println("Tax is: " + tax_due);
writeTaxReport(new Employee(employee_id, income, tax_due), TAXREPORT);
return;
}
// calculate tax due for income in top most tax bracket
// again, tax is applied progressively:
//get tax in bottom-most bracket
tax_due += tax_table[0] * tax_rates[0];
//get top-most tax bracket
tax_due += (income - tax_table[len - 1]) *
tax_rates[tax_rates.length - 1];
for (int i = 1; i < len; i++) {
tax_due += (tax_table[i] - tax_table[i - 1]) * tax_rates[i];
}
System.out.println("Tax is: " + tax_due);
writeTaxReport(new Employee(employee_id, income, tax_due), TAXREPORT);
}
static void search_tax(String employee_id) {
String str;
String tax = "";
// STEP:
// Search the report for the most recent record matching the given
id
try (BufferedReader in = new BufferedReader(new
FileReader(TAXREPORT))) {
while ((str = in.readLine()) != null) {
String[] stringArr = str.split(",");
if (stringArr[0].equals(employee_id)) {
tax = stringArr[2];
}
}
in.close();
} catch (IOException e) {
System.out.println("File Read Error");
}
// STEP:
// "If the taxreport.txt file contains the multiple tax for the
same employee then the program will
// get the latest tax amount of that employee. If the taxreport.txt
file does not contain the
// employee id then the program should give an warning message that
the taxreport.txt file does not
// contain the tax of that employee."
if (tax.isEmpty()) {
System.out.println("No record exists for the ID supplied.");
} else {
System.out.println("The most recent tax value was: $" + tax);
}
// STEP:
// "Once searching tax (based on employee id) is done for one
employee then the program will ask if
// XZY wants to search tax for another employee, if yes then the
above process will continue again."
Scanner input = new Scanner(System.in);
System.out.println("Search again? [y/n]");
System.out.println(">>>");
String answer = input.next();
switch (answer.toLowerCase()) {
case "y":
System.out.println("Enter the Employee ID: ");
System.out.println(">>>");
answer = input.next();
// recursively call the search_tax method
search_tax(answer);
}
}
private static double[] readTaxRates(String fileName) {
initialiseTaxRates(fileName);
String str;
String[] stringArr;
int len = 0; // number of tax rates (initialised to zero)
ArrayList<Double> temp = new ArrayList<>();
// try-with-resources construct
try (BufferedReader in = new BufferedReader(new
FileReader(fileName))) {
while ((str = in.readLine()) != null) {
stringArr = str.split(",");
len = stringArr.length;
for (int index = 0; index < len; index++) {
temp.add(Double.parseDouble(stringArr[index]));
}
}
in.close();
} catch (IOException e) {
System.out.println("File Read Error");
}
// translate arraylist to array
double[] returnValue = new double[len];
for (int index = 0; index < len; index++) {
returnValue[index] = temp.get(index);
}
return returnValue;
}
private static void initialiseTaxRates(String fileName) {
File f = new File(fileName);
if (!f.exists()) {
double first_bracket_tax_rate = 0, second_bracket_tax_rate =
0.19, third_bracket_tax_rate = 0.325,
fourth_bracket_tax_rate = 0.37, fifth_bracket_tax_rate = 0.45;
double[] tax_rates = {first_bracket_tax_rate, second_bracket_tax_rate, third_bracket_tax_rate,
fourth_bracket_tax_rate, fifth_bracket_tax_rate};
encodeTaxRatesToFile(tax_rates, fileName);
}
}
private static void encodeTaxRatesToFile(double[] data, String fileName) {
File file = new File(fileName);
// creates the file
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
// create a FileWriter Object
// try-with-resources construct
try (FileWriter writer = new FileWriter(file)) {
// Writes the content to the file as Comma Separated Values
(CSV)
for (int index = 0; index < data.length; index++) {
if (!(index == data.length - 1)) {
writer.write(Double.toString(data[index]) + ",");
} else {
writer.write(Double.toString(data[index]));
}
}
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void writeTaxReport(Employee employee, String fileName) {
//Delimiter used in CSV file
String COMMA_DELIMITER = ",";
//String TAB_DELIMITER = "\t";
String NEW_LINE_SEPARATOR = System.lineSeparator();
// file location is the project root directory
File f = new File(fileName);
// STEP:
// "if the taxreport.txt file does not exist in the directory of
the source code then the program should
// provide the taxreport.txt file as an input."
if (!f.exists()) {
try {
f.createNewFile();
try (PrintWriter pw = new PrintWriter(new FileOutputStream(f))) {
pw.write("Employee Id" + COMMA_DELIMITER + "Taxable Income" + COMMA_DELIMITER + "Tax" + NEW_LINE_SEPARATOR);
}
} catch (IOException e) {
e.printStackTrace();
}
}
// try-with-resources construct
// append mode
try (PrintWriter pw = new PrintWriter(new FileOutputStream(f,
true))) {
// platform independent line separator implementation
pw.append(employee.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
class Employee {
private String id;
private double income;
private double tax;
private static String REGEX = "[0-9][0-9][0-9][0-9]";
// constructor
Employee(String id, double income, double tax){
this.id = id;
this.income = income;
this.tax = tax;
}
public String getId() {
return id;
}
public double getIncome() {
return income;
}
public double getTax() {
return tax;
}
@Override
public String toString() {
String COMMA_DELIMITER = ",";
String income = String.format( "%.2f", getIncome() );
String tax = String.format( "%.2f", getTax());
return String.format("%s %s %s %n", getId() + COMMA_DELIMITER,
income + COMMA_DELIMITER, tax);
}
static boolean isValidId(String id){
return id.matches(REGEX);
}
}
--------------------------------------------------
taxrates.txt
-------------------
0.0,0.19,0.325,0.37,0.45
--------------------------------------------------
taxreport.txt
-------------------------
Employee Id,Taxable Income,Tax
4567, 90876.00, 21256.124561, 120000.00, 32032.00
Kindly revert for any queries
Thanks.
Get Answers For Free
Most questions answered within 1 hours.