just need help with formatting a string to look like this:
Here is your tax breakdown:
Income $25,000.00
Dependants 1
----------------------------
Federal Tax $4,250.00
Provincial Tax $1,317.75
============================
Total Tax $5,567.75"
heres my code, please help me format as above with spaces and decimals
import java.util.Scanner;
public class HelloWorld {
public static void main(String args[]) {
double income, fedTax, provTax;
int dependents;
Scanner input = new Scanner(System.in);
System.out.print("Please enter your taxable income: ");
income = input.nextInt();
System.out.println();
System.out.print("Please enter your number of dependents: ");
dependents = input.nextInt();
System.out.println();
if (income <= 29590) {
fedTax = 0.34 * income;
}
else if (income <= 59179.99) {
fedTax = 0.34 * 29590 + 0.26 * (income - 29590);
}
else {
fedTax = 0.34 * 29590 + 0.26 * 29590 + (0.29 * (income - 59180));
}
double base = 42.5 * fedTax;
double deductions = 160.50 + 328 * dependents;
provTax = base - deductions;
if (base < deductions) {
provTax = 0;
}
else {
provTax = base - deductions;
}
double totalTax = fedTax + provTax;
System.out.println("Here is your tax breakdown: ");
System.out.println(String.format(" Income%,10.d", income));
System.out.println(String.format(" Dependants%10.d", dependents));
System.out.println("-------------------------");
System.out.println(String.format(" Federal Tax%,10.2f", fedTax));
System.out.println(String.format("Provincial tax%,10.2f", provTax));
System.out.println("=========================");
System.out.println(String.format(" Total Tax%,10.2f", totalTax));
}
}
one of the main problems that was happening was because I was trying to insert input in my tests using the system.out and printstream to sort of rig the test inputs for this program. Using this it is much easier to allow for multiple inputs in a row when the order of function calls overwrites the buffer for the program.
public class StringInputProvider implements InputProvider {
private StringBuilder inputBuilder = new StringBuilder();
private int currentIndex = 0;
public StringInputProvider(String... inputs) {
for(String input : inputs){
this.inputBuilder.append(input).append(System.lineSeparator());
}
}
public void addInput(String input) {
inputBuilder.append(input).append(System.lineSeparator());
}
public String nextLine() {
int nextLineStart = currentIndex;
while (currentIndex < inputBuilder.length() && inputBuilder.charAt(currentIndex) != '\n') {
currentIndex++;
}
String line = inputBuilder.substring(nextLineStart, currentIndex).trim();
currentIndex++; // Move past the newline character
return line;
}
}
EDIT: Added new values for new version