Skip to content

Instantly share code, notes, and snippets.

@imryan
Last active December 28, 2015 16:39
Show Gist options
  • Save imryan/7530500 to your computer and use it in GitHub Desktop.
Save imryan/7530500 to your computer and use it in GitHub Desktop.
CS 2 test. Calculates tax rate based on income.
import java.util.*;
import java.text.*;
public class Tax {
public static void main(String[] args) {
// Declare double values to be used in the program
double income = 0;
String taxRate = "";
double tax = 0;
// Assign income to a new random income amount from a method
income = getRandomIncome();
// Tax rate is 40% if income is more than $100,000
if (income > 100000) {
taxRate = "40%";
tax = .4;
}
// Tax rate is 30% if income is more than $15,000 but less than or equal to $100,000
else if (income > 15000 && income <= 100000) {
taxRate = "30%";
tax = .30;
}
// Tax rate is 5% if income is more than $5,000 but less than or equal to $15,000
else if (income > 5000 && income <= 15000) {
taxRate = "5%";
tax = .5;
// Otherwise, if income doesn't fall into these breakdowns, it is 0%. You are most likely poor
} else {
taxRate = "0%";
tax = 0;
}
// Create new NumberFormat instance
NumberFormat nf = NumberFormat.getCurrencyInstance();
// Output the formatted income total, tax rate, and the total of taxes
System.out.println("\nIncome: " + nf.format(income));
System.out.println("Tax rate: " + taxRate);
System.out.println("Price of taxes: " + nf.format((income * tax)));
}
public static double getRandomIncome() {
// Create a new random generator and return the rounded amount of income
// between 0 and around 150,000
Random random = new Random();
return Math.ceil(random.nextDouble() * 149000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment