Specifics: Create a TaxReturn class with fields that hold a taxpayer’s Social Se
ID: 3679759 • Letter: S
Question
Specifics:
Create a TaxReturn class with fields that hold a taxpayer’s Social Security number, last name, first name, street address, city, state, zip code, annual income, marital status, and tax liability. Include a constructor that requires arguments that provide values for all the fields other than the tax liability. The constructor calculates the tax liability based on annual income and the percentages in the following table.
Marital Status
Income ($)
Single
Married
0-20,000
15%
14%
20,000-50,000
22%
20%
50,000 and over
30%
28%
In the TaxReturn class, also include a display method that displays all the TaxReturn data. Save the file as TaxReturn.java. Create an application that prompts a user for the data needed to create a TaxReturn. Continue to prompt the user for data as long as any of the following are true:
The Social Security number is not in the correct format, with digits and dashes in the appropriate positions; for example, 999-99-9999.
The zip code is not five digits.
The marital status does not begin with one of the following: “S”, “s”, “M”, or “m”.
The annual income is negative.
After all the input data is correct, create a TaxReturn object and then display its values. Save the file as PrepareTax.java.
Marital Status
Income ($)
Single
Married
0-20,000
15%
14%
20,000-50,000
22%
20%
50,000 and over
30%
28%
Explanation / Answer
TaxReturn.java
import java.text.NumberFormat;
import javax.swing.JOptionPane;
import javax.swing.*;
public class TaxReturn {
//Constructor that calculates tax liability
public TaxReturn(TaxPayer show){
double taxLib;
double taxRate = 0;
String socialNum = show.getSocial();
String first = show.getfirstName();
String last = show.getlastName();
double income = show.getIncome();
String maritalStatus = show.getMarried();
String street = show.getStreet();
String city = show.getCity();
String state = show.getState();
int zip = show.getZip();
NumberFormat format = NumberFormat.getCurrencyInstance();
//Calculates tax rate for income 0-20000
if((income <= 20000) && (maritalStatus == "Single"))
{
taxRate = .15;
}
else if ((income <= 20000) && (maritalStatus == "Married")){
taxRate = .14;
}
//Calculates tax rate for income 20,001-50,0000
if(((income > 20000) && (income <= 50000)) && (maritalStatus == "Single"))
{
taxRate = .22;
}
else if (((income > 20000) && (income <= 50000)) && (maritalStatus == "Married"))
{
taxRate = .20;
}
//Calculates tax rate for income 50,001 and over
if((income > 50000) && (maritalStatus == "Single")){
taxRate = .30;
}
else if ((income > 50000) && (maritalStatus == "Married")){
taxRate = .28;
}
//Calculate how much is owned in taxes
taxLib = (income * taxRate);
//Displays information
JOptionPane.showMessageDialog(null,"Social: " + socialNum +
" Name: " + first + " " + last +
" Income: " + format.format(income) +
" Marital Status: " + maritalStatus +
" Address: " + street + " " + city + " " + zip + " " + state +
" Tax Rate: " + (taxRate * 100) + "%" +
" Total Tax Liability: " + format.format(taxLib), "Your Tax Info:", JOptionPane.INFORMATION_MESSAGE);
}
}
TaxPayer.java
public class TaxPayer {
private int zip;
private String social, street, firstName, lastName, city, state, marriedStatus;
private double income;
//default constructor
public TaxPayer(){
social = "999-999-999";
firstName = "Taxpayer first name";
lastName = "Taxpayer last name";
city = "Taxpayer city";
state = "Taxpayer state";
marriedStatus = "S";
income = 0.00;
}
//Getter setter for social security
public String getSocial(){
return social;
}
public void setSocial(String socialNum){
social = socialNum;
}
//Getter setter for first name
public String getfirstName(){
return firstName;
}
public void setfirstName(String name){
firstName = name;
}
//Getter setter for last name
public String getlastName(){
return lastName;
}
public void setlastName(String lname){
lastName = lname;
}
//Getter setter for street
public String getStreet(){
return street;
}
public void setStreet(String taxStreet){
street = taxStreet;
}
//Getter setter for city
public String getCity(){
return city;
}
public void setCity(String taxCity){
city = taxCity;
}
//Getter setter for state
public String getState(){
return state;
}
public void setState(String taxState){
state = taxState;
}
//Getter setter for street
public int getZip(){
return zip;
}
public void setZip(int taxZip){
zip = taxZip;
}
//Getter setter for marriedStatus
public String getMarried(){
return marriedStatus;
}
public void setMarried(String isMarried){
marriedStatus = isMarried;
}
//Getter setter for income
public double getIncome(){
return income;
}
public void setIncome(double taxIncome){
income = taxIncome;
}
}
PrepareTaxMainProgram.java
package tax;
import javax.swing.*;
import javax.swing.JOptionPane;
public class PrepareTaxMainProgram {
public static void main(String[] args) {
//Object variable declarations
TaxPayer taxPayer1;
taxPayer1 = getData();
TaxReturn tax = new TaxReturn(taxPayer1);
System.exit(0);
}
public static TaxPayer getData(){
String[] choices = {"Single", "Married"};
String socialNum = JOptionPane.showInputDialog(null, "Please input your 9 digit social security number in 999-99-9999 format: ", "Social Security Number", JOptionPane.QUESTION_MESSAGE);
//checks if the SSN is valid and loops until the correct format is entered
if(checkSSN(socialNum) == false){
while(checkSSN(socialNum) == false)
{
JOptionPane.showMessageDialog(null, "You entered " + socialNum + " as your social security number", "Incorrect SSN input", JOptionPane.QUESTION_MESSAGE);
socialNum = JOptionPane.showInputDialog(null, "Please check your again and input it in this format 999-99-9999 inculding dashes: ", "Try Again SSN Input", JOptionPane.QUESTION_MESSAGE);
}
}
String name = JOptionPane.showInputDialog(null, "Please enter your First name: ", "First Name", JOptionPane.QUESTION_MESSAGE);
String lname = JOptionPane.showInputDialog(null, "Please enter your Last name: ", "Last Name", JOptionPane.QUESTION_MESSAGE);
String taxIncomeString = JOptionPane.showInputDialog(null, "Please enter your annual income: ", "Annual Income", JOptionPane.QUESTION_MESSAGE);
//Checks to see if income are parsed as only digits
if(isParsable(taxIncomeString) == false){
//loop that displays what user input and re-prompts them to enter income
while(isParsable(taxIncomeString) == false){
JOptionPane.showMessageDialog(null, "You entered " + taxIncomeString + "as your Income", "Incorrect Income Input", JOptionPane.QUESTION_MESSAGE);
taxIncomeString = JOptionPane.showInputDialog(null, "Please input ONLY DIGITS for your Income: ", "Try Again Income", JOptionPane.QUESTION_MESSAGE);
} //end while loop
}
//Converts tacIncomeString to double
double taxIncome = Double.parseDouble(taxIncomeString);
if(Math.signum(taxIncome) == -1.0)
{
//while loop that displays what the user input and prompts to enter a positive number
while(Math.signum(taxIncome) == -1.0){
JOptionPane.showMessageDialog(null, "You entered " + taxIncome + " as your income", "Incorrect Income Input", JOptionPane.QUESTION_MESSAGE);
taxIncome = Double.parseDouble(JOptionPane.showInputDialog(null, "Please enter A POSITIVE NUMBER for your annual income: ", "Try Again Annual Income", JOptionPane.QUESTION_MESSAGE));
//This will error here if a non integer is input
}
}
String married = (String) JOptionPane.showInputDialog(null, "Please select your marital status: ", "Marital Status", JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);
String taxStreet = JOptionPane.showInputDialog(null, "Please enter your street address: ", "Street Address", JOptionPane.QUESTION_MESSAGE);
String taxCity = JOptionPane.showInputDialog(null, "Please enter your city: ", "City", JOptionPane.QUESTION_MESSAGE);
String state = JOptionPane.showInputDialog(null, "Please enter your state: ", "State", JOptionPane.QUESTION_MESSAGE);
String zipString = JOptionPane.showInputDialog(null, "Please input your 5 digit zip code: ", "Zip Code", JOptionPane.QUESTION_MESSAGE);
//checks to see if the zipCode input are digits and prompts for input again if not
if(isParsable(zipString) == false){
while(isParsable(zipString) == false){
JOptionPane.showMessageDialog(null, "You entered " + zipString + "as your zip code", "Incorrect Zipcode", JOptionPane.QUESTION_MESSAGE);
zipString = JOptionPane.showInputDialog(null, "Please input ONLY DIGITS for your zip code: ", "Try Again Zipcode", JOptionPane.QUESTION_MESSAGE);
} //end while loop
}
//Converts the zipcode into int
int zip = Integer.parseInt(zipString);
//checks if the Zipcode is only 5 digits
if((Math.floor(Math.log10(zip) + 1) != 5))
{
//while the zip entered is not 5 digits: display what was entered and prompt again
while ((Math.floor(Math.log10(zip) + 1) != 5))
{
JOptionPane.showMessageDialog(null, "You entered " + zip + " as your zip code", "Try Again Zipcode", JOptionPane.QUESTION_MESSAGE);
zip = Integer.parseInt(JOptionPane.showInputDialog(null, "Make sure you zip code is ONLY 5 digits: ", "Try Again Zipcode", JOptionPane.QUESTION_MESSAGE));
}
};
TaxPayer data = new TaxPayer();
data.setSocial(socialNum);
data.setfirstName(name);
data.setlastName(lname);
data.setIncome(taxIncome);
data.setMarried(married);
data.setStreet(taxStreet);
data.setCity(taxCity);
data.setState(state);
data.setZip(zip);
return data;
}
//Checks if the social security number is input correctly using Regular Expression
public static boolean checkSSN(String social) {
return social.matches("\d{3}-\d{2}-\d{4}");
}
//Checks string input to make sure only digit are entered. Returns if parsable as int
public static boolean isParsable(String input){
boolean parsable = true;
try{
Integer.parseInt(input);
}catch(NumberFormatException e){
parsable = false;
}
return parsable;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.