Project: A Rental Invoice Calculator
Build a small program that prices several rentals, applies weekend rates and a long-rental discount, prints a formatted invoice and is verified against an independent calculation.
In this lesson you will
- Combine classes, loops and methods
- Format output with printf
- Keep money in whole cents
- Verify totals independently
About 28 minutes
From pieces to a program
This lesson joins the first seven. Hana wants an invoice for a customer who rented several bikes for different numbers of days, with a weekend surcharge and a discount for long rentals. You will model a rental as a class, store the rentals in an array, loop through them, format the result, and prove that the arithmetic is right with a second method. Before writing code, write the rules in plain words, because a program that implements unclear rules cannot be correct.
| Rule | Value |
|---|---|
| Weekday price per day | 12.00 |
| Weekend price per day | 18.00 |
| Discount for 5 or more days | 10 percent off the rental |
| Currency | Euro, kept in whole cents inside the program |
Money is kept as whole cents in an int or long, never in a double, for the reason that lesson 2 showed. A price of 12.00 euro is 1200 cents, the discount is applied with integer arithmetic, and only the printing converts back to euros with two decimals. The printf method formats text with placeholders: %s for text, %d for whole numbers, and %.2f for a decimal with two places, and %n gives a line break.
public class Main {
static final int WEEKDAY = 1200, WEEKEND = 1800;
static class Rental {
final String bike; final int weekdays, weekendDays;
Rental(String bike, int weekdays, int weekendDays) { this.bike = bike; this.weekdays = weekdays; this.weekendDays = weekendDays; }
int days() { return weekdays + weekendDays; }
int gross() { return weekdays * WEEKDAY + weekendDays * WEEKEND; }
int discount() { return days() >= 5 ? gross() / 10 : 0; }
int net() { return gross() - discount(); }
}
public static void main(String[] args) {
Rental[] rentals = { new Rental("City 1", 3, 0), new Rental("Trail 2", 2, 2), new Rental("Tandem 3", 4, 3), new Rental("City 4", 0, 1) };
int total = 0, totalDiscount = 0;
System.out.printf("%-10s %5s %9s %9s %9s%n", "Bike", "Days", "Gross", "Discount", "Net");
for (Rental r : rentals) {
System.out.printf("%-10s %5d %9.2f %9.2f %9.2f%n", r.bike, r.days(), r.gross() / 100.0, r.discount() / 100.0, r.net() / 100.0);
total += r.net(); totalDiscount += r.discount();
}
System.out.printf("%-10s %5s %9s %9.2f %9.2f%n", "TOTAL", "", "", totalDiscount / 100.0, total / 100.0);
System.out.println("total in cents: " + total);
}
}
Bike Days Gross Discount Net
City 1 3 36.00 0.00 36.00
Trail 2 4 60.00 0.00 60.00
Tandem 3 7 102.00 10.20 91.80
City 4 1 18.00 0.00 18.00
TOTAL 10.20 205.80
total in cents: 20580
The program printed one line for each of the four rentals and a total of 205.80 euro, which is 20580 cents. The columns line up because printf pads each value to a fixed width, and the minus sign in %-10s left-aligns the bike name. Only the 7-day tandem qualifies for the discount, since the Trail bike has 4 days (2 + 2) and falls just short, and the program prints a discount of zero for the other three.
To check the totals independently, the course recomputed every rental in Python from the same rules, with the gross as weekdays x 1200 + weekend days x 1800, the discount as a tenth of the gross for 5 days or more, and the net as the difference. The sum of the four nets was 20580 cents, equal to the Java total, so the invoice is right. Notice what made the check possible: the rules were written down first, the arithmetic is in whole cents, and each rental is a small object with methods that do one thing.
Check by hand: City 1 is 3 weekdays, so 3 x 1200 = 3600. Trail 2 is 2 x 1200 + 2 x 1800 = 6000. Tandem 3 is 4 x 1200 + 3 x 1800 = 10,200, with 7 days so a discount of 1,020 and a net of 9,180. City 4 is one weekend day, 1800. The total is 3600 + 6000 + 9180 + 1800 = 20,580 cents.
The discount starts at exactly 5 days. Test with 4, 5 and 6 days, because off-by-one mistakes hide at the boundary.
A short table of rules is easy to review with the customer, and it becomes the checklist for your tests.
Every program in this lesson was compiled and run with OpenJDK 21 while the course was written. Java releases twice a year, so check the official Java documentation for the current version and for features added after Java 21.
Practice
Extend the project: add a deposit of 20 euro per bike that is not discounted, read the rentals from command-line arguments, or add a rule that a family of three or more bikes gets 5 percent off the total. After each change, recompute one case by hand.
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
Why are prices kept in cents?
- To avoid floating point rounding errors
- Because Java requires it
- To make programs slower
- Because cents are bigger
Whole numbers are exact.
What does %.2f print?
- A text
- A whole number
- A line break
- A decimal number with two digits after the point
printf placeholder.
What does %-10s do?
- Removes text
- Rounds a number
- Uses 10 decimals
- Pads text to 10 characters, left-aligned
It aligns the columns.
How was the total verified?
- By trusting the output
- By printing it twice
- By recomputing the rules in another language
- It was not
Both gave the same cents.
What is the first step of the project?
- Choose colours
- Write the main method
- Write the rules in plain words
- Buy a bike
Code cannot be right if the rules are unclear.
Summary
- A project joins classes, loops and methods.
- Keep money in whole cents and format only on output.
- printf formats text and numbers into columns.
- Verify results with an independent calculation.
Questions
Can I read input from the keyboard?
Yes, with the Scanner class or command-line arguments. The next lessons use both.
Why is Rental a static nested class?
It lets the example stay in one file. In a real project each class lives in its own file.
Should the discount be an int divided by 10?
For a simple tenth, yes. For other percentages, multiply first and divide last to avoid rounding errors.
