Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package has; import java.io.FileOutputStream; import java.util.Date; import com.itextpdf.text.Anchor; import com.itextpdf.text.BadElementException; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Chapter; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.List; import com.itextpdf.text.ListItem; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Phrase; import com.itextpdf.text.Section; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import static has.Url.password; import static has.Url.url; import static has.Url.user; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.Random; import java.lang.*; import static java.lang.Long.max; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Vector; import java.util.logging.Level; import java.util.logging.Logger; import static javax.swing.JOptionPane.showMessageDialog; public class GenerateReceipt { static Random rand = new Random(); static int min = 0, max = 1000; static int randomNum = rand.nextInt((max - min) + 1) + min; static String name = "Bill" + randomNum; //private static String FILE = "c:/temp/Bill3.pdf"; private static final String FILE = "F:/" + name + ".pdf"; private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD); private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL, BaseColor.RED); private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD); private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD); static final String DB_URL1 = "jdbc:mysql://10.117.13.215:3306/dbhas"; static final String USER1 = "kas"; static final String PASS1 = "89878"; public GenerateReceipt() { try { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(FILE)); document.open(); addMetaData(document); addTitlePage(document); addContent(document); document.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { try { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(FILE)); document.open(); addMetaData(document); addTitlePage(document); addContent(document); document.close(); } catch (Exception e) { e.printStackTrace(); } } private static void addMetaData(Document document) { document.addTitle("RECEPIT "); document.addSubject("Using iText"); document.addKeywords("Java, PDF, iText"); document.addAuthor("Kesava"); document.addCreator("Kesava"); } static Paragraph preface; private static void addTitlePage(Document document) throws DocumentException { preface = new Paragraph(); // We add one empty line addEmptyLine(preface, 1); // Lets write a big header preface.add(new Paragraph(" RECEIPT ", catFont)); addEmptyLine(preface, 1); // Will create: Report generated by: _name, _date preface.add(new Paragraph("Report generated by: BAS , " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ smallBold)); addEmptyLine(preface, 1); preface.add(new Paragraph("Please keep the bill safe for future advantages ", smallBold)); //addEmptyLine(preface, 8); // preface.add(new Paragraph("This document is a preliminary version and not subject to your license agreement or any other agreement with vogella.com ;-).", // redFont)); // document.add(preface); // Start a new page // document.newPage(); } private static void addContent(Document document) throws DocumentException, IOException, SQLException, ClassNotFoundException { Anchor anchor = new Anchor("BILL DETAILS(Generated by HAS) on " + new Date(), catFont); // anchor.setName("First Chapter"); // Second parameter is the number of the chapter Chapter catPart = new Chapter(new Paragraph(anchor), 1); addEmptyLine(preface, 2); Statement stmt = null, stmt1 = null, stmt2 = null, stmt3 = null, stmt4 = null, stmt5 = null; Connection conn = null; String w = null; int found = 0; // System.out.println("At the start"); try { conn = DriverManager.getConnection(url, user, password); stmt = conn.createStatement(); stmt1 = conn.createStatement(); stmt2 = conn.createStatement(); stmt3 = conn.createStatement(); stmt4 = conn.createStatement(); stmt5 = conn.createStatement(); // stmt1 = conn.createStatement(); } catch (Exception rx) { System.out.println("in catch"); } int present = 0; double minfre = 0; String sql2 = "SELECT id,Value FROM central"; Vector tariff = null, discount = null, rates = null, totalbookings = null; ResultSet rsz = stmt.executeQuery(sql2); int iad; while (rsz.next()) { iad = rsz.getInt("id"); if (iad == 9) { minfre = rsz.getDouble("Value"); } if (iad == 10) { ObjectInputStream o = null; byte[] arr = rsz.getBytes("Value"); tariff = new Vector(); if (arr != null) { o = new ObjectInputStream(new ByteArrayInputStream(arr)); tariff = (Vector) o.readObject(); } } if (iad == 11) { ObjectInputStream o = null; byte[] arr = rsz.getBytes("Value"); rates = new Vector(); if (arr != null) { o = new ObjectInputStream(new ByteArrayInputStream(arr)); rates = (Vector) o.readObject(); } } if (iad == 12) { ObjectInputStream o = null; byte[] arr = rsz.getBytes("Value"); totalbookings = new Vector(); if (arr != null) { o = new ObjectInputStream(new ByteArrayInputStream(arr)); totalbookings = (Vector) o.readObject(); } } if (iad == 13) { ObjectInputStream o = null; byte[] arr = rsz.getBytes("Value"); discount = new Vector(); if (arr != null) { o = new ObjectInputStream(new ByteArrayInputStream(arr)); discount = (Vector) o.readObject(); } } if (iad == 14) { present = rsz.getInt("Value"); } } String sql = "SELECT * FROM Customer "; ResultSet rs = null, rs1 = null; try { rs = stmt.executeQuery(sql); } catch (SQLException ex) { Logger.getLogger(CheckOut.class.getName()).log(Level.SEVERE, null, ex); } while (rs.next()) { int x = rs.getInt("ISAC"); if (x == 2) { int y = rs.getInt("SCOD"); if (present == y) { double Totalbill = 0, amount = 0; ObjectInputStream o = null; byte[] arr = rs.getBytes("Catering"); Vector Catering = new Vector(); if (arr != null) { o = new ObjectInputStream(new ByteArrayInputStream(arr)); Catering = (Vector) o.readObject(); } Paragraph subPara2 = new Paragraph(rs.getString("Name") + " " + rs.getString("I"), subFont); Section subCatPart = catPart.addSection(subPara2); // Section subCatPart2 = catPart.addSection(subPara2); if (Catering.size() != 0) { createTable(subCatPart, Catering); System.out.println("Enterte in catering"); CateringObject obj = (CateringObject) Catering.elementAt(0); Totalbill = obj.bill; // tb.addRow(new Object[]{new String(rs.getString("Name")),new String(rs.getString("I")),new String(obj.date),new String(obj.item),new Double(obj.bill) }); for (int f = 1; f < Catering.size(); f++) { obj = (CateringObject) Catering.elementAt(f); // tb.addRow(new Object[]{new String(""),new String(""),new String(obj.date),new String(obj.item),new Double(obj.bill) }); Totalbill = Totalbill + obj.bill; } String updatestr = "UPDATE customer SET Catering = ? WHERE id = " + rs.getInt("id"); PreparedStatement p = conn.prepareStatement(updatestr); p.setObject(1, null); p.executeUpdate(); } int ap1 = rs.getInt("SCOD") - rs.getInt("SCID") + 1; int ap = rs.getInt("History") + ap1; System.out.println("ap1=" + ap1); System.out.println("ap=" + ap); sql = "UPDATE customer SET history= " + ap + " WHERE id= " + rs.getInt("id"); System.out.println(sql); stmt1.executeUpdate(sql); sql = "UPDATE customer SET ISAC= 0 WHERE id= " + rs.getInt("id"); System.out.println(sql); stmt5.executeUpdate(sql); int to = 0; String room = rs.getString("Room"); String roompart = room.substring(0, 2); double roomrent = 0, tariffa = 0, discounta = 0, advance = 0; advance = rs.getDouble("RTP"); System.out.println(roompart); if (roompart.equalsIgnoreCase("SA")) { amount = ap1 * (int) rates.elementAt(0); roomrent = amount; System.out.println("roomrent=" + roomrent); amount = amount + Totalbill; tariffa = (amount * (double) tariff.elementAt(0)) / 100; amount = amount + tariffa; to = (int) totalbookings.elementAt(0); to = to + ap1; totalbookings.setElementAt(to, 0); } if (roompart.equalsIgnoreCase("SNA")) { amount = ap1 * (int) rates.elementAt(1); roomrent = amount; amount = amount + Totalbill; tariffa = (amount * (double) tariff.elementAt(1)) / 100; amount = amount + (amount * (double) tariff.elementAt(1)) / 100; to = (int) totalbookings.elementAt(1); to = to + ap1; totalbookings.setElementAt(to, 1); } if (roompart.equalsIgnoreCase("DA")) { amount = ap1 * (int) rates.elementAt(2); roomrent = amount; amount = amount + Totalbill; tariffa = (amount * (double) tariff.elementAt(2)) / 100; amount = amount + (amount * (double) tariff.elementAt(2)) / 100; to = (int) totalbookings.elementAt(2); to = to + ap1; totalbookings.setElementAt(to, 2); } if (roompart.equalsIgnoreCase("DNA")) { amount = ap1 * (int) rates.elementAt(3); roomrent = amount; amount = amount + Totalbill; tariffa = (amount * (double) tariff.elementAt(3)) / 100; amount = amount + (amount * (double) tariff.elementAt(3)) / 100; to = (int) totalbookings.elementAt(3); to = to + ap1; totalbookings.setElementAt(to, 3); } String updatestr = "UPDATE central SET Value = ? WHERE id =12 "; PreparedStatement p = conn.prepareStatement(updatestr); p.setObject(1, totalbookings); p.executeUpdate(); int history = rs.getInt("history"); double cusfre = history / present; double minfree = (double) discount.elementAt(0); double minbill = (double) discount.elementAt(1); double d1 = (double) discount.elementAt(2); double d2 = (double) discount.elementAt(3); double d3 = (double) discount.elementAt(4); double d4 = (double) discount.elementAt(5); if (cusfre < minfree && amount < minbill) { discounta = (amount * d1) / 100; amount = amount - discounta; } if (cusfre < minfree && amount > minbill) { discounta = (amount * d2) / 100; amount = amount - discounta; } if (cusfre > minfree && amount < minbill) { discounta = (amount * d3) / 100; amount = amount - discounta; } if (cusfre > minfree && amount > minbill) { discounta = (amount * d4) / 100; amount = amount - discounta; } amount = amount - advance; // tb2.addRow(new Object[]{new String(rs.getString("Name")),new String(rs.getString("I")),new Double(Totalbill),new Double(advance),new Double(roomrent),new Double(tariffa),new Double(discounta),new Double(amount)}); } } if (x == 1) { Vector advancev = null, ecid = null, ecod = null, advanceroom = null; ObjectInputStream o = null; byte[] arr = rs.getBytes("Advance"); advancev = new Vector(); if (arr != null) { o = new ObjectInputStream(new ByteArrayInputStream(arr)); advancev = (Vector) o.readObject(); } byte[] arr2 = rs.getBytes("Advance_room"); advanceroom = new Vector(); if (arr2 != null) { o = new ObjectInputStream(new ByteArrayInputStream(arr2)); advanceroom = (Vector) o.readObject(); } byte[] arr3 = rs.getBytes("ECID"); ecid = new Vector(); if (arr3 != null) { o = new ObjectInputStream(new ByteArrayInputStream(arr3)); ecid = (Vector) o.readObject(); } byte[] arr4 = rs.getBytes("ECOD"); ecod = new Vector(); if (arr4 != null) { o = new ObjectInputStream(new ByteArrayInputStream(arr4)); ecod = (Vector) o.readObject(); } for (int l = 0; l < ecod.size(); l++) { long y = (long) ecod.elementAt(l); if (present == y) { double Totalbill = 0, amount = 0; // ObjectInputStream o = null; byte[] arr1 = rs.getBytes("Catering"); Vector Catering = new Vector(); if (arr1 != null) { o = new ObjectInputStream(new ByteArrayInputStream(arr1)); Catering = (Vector) o.readObject(); } Paragraph subPara2 = new Paragraph(rs.getString("Name") + " " + rs.getString("I"), subFont); Section subCatPart = catPart.addSection(subPara2); if (Catering.size() != 0) { createTable(subCatPart, Catering); CateringObject obj = (CateringObject) Catering.elementAt(0); Totalbill = obj.bill; //tb.addRow(new Object[]{new String(rs.getString("Name")),new String(rs.getString("I")),new String(obj.date),new String(obj.item),new Double(obj.bill) }); for (int f = 1; f < Catering.size(); f++) { obj = (CateringObject) Catering.elementAt(f); // tb.addRow(new Object[]{new String(""),new String(""),new String(obj.date),new String(obj.item),new Double(obj.bill) }); Totalbill = Totalbill + obj.bill; } String updatestr = "UPDATE customer SET Catering = ? WHERE id = " + rs.getInt("id"); PreparedStatement p = conn.prepareStatement(updatestr); p.setObject(1, null); p.executeUpdate(); } System.out.println("l == " + l); System.out.println("size = " + ecod.size()); System.out.println(ecod); System.out.println(ecid); long ppp = (long) ecod.get(l); long qqq = (long) ecid.get(l); System.out.println("ecod = " + ppp); System.out.println("ecid = " + qqq); long ap1 = ppp - qqq + 1; // sql="UPDATE customer SET ISAC=0 WHERE id= "+rs.getInt("id"); // stmt3.executeUpdate(sql); int ap = rs.getInt("History") + (int) ap1; sql = "UPDATE customer SET history=" + ap + " WHERE id= " + rs.getInt("id"); stmt4.executeUpdate(sql); int to = 0; String room = (String) advanceroom.elementAt(l); String roompart = room.substring(0, 1); double roomrent = 0, tariffa = 0, discounta = 0, advance = 0; advance = Double.parseDouble((String) advancev.elementAt(l)); if (roompart.equalsIgnoreCase("SA")) { amount = ap1 * (int) rates.elementAt(0); roomrent = amount; amount = amount + Totalbill; tariffa = (amount * (double) tariff.elementAt(0)) / 100; amount = amount + tariffa; to = (int) totalbookings.elementAt(0); to = to + (int) ap1; totalbookings.setElementAt(to, 0); } if (roompart.equalsIgnoreCase("SNA")) amount = (double) rates.elementAt(1); { amount = ap1 * (int) rates.elementAt(1); roomrent = amount; amount = amount + Totalbill; tariffa = (amount * (double) tariff.elementAt(1)) / 100; amount = amount + (amount * (double) tariff.elementAt(1)) / 100; to = (int) totalbookings.elementAt(1); to = to + (int) ap1; totalbookings.setElementAt(to, 1); } if (roompart.equalsIgnoreCase("DA")) { amount = ap1 * (int) rates.elementAt(2); roomrent = amount; amount = amount + Totalbill; tariffa = (amount * (double) tariff.elementAt(2)) / 100; amount = amount + (amount * (double) tariff.elementAt(2)) / 100; to = (int) totalbookings.elementAt(2); to = to + (int) ap1; totalbookings.setElementAt(to, 2); } if (roompart.equalsIgnoreCase("DNA")) { amount = ap1 * (int) rates.elementAt(3); roomrent = amount; amount = amount + Totalbill; tariffa = (amount * (double) tariff.elementAt(3)) / 100; amount = amount + (amount * (double) tariff.elementAt(3)) / 100; to = (int) totalbookings.elementAt(3); to = to + (int) ap1; totalbookings.setElementAt(to, 3); } String updatestr = "UPDATE central SET Value = ? WHERE id =12 "; PreparedStatement p = conn.prepareStatement(updatestr); p.setObject(1, totalbookings); p.executeUpdate(); int history = rs.getInt("history"); double cusfre = history / present; double minfree = (double) discount.elementAt(0); double minbill = (double) discount.elementAt(1); double d1 = (double) discount.elementAt(2); double d2 = (double) discount.elementAt(3); double d3 = (double) discount.elementAt(4); double d4 = (double) discount.elementAt(5); if (cusfre < minfree && amount < minbill) { discounta = (amount * d1) / 100; amount = amount - discounta; } if (cusfre < minfree && amount > minbill) { discounta = (amount * d2) / 100; amount = amount - discounta; } if (cusfre > minfree && amount < minbill) { discounta = (amount * d3) / 100; amount = amount - discounta; } if (cusfre > minfree && amount > minbill) { discounta = (amount * d4) / 100; amount = amount - discounta; } amount = amount - advance; // tb2.addRow(new Object[]{new String(rs.getString("Name")),new String(rs.getString("I")),new Double(Totalbill),new Double(advance),new Double(roomrent),new Double(tariffa),new Double(discounta),new Double(amount)}); } } } // add a table //createTable(subCatPart); } Paragraph subPara2 = new Paragraph("Thanks for choosing us .. please visit again", subFont); Section subCatPart2 = catPart.addSection(subPara2); /* Anchor anchor2 = new Anchor("Thanks for choosing us .. please visit again", catFont); Chapter catPart2 = new Chapter(new Paragraph(anchor), 1);*/ // now add all this to the document document.add(catPart); //document.add(catPart2); } private static void createTable(Section subCatPart, Vector Catering) throws BadElementException { PdfPTable table = new PdfPTable(3); // t.setBorderColor(BaseColor.GRAY); // t.setPadding(4); // t.setSpacing(4); // t.setBorderWidth(1); // addEmptyLine(preface, 2); PdfPCell c1 = new PdfPCell(new Phrase("slno")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Date")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Item")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("price")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); table.setHeaderRows(1); CateringObject obj = (CateringObject) Catering.elementAt(0); float Totalbill = (float) obj.bill; // tb.addRow(new Object[]{new String(rs.getString("Name")),new String(rs.getString("I")),new String(obj.date),new String(obj.item),new Double(obj.bill) }); for (int f = 1; f < Catering.size(); f++) { obj = (CateringObject) Catering.elementAt(f); table.addCell("" + obj.date); table.addCell("" + obj.item); table.addCell("" + obj.bill); // tb.addRow (new Object[]{new String(""),new String(""),new String(obj.date),new String(obj.item),new Double(obj.bill) }); Totalbill = (float) (Totalbill + obj.bill); } /*table.addCell("1.0"); table.addCell("1.1"); table.addCell("1.2"); table.addCell("2.1"); table.addCell("2.2"); table.addCell("2.3");*/ subCatPart.add(table); } private static void addEmptyLine(Paragraph paragraph, int number) { for (int i = 0; i < number; i++) { paragraph.add(new Paragraph(" ")); } } /* private static void addEmptyLine(Paragraph paragraph, int number) { for (int i = 0; i < number; i++) { paragraph.add(new Paragraph(" ")); } }*/ }