POJOs for our simple invoice database

This is a code example of iText PDF, discover more.

30th August 2015
admin-marketing

Switch code for this example

Invoice.java
  1. /*
  2.  * Part of a set of classes based on a sample database.
  3.  */
  4. package zugferd.pojo;
  5.  
  6. import java.util.Date;
  7. import java.util.List;
  8.  
  9. /**
  10.  * Plain Old Java Object containing info about an Invoice.
  11.  * @author Bruno Lowagie (iText Software)
  12.  */
  13. public class Invoice {
  14.     protected int id;
  15.     protected Customer customer;
  16.     protected double total;
  17.     protected List items;
  18.     protected Date invoiceDate;
  19.  
  20.     public int getId() {
  21.         return id;
  22.     }
  23.  
  24.     public void setId(int id) {
  25.         this.id = id;
  26.     }
  27.  
  28.     public Customer getCustomer() {
  29.         return customer;
  30.     }
  31.  
  32.     public void setCustomer(Customer customer) {
  33.         this.customer = customer;
  34.     }
  35.  
  36.     public double getTotal() {
  37.         return total;
  38.     }
  39.  
  40.     public void setTotal(double total) {
  41.         this.total = total;
  42.     }
  43.  
  44.     public List getItems() {
  45.         return items;
  46.     }
  47.  
  48.     public void setItems(List items) {
  49.         this.items = items;
  50.     }
  51.  
  52.     public Date getInvoiceDate() {
  53.         return invoiceDate;
  54.     }
  55.  
  56.     public void setInvoiceDate(Date invoiceDate) {
  57.         this.invoiceDate = invoiceDate;
  58.     }
  59.    
  60.     @Override
  61.     public String toString() {
  62.         StringBuilder sb = new StringBuilder();
  63.         sb.append("Invoice id: ").append(id).append(" Date: ").append(invoiceDate).append(" Total cost: ").append(total).append("\u20ac\n");
  64.         sb.append("Customer: ").append(customer.toString()).append("\n");
  65.         for (Item item : items) {
  66.             sb.append(item.toString()).append("\n");
  67.         }
  68.         return sb.toString();
  69.     }
  70. }
Customer.java
  1. /*
  2.  * Part of a set of classes based on a sample database.
  3.  */
  4. package zugferd.pojo;
  5.  
  6. /**
  7.  * Plain Old Java Object containing info about a Customer.
  8.  * @author Bruno Lowagie (iText Software)
  9.  */
  10. public class Customer {
  11.     protected int id;
  12.     protected String firstName;
  13.     protected String lastName;
  14.     protected String street;
  15.     protected String postalcode;
  16.     protected String city;
  17.     protected String countryId;
  18.  
  19.     public int getId() {
  20.         return id;
  21.     }
  22.  
  23.     public void setId(int id) {
  24.         this.id = id;
  25.     }
  26.  
  27.     public String getFirstName() {
  28.         return firstName;
  29.     }
  30.  
  31.     public void setFirstName(String firstName) {
  32.         this.firstName = firstName;
  33.     }
  34.  
  35.     public String getLastName() {
  36.         return lastName;
  37.     }
  38.  
  39.     public void setLastName(String lastName) {
  40.         this.lastName = lastName;
  41.     }
  42.  
  43.     public String getStreet() {
  44.         return street;
  45.     }
  46.  
  47.     public void setStreet(String street) {
  48.         this.street = street;
  49.     }
  50.  
  51.     public String getCity() {
  52.         return city;
  53.     }
  54.  
  55.     public void setCity(String city) {
  56.         this.city = city;
  57.     }
  58.  
  59.     public String getPostalcode() {
  60.         return postalcode;
  61.     }
  62.  
  63.     public void setPostalcode(String postalcode) {
  64.         this.postalcode = postalcode;
  65.     }
  66.  
  67.     public String getCountryId() {
  68.         return countryId;
  69.     }
  70.  
  71.     public void setCountryId(String countryId) {
  72.         this.countryId = countryId;
  73.     }
  74.    
  75.     @Override
  76.     public String toString() {
  77.         StringBuilder sb = new StringBuilder();
  78.         sb.append(id).append("\n");
  79.         sb.append("    First Name: ").append(firstName).append("\n");
  80.         sb.append("    Last Name: ").append(lastName).append("\n");
  81.         sb.append("    Street: ").append(street).append("\n");
  82.         sb.append("    City: ").append(countryId).append(" ").append(postalcode).append(" ").append(city);
  83.         return sb.toString();
  84.     }
  85. }
