In: Computer Science
Objectives:
Write a program which reads User Input using Scanner
Print formatted output using printf or DecimalFormat
Practice programming simple mathematical calculations
Instructions:
Samwise Gamgee has finally decided to submit his expense report for all of his adventures regarding his travels to Mordor. Part of those expenses are his stay at the Prancing Pony Inn located in Bree.
You are to write a simple Java program which will generate an Invoice for his stay at the Inn.
Your program should ask for user input which represents the following information:
the number of nights Sam stayed at the Inn
the tax rate for the taxes collected by the Shire
the tax rate for the taxes collected by the City of Bree
cost per night of staying at the Inn
The program should then print out an Invoice which is similar to:
The Prancing Pony Inn, City of Bree, Shire
Invoice for Samwise Gamgee
Cost per night: $110
Shire Taxes per night: @Rate of 6.75% is $7.42
City of Bree Taxes per night: @Rate of 1.25% is $1.37
Total Cost per night: $118.79
Number of nights Stayed: 4
Total Cost: $475.16
(Bonus: Change the program so that the name of the Person and the two Tax Districts can also be entered by the user. These names should be allowed to contain spaces)
import java.util.Scanner;
public class HelloWorld{
public static void main(String []args){
Scanner sc = new Scanner(System.in);
System.out.print("the number of nights Sam stayed at the Inn:
");
int nights = sc.nextInt();
System.out.print("the tax rate for the taxes collected by the
Shire: ");
float shireTaxRate = sc.nextFloat();
System.out.print("the tax rate for the taxes collected by the City
of Bree: ");
float breeTaxRate = sc.nextFloat();
System.out.print("cost per night of staying at the Inn: ");
int costPerNight = sc.nextInt();
float ShireTaxPerNight = (costPerNight*shireTaxRate)/100;
float breeTaxPerNight = (costPerNight*breeTaxRate)/100;
float totalCostPerNight = costPerNight + ShireTaxPerNight +
breeTaxPerNight;
float totalCost = totalCostPerNight*nights;
System.out.println("The Prancing Pony Inn, City of Bree,
Shire");
System.out.println("Invoice for Samwise Gamgee");
System.out.printf("Cost per night: $%d\n", costPerNight);
System.out.printf("Shire Taxes per night: @Rate of %.2f%% is
$%.2f\n", shireTaxRate, ShireTaxPerNight);
System.out.printf("City of Bree Taxes per night: @Rate of %.2f%% is
$%.2f\n", breeTaxRate, breeTaxPerNight);
System.out.printf("Total Cost per night: $%.2f\n",
totalCostPerNight);
System.out.printf("Number of nights Stayed: %d\n", nights);
System.out.printf("Total Cost: $%.2f\n", totalCost);
}
}