Created
February 1, 2016 18:27
-
-
Save Prologic200/adb6fafbc6b5f3d18cff to your computer and use it in GitHub Desktop.
Coupon.Core.Project.DB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package facade; | |
import java.util.Collection; | |
import dao.CompanyDBDAO; | |
import dao.CouponDBDAO; | |
import dao.CustomerDBDAO; | |
import exceptions.CompanySystemException; | |
import exceptions.ConnectionSystemException; | |
import exceptions.CouponSystemException; | |
import exceptions.CustomerSystemException; | |
import javabeans.Company; | |
import javabeans.Coupon; | |
import javabeans.Customer; | |
public class AdminFacade implements CouponClientFacade { | |
private CompanyDBDAO compdb; | |
private CustomerDBDAO custdb; | |
private CouponDBDAO coupdb; | |
public AdminFacade() { | |
compdb = new CompanyDBDAO(); | |
custdb = new CustomerDBDAO(); | |
coupdb = new CouponDBDAO(); | |
} | |
public void createcompany(Company company) throws CompanySystemException, ConnectionSystemException { | |
compdb.createCompany(company); | |
} | |
public void removeCompany(Company company) throws CouponSystemException, CompanySystemException, ConnectionSystemException { | |
Collection<Coupon> coup = compdb.getCoupons(company.getId()); | |
if (coup == null) { | |
System.out.println("there is no coupon"); | |
compdb.removeCompany(company); | |
} else { | |
for (Coupon coupon : coup) { | |
coupdb.removeCoupon(coupon); | |
} | |
compdb.removeCompany(company); | |
} | |
} | |
public void updateCompany(Company company) throws CompanySystemException, ConnectionSystemException { | |
compdb.updateCompany(company); | |
} | |
public Company getCompany(long companyId) throws CompanySystemException, ConnectionSystemException { | |
return compdb.getcompany(companyId); | |
} | |
public Collection<Company> getAllCompanies() throws CompanySystemException, ConnectionSystemException { | |
return compdb.getAllCompanies(); | |
} | |
public void createCustomer(Customer customer) throws CustomerSystemException, ConnectionSystemException { | |
custdb.createCustomer(customer); | |
} | |
public void removeCustomer(Customer customer) throws CustomerSystemException, ConnectionSystemException { | |
custdb.removeCustomer(customer); | |
} | |
public void updateCustomer(Customer customer) throws CustomerSystemException, ConnectionSystemException { | |
custdb.updateCustomer(customer); | |
} | |
public Collection<Customer> getAllCustomer() throws ConnectionSystemException, CustomerSystemException { | |
return custdb.getAllCustomer(); | |
} | |
public Customer getCustomer(long CustomerId) throws ConnectionSystemException, CustomerSystemException { | |
return custdb.getCustomer(CustomerId); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package facade; | |
public enum ClientType { | |
ADMIN , COMPANY , CUSTOMER | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package javabeans; | |
import java.util.Collection; | |
public class Company { | |
private long id; | |
private String compName; | |
private String password; | |
private String email; | |
private Collection<Coupon> coupons; | |
public Company() { | |
super(); | |
} | |
public Company(long id, String compName, String password, String email) { | |
super(); | |
this.id = id; | |
this.compName = compName; | |
this.password = password; | |
this.email = email; | |
} | |
public Company(String compName, String password, String email) { | |
super(); | |
this.compName = compName; | |
this.password = password; | |
this.email = email; | |
} | |
@Override | |
public String toString() { | |
return "Company [id=" + id + ", compName=" + compName + ", password=" + password + ", email=" + email + "]"; | |
} | |
public long getId() { | |
return id; | |
} | |
public void setId(long id) { | |
this.id = id; | |
} | |
public String getCompName() { | |
return compName; | |
} | |
public void setCompName(String compName) { | |
this.compName = compName; | |
} | |
public String getPassword() { | |
return password; | |
} | |
public void setPassword(String password) { | |
this.password = password; | |
} | |
public String getEmail() { | |
return email; | |
} | |
public void setEmail(String email) { | |
this.email = email; | |
} | |
public Collection<Coupon> getCoupons() { | |
return coupons; | |
} | |
public void setCoupons(Collection<Coupon> coupons) { | |
this.coupons = coupons; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dao; | |
import exceptions.ConnectionSystemException; | |
import exceptions.CouponSystemException; | |
import javabeans.Company; | |
import javabeans.Coupon; | |
public interface Company_CouponDAO { | |
void addCompanyJoin(long idCompany,Coupon coupon) throws CouponSystemException, ConnectionSystemException; | |
void removeCompanyJoin(Company company) throws CouponSystemException, ConnectionSystemException; | |
Coupon getCouponJoin(long idCoupon) throws CouponSystemException, ConnectionSystemException; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dao; | |
import java.sql.Connection; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import exceptions.ConnectionSystemException; | |
import exceptions.CouponSystemException; | |
import javabeans.Company; | |
import javabeans.Coupon; | |
public class Company_CouponDBDAO implements Company_CouponDAO { | |
ConnectionPool pool; | |
private long couponId; | |
@Override | |
public void addCompanyJoin(long idCompany, Coupon coupon) throws CouponSystemException, ConnectionSystemException { | |
Connection con = null; | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "SELECT * FROM coupon where id=(SELECT MAX(id) FROM coupon)"; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ResultSet rs = ps.executeQuery(); | |
while (rs.next()) { | |
couponId = rs.getLong(1); | |
} | |
} catch (SQLException e) { | |
throw new CouponSystemException("Coupon parameters are invaled, or exists. Please try again"); | |
} | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "INSERT INTO company_coupon VALUES(?,?)"; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ps.setLong(1, idCompany); | |
ps.setLong(2, couponId); | |
ps.executeUpdate(); | |
} catch (SQLException e1) { | |
throw new CouponSystemException("Coupon parameters are invaled, or doesn't exists. Please try " | |
+ "again"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
} | |
@Override | |
public void removeCompanyJoin(Company company) throws CouponSystemException, ConnectionSystemException{ | |
Connection con = null; | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "DELETE FROM company_coupon WHERE COMP_ID = ?"; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ps.setLong(1, company.getId()); | |
ps.executeUpdate(); | |
} catch (SQLException e) { | |
throw new CouponSystemException("Removing company is not successed. Try again"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
} | |
@Override | |
public Coupon getCouponJoin(long idCoupon) throws CouponSystemException, ConnectionSystemException{ | |
Connection con = null; | |
Coupon coupon = null; | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "SELECT * FROM company_coupon WHERE COUPON_ID = ?"; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ps.setLong(1, idCoupon); | |
ResultSet rs = ps.executeQuery(); | |
if (rs.next()) { | |
coupon = new Coupon(); | |
coupon.setId(rs.getLong(1)); | |
} | |
} catch (SQLException e) { | |
throw new CouponSystemException("Customer id doesn't exsist"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
return coupon; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dao; | |
import java.util.Collection; | |
import exceptions.CompanySystemException; | |
import exceptions.ConnectionSystemException; | |
import javabeans.Company; | |
import javabeans.Coupon; | |
public interface CompanyDAO { | |
void createCompany(Company company) throws CompanySystemException, ConnectionSystemException; | |
void removeCompany(Company company) throws CompanySystemException, ConnectionSystemException; | |
void updateCompany(Company company) throws CompanySystemException, ConnectionSystemException; | |
Company getcompany(long idCompany) throws CompanySystemException, ConnectionSystemException; | |
Collection<Company> getAllCompanies() throws CompanySystemException, ConnectionSystemException; | |
Collection<Coupon> getCoupons(long companyID) throws CompanySystemException, ConnectionSystemException; | |
Collection<Coupon> getCouponsByPrice(long companyID, Double price) throws CompanySystemException, ConnectionSystemException; | |
boolean login(String compName,String password) throws CompanySystemException, ConnectionSystemException; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dao; | |
import java.sql.Connection; | |
import java.sql.Date; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.List; | |
import exceptions.CompanySystemException; | |
import exceptions.ConnectionSystemException; | |
import javabeans.Company; | |
import javabeans.Coupon; | |
import javabeans.CouponType; | |
public class CompanyDBDAO implements CompanyDAO { | |
private ConnectionPool pool; | |
public CompanyDBDAO() { | |
} | |
@Override | |
public void createCompany(Company company) throws CompanySystemException, ConnectionSystemException { | |
Connection con = null; | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "INSERT INTO company (COMP_NAME ,PASSWORD ,EMAIL) VALUES(?,?,?)"; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ps.setString(1, company.getCompName()); | |
ps.setString(2, company.getPassword()); | |
ps.setString(3, company.getEmail()); | |
ps.executeUpdate(); | |
} catch (SQLException e) { | |
throw new CompanySystemException("Company or companyId already exists"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
} | |
@Override | |
public void removeCompany(Company company) throws CompanySystemException, ConnectionSystemException { | |
Connection con = null; | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "SELECT * FROM company WHERE ID = ? "; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ps.setLong(1, company.getId()); | |
ResultSet rs = ps.executeQuery(); | |
if (!rs.next()) { | |
throw new CompanySystemException("Company id doesn't exist"); | |
} | |
sql = "DELETE FROM company WHERE ID = ?"; | |
ps = con.prepareStatement(sql); | |
ps.setLong(1, company.getId()); | |
ps.executeUpdate(); | |
} catch (SQLException e) { | |
throw new CompanySystemException("Company or companyId doesn't exists"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
} | |
@Override | |
public void updateCompany(Company company) throws CompanySystemException, ConnectionSystemException { | |
Connection con = null; | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "SELECT * FROM company WHERE ID = ? "; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ps.setLong(1, company.getId()); | |
ResultSet rs = ps.executeQuery(); | |
if (!rs.next()) { | |
throw new CompanySystemException("company id doesn't exist"); | |
} | |
sql = "UPDATE company SET EMAIL = ?,PASSWORD = ? WHERE ID = ?"; | |
ps = con.prepareStatement(sql); | |
ps.setString(1, company.getEmail()); | |
ps.setString(2, company.getPassword()); | |
ps.setLong(3, company.getId()); | |
ps.executeUpdate(); | |
} catch (SQLException e) { | |
throw new CompanySystemException("Cannot change company id/Company name id doesn't exist "); | |
} finally { | |
pool.returnConnection(con); | |
} | |
} | |
@Override | |
public Company getcompany(long idCompany) throws CompanySystemException, ConnectionSystemException { | |
Connection con = null; | |
Company comp = null; | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "SELECT * FROM company WHERE ID = ?"; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ps.setLong(1, idCompany); | |
ResultSet rs = ps.executeQuery(); | |
if (rs.next()) { | |
comp = new Company(); | |
comp.setId(rs.getLong(1)); | |
comp.setCompName(rs.getString(2)); | |
comp.setPassword(rs.getString(3)); | |
comp.setEmail(rs.getString(4)); | |
} | |
} catch (SQLException e) { | |
throw new CompanySystemException("Company id doesn't exist"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
return comp; | |
} | |
public Company getCompanyUser(String companyUser) throws CompanySystemException, ConnectionSystemException { | |
Connection con = null; | |
Company comp = null; | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "SELECT * FROM company WHERE COMP_NAME = ?"; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ps.setString(1, companyUser); | |
ResultSet rs = ps.executeQuery(); | |
if (rs.next()) { | |
comp = new Company(); | |
comp.setId(rs.getLong(1)); | |
comp.setCompName(rs.getString(2)); | |
comp.setPassword(rs.getString(3)); | |
comp.setEmail(rs.getString(4)); | |
} | |
} catch (SQLException e) { | |
throw new CompanySystemException("Company name doesn't exist"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
return comp; | |
} | |
@Override | |
public Collection<Company> getAllCompanies() throws CompanySystemException, ConnectionSystemException { | |
Connection con = null; | |
List<Company> companys = new ArrayList<>(); | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "SELECT * FROM company"; | |
Statement st = con.createStatement(); | |
ResultSet rs = st.executeQuery(sql); | |
Company comp; | |
while (rs.next()) { | |
comp = new Company(); | |
comp.setId(rs.getLong(1)); | |
comp.setCompName(rs.getString(2)); | |
comp.setPassword(rs.getString(3)); | |
comp.setEmail(rs.getString(4)); | |
companys.add(comp); | |
} | |
} catch (SQLException e) { | |
throw new CompanySystemException("Parameters are invalid, please try again"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
return companys; | |
} | |
@Override | |
public Collection<Coupon> getCoupons(long companyID) throws CompanySystemException, ConnectionSystemException { | |
Connection con = null; | |
List<Coupon> coupons = new ArrayList<>(); | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "SELECT * FROM company_coupon where COMP_ID=" + companyID; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ResultSet rs = ps.executeQuery(); | |
while (rs.next()) { | |
String sql1 = "SELECT * FROM coupon WHERE ID =" + rs.getLong(2); | |
PreparedStatement ps1 = con.prepareStatement(sql1); | |
ResultSet rs1 = ps1.executeQuery(); | |
while (rs1.next()) { | |
Coupon coupon = new Coupon(); | |
coupon.setId(rs1.getLong(1)); | |
coupon.setTitle(rs1.getString(2)); | |
coupon.setStartDate(rs1.getDate(3)); | |
coupon.setEndDate(rs1.getDate(4)); | |
coupon.setAmount(rs1.getInt(5)); | |
coupon.setType(CouponType.valueOf(rs1.getString(6))); | |
coupon.setMessage(rs1.getString(7)); | |
coupon.setPrice(rs1.getDouble(8)); | |
coupon.setImage(rs1.getString(9)); | |
coupons.add(coupon); | |
} | |
} | |
} catch (SQLException e) { | |
throw new CompanySystemException("Company id doesn't exist"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
return coupons; | |
} | |
public Collection<Coupon> getCouponsByType(long companyID, CouponType couponType) throws CompanySystemException, ConnectionSystemException { | |
Connection con = null; | |
List<Coupon> allCoupons = new ArrayList<>(); | |
List<Coupon> coupons = new ArrayList<>(); | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "SELECT * FROM company_coupon where COMP_ID=" + companyID; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ResultSet rs = ps.executeQuery(); | |
while (rs.next()) { | |
String sql1 = "SELECT * FROM coupon WHERE ID =" + rs.getLong(2); | |
PreparedStatement ps1 = con.prepareStatement(sql1); | |
ResultSet rs1 = ps1.executeQuery(); | |
while (rs1.next()) { | |
Coupon coupon = new Coupon(); | |
coupon.setId(rs1.getLong(1)); | |
coupon.setTitle(rs1.getString(2)); | |
coupon.setStartDate(rs1.getDate(3)); | |
coupon.setEndDate(rs1.getDate(4)); | |
coupon.setAmount(rs1.getInt(5)); | |
coupon.setType(CouponType.valueOf(rs1.getString(6))); | |
coupon.setMessage(rs1.getString(7)); | |
coupon.setPrice(rs1.getDouble(8)); | |
coupon.setImage(rs1.getString(9)); | |
allCoupons.add(coupon); | |
} | |
} | |
for (int i = 0; i < allCoupons.size(); i++) { | |
if (allCoupons.get(i).getType() == couponType) { | |
coupons.add(allCoupons.get(i)); | |
} | |
} | |
} catch (SQLException e) { | |
throw new CompanySystemException("Company id doesn't exist"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
return coupons; | |
} | |
public Collection<Coupon> getCouponsByDate(long companyID, Date endDate) throws CompanySystemException, ConnectionSystemException { | |
Connection con = null; | |
List<Coupon> allCoupons = new ArrayList<>(); | |
List<Coupon> coupons = new ArrayList<>(); | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "SELECT * FROM company_coupon where COMP_ID=" + companyID; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ResultSet rs = ps.executeQuery(); | |
while (rs.next()) { | |
String sql1 = "SELECT * FROM coupon WHERE ID =" + rs.getLong(2); | |
PreparedStatement ps1 = con.prepareStatement(sql1); | |
ResultSet rs1 = ps1.executeQuery(); | |
while (rs1.next()) { | |
Coupon coupon = new Coupon(); | |
coupon.setId(rs1.getLong(1)); | |
coupon.setTitle(rs1.getString(2)); | |
coupon.setStartDate(rs1.getDate(3)); | |
coupon.setEndDate(rs1.getDate(4)); | |
coupon.setAmount(rs1.getInt(5)); | |
coupon.setType(CouponType.valueOf(rs1.getString(6))); | |
coupon.setMessage(rs1.getString(7)); | |
coupon.setPrice(rs1.getDouble(8)); | |
coupon.setImage(rs1.getString(9)); | |
allCoupons.add(coupon); | |
} | |
} | |
for (int i = 0; i < allCoupons.size(); i++) { | |
if (!allCoupons.get(i).getEndDate().after(endDate)) { | |
coupons.add(allCoupons.get(i)); | |
} | |
} | |
} catch (SQLException e) { | |
throw new CompanySystemException("Company id doesn't exist"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
return coupons; | |
} | |
public Collection<Coupon> getCouponsByPrice(long companyID, Double price) throws CompanySystemException, ConnectionSystemException { | |
Connection con = null; | |
List<Coupon> allCoupons = new ArrayList<>(); | |
List<Coupon> coupons = new ArrayList<>(); | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "SELECT * FROM company_coupon where COMP_ID=" + companyID; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ResultSet rs = ps.executeQuery(); | |
while (rs.next()) { | |
String sql1 = "SELECT * FROM coupon WHERE ID =" + rs.getLong(2); | |
PreparedStatement ps1 = con.prepareStatement(sql1); | |
ResultSet rs1 = ps1.executeQuery(); | |
while (rs1.next()) { | |
Coupon coupon = new Coupon(); | |
coupon.setId(rs1.getLong(1)); | |
coupon.setTitle(rs1.getString(2)); | |
coupon.setStartDate(rs1.getDate(3)); | |
coupon.setEndDate(rs1.getDate(4)); | |
coupon.setAmount(rs1.getInt(5)); | |
coupon.setType(CouponType.valueOf(rs1.getString(6))); | |
coupon.setMessage(rs1.getString(7)); | |
coupon.setPrice(rs1.getDouble(8)); | |
coupon.setImage(rs1.getString(9)); | |
allCoupons.add(coupon); | |
} | |
} | |
for (int i = 0; i < allCoupons.size(); i++) { | |
if (allCoupons.get(i).getPrice() <= price) { | |
coupons.add(allCoupons.get(i)); | |
} | |
} | |
} catch (SQLException e) { | |
throw new CompanySystemException("Company id doesn't exist"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
return coupons; | |
} | |
@Override | |
public boolean login(String compName, String password) throws CompanySystemException, ConnectionSystemException { | |
Connection con = null; | |
boolean bool = false; | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "SELECT ID FROM company WHERE COMP_NAME=? AND PASSWORD=?"; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ps.setString(1, compName); | |
ps.setString(2, password); | |
ResultSet rs = ps.executeQuery(); | |
if (rs.next()) { | |
bool = true; | |
} else { | |
} | |
} catch (SQLException e) { | |
throw new CompanySystemException("Company login failed"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
return bool; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package facade; | |
import java.sql.Date; | |
import java.util.Collection; | |
import dao.Company_CouponDBDAO; | |
import dao.CompanyDBDAO; | |
import dao.CouponDBDAO; | |
import exceptions.CompanySystemException; | |
import exceptions.ConnectionSystemException; | |
import exceptions.CouponSystemException; | |
import javabeans.Coupon; | |
import javabeans.CouponType; | |
public class CompanyFacade implements CouponClientFacade { | |
private CompanyDBDAO compdb; | |
private CouponDBDAO coupdb; | |
private Company_CouponDBDAO compcoupdb; | |
private long companyID; | |
public CompanyFacade(long companyID) { | |
this.setCompanyID(companyID); | |
compdb = new CompanyDBDAO(); | |
coupdb = new CouponDBDAO(); | |
compcoupdb = new Company_CouponDBDAO(); | |
} | |
public void creatCoupon(long idCompany, Coupon coupon) throws CouponSystemException, ConnectionSystemException { | |
coupdb.createCoupon(coupon); | |
compcoupdb.addCompanyJoin(idCompany, coupon); | |
} | |
public void removeCoupon(Coupon coupon) throws CouponSystemException, ConnectionSystemException { | |
coupdb.removeCoupon(coupon); | |
} | |
public void updateCoupon(Coupon coupon) throws CouponSystemException, ConnectionSystemException { | |
coupdb.updateCoupon(coupon); | |
} | |
public Coupon getCoupon(long couponId) throws CouponSystemException, ConnectionSystemException { | |
return coupdb.getCoupon(couponId); | |
} | |
public Collection<Coupon> getAllCoupon(long idCompany) throws CompanySystemException, CouponSystemException, ConnectionSystemException { | |
Collection<Coupon> coupons = (Collection<Coupon>) compdb.getCoupons(idCompany); | |
return coupons; | |
} | |
public Collection<Coupon> getCouponByType(long idCompany, CouponType couponType) | |
throws CompanySystemException, CouponSystemException, ConnectionSystemException { | |
Collection<Coupon> coupons = compdb.getCouponsByType(idCompany, couponType); | |
return coupons; | |
} | |
public Collection<Coupon> getCouponByPrice(long idCompany, double couponPrice) | |
throws CompanySystemException, CouponSystemException, ConnectionSystemException { | |
Collection<Coupon> coupons = compdb.getCouponsByPrice(idCompany, couponPrice); | |
return coupons; | |
} | |
public Collection<Coupon> getCouponByDate(long idCompany, Date endDate) | |
throws CompanySystemException, CouponSystemException, ConnectionSystemException { | |
Collection<Coupon> coupons = compdb.getCouponsByDate(idCompany, endDate); | |
return coupons; | |
} | |
public long getCompanyID() { | |
return companyID; | |
} | |
public void setCompanyID(long companyID) { | |
this.companyID = companyID; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package exceptions; | |
public class CompanySystemException extends Exception{ | |
/** | |
* | |
*/ | |
private static final long serialVersionUID = 1L; | |
public CompanySystemException() { | |
super(); | |
// TODO Auto-generated constructor stub | |
} | |
public CompanySystemException(String message, Throwable cause, boolean enableSuppression, | |
boolean writableStackTrace) { | |
super(message, cause, enableSuppression, writableStackTrace); | |
// TODO Auto-generated constructor stub | |
} | |
public CompanySystemException(String message, Throwable cause) { | |
super(message, cause); | |
// TODO Auto-generated constructor stub | |
} | |
public CompanySystemException(String message) { | |
super(message); | |
// TODO Auto-generated constructor stub | |
} | |
public CompanySystemException(Throwable cause) { | |
super(cause); | |
// TODO Auto-generated constructor stub | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dao; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.SQLException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import exceptions.ConnectionSystemException; | |
public class ConnectionPool { | |
private List<Connection> cons; | |
private List<Connection> cons2; | |
private int size = 5; | |
private String dbname = "dbproject_2"; | |
private String dbUrl = "jdbc:derby://localhost:1527/" + dbname; | |
private final static ConnectionPool singleton = new ConnectionPool(); | |
private ConnectionPool() { | |
cons = new ArrayList<>(); | |
cons2 = new ArrayList<>(); | |
try { | |
for (int i = 0; i < size; i++) { | |
Connection con = DriverManager.getConnection(dbUrl); | |
cons.add(con); | |
cons2.add(con); | |
} | |
System.out.println("Connection pool initialized all connection: " + cons.size()); | |
} catch (SQLException e) { | |
System.out.println("Connection is not stable"); | |
} | |
} | |
public static ConnectionPool getInstance() { | |
return singleton; | |
} | |
public synchronized Connection getconnection() throws ConnectionSystemException { | |
while (cons.isEmpty()) { | |
try { | |
wait(); | |
} catch (InterruptedException e) { | |
throw new ConnectionSystemException("Connection is not stable. Contact us"); | |
} | |
} | |
return cons.remove(0); | |
} | |
public synchronized void returnConnection(Connection connection) { | |
cons.add(connection); | |
notifyAll(); | |
} | |
public synchronized void closeAllConnections()throws ConnectionSystemException { | |
for (Connection con : cons2) { | |
try { | |
con.close(); | |
} catch (SQLException e) { | |
throw new ConnectionSystemException("Connection isn't closed"); | |
} | |
} | |
System.out.println("connection pool shutdown - closed " + cons.size() + " connections"); | |
if (cons.size() != size) { | |
System.out.println("Not all of the connections are closed"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package exceptions; | |
public class ConnectionSystemException extends Exception { | |
/** | |
* | |
*/ | |
private static final long serialVersionUID = 1L; | |
public ConnectionSystemException() { | |
super(); | |
// TODO Auto-generated constructor stub | |
} | |
public ConnectionSystemException(String message, Throwable cause, boolean enableSuppression, | |
boolean writableStackTrace) { | |
super(message, cause, enableSuppression, writableStackTrace); | |
// TODO Auto-generated constructor stub | |
} | |
public ConnectionSystemException(String message, Throwable cause) { | |
super(message, cause); | |
// TODO Auto-generated constructor stub | |
} | |
public ConnectionSystemException(String message) { | |
super(message); | |
// TODO Auto-generated constructor stub | |
} | |
public ConnectionSystemException(Throwable cause) { | |
super(cause); | |
// TODO Auto-generated constructor stub | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package javabeans; | |
import java.sql.Date; | |
public class Coupon { | |
private long id; | |
private String title; | |
private Date startDate; | |
private Date endDate; | |
private int amount; | |
private CouponType type; | |
private String message; | |
private double price; | |
private String image; | |
public Coupon() { | |
super(); | |
} | |
public Coupon(long id, String title, Date startDate, Date endDate, int amount, CouponType type, String message, | |
double price, String image) { | |
super(); | |
this.id = id; | |
this.title = title; | |
this.startDate = startDate; | |
this.endDate = endDate; | |
this.amount = amount; | |
this.type = type; | |
this.message = message; | |
this.price = price; | |
this.image = image; | |
} | |
public Coupon(String title, Date startDate, Date endDate, int amount, CouponType type, String message, double price, | |
String image) { | |
super(); | |
this.title = title; | |
this.startDate = startDate; | |
this.endDate = endDate; | |
this.amount = amount; | |
this.type = type; | |
this.message = message; | |
this.price = price; | |
this.image = image; | |
} | |
public long getId() { | |
return id; | |
} | |
public void setId(long id) { | |
this.id = id; | |
} | |
public String getTitle() { | |
return title; | |
} | |
public void setTitle(String title) { | |
this.title = title; | |
} | |
public Date getStartDate() { | |
return startDate; | |
} | |
public void setStartDate(Date startDate) { | |
this.startDate = startDate; | |
} | |
public Date getEndDate() { | |
return endDate; | |
} | |
public void setEndDate(Date endDate) { | |
this.endDate = endDate; | |
} | |
public int getAmount() { | |
return amount; | |
} | |
public void setAmount(int amount) { | |
this.amount = amount; | |
} | |
public CouponType getType() { | |
return type; | |
} | |
public void setType(CouponType type) { | |
this.type = type; | |
} | |
public String getMessage() { | |
return message; | |
} | |
public void setMessage(String message) { | |
this.message = message; | |
} | |
public double getPrice() { | |
return price; | |
} | |
public void setPrice(double price) { | |
this.price = price; | |
} | |
public String getImage() { | |
return image; | |
} | |
public void setImage(String image) { | |
this.image = image; | |
} | |
@Override | |
public String toString() { | |
return "Coupon [id=" + id + ", title=" + title + ", startDate=" + startDate + ", endDate=" + endDate | |
+ ", amount=" + amount + ", type=" + type + ", message=" + message + ", price=" + price + ", image=" | |
+ image + "]"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package facade; | |
public interface CouponClientFacade { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dao; | |
import java.sql.Date; | |
import java.util.Collection; | |
import exceptions.ConnectionSystemException; | |
import exceptions.CouponSystemException; | |
import javabeans.Coupon; | |
import javabeans.CouponType; | |
public interface CouponDAO { | |
void createCoupon(Coupon coupon) throws CouponSystemException, ConnectionSystemException; | |
void removeCoupon(Coupon coupon) throws CouponSystemException, ConnectionSystemException; | |
void updateCoupon(Coupon coupon) throws CouponSystemException, ConnectionSystemException; | |
Coupon getCoupon(long idCoupon) throws CouponSystemException, ConnectionSystemException; | |
Collection<Coupon> getCoupons() throws CouponSystemException, ConnectionSystemException; | |
Collection<Coupon> getCouponByType(CouponType couponType) throws CouponSystemException, ConnectionSystemException; | |
Collection<Coupon> getCouponByPrice(double couponPrise) throws CouponSystemException, ConnectionSystemException; | |
Collection<Coupon> getCouponByDate(Date endDate) throws CouponSystemException, ConnectionSystemException; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dao; | |
import java.sql.Connection; | |
import java.sql.Date; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.List; | |
import exceptions.ConnectionSystemException; | |
import exceptions.CouponSystemException; | |
import javabeans.Coupon; | |
import javabeans.CouponType; | |
public class CouponDBDAO implements CouponDAO { | |
private ConnectionPool pool; | |
@Override | |
public void createCoupon(Coupon coupon) throws CouponSystemException, ConnectionSystemException { | |
Connection con = null; | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "INSERT INTO coupon (TITLE,START_DATE ,END_DATE ,AMOUNT ,TYPE,MESSAGE,PRICE ,IMAGE ) VALUES(?,?,?,?,?,?,?,?)"; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ps.setString(1, coupon.getTitle()); | |
ps.setDate(2, coupon.getStartDate()); | |
ps.setDate(3, coupon.getEndDate()); | |
ps.setInt(4, coupon.getAmount()); | |
ps.setString(5, coupon.getType().toString()); | |
ps.setString(6, coupon.getMessage()); | |
ps.setDouble(7, coupon.getPrice()); | |
ps.setString(8, coupon.getImage()); | |
ps.executeUpdate(); | |
System.out.println("Coupon created"); | |
} catch (SQLException e) { | |
throw new CouponSystemException("Coupon parameters are invaled, or exists. Please try again"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
} | |
@Override | |
public void removeCoupon(Coupon coupon) throws CouponSystemException, ConnectionSystemException { | |
Connection con = null; | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "SELECT * FROM coupon WHERE ID = ? "; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ps.setLong(1, coupon.getId()); | |
ResultSet rs = ps.executeQuery(); | |
if (!rs.next()) { | |
throw new CouponSystemException("Company ID doesn't exist"); | |
} | |
sql = "DELETE FROM coupon WHERE ID = ?"; | |
ps = con.prepareStatement(sql); | |
ps.setLong(1, coupon.getId()); | |
ps.executeUpdate(); | |
} catch (SQLException e) { | |
throw new CouponSystemException("Parameters invalid. Please try again"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
} | |
@Override | |
public void updateCoupon(Coupon coupon) throws CouponSystemException, ConnectionSystemException { | |
Connection con = null; | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "SELECT * FROM coupon WHERE ID = ? "; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ps.setLong(1, coupon.getId()); | |
ResultSet rs = ps.executeQuery(); | |
if (!rs.next()) { | |
throw new CouponSystemException("Company id doesn't exist"); | |
} | |
sql = "UPDATE coupon SET PRICE=?,END_DATE=?,AMOUNT=? WHERE ID = ?"; | |
ps = con.prepareStatement(sql); | |
ps.setDouble(1, coupon.getPrice()); | |
ps.setDate(2, coupon.getEndDate()); | |
ps.setInt(3, coupon.getAmount()); | |
ps.setLong(4, coupon.getId()); | |
ps.executeUpdate(); | |
} catch (SQLException e) { | |
throw new CouponSystemException("Parameters invalid. Please try again"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
} | |
@Override | |
public Coupon getCoupon(long idCoupon) throws CouponSystemException, ConnectionSystemException { | |
Connection con = null; | |
Coupon coupon = null; | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "SELECT * FROM coupon WHERE ID = ?"; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ps.setLong(1, idCoupon); | |
ResultSet rs = ps.executeQuery(); | |
if (rs.next()) { | |
coupon = new Coupon(); | |
coupon.setId(rs.getLong(1)); | |
coupon.setTitle(rs.getString(2)); | |
coupon.setStartDate(rs.getDate(3)); | |
coupon.setEndDate(rs.getDate(4)); | |
coupon.setAmount(rs.getInt(5)); | |
coupon.setType(CouponType.valueOf(rs.getString(6))); | |
coupon.setMessage(rs.getString(7)); | |
coupon.setPrice(rs.getDouble(8)); | |
coupon.setImage(rs.getString(9)); | |
} | |
} catch (SQLException e) { | |
throw new CouponSystemException("Company id doesn't exist"); | |
} finally { | |
pool.returnConnection(con); | |
if (coupon == null) { | |
throw new CouponSystemException("CouponId is not exist"); | |
} | |
} | |
return coupon; | |
} | |
@Override | |
public Collection<Coupon> getCoupons() throws CouponSystemException, ConnectionSystemException { | |
Connection con = null; | |
List<Coupon> coupons = new ArrayList<>(); | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "SELECT * FROM coupon"; | |
Statement st = con.createStatement(); | |
ResultSet rs = st.executeQuery(sql); | |
Coupon coupon; | |
while (rs.next()) { | |
coupon = new Coupon(); | |
coupon.setId(rs.getLong(1)); | |
coupon.setTitle(rs.getString(2)); | |
coupon.setStartDate(rs.getDate(3)); | |
coupon.setEndDate(rs.getDate(4)); | |
coupon.setAmount(rs.getInt(5)); | |
coupon.setType(CouponType.valueOf(rs.getString(6))); | |
coupon.setMessage(rs.getString(7)); | |
coupon.setPrice(rs.getDouble(8)); | |
coupon.setImage(rs.getString(9)); | |
coupons.add(coupon); | |
} | |
} catch (SQLException e) { | |
throw new CouponSystemException("Parameters invalid. Please try again"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
return coupons; | |
} | |
@Override | |
public Collection<Coupon> getCouponByType(CouponType couponType) throws CouponSystemException, ConnectionSystemException { | |
Connection con = null; | |
List<Coupon> coupons = new ArrayList<>(); | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "SELECT * FROM coupon where TYPE = " + "'" + couponType + "'" + ""; | |
Statement st = con.createStatement(); | |
ResultSet rs = st.executeQuery(sql); | |
Coupon coupon; | |
while (rs.next()) { | |
coupon = new Coupon(); | |
coupon.setId(rs.getLong(1)); | |
coupon.setTitle(rs.getString(2)); | |
coupon.setStartDate(rs.getDate(3)); | |
coupon.setEndDate(rs.getDate(4)); | |
coupon.setAmount(rs.getInt(5)); | |
coupon.setType(CouponType.valueOf(rs.getString(6))); | |
coupon.setMessage(rs.getString(7)); | |
coupon.setPrice(rs.getDouble(8)); | |
coupon.setImage(rs.getString(9)); | |
coupons.add(coupon); | |
} | |
} catch (SQLException e) { | |
throw new CouponSystemException("Coupon type is not correct!"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
return coupons; | |
} | |
public Collection<Coupon> getCouponByPrice(double couponPrise) throws CouponSystemException, ConnectionSystemException { | |
Connection con = null; | |
List<Coupon> coupons = new ArrayList<>(); | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "SELECT * FROM coupon where PRICE <= " + couponPrise; | |
Statement st = con.createStatement(); | |
ResultSet rs = st.executeQuery(sql); | |
Coupon coupon; | |
while (rs.next()) { | |
coupon = new Coupon(); | |
coupon.setId(rs.getLong(1)); | |
coupon.setTitle(rs.getString(2)); | |
coupon.setStartDate(rs.getDate(3)); | |
coupon.setEndDate(rs.getDate(4)); | |
coupon.setAmount(rs.getInt(5)); | |
coupon.setType(CouponType.valueOf(rs.getString(6))); | |
coupon.setMessage(rs.getString(7)); | |
coupon.setPrice(rs.getDouble(8)); | |
coupon.setImage(rs.getString(9)); | |
coupons.add(coupon); | |
} | |
} catch (SQLException e) { | |
throw new CouponSystemException("Parameters invalid. Please try again"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
return coupons; | |
} | |
public Collection<Coupon> getCouponByDate(Date endDate) throws CouponSystemException, ConnectionSystemException { | |
Connection con = null; | |
List<Coupon> coupons = new ArrayList<>(); | |
Date date = new Date(0); | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "SELECT * FROM coupon where END_DATE BETWEEN '" + date + "' and '" + endDate + "'"; | |
Statement st = con.createStatement(); | |
ResultSet rs = st.executeQuery(sql); | |
Coupon coupon; | |
while (rs.next()) { | |
coupon = new Coupon(); | |
coupon.setId(rs.getLong(1)); | |
coupon.setTitle(rs.getString(2)); | |
coupon.setStartDate(rs.getDate(3)); | |
coupon.setEndDate(rs.getDate(4)); | |
coupon.setAmount(rs.getInt(5)); | |
coupon.setType(CouponType.valueOf(rs.getString(6))); | |
coupon.setMessage(rs.getString(7)); | |
coupon.setPrice(rs.getDouble(8)); | |
coupon.setImage(rs.getString(9)); | |
coupons.add(coupon); | |
} | |
} catch (SQLException e) { | |
throw new CouponSystemException("Parameters invalid. Please try again"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
return coupons; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package loginsystem; | |
import java.sql.SQLException; | |
import dao.CompanyDBDAO; | |
import dao.ConnectionPool; | |
import dao.CustomerDBDAO; | |
import exceptions.CompanySystemException; | |
import exceptions.ConnectionSystemException; | |
import exceptions.CustomerSystemException; | |
import facade.AdminFacade; | |
import facade.ClientType; | |
import facade.CompanyFacade; | |
import facade.CouponClientFacade; | |
import facade.CustomerFacade; | |
import javabeans.Company; | |
import javabeans.Customer; | |
import thread.DailyCouponExpirationTask; | |
public class CouponSystem { | |
private CompanyDBDAO compdb = new CompanyDBDAO(); | |
private CustomerDBDAO custdb = new CustomerDBDAO(); | |
private long companyID; | |
private long customerID; | |
private final static CouponSystem singleton = new CouponSystem(); | |
private DailyCouponExpirationTask task = new DailyCouponExpirationTask(); | |
private ConnectionPool pool = ConnectionPool.getInstance(); | |
private Thread t; | |
private CouponSystem() { | |
// t = new Thread(task); | |
} | |
public static CouponSystem getInstance() { | |
return singleton; | |
} | |
/** | |
* Login method checking username and password, that returns facade by client type. | |
*/ | |
public CouponClientFacade login(String username, String password, ClientType clientType) | |
throws CompanySystemException, ConnectionSystemException, CustomerSystemException { | |
CouponClientFacade ccf = null; | |
if (clientType == ClientType.COMPANY && compdb.login(username, password)) { | |
Company company = compdb.getCompanyUser(username); | |
companyID = company.getId(); | |
ccf = new CompanyFacade(companyID); | |
} else if (clientType == ClientType.CUSTOMER && custdb.login(username, password)) { | |
Customer customer = custdb.getCustomerUser(username); | |
customerID = customer.getId(); | |
ccf = new CustomerFacade(customerID); | |
} else if (clientType == ClientType.ADMIN) { | |
String user = "user"; | |
String pass = "1234"; | |
if (username.equals(user) && password.equals(pass)) { | |
ccf = new AdminFacade(); | |
} else { | |
throw new CompanySystemException(); | |
} | |
} else { | |
throw new CompanySystemException(); | |
} | |
return ccf; | |
} | |
/** | |
* Method that closes connections and stops daily check thread. | |
*/ | |
public void shutdown() throws SQLException, ConnectionSystemException { | |
t.interrupt(); | |
task.stopTask(); | |
pool.closeAllConnections(); | |
} | |
public long getCompanyID() { | |
return companyID; | |
} | |
public long getCustomerID() { | |
return customerID; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package exceptions; | |
public class CouponSystemException extends Exception { | |
/** | |
* | |
*/ | |
private static final long serialVersionUID = 1L; | |
public CouponSystemException() { | |
super(); | |
// TODO Auto-generated constructor stub | |
} | |
public CouponSystemException(String message, Throwable cause, boolean enableSuppression, | |
boolean writableStackTrace) { | |
super(message, cause, enableSuppression, writableStackTrace); | |
// TODO Auto-generated constructor stub | |
} | |
public CouponSystemException(String message, Throwable cause) { | |
super(message, cause); | |
// TODO Auto-generated constructor stub | |
} | |
public CouponSystemException(String message) { | |
super(message); | |
// TODO Auto-generated constructor stub | |
} | |
public CouponSystemException(Throwable cause) { | |
super(cause); | |
// TODO Auto-generated constructor stub | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Test; | |
import java.sql.Date; | |
import java.sql.SQLException; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.InputMismatchException; | |
import java.util.Scanner; | |
import dao.CouponDBDAO; | |
import exceptions.CompanySystemException; | |
import exceptions.ConnectionSystemException; | |
import exceptions.CouponSystemException; | |
import exceptions.CustomerSystemException; | |
import facade.AdminFacade; | |
import facade.ClientType; | |
import facade.CompanyFacade; | |
import facade.CustomerFacade; | |
import javabeans.Company; | |
import javabeans.Coupon; | |
import javabeans.CouponType; | |
import javabeans.Customer; | |
import loginsystem.CouponSystem; | |
public class CouponTester { | |
private Scanner sc = new Scanner(System.in); | |
private String userCommand; | |
private String userCommand1; | |
private String userCommand2; | |
private String userCommand3; | |
private int type; | |
private boolean timeToQuit = false; | |
private static CouponSystem system = CouponSystem.getInstance(); | |
private boolean login = false; | |
private CompanyFacade cf = null; | |
private AdminFacade af = null; | |
private CustomerFacade custfacade = null; | |
private Date startdate = null; | |
private Date enddate = null; | |
private long idCompany; | |
private long idCustomer; | |
public void setIdCompany(long idCompany) { | |
this.idCompany = idCompany; | |
} | |
public static void main(String[] args) throws CompanySystemException, | |
SQLException, CustomerSystemException, CouponSystemException, | |
ParseException, ConnectionSystemException { | |
CouponTester csv = new CouponTester(); | |
csv.typeMain(); | |
csv.login(); | |
csv.type(); | |
} | |
private void typeMain() { | |
loginMenue(); | |
System.out.println("enter choice: "); | |
userCommand3 = sc.nextLine(); | |
while (!(userCommand3.equals("1") || userCommand3.equals("2") || userCommand3 | |
.equals("3"))) { | |
typeMain(); | |
} | |
} | |
public Date getNewDate() { | |
String expectedPattern = "dd-MM-yyyy"; | |
SimpleDateFormat formatter = new SimpleDateFormat(expectedPattern); | |
System.out.println("enter Date (" +expectedPattern+")"); | |
String userInput = sc.next(); | |
java.util.Date date = new java.util.Date(); | |
try { | |
date = formatter.parse(userInput); | |
} catch (ParseException e) { | |
System.out.println("Wrong date, try again"); | |
getNewDate(); | |
} | |
java.sql.Date sqlDate = new java.sql.Date(date.getTime()); | |
return sqlDate; | |
} | |
private void login() throws CompanySystemException, SQLException, | |
CustomerSystemException, ConnectionSystemException { | |
while (!login) { | |
System.out.println("\n=== login ================="); | |
System.out.println("enter username : "); | |
userCommand1 = sc.nextLine(); | |
System.out.println("enter password : "); | |
userCommand2 = sc.nextLine(); | |
switch (userCommand3) { | |
case "1": | |
try { | |
af = (AdminFacade) system.login(userCommand1, userCommand2, | |
ClientType.ADMIN); | |
login = true; | |
type = 1; | |
} catch (CompanySystemException | NullPointerException e) { | |
System.out | |
.println("username/password incorrect. please try again"); | |
typeMain(); | |
} | |
break; | |
case "2": | |
try { | |
cf = (CompanyFacade) system.login(userCommand1, | |
userCommand2, ClientType.COMPANY); | |
type = 2; | |
login = true; | |
idCompany = system.getCompanyID(); | |
} catch (CompanySystemException e) { | |
System.out | |
.println("username/password incorrect. please try again"); | |
typeMain(); | |
} | |
break; | |
case "3": | |
try { | |
custfacade = (CustomerFacade) system.login(userCommand1, | |
userCommand2, ClientType.CUSTOMER); | |
type = 3; | |
login = true; | |
idCustomer = system.getCustomerID(); | |
} catch (CompanySystemException e) { | |
System.out | |
.println("username/password incorrect. please try again"); | |
typeMain(); | |
} | |
break; | |
default: | |
System.out.println("try again"); | |
break; | |
} | |
} | |
} | |
private void type() throws CompanySystemException, SQLException, | |
CustomerSystemException, CouponSystemException, ParseException, | |
ConnectionSystemException { | |
if (type == 1) { | |
while (!timeToQuit) { | |
adminMenue(); | |
setUserCommand(); | |
doAdminCommand(); | |
} | |
} else if (type == 2) { | |
while (!timeToQuit) { | |
companyMenue(); | |
setUserCommand(); | |
doCompanyCommand(); | |
} | |
} else if (type == 3) { | |
while (!timeToQuit) { | |
customerMenue(); | |
setUserCommand(); | |
doCustCommand(); | |
} | |
} | |
} | |
private void loginMenue() { | |
System.out.println("\n=== menu ================="); | |
System.out.println("1 ............ admin"); | |
System.out.println("2 ............ company"); | |
System.out.println("3 ............ customer"); | |
} | |
private void adminMenue() { | |
System.out.println("\n=== menu ================="); | |
System.out.println("1 ............ add company"); | |
System.out.println("2 ............ remove company"); | |
System.out.println("3 ............ update company"); | |
System.out.println("4 ............ show all companies details"); | |
System.out.println("5 ............ show company details"); | |
System.out.println("6 ............ add customer"); | |
System.out.println("7 ............ remove customer"); | |
System.out.println("8 ............ update customer"); | |
System.out.println("9 ............ show all customers"); | |
System.out.println("10 ............ show customer details"); | |
System.out.println("Q ............ to quit"); | |
} | |
private void companyMenue() { | |
System.out.println("\n=== menu ================="); | |
System.out.println("1 ............ add coupon"); | |
System.out.println("2 ............ remove coupon"); | |
System.out.println("3 ............ update coupon"); | |
System.out.println("4 ............ coupon details"); | |
System.out.println("5 ............ get all coupons"); | |
System.out.println("6 ............ get coupons by type"); | |
System.out.println("7 ............ get coupons by date"); | |
System.out.println("8 ............ get coupons by price"); | |
System.out.println("Q ............ to quit"); | |
} | |
private void customerMenue() { | |
System.out.println("\n=== menu ================="); | |
System.out.println("1 ............ purchase coupon"); | |
System.out.println("2 ............ read all purchased coupon"); | |
System.out.println("3 ............ read all purchased coupon by type"); | |
System.out.println("4 ............ read all purchased coupon by price"); | |
System.out.println("Q ............ quit"); | |
} | |
private void setUserCommand() { | |
System.out.print("enter choice: "); | |
userCommand = sc.next(); | |
} | |
private void doAdminCommand() throws CompanySystemException, SQLException, | |
CustomerSystemException, ConnectionSystemException { | |
switch (userCommand) { | |
case "Q": | |
case "q": | |
timeToQuit = true; | |
System.out.println("thank you bye bye"); | |
system.shutdown(); | |
break; | |
case "1": | |
boolean checkCompany = false; | |
do { | |
System.out.println("enter COMPANY NAME"); | |
String compname = sc.next(); | |
System.out.println("enter PASSWORD"); | |
String pass = sc.next(); | |
System.out.println("enter EMAIL"); | |
String email = sc.next(); | |
Company c = new Company(compname, pass, email); | |
try { | |
af.createcompany(c); | |
} catch (CompanySystemException e) { | |
System.out.println("Company name or ID already exists"); | |
System.out.println("try again"); | |
checkCompany = true; | |
continue; | |
} | |
System.out.println("company '" + compname + "' created"); | |
checkCompany = false; | |
} while (checkCompany); | |
break; | |
case "2": | |
boolean checkCompany1 = false; | |
do { | |
System.out.println("enter COMPANY ID to remove"); | |
long compid = 0; | |
try{compid = sc.nextLong(); | |
}catch (InputMismatchException e){ | |
System.out.println("Enter numbers only please"); | |
break; | |
} | |
Company c1 = new Company(compid, "*******", "***", "***"); | |
try { | |
af.removeCompany(c1); | |
System.out.println("company " + compid + " removed"); | |
checkCompany1 = false; | |
showAllCompanies(); | |
} catch (CouponSystemException | CompanySystemException e1) { | |
System.out.println("Company ID doesn't exist"); | |
checkCompany1 = true; | |
} | |
} while (checkCompany1); | |
break; | |
case "3": | |
boolean checkCompany2 = false; | |
do { | |
try { | |
System.out.println("enter COMPANY ID to update"); | |
long compid = 0; | |
try{compid = sc.nextLong(); | |
}catch (InputMismatchException e){ | |
System.out.println("Enter numbers only please"); | |
break; | |
} | |
String pass = af.getCompany(compid).getPassword(); | |
String email = af.getCompany(compid).getEmail(); | |
System.out | |
.println("Do you want to update the PASSWORD? (Y/N)"); | |
String yes = sc.next(); | |
if (yes.equals("y") || yes.equals("Y")) { | |
System.out.println("Enter new password:"); | |
pass = sc.next(); | |
} else | |
; | |
System.out | |
.println("Do you want to update the EMAIL? (Y/N)"); | |
yes = sc.next(); | |
if (yes.equals("y") || yes.equals("Y")) { | |
System.out.println("Enter new Email:"); | |
email = sc.next(); | |
} else { | |
} | |
; | |
Company c2 = new Company(compid, "*****", pass, email); | |
af.updateCompany(c2); | |
System.out.println("compnay " | |
+ af.getCompany(compid).getCompName() + " updated"); | |
System.out.println(af.getCompany(compid)); | |
checkCompany2 = false; | |
} catch (NullPointerException e) { | |
System.out.println("company ID doesn't exist"); | |
checkCompany2 = true; | |
} | |
} while (checkCompany2); | |
break; | |
case "4": | |
try { | |
showAllCompanies(); | |
} catch (SQLException e) { | |
System.out | |
.println("All companies table can't be showed. Try again"); | |
} | |
break; | |
case "5": | |
boolean check = false; | |
do { | |
System.out.println("enter COMPANY ID "); | |
long compid = 0; | |
try{compid = sc.nextLong(); | |
}catch (InputMismatchException e){ | |
System.out.println("Enter numbers only please"); | |
break; | |
} | |
if (af.getCompany(compid) == null) { | |
} else { | |
System.out.println("Company ID = " | |
+ af.getCompany(compid).getId()); | |
System.out.println("Company NAME = " | |
+ af.getCompany(compid).getCompName()); | |
System.out.println("Company PASSWORD = " | |
+ af.getCompany(compid).getPassword()); | |
System.out.println("Company EMAIL = " | |
+ af.getCompany(compid).getEmail()); | |
check = false; | |
} | |
} while (check); | |
break; | |
case "6": | |
boolean checkCust = false; | |
do { | |
try { | |
System.out.println("enter CUSTOMER NAME"); | |
String custname = sc.next(); | |
System.out.println("enter PASSWORD"); | |
String custpass = sc.next(); | |
Customer cust = new Customer(custname, custpass); | |
af.createCustomer(cust); | |
System.out.println("customer '" + custname + "' created"); | |
showAllCostumers(); | |
checkCust = false; | |
} catch (CustomerSystemException e) { | |
System.out.println("customer name already exists"); | |
checkCust = true; | |
} | |
} while (checkCust); | |
break; | |
case "7": | |
boolean checkCust1 = false; | |
do { | |
try { | |
System.out.println("enter CUSTOMER ID to remove"); | |
long custid = 0; | |
try{custid = sc.nextLong(); | |
}catch (InputMismatchException e){ | |
System.out.println("Enter numbers only please"); | |
break; | |
} | |
Customer c3 = new Customer(custid, "*******", "*******"); | |
af.removeCustomer(c3); | |
System.out.println("costumer removed"); | |
showAllCostumers(); | |
checkCust1 = false; | |
} catch (CustomerSystemException e) { | |
System.out.println("customer ID doesn't exist"); | |
checkCust1 = true; | |
} | |
} while (checkCust1); | |
break; | |
case "8": | |
boolean checkCust2 = false; | |
do { | |
try { | |
System.out.println("enter CUSTOMER ID to update"); | |
long custid = 0; | |
try{custid = sc.nextLong(); | |
}catch (InputMismatchException e){ | |
System.out.println("Enter numbers only please"); | |
break; | |
} | |
String custname = af.getCustomer(custid).getCustName(); | |
System.out.println("Enter new password:"); | |
String pass = sc.next(); | |
Customer c4 = new Customer(custid, custname, pass); | |
af.updateCustomer(c4); | |
System.out.println("Customer updated"); | |
System.out.println(af.getCustomer(custid)); | |
checkCust2 = false; | |
} catch (NullPointerException e) { | |
checkCust2 = true; | |
} | |
} while (checkCust2); | |
break; | |
case "9": | |
showAllCostumers(); | |
break; | |
case "10": | |
boolean check1 = false; | |
do { | |
System.out.println("enter CUSTOMER ID "); | |
long custid = 0; | |
try{custid = sc.nextLong(); | |
}catch (InputMismatchException e){ | |
System.out.println("Enter numbers only please"); | |
break; | |
} | |
if (af.getCustomer(custid) == null) { | |
check1 = true; | |
} else { | |
System.out.println("Customer ID = " | |
+ af.getCustomer(custid).getId()); | |
System.out.println("Customer NAME = " | |
+ af.getCustomer(custid).getCustName()); | |
System.out.println("Customer PASSWORD = " | |
+ af.getCustomer(custid).getPassword()); | |
check1 = false; | |
} | |
} while (check1); | |
break; | |
default: | |
System.out.println("invalid choice. try again"); | |
break; | |
} | |
} | |
public void showAllCostumers() throws SQLException, | |
ConnectionSystemException, CustomerSystemException { | |
AdminFacade af = new AdminFacade(); | |
af.getAllCustomer(); | |
System.out.println("Customers List"); | |
System.out.println("==================================="); | |
System.out.println("ID" + "\t" + "Customer name " + "\t" + "Password"); | |
System.out.println("-----------------------------------"); | |
for (Customer c : af.getAllCustomer()) | |
System.out.println(c.getId() + "\t" + c.getCustName() + "\t" + "\t" | |
+ c.getPassword()); | |
} | |
public void showAllCompanies() throws SQLException, CompanySystemException, | |
ConnectionSystemException { | |
AdminFacade af = new AdminFacade(); | |
af.getAllCompanies(); | |
System.out.println("Companies List"); | |
System.out | |
.println("==================================================="); | |
System.out.println("ID" + "\t" + "Company name " + "\t" + "Password" | |
+ "\t" + "Email"); | |
System.out | |
.println("--------------------------------------------------"); | |
for (Company c : af.getAllCompanies()) | |
System.out.println(c.getId() + "\t" + c.getCompName() + "\t" + "\t" | |
+ c.getPassword() + "\t" + "\t" + c.getEmail()); | |
} | |
public void doCompanyCommand() throws CompanySystemException, | |
CouponSystemException, SQLException, ParseException, | |
ConnectionSystemException { | |
CouponType type2 = null; | |
switch (userCommand) { | |
case "Q": | |
case "q": | |
timeToQuit = true; | |
System.out.println("thank you bye bye"); | |
system.shutdown(); | |
break; | |
case "1": | |
System.out.println("enter COUPON NAME"); | |
String coupname = sc.next(); | |
System.out.println("enter START DATE of coupon (DD-MM-YYYY)"); | |
startdate = getNewDate(); | |
System.out.println("enter END DATE of coupon (DD-MM-YYYY)"); | |
enddate = getNewDate(); | |
if (startdate.after(enddate)) { | |
System.out | |
.println("coupon start date cannot be after end date"); | |
System.out.println("please type again"); | |
doCompanyCommand(); | |
} | |
System.out.println("enter AMOUNT"); | |
int amount = 0; | |
try{amount = sc.nextInt(); | |
}catch (InputMismatchException e){ | |
System.out.println("Enter numbers only please"); | |
break; | |
} | |
System.out.println("enter COUPON TYPE (by number 1 to 7)"); | |
System.out.println("1 ............ RESTURANS"); | |
System.out.println("2 ............ ELECTRICITY"); | |
System.out.println("3 ............ FOOD"); | |
System.out.println("4 ............ HEALTH"); | |
System.out.println("5 ............ SPORTS"); | |
System.out.println("6 ............ CAMPING"); | |
System.out.println("7 ............ TRAVELLING"); | |
System.out.println("enter choice: "); | |
int type = 0; | |
try{type = sc.nextInt(); | |
}catch (InputMismatchException e){ | |
System.out.println("Enter numbers only please"); | |
break; | |
} | |
if(type<1 || type>7){ | |
System.out.println("Please enter number, to choose coupon type"); | |
doCompanyCommand(); | |
} | |
System.out.println("enter MESSAGE"); | |
String message = sc.next(); | |
System.out.println("enter PRICE"); | |
double price =0d; | |
try{price = sc.nextDouble(); | |
}catch (InputMismatchException e){ | |
System.out.println("Enter numbers please"); | |
break; | |
} | |
System.out.println("enter IMAGE"); | |
String image = sc.next(); | |
switch (type) { | |
case 1: | |
type2 = CouponType.RESTURANS; | |
break; | |
case 2: | |
type2 = CouponType.ELECTRICITY; | |
break; | |
case 3: | |
type2 = CouponType.FOOD; | |
break; | |
case 4: | |
type2 = CouponType.HEALTH; | |
break; | |
case 5: | |
type2 = CouponType.SPORTS; | |
break; | |
case 6: | |
type2 = CouponType.CAMPING; | |
break; | |
case 7: | |
type2 = CouponType.TRAVELLING; | |
break; | |
default: | |
type2 = CouponType.FOOD; | |
break; | |
} | |
Coupon c1 = new Coupon(coupname, startdate, enddate, amount, type2, | |
message, price, image); | |
try { | |
cf.creatCoupon(idCompany, c1); | |
} catch (CouponSystemException e) { | |
System.out.println("Coupon can't be created. Try again"); | |
} | |
break; | |
case "2": | |
boolean checkCoup = false; | |
do { | |
System.out.println("enter COUPON ID to remove"); | |
long compid = 0; | |
try{compid = sc.nextLong(); | |
}catch (InputMismatchException e){ | |
System.out.println("Enter numbers only please"); | |
break; | |
} | |
@SuppressWarnings("deprecation") | |
Coupon c2 = new Coupon(compid, "", new Date(1, 1, 1), new Date( | |
1, 1, 1), 0, CouponType.CAMPING, "", 0, ""); | |
try { | |
cf.removeCoupon(c2); | |
System.out.println("Coupon removed"); | |
checkCoup = false; | |
} catch (CouponSystemException e1) { | |
System.out.println("Coupon Id doesn't exists"); | |
checkCoup = true; | |
} | |
} while (checkCoup); | |
break; | |
case "3": | |
try { | |
System.out.println("enter COUPON ID to update"); | |
long coupid = 0; | |
try{coupid = sc.nextLong(); | |
}catch (InputMismatchException e){ | |
System.out.println("Enter numbers only please"); | |
break; | |
} | |
double price1=0d; | |
int amount1 = 0; | |
Date date1=null; | |
try{ | |
price1 = cf.getCoupon(coupid).getPrice(); | |
amount1 = cf.getCoupon(coupid).getAmount(); | |
date1 = cf.getCoupon(coupid).getEndDate(); | |
}catch(CouponSystemException e){ | |
System.out.println("CouponId is not exist. Try another ID, or see all coupons table (choose 5)"); | |
break; | |
} | |
System.out.println("Do you want to update the PRICE? (Y/N)"); | |
String yes = sc.next(); | |
if (yes.equals("y") || yes.equals("Y")) { | |
System.out.println("Enter new PRICE:"); | |
try{price1 = sc.nextDouble(); | |
}catch (InputMismatchException e){ | |
System.out.println("Enter numbers only please"); | |
break; | |
} | |
} else | |
; | |
System.out.println("Do you want to update the AMOUNT? (Y/N)"); | |
yes = sc.next(); | |
if (yes.equals("y") || yes.equals("Y")) { | |
System.out.println("Enter new Email:"); | |
try{amount1 = sc.nextInt(); | |
}catch (InputMismatchException e){ | |
System.out.println("Enter numbers only please"); | |
break; | |
} | |
} else | |
; | |
System.out | |
.println("Do you want to update the END DATE of the coupon? (Y/N)"); | |
yes = sc.next(); | |
if (yes.equals("y") || yes.equals("Y")) { | |
System.out.println("enter END DATE"); | |
date1 = getNewDate(); | |
Coupon coup2 = new Coupon(coupid, "***", date1, date1, | |
amount1, CouponType.CAMPING, "", price1, ""); | |
cf.updateCoupon(coup2); | |
break; | |
} else | |
; | |
} catch (NullPointerException e) { | |
System.out.println("Coupon didn't updated. Try again"); | |
} | |
case "4": | |
boolean checkCoup1 = false; | |
do { | |
try { | |
System.out.println("enter COUPON ID "); | |
long coupid = 0; | |
try{coupid = sc.nextLong(); | |
}catch (InputMismatchException e){ | |
System.out.println("Enter numbers only please"); | |
break; | |
} | |
System.out.println(cf.getCoupon(coupid)); | |
checkCoup1 = false; | |
} catch (NullPointerException | CouponSystemException e) { | |
System.out.println("coupon Id doesn't exist"); | |
checkCoup1 = true; | |
} | |
} while (checkCoup1); | |
break; | |
case "5": | |
cf.getAllCoupon(idCompany); | |
System.out.println("Coupon List"); | |
System.out | |
.println("================================================================================================="); | |
System.out.printf("%-6s%-12s%-12s%-12s%-10s%-15s%-15s%-10s%-10s%n", | |
"ID", "Title", "Start date", "End date", "Amount", "Type", | |
"Message", "Price", "Image"); | |
System.out | |
.println("-------------------------------------------------------------------------------------------------"); | |
try { | |
for (Coupon c : cf.getAllCoupon(idCompany)) | |
System.out.printf( | |
"%-6s%-12s%-12s%-12s%-10s%-15s%-15s%-10s%-10s%n", | |
c.getId(), c.getTitle(), c.getStartDate(), | |
c.getEndDate(), c.getAmount(), c.getType(), | |
c.getMessage(), c.getPrice(), c.getImage()); | |
} catch (CompanySystemException e) { | |
System.out.println("Coupons table isn't accessble. Try again"); | |
} | |
break; | |
case "6": | |
boolean checkCoup3 = false; | |
do { | |
try { | |
System.out.println("enter COUPON TYPE"); | |
System.out | |
.println("RESTURANS , ELECTRICITY , FOOD , HEALTH , SPORTS , CAMPING , TRAVELLING"); | |
String type1 = sc.next(); | |
cf.getCouponByType(idCompany, CouponType.valueOf(type1)); | |
System.out.println("Coupon List"); | |
System.out | |
.println("================================================================================================="); | |
System.out.printf( | |
"%-6s%-12s%-12s%-12s%-10s%-15s%-15s%-10s%-10s%n", | |
"ID", "Title", "Start date", "End date", "Amount", | |
"Type", "Message", "Price", "Image"); | |
System.out | |
.println("-------------------------------------------------------------------------------------------------"); | |
for (Coupon c : cf.getCouponByType(idCompany, | |
CouponType.valueOf(type1))) | |
System.out | |
.printf("%-6s%-12s%-12s%-12s%-10s%-15s%-15s%-10s%-10s%n", | |
c.getId(), c.getTitle(), | |
c.getStartDate(), c.getEndDate(), | |
c.getAmount(), c.getType(), | |
c.getMessage(), c.getPrice(), | |
c.getImage()); | |
checkCoup3 = false; | |
} catch (IllegalArgumentException e) { | |
System.out.println("Coupon type invalid."); | |
checkCoup3 = true; | |
} catch (CompanySystemException e) { | |
System.out | |
.println("Coupons table isn't accessble. Try again"); | |
} | |
} while (checkCoup3); | |
break; | |
case "7": | |
try { | |
System.out.println("enter END DATE"); | |
Date date2 = getNewDate(); | |
cf.getCouponByDate(idCompany, date2); | |
System.out.println("Coupon List"); | |
System.out | |
.println("================================================================================================="); | |
System.out.printf( | |
"%-6s%-12s%-12s%-12s%-10s%-15s%-15s%-10s%-10s%n", "ID", | |
"Title", "Start date", "End date", "Amount", "Type", | |
"Message", "Price", "Image"); | |
System.out | |
.println("-------------------------------------------------------------------------------------------------"); | |
for (Coupon c : cf.getCouponByDate(idCompany, date2)) | |
System.out.printf( | |
"%-6s%-12s%-12s%-12s%-10s%-15s%-15s%-10s%-10s%n", | |
c.getId(), c.getTitle(), c.getStartDate(), | |
c.getEndDate(), c.getAmount(), c.getType(), | |
c.getMessage(), c.getPrice(), c.getImage()); | |
} catch (InputMismatchException e) { | |
System.out.println("please enter a valid number"); | |
} | |
break; | |
case "8": | |
try { | |
System.out.println("enter PRICE"); | |
double price2 = 0d; | |
try{price2 = sc.nextDouble(); | |
}catch (InputMismatchException e){ | |
System.out.println("Enter numbers only please"); | |
break; | |
} | |
cf.getCouponByPrice(idCompany, price2); | |
System.out.println("Coupon List"); | |
System.out | |
.println("================================================================================================="); | |
System.out.printf( | |
"%-6s%-12s%-12s%-12s%-10s%-15s%-15s%-10s%-10s%n", "ID", | |
"Title", "Start date", "End date", "Amount", "Type", | |
"Message", "Price", "Image"); | |
System.out | |
.println("-------------------------------------------------------------------------------------------------"); | |
for (Coupon c : cf.getCouponByPrice(idCompany, price2)) | |
System.out.printf( | |
"%-6s%-12s%-12s%-12s%-10s%-15s%-15s%-10s%-10s%n", | |
c.getId(), c.getTitle(), c.getStartDate(), | |
c.getEndDate(), c.getAmount(), c.getType(), | |
c.getMessage(), c.getPrice(), c.getImage()); | |
} catch (InputMismatchException e) { | |
System.out.println("please enter a valid number"); | |
break; | |
} | |
break; | |
default: | |
System.out.println("invalid choice. try again"); | |
break; | |
} | |
} | |
public void doCustCommand() throws SQLException, CouponSystemException, | |
ConnectionSystemException, CustomerSystemException { | |
switch (userCommand) { | |
case "Q": | |
case "q": | |
timeToQuit = true; | |
System.out.println("thank you bye bye"); | |
system.shutdown(); | |
break; | |
case "1": | |
boolean custcheck = false; | |
do { | |
try { | |
CouponDBDAO cpdb = new CouponDBDAO(); | |
System.out.println("Coupon List"); | |
System.out | |
.println("================================================================================================="); | |
System.out.printf( | |
"%-6s%-12s%-12s%-12s%-10s%-15s%-15s%-10s%-10s%n", | |
"ID", "Title", "Start date", "End date", "Amount", | |
"Type", "Message", "Price", "Image"); | |
System.out | |
.println("-------------------------------------------------------------------------------------------------"); | |
for (Coupon c : cpdb.getCoupons()) | |
System.out | |
.printf("%-6s%-12s%-12s%-12s%-10s%-15s%-15s%-10s%-10s%n", | |
c.getId(), c.getTitle(), | |
c.getStartDate(), c.getEndDate(), | |
c.getAmount(), c.getType(), | |
c.getMessage(), c.getPrice(), | |
c.getImage()); | |
System.out.println("enter COUPON ID "); | |
long coupid = 0; | |
try{coupid = sc.nextLong(); | |
}catch (InputMismatchException e){ | |
System.out.println("Enter numbers only please"); | |
break; | |
} | |
CouponDBDAO coupdb = new CouponDBDAO(); | |
Coupon c1 = coupdb.getCoupon(coupid); | |
custfacade.purchaseCoupon(c1); | |
System.out.println("Coupon purchased"); | |
custcheck = false; | |
} catch (CouponSystemException e) { | |
System.out.println("Coupon ID doesn't exist"); | |
custcheck = true; | |
} | |
} while (custcheck); | |
break; | |
case "2": | |
custfacade.getPurchaseCoupon(); | |
System.out.println("Coupon List"); | |
System.out | |
.println("================================================================================================="); | |
System.out.printf("%-6s%-12s%-12s%-12s%-10s%-15s%-15s%-10s%-10s%n", | |
"ID", "Title", "Start date", "End date", "Amount", "Type", | |
"Message", "Price", "Image"); | |
System.out | |
.println("-------------------------------------------------------------------------------------------------"); | |
for (Coupon c : custfacade.getPurchaseCoupon()) | |
System.out.printf( | |
"%-6s%-12s%-12s%-12s%-10s%-15s%-15s%-10s%-10s%n", | |
c.getId(), c.getTitle(), c.getStartDate(), | |
c.getEndDate(), c.getAmount(), c.getType(), | |
c.getMessage(), c.getPrice(), c.getImage()); | |
break; | |
case "3": | |
boolean checkCoup3 = false; | |
do { | |
try { | |
System.out.println("enter COUPON TYPE"); | |
System.out | |
.println("RESTURANS , ELECTRICITY , FOOD , HEALTH , SPORTS , CAMPING , TRAVELLING"); | |
String type1 = sc.next(); | |
custfacade.getPurchaseCouponByType(idCustomer, | |
CouponType.valueOf(type1)); | |
System.out.println("Coupon List"); | |
System.out | |
.println("================================================================================================="); | |
System.out.printf( | |
"%-6s%-12s%-12s%-12s%-10s%-15s%-15s%-10s%-10s%n", | |
"ID", "Title", "Start date", "End date", "Amount", | |
"Type", "Message", "Price", "Image"); | |
System.out | |
.println("-------------------------------------------------------------------------------------------------"); | |
System.out | |
.println("-------------------------------------------------------------------------------------------------------"); | |
for (Coupon c : custfacade.getPurchaseCouponByType( | |
idCustomer, CouponType.valueOf(type1))) | |
System.out | |
.printf("%-6s%-12s%-12s%-12s%-10s%-15s%-15s%-10s%-10s%n", | |
c.getId(), c.getTitle(), | |
c.getStartDate(), c.getEndDate(), | |
c.getAmount(), c.getType(), | |
c.getMessage(), c.getPrice(), | |
c.getImage()); | |
checkCoup3 = true; | |
break; | |
} catch (IllegalArgumentException e) { | |
System.out.println("Coupon type invalid."); | |
checkCoup3 = false; | |
} | |
} while (!checkCoup3); | |
break; | |
case "4": | |
try { | |
System.out.println("enter PRICE"); | |
double price2 = 0d; | |
try{price2 = sc.nextDouble(); | |
}catch (InputMismatchException e){ | |
System.out.println("Enter numbers only please"); | |
break; | |
} | |
System.out.println("Coupon List"); | |
System.out | |
.println("================================================================================================="); | |
System.out.printf( | |
"%-6s%-12s%-12s%-12s%-10s%-15s%-15s%-10s%-10s%n", "ID", | |
"Title", "Start date", "End date", "Amount", "Type", | |
"Message", "Price", "Image"); | |
System.out | |
.println("-------------------------------------------------------------------------------------------------"); | |
for (Coupon c : custfacade.getPurchaseCouponByPrice(idCustomer, | |
price2)) | |
System.out.printf( | |
"%-6s%-12s%-12s%-12s%-10s%-15s%-15s%-10s%-10s%n", | |
c.getId(), c.getTitle(), c.getStartDate(), | |
c.getEndDate(), c.getAmount(), c.getType(), | |
c.getMessage(), c.getPrice(), c.getImage()); | |
} catch (InputMismatchException e) { | |
System.out.println("please enter a valid number"); | |
} | |
break; | |
default: | |
System.out.println("invalid choice. try again"); | |
break; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package javabeans; | |
public enum CouponType { | |
RESTURANS , ELECTRICITY , FOOD , HEALTH , SPORTS , CAMPING , TRAVELLING | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package javabeans; | |
import java.util.Collection; | |
public class Customer { | |
private long id; | |
private String custName; | |
private String password; | |
private Collection<Coupon> coupons; | |
public Customer() { | |
super(); | |
} | |
public Customer(long id, String custName, String password) { | |
super(); | |
this.id = id; | |
this.custName = custName; | |
this.password = password; | |
} | |
public Customer(String custName, String password) { | |
super(); | |
this.custName = custName; | |
this.password = password; | |
} | |
public long getId() { | |
return id; | |
} | |
public void setId(long id) { | |
this.id = id; | |
} | |
public String getCustName() { | |
return custName; | |
} | |
public void setCustName(String custName) { | |
this.custName = custName; | |
} | |
public String getPassword() { | |
return password; | |
} | |
public void setPassword(String password) { | |
this.password = password; | |
} | |
public Collection<Coupon> getCoupons() { | |
return coupons; | |
} | |
public void setCoupons(Collection<Coupon> coupons) { | |
this.coupons = coupons; | |
} | |
@Override | |
public String toString() { | |
return "Customer [id=" + id + ", custName=" + custName + ", password=" + password + ", coupons=" + coupons | |
+ "]"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dao; | |
import exceptions.ConnectionSystemException; | |
import exceptions.CouponSystemException; | |
import javabeans.Coupon; | |
import javabeans.Customer; | |
public interface Customer_CouponDAO { | |
void creatCustomer(long customerID, Coupon coupon) throws ConnectionSystemException, CouponSystemException; | |
void removeCustomer(Customer customer) throws ConnectionSystemException, CouponSystemException; | |
Customer getCustomer(long idCustomer) throws ConnectionSystemException, CouponSystemException; | |
void removeCouponJoin(Coupon coupon) throws ConnectionSystemException, CouponSystemException; | |
Coupon getCouponJoin(long idCoupon) throws ConnectionSystemException, CouponSystemException; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dao; | |
import java.sql.Connection; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import exceptions.ConnectionSystemException; | |
import exceptions.CouponSystemException; | |
import javabeans.Coupon; | |
import javabeans.Customer; | |
public class Customer_CouponDBDAO implements Customer_CouponDAO { | |
ConnectionPool pool; | |
@Override | |
public void creatCustomer(long customerID, Coupon coupon) throws ConnectionSystemException, CouponSystemException { | |
Connection con = null; | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "INSERT INTO customer_coupon VALUES(?,?)"; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ps.setLong(1, customerID); | |
ps.setLong(2, coupon.getId()); | |
ps.executeUpdate(); | |
} catch (SQLException e) { | |
throw new CouponSystemException("Customer exist. Try again"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
} | |
@Override | |
public void removeCustomer(Customer customer) throws ConnectionSystemException, CouponSystemException { | |
Connection con = null; | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "DELETE FROM customer_coupon WHERE CUST_ID = ?"; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ps.setLong(1, customer.getId()); | |
ps.executeUpdate(); | |
} catch (SQLException e) { | |
throw new CouponSystemException("Remove action is not successed, try again"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
} | |
@Override | |
public Customer getCustomer(long idCustomer) throws ConnectionSystemException, CouponSystemException { | |
Connection con = null; | |
Customer customer = null; | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "SELECT * FROM customer_coupon WHERE CUST_ID = ?"; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ps.setLong(1, idCustomer); | |
ResultSet rs = ps.executeQuery(); | |
if (rs.next()) { | |
customer = new Customer(); | |
customer.setId(rs.getLong(1)); | |
} else { | |
System.out.println("Customer id no exsist"); | |
} | |
} catch (SQLException e) { | |
throw new CouponSystemException("Could not recieve customer, check parametrs and" | |
+ " try again"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
return customer; | |
} | |
@Override | |
public void removeCouponJoin(Coupon coupon) throws ConnectionSystemException, CouponSystemException { | |
Connection con = null; | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "DELETE FROM customer_coupon WHERE COUPON_ID = ?"; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ps.setLong(1, coupon.getId()); | |
ps.executeUpdate(); | |
} catch (SQLException e) { | |
throw new CouponSystemException("Remove action is not successed."); | |
} finally { | |
pool.returnConnection(con); | |
} | |
} | |
@Override | |
public Coupon getCouponJoin(long idCoupon) throws ConnectionSystemException, CouponSystemException { | |
Connection con = null; | |
Coupon coupon = null; | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "SELECT * FROM customer_coupon WHERE COUPON_ID = ?"; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ps.setLong(1, idCoupon); | |
ResultSet rs = ps.executeQuery(); | |
if (rs.next()) { | |
coupon = new Coupon(); | |
coupon.setId(rs.getLong(1)); | |
} else { | |
System.out.println("Customer id no exsist"); | |
} | |
} catch (SQLException e) { | |
throw new CouponSystemException("Could not recieve coupon, check parametrs and" | |
+ " try again"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
return coupon; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dao; | |
import java.util.Collection; | |
import exceptions.ConnectionSystemException; | |
import exceptions.CustomerSystemException; | |
import javabeans.Coupon; | |
import javabeans.CouponType; | |
import javabeans.Customer; | |
public interface CustomerDAO { | |
void createCustomer(Customer customer) throws CustomerSystemException, ConnectionSystemException; | |
void removeCustomer(Customer customer)throws CustomerSystemException, ConnectionSystemException; | |
void updateCustomer(Customer customer)throws CustomerSystemException, ConnectionSystemException; | |
Customer getCustomer(long idCustomer)throws CustomerSystemException, ConnectionSystemException; | |
Collection<Customer> getAllCustomer()throws CustomerSystemException, ConnectionSystemException; | |
Collection<Coupon> getCoupons(long customerID)throws CustomerSystemException, ConnectionSystemException; | |
Collection<Coupon> getCouponsByType(long idCustomer, CouponType couponType)throws CustomerSystemException, ConnectionSystemException; | |
Collection<Coupon> getCouponsByPrice(long idCustomer, double price)throws CustomerSystemException, ConnectionSystemException; | |
boolean login(String custName,String password)throws CustomerSystemException, ConnectionSystemException; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dao; | |
import java.sql.Connection; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.List; | |
import exceptions.ConnectionSystemException; | |
import exceptions.CustomerSystemException; | |
import javabeans.Coupon; | |
import javabeans.CouponType; | |
import javabeans.Customer; | |
public class CustomerDBDAO implements CustomerDAO { | |
private ConnectionPool pool; | |
@Override | |
public void createCustomer(Customer customer) throws CustomerSystemException, ConnectionSystemException { | |
Connection con = null; | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "INSERT INTO customer (CUST_NAME,PASSWORD) VALUES(?,?)"; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ps.setString(1, customer.getCustName()); | |
ps.setString(2, customer.getPassword()); | |
ps.executeUpdate(); | |
} catch (SQLException e) { | |
throw new CustomerSystemException("customer name already exists"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
} | |
@Override | |
public void removeCustomer(Customer customer) throws CustomerSystemException, ConnectionSystemException { | |
Connection con = null; | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "SELECT * FROM customer WHERE ID = ? "; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ps.setLong(1, customer.getId()); | |
ResultSet rs = ps.executeQuery(); | |
if (!rs.next()) { | |
throw new CustomerSystemException("customer id doesn't exist"); | |
} | |
sql = "DELETE FROM customer WHERE ID = ?"; | |
ps = con.prepareStatement(sql); | |
ps.setLong(1, customer.getId()); | |
ps.executeUpdate(); | |
} catch (SQLException e) { | |
throw new CustomerSystemException("Company id doesn't exist"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
} | |
@Override | |
public void updateCustomer(Customer customer) throws CustomerSystemException, ConnectionSystemException { | |
Connection con = null; | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "SELECT * FROM customer WHERE ID = ? "; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ps.setLong(1, customer.getId()); | |
ResultSet rs = ps.executeQuery(); | |
if (!rs.next()) { | |
throw new CustomerSystemException("custumoer id doesn't exist"); | |
} | |
sql = "UPDATE customer SET PASSWORD = ? WHERE ID = ?"; | |
ps = con.prepareStatement(sql); | |
ps.setString(1, customer.getPassword()); | |
ps.setLong(2, customer.getId()); | |
ps.executeUpdate(); | |
} catch (SQLException e) { | |
throw new CustomerSystemException("Customer can't be updated"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
} | |
@Override | |
public Customer getCustomer(long idCustomer) throws ConnectionSystemException, CustomerSystemException { | |
Connection con = null; | |
Customer customer = null; | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "SELECT * FROM customer WHERE ID = ?"; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ps.setLong(1, idCustomer); | |
ResultSet rs = ps.executeQuery(); | |
if (rs.next()) { | |
customer = new Customer(); | |
customer.setId(rs.getLong(1)); | |
customer.setCustName(rs.getString(2)); | |
customer.setPassword(rs.getString(3)); | |
} else { | |
System.out.println("Customer id no exsist"); | |
} | |
} catch (SQLException e) { | |
throw new CustomerSystemException("Customer parameters failed. Try again"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
return customer; | |
} | |
public Customer getCustomerUser(String user) throws ConnectionSystemException, CustomerSystemException { | |
Connection con = null; | |
Customer customer = null; | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "SELECT * FROM customer WHERE CUST_NAME = ?"; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ps.setString(1, user); | |
ResultSet rs = ps.executeQuery(); | |
if (rs.next()) { | |
customer = new Customer(); | |
customer.setId(rs.getLong(1)); | |
customer.setCustName(rs.getString(2)); | |
customer.setPassword(rs.getString(3)); | |
} else { | |
System.out.println("Customer name no exsist"); | |
} | |
} catch (SQLException e) { | |
throw new CustomerSystemException("Customer parameters failed. Try again"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
return customer; | |
} | |
@Override | |
public Collection<Customer> getAllCustomer() throws ConnectionSystemException, CustomerSystemException { | |
Connection con = null; | |
List<Customer> customers = new ArrayList<>(); | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "SELECT * FROM customer"; | |
Statement st = con.createStatement(); | |
ResultSet rs = st.executeQuery(sql); | |
Customer cust; | |
while (rs.next()) { | |
cust = new Customer(); | |
cust.setId(rs.getLong(1)); | |
cust.setCustName(rs.getString(2)); | |
cust.setPassword(rs.getString(3)); | |
customers.add(cust); | |
} | |
} catch (SQLException e) { | |
throw new CustomerSystemException("Table is availble. Try again"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
return customers; | |
} | |
@Override | |
public Collection<Coupon> getCoupons(long customerID) throws ConnectionSystemException, CustomerSystemException { | |
Connection con = null; | |
List<Coupon> coupons = new ArrayList<>(); | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "SELECT * FROM customer_coupon WHERE CUST_ID = ?"; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ps.setLong(1, customerID); | |
ResultSet rs = ps.executeQuery(); | |
Coupon coupon; | |
while (rs.next()) { | |
String sql1 = "SELECT * FROM coupon WHERE ID = ?"; | |
PreparedStatement ps1 = con.prepareStatement(sql1); | |
ps1.setLong(1, rs.getLong(2)); | |
ResultSet rs1 = ps1.executeQuery(); | |
while (rs1.next()) { | |
coupon = new Coupon(); | |
coupon.setId(rs1.getLong(1)); | |
coupon.setTitle(rs1.getString(2)); | |
coupon.setStartDate(rs1.getDate(3)); | |
coupon.setEndDate(rs1.getDate(4)); | |
coupon.setAmount(rs1.getInt(5)); | |
coupon.setType(CouponType.valueOf(rs1.getString(6))); | |
coupon.setMessage(rs1.getString(7)); | |
coupon.setPrice(rs1.getDouble(8)); | |
coupon.setImage(rs1.getString(9)); | |
coupons.add(coupon); | |
} | |
} | |
} catch (SQLException e) { | |
throw new CustomerSystemException("Coupones is not availble. Try again"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
return coupons; | |
} | |
@Override | |
public Collection<Coupon> getCouponsByType(long customerID, CouponType couponType) | |
throws ConnectionSystemException, CustomerSystemException { | |
Connection con = null; | |
List<Coupon> allCoupons = new ArrayList<>(); | |
List<Coupon> coupons = new ArrayList<>(); | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "SELECT * FROM customer_coupon WHERE CUST_ID =" + customerID; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ResultSet rs = ps.executeQuery(); | |
Coupon coupon; | |
while (rs.next()) { | |
String sql1 = "SELECT * FROM coupon WHERE ID =" + rs.getLong(2); | |
PreparedStatement ps1 = con.prepareStatement(sql1); | |
ResultSet rs1 = ps1.executeQuery(); | |
while (rs1.next()) { | |
coupon = new Coupon(); | |
coupon.setId(rs1.getLong(1)); | |
coupon.setTitle(rs1.getString(2)); | |
coupon.setStartDate(rs1.getDate(3)); | |
coupon.setEndDate(rs1.getDate(4)); | |
coupon.setAmount(rs1.getInt(5)); | |
coupon.setType(CouponType.valueOf(rs1.getString(6))); | |
coupon.setMessage(rs1.getString(7)); | |
coupon.setPrice(rs1.getDouble(8)); | |
coupon.setImage(rs1.getString(9)); | |
allCoupons.add(coupon); | |
} | |
} | |
for (int i = 0; i < allCoupons.size(); i++) { | |
if (allCoupons.get(i).getType() == couponType) { | |
coupons.add(allCoupons.get(i)); | |
} | |
} | |
} catch (SQLException e) { | |
throw new CustomerSystemException("Coupones is not availble. Check coupon type. Try again"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
return coupons; | |
} | |
@Override | |
public Collection<Coupon> getCouponsByPrice(long customerID, double price) | |
throws ConnectionSystemException, CustomerSystemException { | |
Connection con = null; | |
List<Coupon> allCoupons = new ArrayList<>(); | |
List<Coupon> coupons = new ArrayList<>(); | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "SELECT * FROM customer_coupon WHERE CUST_ID =" + customerID; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ResultSet rs = ps.executeQuery(); | |
Coupon coupon; | |
while (rs.next()) { | |
String sql1 = "SELECT * FROM coupon WHERE ID =" + rs.getLong(2); | |
PreparedStatement ps1 = con.prepareStatement(sql1); | |
ResultSet rs1 = ps1.executeQuery(); | |
while (rs1.next()) { | |
coupon = new Coupon(); | |
coupon.setId(rs1.getLong(1)); | |
coupon.setTitle(rs1.getString(2)); | |
coupon.setStartDate(rs1.getDate(3)); | |
coupon.setEndDate(rs1.getDate(4)); | |
coupon.setAmount(rs1.getInt(5)); | |
coupon.setType(CouponType.valueOf(rs1.getString(6))); | |
coupon.setMessage(rs1.getString(7)); | |
coupon.setPrice(rs1.getDouble(8)); | |
coupon.setImage(rs1.getString(9)); | |
allCoupons.add(coupon); | |
} | |
} | |
for (int i = 0; i < allCoupons.size(); i++) { | |
if (allCoupons.get(i).getPrice() <= price) { | |
coupons.add(allCoupons.get(i)); | |
} | |
} | |
} catch (SQLException e) { | |
throw new CustomerSystemException("Coupones is not availble. Check coupon price. Try again"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
return coupons; | |
} | |
@Override | |
public boolean login(String custName, String password) throws ConnectionSystemException, CustomerSystemException { | |
Connection con = null; | |
boolean bool = false; | |
try { | |
pool = ConnectionPool.getInstance(); | |
con = pool.getconnection(); | |
String sql = "SELECT ID FROM customer WHERE cust_name=? AND password=?"; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ps.setString(1, custName); | |
ps.setString(2, password); | |
ResultSet rs = ps.executeQuery(); | |
if (rs.next()) { | |
bool = true; | |
} else { | |
System.out.println("Company login failed"); | |
} | |
} catch (SQLException e) { | |
throw new CustomerSystemException("Company login failed"); | |
} finally { | |
pool.returnConnection(con); | |
} | |
return bool; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package facade; | |
import java.util.Collection; | |
import dao.CouponDBDAO; | |
import dao.CustomerDBDAO; | |
import dao.Customer_CouponDBDAO; | |
import exceptions.ConnectionSystemException; | |
import exceptions.CouponSystemException; | |
import exceptions.CustomerSystemException; | |
import javabeans.Coupon; | |
import javabeans.CouponType; | |
public class CustomerFacade implements CouponClientFacade { | |
private CustomerDBDAO custdb; | |
private CouponDBDAO coupdb; | |
private Customer_CouponDBDAO custcoupdb; | |
private long customerID; | |
private java.util.Date date = new java.util.Date(); | |
public CustomerFacade(long customerID) { | |
this.customerID = customerID; | |
custdb = new CustomerDBDAO(); | |
coupdb = new CouponDBDAO(); | |
custcoupdb = new Customer_CouponDBDAO(); | |
} | |
public void purchaseCoupon(Coupon coupon) throws CouponSystemException, ConnectionSystemException { | |
if (coupon.getAmount() != 0 || coupon.getEndDate().after(date)) { | |
custcoupdb.creatCustomer(customerID, coupon); | |
coupon.setAmount(coupon.getAmount() - 1); | |
coupdb.updateCoupon(coupon); | |
} else { | |
throw new CouponSystemException("The coupon is out of stock or coupon's date expired"); | |
} | |
} | |
public Collection<Coupon> getPurchaseCoupon() throws ConnectionSystemException, CustomerSystemException { | |
return custdb.getCoupons(customerID); | |
} | |
public Collection<Coupon> getPurchaseCouponByType(long idCustomer, CouponType couponType) throws ConnectionSystemException, CustomerSystemException { | |
Collection<Coupon> coupons = custdb.getCouponsByType(idCustomer, couponType); | |
return coupons; | |
} | |
public Collection<Coupon> getPurchaseCouponByPrice(long idCustomer, double price) throws ConnectionSystemException, CustomerSystemException { | |
Collection<Coupon> coupons = custdb.getCouponsByPrice(idCustomer, price); | |
return coupons; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package exceptions; | |
public class CustomerSystemException extends Exception { | |
/** | |
* | |
*/ | |
private static final long serialVersionUID = 1L; | |
public CustomerSystemException() { | |
super(); | |
// TODO Auto-generated constructor stub | |
} | |
public CustomerSystemException(String message, Throwable cause, boolean enableSuppression, | |
boolean writableStackTrace) { | |
super(message, cause, enableSuppression, writableStackTrace); | |
// TODO Auto-generated constructor stub | |
} | |
public CustomerSystemException(String message, Throwable cause) { | |
super(message, cause); | |
// TODO Auto-generated constructor stub | |
} | |
public CustomerSystemException(String message) { | |
super(message); | |
// TODO Auto-generated constructor stub | |
} | |
public CustomerSystemException(Throwable cause) { | |
super(cause); | |
// TODO Auto-generated constructor stub | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package thread; | |
import java.util.Collection; | |
import java.util.Date; | |
import dao.CouponDBDAO; | |
import exceptions.ConnectionSystemException; | |
import exceptions.CouponSystemException; | |
import javabeans.Coupon; | |
public class DailyCouponExpirationTask implements Runnable { | |
private CouponDBDAO dao = new CouponDBDAO(); | |
private boolean quit; | |
private Collection<Coupon> coupons = null; | |
private Date date = null; | |
public DailyCouponExpirationTask() { | |
} | |
public void run() { | |
while (!quit) { | |
System.out.println("start daily check"); | |
date = new Date(); | |
try { | |
coupons = dao.getCoupons(); | |
} catch (CouponSystemException e) { | |
System.out.println("Daily thread failed. Contact us"); | |
} catch (ConnectionSystemException e) { | |
System.out.println("Connection problem. Try again"); | |
} | |
try { | |
Thread.sleep(1000 * 60 * 60 * 24); | |
} catch (InterruptedException e) { | |
System.out.println("Daily remove thread failed. Contact us"); | |
} | |
for (Coupon coupon : coupons) { | |
if (coupon.getEndDate().before(date)) { | |
try { | |
dao.removeCoupon(coupon); | |
} catch (CouponSystemException e) { | |
System.out.println("Daily remove thread failed. Contact us"); | |
} catch (ConnectionSystemException e) { | |
System.out.println("Connection problem. Try again"); | |
}}}}} | |
public void stopTask() { | |
quit = true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package createTableDB; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
public class ProjectTable { | |
/** | |
* Creating tables | |
*/ | |
public static void main(String[] args) { | |
boolean create; | |
create = true ; | |
if (create){ | |
String url = "jdbc:derby://localhost:1527/dbproject_2;create=true"; | |
try(Connection con = DriverManager.getConnection(url);) { | |
System.out.println("connect"); | |
Statement st = con.createStatement(); | |
String sql1 = "CREATE TABLE company(ID BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),COMP_NAME VARCHAR(15) UNIQUE,PASSWORD VARCHAR(15),EMAIL VARCHAR(15),PRIMARY KEY(ID))"; | |
st.executeUpdate(sql1); | |
System.out.println(" company table created"); | |
Thread.sleep(500); | |
String sql2 = "CREATE TABLE customer(ID BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),CUST_NAME VARCHAR(15) UNIQUE,PASSWORD VARCHAR(15),PRIMARY KEY(ID))"; | |
st.executeUpdate(sql2); | |
System.out.println(" customer table created"); | |
Thread.sleep(500); | |
String sql3 = "CREATE TABLE coupon(ID BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),TITLE VARCHAR(15) UNIQUE,START_DATE DATE,END_DATE DATE,AMOUNT INT,TYPE VARCHAR(15),MESSAGE VARCHAR(20),PRICE DOUBLE,IMAGE VARCHAR(30),PRIMARY KEY(ID))"; | |
st.executeUpdate(sql3); | |
System.out.println(" coupon table created"); | |
Thread.sleep(500); | |
String sql4 = "CREATE TABLE customer_coupon(CUST_ID BIGINT,COUPON_ID BIGINT,PRIMARY KEY(CUST_ID,COUPON_ID))"; | |
st.executeUpdate(sql4); | |
System.out.println(" customer_coupon table created"); | |
Thread.sleep(500); | |
String sql5 = "CREATE TABLE company_coupon(COMP_ID BIGINT,COUPON_ID BIGINT,PRIMARY KEY(COMP_ID,COUPON_ID))"; | |
st.executeUpdate(sql5); | |
System.out.println(" company_coupon table created"); | |
Thread.sleep(500); | |
} catch (SQLException | InterruptedException e) { | |
System.out.println("Connection failed. Contact us to fix"); | |
} | |
System.out.println("disconnect"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Core system project - Data Base (JDBC, DAO, DBDAO, Tables), Facades (Project logic), Beans