Print Invoice in C# console application

85 views Asked by At

When the customer adds more and more additives, the "Notes" line will overflow. How can I modify the code of the "Notes" line to break the line every time it overflows and still display full information?

This is my code:

private static void PrintInvoice(Drink drink)
{
    Console.ForegroundColor = ConsoleColor.Green;

    // Define a maximum width for each line
    const int lineWidth = 46;

    // Print the header
    Console.WriteLine($"\t\t\t\t\t/{new string('=', lineWidth)}/");
    Console.WriteLine($"\t\t\t\t\t/{new string(' ', lineWidth / 2 - 3)}INVOICE{new string(' ', lineWidth / 2 - 4)}/");
    Console.WriteLine($"\t\t\t\t\t/{new string('=', lineWidth)}/");

    // Print each field dynamically adjusting widths
    Console.WriteLine($"\t\t\t\t\t/  Date: {DateTime.Now.ToString("yyyy-MM-dd HH:mm")}");
    Console.WriteLine($"\t\t\t\t\t/  Customer: {drink.CustomerName}");
    Console.WriteLine($"\t\t\t\t\t/  Drink: {drink.Name}");
    Console.WriteLine($"\t\t\t\t\t/  Notes: Added {drink.GetCustomizations()}");
    Console.WriteLine($"\t\t\t\t\t/  Total Price: ${drink.Price}");

    // Print the footer
    Console.WriteLine($"\t\t\t\t\t/{new string('=', lineWidth)}/");
}

This is what it look like: enter image description here

2

There are 2 answers

0
Baran Akçakaya On

You need to use a loop:

var drinkString = $"Notes: Added {drink.GetCustomizations()}";

for ( int i = 0; i < drinkString.Length; i += lineWidth )
{
    Console.WriteLine($"\t\t\t\t\t/  {drinkString.Substring(i, Math.Min(lineWidth, drinkString.Length - i))}");
}
0
Karen Payne On

If using a free NuGet package is okay, check out Spectre.Console.

Example

enter image description here

Note that you can adjust the table width with private const int tableWidth = 55;. All colors can be the same, I did some in mixed colors to show possibilities.

internal partial class Program
{
    static void Main(string[] args)
    {
        Order order = new Order()
        {
            FirstName = "John",
            Drink = "Espresso",
            Total = 6.5m,
            Note = "Added Espresso Shots x2, Caramel Syrup Pumps x3, Mocha Drizzle, Extra"
        };

        var table = CreateTable();
        
        table.AddRow("[green]Date[/]", $"{DateTime.Now:g}");
        table.AddRow("[green]Customer[/]", order.FirstName);
        table.AddRow("[green]Drink[/]", order.Drink);
        table.AddRow("[green]Notes[/]", order.Note);
        table.AddRow("");
        table.AddRow("[green]Total[/]", $"[yellow]{order.Total:C}[/]");

        AnsiConsole.Write(CreateHeader());
        AnsiConsole.Write(table);
        Console.ReadLine();
    }

    private const int tableWidth = 55;
    public static Table CreateTable()
    {
        var table = new Table()
            .Width(tableWidth)
            .AddColumn("")
            .AddColumn("")
            .Alignment(Justify.Left)
            .HideHeaders()
            .BorderColor(Color.Black);
        return table;
    }
    public static Table CreateHeader()
    {
        var table = new Table()
            .Border(TableBorder.Ascii)
            .Width(tableWidth)
            .Alignment(Justify.Left)
            .BorderColor(Color.Green);

        table.AddColumn(new TableColumn("[green]INVOICE[/]").Centered());



        return table;
    }
}

public class Order
{
    public string FirstName { get; set; }
    public string Drink { get; set; }
    public string Note { get; set; }
    public decimal Total { get; set; }
}