×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Israel Edet
Added: Jan 4, 2017 10:05 PM
Views: 6
Tags: no tags
Customer Data
  1. package inventorysystem.data;
  2.  
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.util.Date;
  7. import java.util.logging.Logger;
  8.  
  9. /**
  10.  * This Class Handle all the functions that can be applied to a customer registered in this shop
  11.  * @author Israel Edet
  12.  * @version 1.0
  13.  */
  14. public class Customer {
  15.  
  16.     private String customerFirstName;
  17.     private String customerLastName;
  18.     private String customerGender;
  19.     private Date customerBirthDate;
  20.  
  21.     private int customerId;
  22.     private String customerTelephone;
  23.     private String customerAddress;
  24.     private String paymentMode;
  25.     private DBConnectionClass bConnectionClass = null;
  26.     private static final Logger LOG = Logger.getLogger(Customer.class.getName());
  27.  
  28.     public Customer() {
  29.         if (this.bConnectionClass.getConnection() == null) {
  30.             this.bConnectionClass = new DBConnectionClass();
  31.         }
  32.  
  33.     }
  34. /**
  35.  * Default Constructor
  36.  * @param customerFirstName
  37.  * @param customerLastName
  38.  * @param customerGender
  39.  * @param customerBirthDate
  40.  * @param customerId
  41.  * @param customerTelephone
  42.  * @param customerAddress
  43.  * @param paymentMode
  44.  */
  45.     public Customer(String customerFirstName, String customerLastName, String customerGender, Date customerBirthDate, int customerId, String customerTelephone, String customerAddress, String paymentMode) {
  46.         this.customerFirstName = customerFirstName;
  47.         this.customerLastName = customerLastName;
  48.         this.customerGender = customerGender;
  49.         this.customerBirthDate = customerBirthDate;
  50.         this.customerId = customerId;
  51.         this.customerTelephone = customerTelephone;
  52.         this.customerAddress = customerAddress;
  53.         this.paymentMode = paymentMode;
  54.         bConnectionClass = new DBConnectionClass();
  55.     }
  56.  
  57.     /**
  58.      * @return the customerFirstName
  59.      */
  60.     public String getCustomerFirstName() {
  61.         return customerFirstName;
  62.     }
  63.  
  64.     /**
  65.      * @param customerFirstName the customerFirstName to set
  66.      */
  67.     public void setCustomerFirstName(String customerFirstName) {
  68.         this.customerFirstName = customerFirstName;
  69.     }
  70.  
  71.     /**
  72.      * @return the customerId
  73.      */
  74.     public int getCustomerId() {
  75.         return customerId;
  76.     }
  77.  
  78.     /**
  79.      * @param customerId the customerId to set
  80.      */
  81.     public void setCustomerId(int customerId) {
  82.         this.customerId = customerId;
  83.     }
  84.  
  85.     /**
  86.      * @return the customerTelephone
  87.      */
  88.     public String getCustomerTelephone() {
  89.         return customerTelephone;
  90.     }
  91.  
  92.     /**
  93.      * @param customerTelephone the customerTelephone to set
  94.      */
  95.     public void setCustomerTelephone(String customerTelephone) {
  96.         this.customerTelephone = customerTelephone;
  97.     }
  98.  
  99.     /**
  100.      * @return the customerAddress
  101.      */
  102.     public String getCustomerAddress() {
  103.         return customerAddress;
  104.     }
  105.  
  106.     /**
  107.      * @param customerAddress the customerAddress to set
  108.      */
  109.     public void setCustomerAddress(String customerAddress) {
  110.         this.customerAddress = customerAddress;
  111.     }
  112.  
  113.     /**
  114.      * @return the paymentMode
  115.      */
  116.     public String getPaymentMode() {
  117.         return paymentMode;
  118.     }
  119.  
  120.     /**
  121.      * @param paymentMode the paymentMode to set
  122.      */
  123.     public void setPaymentMode(String paymentMode) {
  124.         this.paymentMode = paymentMode;
  125.     }
  126.  
  127.     public Boolean addCustomer() throws SQLException {
  128.         boolean isSaved = false;
  129.         if (null != this.bConnectionClass.getConnection()) {
  130.             Statement s = this.bConnectionClass.getConnection().createStatement();
  131.             if(s.execute("INSERT INTO customers "
  132.                     + "(first_name,last_name,gender,birth_date,address,phone) "
  133.                     + "VALUES('"+getCustomerFirstName()+"','"+getCustomerLastName()+"','"+getCustomerGender()+"'"
  134.                     + "'"+getCustomerBirthDate().toString()+"','"+getCustomerAddress()+"','"+getCustomerTelephone()+"')"))
  135.                     isSaved = true;
  136.         }
  137.         return isSaved;
  138.     }
  139.     /**
  140.      * Delete Customer From database
  141.      * @param customerId
  142.      * @return boolean true on delete success
  143.      * @throws SQLException
  144.      */
  145.     public Boolean deleteCustomer(int customerId) throws SQLException {
  146.         boolean isDeleted = false;
  147.         if (null != this.bConnectionClass.getConnection()) {
  148.             Statement s = this.bConnectionClass.getConnection().createStatement();
  149.             if(s.execute("DELETE FROM customers WHERE customer_id='"+customerId+"'"))
  150.                     isDeleted = true;
  151.         }
  152.         return isDeleted;
  153.     }
  154.     /**
  155.      * Update customer information
  156.      * @param customerId
  157.      * @return boolean true on success
  158.      * @throws SQLException
  159.      */
  160.     public Boolean updateCustomer(int customerId) throws SQLException {
  161.         boolean isDeleted = false;
  162.         if (null != this.bConnectionClass.getConnection()) {
  163.             Statement s = this.bConnectionClass.getConnection().createStatement();
  164.             if(s.execute("UPDATE  customers SET  "
  165.                     + "first_name = '"+getCustomerFirstName()+"',last_name = '"+getCustomerLastName()+"',gender = '"+getCustomerGender()+"',birth_date = '"+getCustomerBirthDate().toString()+"',address = '"+getCustomerAddress()+"',phone = '"+getCustomerTelephone()+"' "
  166.                     + "WHERE customer_id = '"+customerId+"'"))
  167.                     isDeleted = true;
  168.         }
  169.         return isDeleted;
  170.     }
  171.     /**
  172.      * Get Customer Object
  173.      * @param customerId the id of customer to retrieve
  174.      * @param allCustomers boolean she retrieve all customers
  175.      * @return A Customer Object
  176.      * @throws SQLException
  177.      */
  178. public Customer getCustomer(int customerId,boolean allCustomers) throws SQLException{
  179.     Customer customer = null;
  180.     String sql = "";
  181.     if(allCustomers){// if shud select all customers
  182.         sql = "SELECT * FROM customers";
  183.     }else {
  184.         sql = "SELECT * FROM customers where customer_id = '"+customerId+"'";
  185.     }
  186.     if(this.bConnectionClass.getConnection() != null && !sql.isEmpty()){
  187.     //if there is a connection to db and sql query is not empty
  188.         Statement s = this.bConnectionClass.getConnection().createStatement();
  189.         ResultSet resultSet = s.executeQuery(sql);
  190.        
  191.         while (resultSet.next()) {
  192.             customer = new Customer(
  193.                     resultSet.getString("first_name"),
  194.                     resultSet.getString("last_name"),
  195.                     resultSet.getString("gender"),
  196.                     new Date(resultSet.getString("birth_date")),
  197.                     resultSet.getInt("customer_id"),
  198.                     resultSet.getString("phone"),
  199.                     resultSet.getString("address"),
  200.                     "No Payment Method Selected."
  201.             );
  202.            
  203.         }
  204.     }
  205.     return customer;
  206. }
  207.  
  208.     /**
  209.      * @return the customerLastName
  210.      */
  211.     public String getCustomerLastName() {
  212.         return customerLastName;
  213.     }
  214.  
  215.     /**
  216.      * @param customerLastName the customerLastName to set
  217.      */
  218.     public void setCustomerLastName(String customerLastName) {
  219.         this.customerLastName = customerLastName;
  220.     }
  221.  
  222.     /**
  223.      * @return the customerGender
  224.      */
  225.     public String getCustomerGender() {
  226.         return customerGender;
  227.     }
  228.  
  229.     /**
  230.      * @param customerGender the customerGender to set
  231.      */
  232.     public void setCustomerGender(String customerGender) {
  233.         this.customerGender = customerGender;
  234.     }
  235.  
  236.     /**
  237.      * @return the customerBirthDate
  238.      */
  239.     public Date getCustomerBirthDate() {
  240.         return customerBirthDate;
  241.     }
  242.  
  243.     /**
  244.      * @param customerBirthDate the customerBirthDate to set
  245.      */
  246.     public void setCustomerBirthDate(Date customerBirthDate) {
  247.         this.customerBirthDate = customerBirthDate;
  248.     }
  249. }
  250.