Problem a (PA5a.java) Write a program that deals with inflation, which is essentially the rising cost of general goods over time. That is, the price of goods, such as a packet of peanuts, goes up as time goes by. So, you will write a program to gauge the expected cost of an item in a specified number of years. The program asks for the cost of the item, the number of years, and the rate of inflation. The output is the estimated cost of the item after that number of years, using the given inflation rate. The user enters the inflation rate as a percentage, for example 4.5. You will have to convert the percentage to a fraction (like 0.045), and then use a loop to estimate the item's price adjusted for inflation. Note that this is similar to computing compound interest on a credit card account or a mortgage. Also note that you must check each of the values provided by the user to make sure that they are reasonable. Finally, you have to print out the price with exactly two places after the decimal (for the cents) after your calculations are done. To adjust the price for inflation, you need to increase the price by the inflation rate each year. For example, if you have an item that is initially $10, with inflation rate of 10%, the adjusted prices will be: • After 1 year: $10.00 ∗ (1 + 0.10) = $11.00 • After 2 years: $11.00 ∗ (1 + 0.10) = $12.10 • After 3 years: $12.10 ∗ (1 + 0.10) = $13.31 … In other words, to calculate the price after another year, you have to use the value from the current year, NOT the original price. To do this, you must use a loop. An example of what your program should output: (The numbers marked with blue color are the user’s input.) Enter the current price of the item: $10 Enter the number of years: 3 Enter the inflation rate as a percentage: 10 After 3 years, the price will be $13.31
******You have been supplied JUnit tests for some simple valid examples, as well as negative price, negative year, and negative interest rate.
******Please make sure to add an error output message for the negative prices, years, and interest rates instead of having the program run the calculations with negative numbers
Below is the code to find inflation for product given the number of years and inflation rate. It displays increased price for every year. The code is well commented to explain all the steps. It gets user input and program stops if values entered are negative integers.
//Code starts here
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int x=1;
double f_price=0;
//Get item price
System.out.print("Enter the current price of the item: $");
double c_price = input.nextDouble();
//If single input is invalid there is no need to get other user
input
//exit the program
if (c_price<=0) {
System.out.println("The current price must be at least 0!");
System.exit(0);
}
//Get number of years
System.out.print("Enter the number of years: ");
int years = input.nextInt();
//Exit if years value is negative
if (years<=0) {
System.out.println("The number of years must be at least
0!");
System.exit(0);
}
//Get inflation rate
System.out.print("Enter the inflation rate as a percentage:
");
double infr = input.nextDouble();
//Exit if inflation rate value is negative
if (infr<=0) {
System.out.println("The inflation rate must be at least 0!");
System.exit(0);
}
//Get rate by dividing by 100
double infrd = infr/100;
f_price = c_price; //Set the initial price to the price input by
user
while ( x <= years) { // Loop from 1 to number of years
f_price = f_price * (1+infrd);
//We need to multiply updated price with (1+rate) to get updated
price for every year
//Display value for each year.
System.out.printf("After %d years, the price will be $%.2f%n", x,
f_price);
x++;
}
}
}
//Code ends here
Output
Get Answers For Free
Most questions answered within 1 hours.