Item.java
  1. /*
  2.  * Part of a set of classes based on a sample database.
  3.  */
  4. package zugferd.pojo;
  5.  
  6. /**
  7.  * Plain Old Java Object containing info about an Item.
  8.  * @author Bruno Lowagie (iText Software)
  9.  */
  10. public class Item {
  11.     protected int item;
  12.     protected Product product;
  13.     protected int quantity;
  14.     protected double cost;
  15.  
  16.     public int getItem() {
  17.         return item;
  18.     }
  19.  
  20.     public void setItem(int item) {
  21.         this.item = item;
  22.     }
  23.  
  24.     public Product getProduct() {
  25.         return product;
  26.     }
  27.  
  28.     public void setProduct(Product product) {
  29.         this.product = product;
  30.     }
  31.  
  32.     public int getQuantity() {
  33.         return quantity;
  34.     }
  35.  
  36.     public void setQuantity(int quantity) {
  37.         this.quantity = quantity;
  38.     }
  39.  
  40.     public double getCost() {
  41.         return cost;
  42.     }
  43.  
  44.     public void setCost(double cost) {
  45.         this.cost = cost;
  46.     }
  47.    
  48.     @Override
  49.     public String toString() {
  50.         StringBuilder sb = new StringBuilder();
  51.         sb.append("  #").append(item);
  52.         sb.append(product.toString());
  53.         sb.append("\tQuantity: ").append(quantity);
  54.         sb.append("\tCost: ").append(cost).append("\u20ac");
  55.         return sb.toString();
  56.     }
  57. }
Product.java
  1. /*
  2.  * Part of a set of classes based on a sample database.
  3.  */
  4. package zugferd.pojo;
  5.  
  6. /**
  7.  * Plain Old Java Object containing info about a Product.
  8.  * @author Bruno Lowagie (iText Software)
  9.  */
  10. public class Product {
  11.     protected int id;
  12.     protected String name;
  13.     protected double price;
  14.     protected double vat;
  15.  
  16.     public int getId() {
  17.         return id;
  18.     }
  19.  
  20.     public void setId(int id) {
  21.         this.id = id;
  22.     }
  23.  
  24.     public String getName() {
  25.         return name;
  26.     }
  27.  
  28.     public void setName(String name) {
  29.         this.name = name;
  30.     }
  31.  
  32.     public double getPrice() {
  33.         return price;
  34.     }
  35.  
  36.     public void setPrice(double price) {
  37.         this.price = price;
  38.     }
  39.  
  40.     public double getVat() {
  41.         return vat;
  42.     }
  43.  
  44.     public void setVat(double vat) {
  45.         this.vat = vat;
  46.     }
  47.    
  48.     @Override
  49.     public String toString() {
  50.         StringBuilder sb = new StringBuilder();
  51.         sb.append("\t(").append(id).append(")\t").append(name).append("\t").append(price).append("\u20ac\tvat ").append(vat).append("%");
  52.         return sb.toString();
  53.     }
  54. }
