Free. Exclusive. Just for you.
Four unique services that make learning easier, faster, and smarter - only on our website.

How to create/generate invoice /bill/receipt in java - Free Printable

How to create/generate invoice /bill/receipt in java

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
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.

---

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.
Print Download

How to use

Click Print to open a print-ready version directly in your browser, or use Download to save the file to your device. The ⭐ Answer button generates an AI answer key instantly - useful for teachers who need a quick reference. Need a different version? Our AI Worksheet Generator lets you create a custom worksheet on any topic in seconds.

(view all print receipt code in java)

Pizza Shop Billing System using Java Swing - GeeksforGeeks
Java Program for Shopping Bill - Javatpoint
Java Receipt OCR API library code example on github open source ...
fees receipt system in java by using netbeans
GitHub - heyteacher/print-receipt: a Point Of Sale Application for ...
Hello everyone,Im actually working on a fast food order program..pdf
Android Bluetooth Printing Example Code with Actual Printer Device
CCSWIKI / Java Receipt Printing
Android Java project — Receipt Generator using Android phone and ...
Solved Using eclipse java oxygen to write the code of an | Chegg.com