Create a Spending application to help examine the spending patterns of a user. The application should prompt the user for the amount spent last month on food, clothing, entertainment, and rent, and then displays a table showing the percentage of expenditures in each category.
import java.util.Scanner;
public class Spending {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double sum = 0;
double food;
double clothing;
double entertainment;
double rent;
System.out.print("Enter the amount spent on food: ");
food = in.nextDouble();
sum += food;
System.out.print("Enter the amount spent on clothing: ");
clothing = in.nextDouble();
sum += clothing;
System.out.print("Enter the amount spent on entertainment: ");
entertainment = in.nextDouble();
sum += entertainment;
System.out.print("Enter the amount spent on rent: ");
rent = in.nextDouble();
sum += rent;
System.out.printf("\n%-40s %.2f %%\n", "The percentage spent on food:", (food / sum) * 100);
System.out.printf("%-40s %.2f %%\n", "The percentage spent on clothing:", (clothing / sum) * 100);
System.out.printf("%-40s %.2f %%\n", "The percentage spent on entertainment:", (entertainment / sum) * 100);
System.out.printf("%-40s %.2f %%\n", "The percentage spent on rent:", (rent / sum) * 100);
}
}
Comments
Leave a comment