PojoFactory.java
  1. /*
  2.  * Part of a set of classes based on a sample database.
  3.  */
  4. package zugferd.pojo;
  5.  
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.sql.Statement;
  12. import java.util.ArrayList;
  13. import java.util.HashMap;
  14. import java.util.List;
  15.  
  16. /**
  17.  * Factory that creates Invoice, Customer, Product, and Item classes.
  18.  * @author Bruno Lowagie (iText Software)
  19.  */
  20. public class PojoFactory {
  21.    
  22.     protected static PojoFactory factory = null;
  23.     protected Connection connection;
  24.     protected HashMap customerCache = new HashMap();
  25.     protected HashMap productCache = new HashMap();
  26.     protected PreparedStatement getCustomer;
  27.     protected PreparedStatement getProduct;
  28.     protected PreparedStatement getItems;
  29.    
  30.     private PojoFactory() throws ClassNotFoundException, SQLException {
  31.         Class.forName("org.hsqldb.jdbcDriver");
  32.         connection = DriverManager.getConnection(
  33.             "jdbc:hsqldb:resources/zugferd/db/invoices", "SA", "");
  34.         getCustomer = connection.prepareStatement("SELECT * FROM Customer WHERE id = ?");
  35.         getProduct = connection.prepareStatement("SELECT * FROM Product WHERE id = ?");
  36.         getItems = connection.prepareStatement("SELECT * FROM Item WHERE invoiceid = ?");
  37.     }
  38.    
  39.     public static PojoFactory getInstance() throws SQLException {
  40.         if (factory == null || factory.connection.isClosed()) {
  41.             try {
  42.                 factory = new PojoFactory();
  43.             } catch (ClassNotFoundException cnfe) {
  44.                 throw new SQLException(cnfe.getMessage());
  45.             }
  46.         }
  47.         return factory;
  48.     }
  49.    
  50.     public void close() throws SQLException {
  51.         connection.close();
  52.     }
  53.    
  54.     public List getInvoices() throws SQLException {
  55.         List invoices = new ArrayList();
  56.         Statement stm = connection.createStatement();
  57.         ResultSet rs = stm.executeQuery("SELECT * FROM Invoice");
  58.         while (rs.next()) {
  59.             invoices.add(getInvoice(rs));
  60.         }
  61.         stm.close();
  62.         return invoices;
  63.     }
  64.    
  65.     public Invoice getInvoice(ResultSet rs) throws SQLException {
  66.         Invoice invoice = new Invoice();
  67.         invoice.setId(rs.getInt("id"));
  68.         invoice.setCustomer(getCustomer(rs.getInt("customerid")));
  69.         List items = getItems(rs.getInt("id"));
  70.         invoice.setItems(items);
  71.         double total = 0;
  72.         for (Item item : items)
  73.             total += item.getCost();
  74.         invoice.setTotal(total);
  75.         invoice.setInvoiceDate(rs.getDate("invoicedate"));
  76.         return invoice;
  77.     }
  78.    
  79.     public Item getItem(ResultSet rs) throws SQLException {
  80.         Item item = new Item();
  81.         item.setItem(rs.getInt("Item"));
  82.         Product product = getProduct(rs.getInt("ProductId"));
  83.         item.setProduct(product);
  84.         item.setQuantity(rs.getInt("Quantity"));
  85.         item.setCost(item.getQuantity() * product.getPrice());
  86.         return item;
  87.     }
  88.    
  89.     public Customer getCustomer(int id) throws SQLException {
  90.         if (customerCache.containsKey(id))
  91.             return customerCache.get(id);
  92.         getCustomer.setInt(1, id);
  93.         ResultSet rs = getCustomer.executeQuery();
  94.         if (rs.next()) {
  95.             Customer customer = new Customer();
  96.             customer.setId(id);
  97.             customer.setFirstName(rs.getString("FirstName"));
  98.             customer.setLastName(rs.getString("LastName"));
  99.             customer.setStreet(rs.getString("Street"));
  100.             customer.setPostalcode(rs.getString("Postalcode"));
  101.             customer.setCity(rs.getString("City"));
  102.             customer.setCountryId(rs.getString("CountryID"));
  103.             customerCache.put(id, customer);
  104.             return customer;
  105.         }
  106.         return null;
  107.     }
  108.    
  109.     public Product getProduct(int id) throws SQLException {
  110.         if (productCache.containsKey(id))
  111.             return productCache.get(id);
  112.         getProduct.setInt(1, id);
  113.         ResultSet rs = getProduct.executeQuery();
  114.         if (rs.next()) {
  115.             Product product = new Product();
  116.             product.setId(id);
  117.             product.setName(rs.getString("Name"));
  118.             product.setPrice(rs.getDouble("Price"));
  119.             product.setVat(rs.getDouble("Vat"));
  120.             productCache.put(id, product);
  121.             return product;
  122.         }
  123.         return null;
  124.     }
  125.    
  126.     public List getItems(int invoiceid) throws SQLException {
  127.         List items = new ArrayList();
  128.         getItems.setInt(1, invoiceid);
  129.         ResultSet rs = getItems.executeQuery();
  130.         while (rs.next()) {
  131.             items.add(getItem(rs));
  132.         }
  133.         return items;
  134.     }
  135. }

Resources

invoices.script

images

https://itextpdf.com/sites/default/files/C3F1_0.png
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now