Question

Write a Java program that will calculate tax for the employees of a company called XYZ....

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.

Homework Answers

Answer #1

`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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a Python program that computes the income tax for an individual. The program should ask...
Write a Python program that computes the income tax for an individual. The program should ask the user to enter the total taxable income for the year. The program then uses the tax bracket (as shown below) to calculate the tax amount. This is based on a progressive income tax system which is how income tax is calculated in the U.S. As a result, this for example means that only income above $500,001 is taxed at 37%. Income of lower...
Write a Python program that computes the income tax for an individual. The program should ask...
Write a Python program that computes the income tax for an individual. The program should ask the user to enter the total taxable income for the year. The program then uses the tax bracket (as shown below) to calculate the tax amount. This is based on a progressive income tax system which is how income tax is calculated in the U.S. As a result, this for example means that only income above $500,001 is taxed at 37%. Income of lower...
This is a Java program Program Description You work for a local cell phone company and...
This is a Java program Program Description You work for a local cell phone company and have been asked to write a program to calculate the price of a cell phone data plan being purchased by a customer. The program should do the following tasks: Display a menu of the data plans that are available to be purchased. Read in the user’s selection from the menu.  Input Validation: If the user enters an invalid option, the program should display an error...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
This program is in C++: Write a program to allow the user to: 1. Create two...
This program is in C++: Write a program to allow the user to: 1. Create two classes. Employee and Departments. The Department class will have: DepartmentID, Departmentname, DepartmentHeadName. The Employee class will have employeeID, emploeename, employeesalary, employeeage, employeeDepartmentID. Both of the above classes should have appropriate constructors, accessor methods. 2. Create two arrays . One for Employee with the size 5 and another one for Department with the size 3. Your program should display a menu for the user to...
Write a Python program named lastNameVolumes that finds the volumes of different 3 D objects such...
Write a Python program named lastNameVolumes that finds the volumes of different 3 D objects such as a cube, sphere, cylinder and cone. In this file there should be four methods defined. Write a method named cubeVolFirstName, which accepts the side of a cube in inches as an argument into the function. The method should calculate the volume of a cube in cubic inches and return the volume. The formula for calculating the volume of a cube is given below....
I was asked to write a program in CodeBlocks. C++ format. I've written the first half...
I was asked to write a program in CodeBlocks. C++ format. I've written the first half of the code but I am using else and if statements. I feel like the code is too long to do a simple calculation. In CodeBlock when you build and run your code, a new terminal window opens (in windows a new console window) and you can enter your inputs there. It is not like eclipse that you enter your inputs in console view....
Problem: A company wants a program that will calculate the weekly paycheck for an employee based...
Problem: A company wants a program that will calculate the weekly paycheck for an employee based on how many hours they worked. For this company, an employee earns $20 an hour for the first 40 hours that they work. The employee earns overtime, $30 an hour, for each hour they work above 40 hours. Example: If an employee works 60 hours in a week, they would earn $20/hr for the first 40 hours. Then they would earn $30/hr for the...
Program Description A local company has requested a program to calculate the cost of different hot...
Program Description A local company has requested a program to calculate the cost of different hot tubs that it sells. Use the following instructions to code the program: File 1 - HotTubLastname.java Write a class that will hold fields for the following: The model of the hot tub The hot tub’s feature package (can be Basic or Premium) The hot tub’s length (in inches) The hot tub’s width (in inches) The hot tub’s depth (in inches) The class should also...
Write a Python 3 program called “parse.py” using the template for a Python program that we...
Write a Python 3 program called “parse.py” using the template for a Python program that we covered in this module. Note: Use this mod7.txt input file. Name your output file “output.txt”. Build your program using a main function and at least one other function. Give your input and output file names as command line arguments. Your program will read the input file, and will output the following information to the output file as well as printing it to the screen:...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT