Guest User

public class Invoice

a guest
Apr 19th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.94 KB | None | 0 0
  1. package zda;
  2.  
  3. import java.time.LocalDate;
  4. import java.time.format.DateTimeFormatter;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7.  
  8.  
  9. public class Invoice
  10. {
  11.  
  12.     private Contractor client;
  13.     private LocalDate dateOfIssue;
  14.  
  15.     private String nr;
  16.  
  17.     private static class Identifier
  18.     {
  19.         static int currentNumber = 0;
  20.         static int currentMonth = 0;
  21.         static int currentYear = 0;
  22.  
  23.         static String generateNr()
  24.         {
  25.             LocalDate ld = LocalDate.now();
  26.  
  27.             int year = ld.getYear();
  28.             int month = ld.getMonthValue();
  29.             if (year > currentYear)
  30.             {
  31.                 currentYear = year;
  32.                 currentMonth = ld.getMonthValue();
  33.                 currentNumber = 0;
  34.             } else if (month > currentMonth)
  35.             {
  36.                 currentMonth = month;
  37.                 currentNumber = 0;
  38.             }
  39.  
  40.             return (++currentNumber) + "/" + currentMonth + "/" + currentYear;
  41.  
  42.         }
  43.     }
  44.  
  45.     public enum Measure
  46.     {
  47.         QTY, M, L, KG, M2
  48.     }
  49.  
  50.     public enum VAT
  51.     {
  52.         s23(.23), s08(.08), s05(.05), s00(.0);
  53.  
  54.         public double rate;
  55.         public String str;
  56.  
  57.         VAT(double rate)
  58.         {
  59.             this.rate = rate;
  60.             this.str = String.format("%.0f%%", 100 * rate);
  61.         }
  62.     }
  63.  
  64.     private class Position
  65.     {
  66.         private int position;
  67.         private String name;
  68.         private Measure mmasure;
  69.         private double quantity;
  70.         private double unitPriceWithoutTax;
  71.         private VAT tax;
  72.  
  73.         public Position(String name, Measure mmasure, double quantity, double price, VAT tax)
  74.         {
  75.             this.name = name;
  76.             this.mmasure = mmasure;
  77.             this.quantity = quantity;
  78.             this.unitPriceWithoutTax = price;
  79.             this.tax = tax;
  80.             this.position = positions.size() + 1;
  81.         }
  82.  
  83.         public double getValue()
  84.         {
  85.             return quantity * unitPriceWithoutTax;
  86.         }
  87.  
  88.         public double getTax()
  89.         {
  90.             double d = quantity * unitPriceWithoutTax * tax.rate;
  91.             d = Math.round(d * 100) / 100.;
  92.             return d;
  93.         }
  94.  
  95.         @Override
  96.         public String toString()
  97.         {
  98.             return String.format("%5d | %30s | %10s | %10s | %10s | %10s | %10.2f", position, name, mmasure, quantity,
  99.                     unitPriceWithoutTax, tax.str, getValue() + getTax());
  100.         }
  101.     }
  102.  
  103.     public static String heading()
  104.     {
  105.         return String.format("%5s | %30s | %10s | %10s | %10s | %10s | %10s", "LP.", "name", "mmasure", "quantity",
  106.                 "Unit price.", "tax", "price with tax");
  107.     }
  108.  
  109.     private ArrayList<Position> positions = new ArrayList<Position>();
  110.  
  111.     public void addPosition(String name, Measure m, double quantity, double price, VAT tax)
  112.     {
  113.         if (closed)
  114.             return;
  115.  
  116.         Position p = new Position(name, m, quantity, price, tax);
  117.         positions.add(p);
  118.         double d = subtotals.get(tax);
  119.         d += p.getTax();
  120.         subtotals.put(tax, d);
  121.         sumWithTax += p.getValue();
  122.         sumPayment += p.getValue() + p.getTax();
  123.     }
  124.  
  125.     private boolean closed = false;
  126.  
  127.     public void close()
  128.     {
  129.         closed = true;
  130.     }
  131.  
  132.     private HashMap<VAT, Double> subtotals = new HashMap<>();
  133.     private double sumWithTax = 0;
  134.     private double sumPayment = 0;
  135.  
  136.     public Invoice(Contractor c)
  137.     {
  138.         nr = Identifier.generateNr();
  139.         client = c;
  140.         dateOfIssue = LocalDate.now();
  141.  
  142.         for (VAT v : VAT.values())
  143.         {
  144.             subtotals.put(v, 0.);
  145.         }
  146.     }
  147.  
  148.     @Override
  149.     public String toString()
  150.     {
  151.         String invoice = "---------------------------------------------\n";
  152.         invoice += (closed ? "C-" : "O-") + "invoice no." + nr + "\n";
  153.         invoice += "from date " + dateOfIssue.format(DateTimeFormatter.ofPattern("d-M-y")) + "\n";
  154.         invoice += "\nFor: \n" + client + "\n\n";
  155.         invoice += heading() + "\n";
  156.         for (Position p : positions)
  157.         {
  158.             invoice += p.toString() + "\n";
  159.         }
  160.         invoice += "\n";
  161.         invoice += String.format("%-15s: %10.2f\n", "Sum without tax", sumWithTax);
  162.         for (HashMap.Entry<VAT, Double> e : subtotals.entrySet())
  163.         {
  164.             if (e.getValue() > 0)
  165.                 invoice += String.format("%-15s: %10.2f\n", "SUM " + e.getKey().str, e.getValue());
  166.         }
  167.         invoice += String.format("%27s\n", "+ ----------");
  168.         invoice += String.format("%-15s: %10.2f\n", "sum to pay", sumPayment);
  169.         invoice += "---------------------------------------------\n";
  170.         return invoice;
  171.     }
  172.  
  173. }
Add Comment
Please, Sign In to add comment