How to create/generate invoice /bill/receipt in java - Free Printable
Educational worksheet: How to create/generate invoice /bill/receipt in java. Download and print for classroom or home learning activities.
JPG
1280×720
97.6 KB
Free · Personal Use
Quality Assured by Worksheets Library Team
Reviewed for educational accuracy and age-appropriateness
ID: #1723363
⭐
Show Answer Key & Explanations
Step-by-step solution for: How to create/generate invoice /bill/receipt in java
▼
Show Answer Key & Explanations
Step-by-step solution for: How to create/generate invoice /bill/receipt in java
To create an invoice in a Java project, you can use simple Java programming concepts such as:
- Classes and Objects
- String formatting
- File I/O (optional, if saving to a file)
- Printing formatted output
Below is a complete example of how to generate the invoice shown in your image using Java.
---
We’ll write a Java program that prints the invoice in the exact format shown in your image.
---
```java
public class InvoiceGenerator {
public static void main(String[] args) {
// Company Information
String companyName = "Company Name";
String contactNo = "Contac No-xxxxxxxx";
String address = "Adres- enter Adress";
// Items with their prices
String[] items = {
"rice-5",
"Biscut-8",
"Chiken-5-kg",
"Suger-2kg",
"Cake-4",
"Visky-4"
};
int[] prices = {1250, 660, 2200, 260, 800, 60000};
// Print the invoice
System.out.println("================================");
System.out.println(companyName);
System.out.println(contactNo);
System.out.println(address);
System.out.println("================================");
// Print each item and price
for (int i = 0; i < items.length; i++) {
System.out.printf("%-20s %d%n", items[i], prices[i]);
}
System.out.println("================================");
System.out.println("Develop by: SynTech");
}
}
```
---
1. `System.out.println()`:
- Used to print text lines.
- `================================` creates the border lines.
2. `printf()` with Format Specifier:
- `%20s`: Left-aligns the string within 20 characters.
- `%d`: Prints integer (price).
- `%-20s`: Left-aligned string (item name), padded on the right.
3. Arrays:
- `items[]`: Stores product descriptions.
- `prices[]`: Stores corresponding prices.
4. Loop:
- Iterates through both arrays and prints them side by side.
---
```
================================
Company Name
Contac No-xxxxxxxx
Adres- enter Adress
================================
rice-5 1250
Biscut-8 660
Chiken-5-kg 2200
Suger-2kg 260
Cake-4 800
Visky-4 60000
================================
Develop by: SynTech
```
> ⚠️ Note: Minor typo in your image ("Biscut" instead of "Biscuit", "Chiken" → "Chicken", "Suger" → "Sugar", "Visky" → "Whiskey"). But we kept it as-is since it's from the input.
---
If you want to save this invoice to a `.txt` file, use `FileWriter`:
```java
import java.io.FileWriter;
import java.io.IOException;
// Inside main(), after printing, add:
try (FileWriter writer = new FileWriter("invoice.txt")) {
writer.write("================================\n");
writer.write(companyName + "\n");
writer.write(contactNo + "\n");
writer.write(address + "\n");
writer.write("================================\n");
for (int i = 0; i < items.length; i++) {
writer.write(String.format("%-20s %d\n", items[i], prices[i]));
}
writer.write("================================\n");
writer.write("Develop by: SynTech\n");
} catch (IOException e) {
System.err.println("Error writing file: " + e.getMessage());
}
```
This saves the invoice to `invoice.txt` in your project directory.
---
You’ve now learned how to:
- Create a formatted invoice in Java.
- Use `printf()` for alignment.
- Store data in arrays.
- Optionally save output to a file.
Let me know if you want to:
- Add tax or total calculation,
- Make it GUI-based (Swing),
- Or export to PDF!
Happy coding! 🚀
- Classes and Objects
- String formatting
- File I/O (optional, if saving to a file)
- Printing formatted output
Below is a complete example of how to generate the invoice shown in your image using Java.
---
✔ Step-by-step Solution
We’ll write a Java program that prints the invoice in the exact format shown in your image.
---
🔧 Java Code: `InvoiceGenerator.java`
```java
public class InvoiceGenerator {
public static void main(String[] args) {
// Company Information
String companyName = "Company Name";
String contactNo = "Contac No-xxxxxxxx";
String address = "Adres- enter Adress";
// Items with their prices
String[] items = {
"rice-5",
"Biscut-8",
"Chiken-5-kg",
"Suger-2kg",
"Cake-4",
"Visky-4"
};
int[] prices = {1250, 660, 2200, 260, 800, 60000};
// Print the invoice
System.out.println("================================");
System.out.println(companyName);
System.out.println(contactNo);
System.out.println(address);
System.out.println("================================");
// Print each item and price
for (int i = 0; i < items.length; i++) {
System.out.printf("%-20s %d%n", items[i], prices[i]);
}
System.out.println("================================");
System.out.println("Develop by: SynTech");
}
}
```
---
📝 Explanation
1. `System.out.println()`:
- Used to print text lines.
- `================================` creates the border lines.
2. `printf()` with Format Specifier:
- `%20s`: Left-aligns the string within 20 characters.
- `%d`: Prints integer (price).
- `%-20s`: Left-aligned string (item name), padded on the right.
3. Arrays:
- `items[]`: Stores product descriptions.
- `prices[]`: Stores corresponding prices.
4. Loop:
- Iterates through both arrays and prints them side by side.
---
💡 Output (Matches Your Image)
```
================================
Company Name
Contac No-xxxxxxxx
Adres- enter Adress
================================
rice-5 1250
Biscut-8 660
Chiken-5-kg 2200
Suger-2kg 260
Cake-4 800
Visky-4 60000
================================
Develop by: SynTech
```
> ⚠️ Note: Minor typo in your image ("Biscut" instead of "Biscuit", "Chiken" → "Chicken", "Suger" → "Sugar", "Visky" → "Whiskey"). But we kept it as-is since it's from the input.
---
🖨️ Optional: Save to File
If you want to save this invoice to a `.txt` file, use `FileWriter`:
```java
import java.io.FileWriter;
import java.io.IOException;
// Inside main(), after printing, add:
try (FileWriter writer = new FileWriter("invoice.txt")) {
writer.write("================================\n");
writer.write(companyName + "\n");
writer.write(contactNo + "\n");
writer.write(address + "\n");
writer.write("================================\n");
for (int i = 0; i < items.length; i++) {
writer.write(String.format("%-20s %d\n", items[i], prices[i]));
}
writer.write("================================\n");
writer.write("Develop by: SynTech\n");
} catch (IOException e) {
System.err.println("Error writing file: " + e.getMessage());
}
```
This saves the invoice to `invoice.txt` in your project directory.
---
✔ Summary
You’ve now learned how to:
- Create a formatted invoice in Java.
- Use `printf()` for alignment.
- Store data in arrays.
- Optionally save output to a file.
Let me know if you want to:
- Add tax or total calculation,
- Make it GUI-based (Swing),
- Or export to PDF!
Happy coding! 🚀
Parent Tip: Review the logic above to help your child master the concept of print receipt code in java.