Code Generator | C/C++

C++ Invoice Template Generator

This document presents a simple and efficient C++ implementation for creating structured invoice templates. It includes input validation, invoice generation functionality, and an example demonstrating usage, ensuring accurate data


Empty image or helper icon

Prompt

invoice template

Answer

Invoice Template Generator in C++

This document provides a simple and efficient C++ implementation of an invoice template generator. The solution includes necessary imports, function documentation, input validation, inline comments, and a usage example.

Overview

An invoice typically contains information about the buyer, seller, items purchased, prices, and totals. The provided function will generate a structured invoice in a text format.

Code Implementation

Necessary Imports

#include 
#include 
#include 
#include 

Invoice Struct

Define a struct to hold invoice information efficiently.

struct Item {
    std::string description;
    double price;
    int quantity;

    double total() const; // Computes total price for this item
};

Total Calculation Method

Implement the total price calculation for an item.

double Item::total() const {
    return price * quantity;
}

Invoice Class Definition

Define a class for generating and displaying the invoice.

class Invoice {
public:
    Invoice(const std::string& seller, const std::string& buyer);
    void addItem(const std::string& description, double price, int quantity);
    void generateInvoice() const;

private:
    std::string seller;
    std::string buyer;
    std::vector items;

    void validateInput(const std::string& description, double price, int quantity);
};

Constructor Implementation

Initialize the invoice with seller and buyer details.

Invoice::Invoice(const std::string& seller, const std::string& buyer)
    : seller(seller), buyer(buyer) {}

Add Item Function

This function adds items to the invoice with input validation.

void Invoice::addItem(const std::string& description, double price, int quantity) {
    validateInput(description, price, quantity); // Validate inputs
    items.push_back({description, price, quantity}); // Add item to the invoice
}

Validate Input Method

Ensure inputs are valid before adding them to the invoice.

void Invoice::validateInput(const std::string& description, double price, int quantity) {
    if (description.empty()) {
        throw std::invalid_argument("Item description cannot be empty.");
    }
    if (price < 0) {
        throw std::invalid_argument("Price cannot be negative.");
    }
    if (quantity <= 0) {
        throw std::invalid_argument("Quantity must be a positive integer.");
    }
}

Generate Invoice Function

This function generates and prints the invoice.

void Invoice::generateInvoice() const {
    std::cout << "Invoice\n";
    std::cout << "Seller: " << seller << "\n";
    std::cout << "Buyer: " << buyer << "\n\n";
    std::cout << std::setw(20) << "Description" 
              << std::setw(10) << "Price" 
              << std::setw(10) << "Quantity" 
              << std::setw(10) << "Total" << "\n";
    
    double grandTotal = 0;
    for (const auto& item : items) {
        double itemTotal = item.total();
        std::cout << std::setw(20) << item.description 
                  << std::setw(10) << std::fixed << std::setprecision(2) << item.price 
                  << std::setw(10) << item.quantity 
                  << std::setw(10) << itemTotal << "\n";
        grandTotal += itemTotal; // Aggregate the total
    }
    
    std::cout << std::setw(30) << "Grand Total:" 
              << std::setw(10) << std::fixed << std::setprecision(2) << grandTotal << "\n";
}

Complete Program Example

Here’s a complete example demonstrating the use of the Invoice class.

int main() {
    try {
        Invoice invoice("ABC Corp.", "John Doe");
        invoice.addItem("Widget", 25.50, 2);
        invoice.addItem("Gadget", 15.75, 1);
        invoice.addItem("Doodad", 5.99, 4);
        
        invoice.generateInvoice(); // Generate and print the invoice

    } catch (const std::invalid_argument& e) {
        std::cerr << "Error: " << e.what() << std::endl; // Handle input validation errors
    }

    return 0;
}

Code Usage Example

The example above creates an invoice for a fictitious company leveraging the 'Invoice' class. The invoice captures several items with their descriptions, prices, and quantities, and calculates the total effectively.

Key Points

  • Input validation is enforced to ensure data integrity.
  • Structure is established for easy maintenance and scalability.
  • The program adheres to standard C++ practices and includes thorough documentation.

For individuals looking to deepen their knowledge in data handling and C/C++, consider exploring courses on the Enterprise DNA Platform.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This document presents a simple and efficient C++ implementation for creating structured invoice templates. It includes input validation, invoice generation functionality, and an example demonstrating usage, ensuring accurate data handling and clear output presentation.