├── cafe.properties ├── lib ├── jasypt-1.9.2.jar └── mysql-connector-java-5.1.29-bin.jar ├── Screenshots ├── capture1.png ├── capture2.png ├── capture3.png ├── capture4.png ├── capture5.png ├── capture6.png ├── capture7.png ├── capture8.png └── capture9].png ├── bin └── cafeteria │ ├── core │ ├── Order.class │ ├── Customer.class │ └── FoodItem.class │ ├── dao │ ├── FoodDAO.class │ ├── OrderDAO.class │ └── CustomerDAO.class │ ├── ui │ ├── BillingApp.class │ ├── BillingApp$1.class │ ├── BillingApp$2.class │ ├── BillingApp$3.class │ ├── BillingApp$4.class │ ├── BillingApp$5.class │ ├── orders │ │ ├── CartTableModel.class │ │ ├── CheckoutDialog.class │ │ ├── FoodMenuDialog.class │ │ ├── FoodTabelModel.class │ │ ├── CheckoutDialog$1.class │ │ ├── CheckoutDialog$2.class │ │ ├── CheckoutDialog$3.class │ │ ├── CheckoutDialog$4.class │ │ ├── FoodMenuDialog$1.class │ │ ├── FoodMenuDialog$2.class │ │ ├── FoodMenuDialog$3.class │ │ ├── FoodMenuDialog$4.class │ │ ├── OrderHistoryDialog.class │ │ ├── OrderHistoryDialog$1.class │ │ └── OrderHistoryTableModel.class │ └── users │ │ ├── CustomerLoginDialog.class │ │ ├── CustomerLoginDialog$1.class │ │ ├── CustomerLoginDialog$2.class │ │ ├── CustomerLoginDialog$3.class │ │ ├── CustomerSignUpDialog.class │ │ ├── CustomerSignUpDialog$1.class │ │ ├── CustomerSignUpDialog$2.class │ │ ├── CustomerSignUpDialog$3.class │ │ └── CustomerSignUpDialog$4.class │ └── util │ └── PasswordUtils.class ├── .settings ├── org.eclipse.core.resources.prefs └── org.eclipse.jdt.core.prefs ├── .classpath ├── .project ├── src └── cafeteria │ ├── core │ ├── Order.java │ ├── Customer.java │ └── FoodItem.java │ ├── util │ └── PasswordUtils.java │ ├── ui │ ├── orders │ │ ├── OrderHistoryTableModel.java │ │ ├── FoodTabelModel.java │ │ ├── CartTableModel.java │ │ ├── OrderHistoryDialog.java │ │ ├── CheckoutDialog.java │ │ └── FoodMenuDialog.java │ ├── BillingApp.java │ └── users │ │ ├── CustomerLoginDialog.java │ │ └── CustomerSignUpDialog.java │ └── dao │ ├── OrderDAO.java │ ├── CustomerDAO.java │ └── FoodDAO.java ├── README.md └── sql └── table-setup.sql /cafe.properties: -------------------------------------------------------------------------------- 1 | user=root 2 | password=root 3 | dburl=jdbc:mysql://localhost:3306/cafe -------------------------------------------------------------------------------- /lib/jasypt-1.9.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/lib/jasypt-1.9.2.jar -------------------------------------------------------------------------------- /Screenshots/capture1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/Screenshots/capture1.png -------------------------------------------------------------------------------- /Screenshots/capture2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/Screenshots/capture2.png -------------------------------------------------------------------------------- /Screenshots/capture3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/Screenshots/capture3.png -------------------------------------------------------------------------------- /Screenshots/capture4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/Screenshots/capture4.png -------------------------------------------------------------------------------- /Screenshots/capture5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/Screenshots/capture5.png -------------------------------------------------------------------------------- /Screenshots/capture6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/Screenshots/capture6.png -------------------------------------------------------------------------------- /Screenshots/capture7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/Screenshots/capture7.png -------------------------------------------------------------------------------- /Screenshots/capture8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/Screenshots/capture8.png -------------------------------------------------------------------------------- /Screenshots/capture9].png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/Screenshots/capture9].png -------------------------------------------------------------------------------- /bin/cafeteria/core/Order.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/core/Order.class -------------------------------------------------------------------------------- /bin/cafeteria/core/Customer.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/core/Customer.class -------------------------------------------------------------------------------- /bin/cafeteria/core/FoodItem.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/core/FoodItem.class -------------------------------------------------------------------------------- /bin/cafeteria/dao/FoodDAO.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/dao/FoodDAO.class -------------------------------------------------------------------------------- /bin/cafeteria/dao/OrderDAO.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/dao/OrderDAO.class -------------------------------------------------------------------------------- /bin/cafeteria/ui/BillingApp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/ui/BillingApp.class -------------------------------------------------------------------------------- /bin/cafeteria/dao/CustomerDAO.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/dao/CustomerDAO.class -------------------------------------------------------------------------------- /bin/cafeteria/ui/BillingApp$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/ui/BillingApp$1.class -------------------------------------------------------------------------------- /bin/cafeteria/ui/BillingApp$2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/ui/BillingApp$2.class -------------------------------------------------------------------------------- /bin/cafeteria/ui/BillingApp$3.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/ui/BillingApp$3.class -------------------------------------------------------------------------------- /bin/cafeteria/ui/BillingApp$4.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/ui/BillingApp$4.class -------------------------------------------------------------------------------- /bin/cafeteria/ui/BillingApp$5.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/ui/BillingApp$5.class -------------------------------------------------------------------------------- /.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/cafeteria/ui/orders/FoodTabelModel.java=UTF-8 3 | -------------------------------------------------------------------------------- /bin/cafeteria/util/PasswordUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/util/PasswordUtils.class -------------------------------------------------------------------------------- /lib/mysql-connector-java-5.1.29-bin.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/lib/mysql-connector-java-5.1.29-bin.jar -------------------------------------------------------------------------------- /bin/cafeteria/ui/orders/CartTableModel.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/ui/orders/CartTableModel.class -------------------------------------------------------------------------------- /bin/cafeteria/ui/orders/CheckoutDialog.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/ui/orders/CheckoutDialog.class -------------------------------------------------------------------------------- /bin/cafeteria/ui/orders/FoodMenuDialog.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/ui/orders/FoodMenuDialog.class -------------------------------------------------------------------------------- /bin/cafeteria/ui/orders/FoodTabelModel.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/ui/orders/FoodTabelModel.class -------------------------------------------------------------------------------- /bin/cafeteria/ui/orders/CheckoutDialog$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/ui/orders/CheckoutDialog$1.class -------------------------------------------------------------------------------- /bin/cafeteria/ui/orders/CheckoutDialog$2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/ui/orders/CheckoutDialog$2.class -------------------------------------------------------------------------------- /bin/cafeteria/ui/orders/CheckoutDialog$3.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/ui/orders/CheckoutDialog$3.class -------------------------------------------------------------------------------- /bin/cafeteria/ui/orders/CheckoutDialog$4.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/ui/orders/CheckoutDialog$4.class -------------------------------------------------------------------------------- /bin/cafeteria/ui/orders/FoodMenuDialog$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/ui/orders/FoodMenuDialog$1.class -------------------------------------------------------------------------------- /bin/cafeteria/ui/orders/FoodMenuDialog$2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/ui/orders/FoodMenuDialog$2.class -------------------------------------------------------------------------------- /bin/cafeteria/ui/orders/FoodMenuDialog$3.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/ui/orders/FoodMenuDialog$3.class -------------------------------------------------------------------------------- /bin/cafeteria/ui/orders/FoodMenuDialog$4.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/ui/orders/FoodMenuDialog$4.class -------------------------------------------------------------------------------- /bin/cafeteria/ui/orders/OrderHistoryDialog.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/ui/orders/OrderHistoryDialog.class -------------------------------------------------------------------------------- /bin/cafeteria/ui/users/CustomerLoginDialog.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/ui/users/CustomerLoginDialog.class -------------------------------------------------------------------------------- /bin/cafeteria/ui/orders/OrderHistoryDialog$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/ui/orders/OrderHistoryDialog$1.class -------------------------------------------------------------------------------- /bin/cafeteria/ui/users/CustomerLoginDialog$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/ui/users/CustomerLoginDialog$1.class -------------------------------------------------------------------------------- /bin/cafeteria/ui/users/CustomerLoginDialog$2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/ui/users/CustomerLoginDialog$2.class -------------------------------------------------------------------------------- /bin/cafeteria/ui/users/CustomerLoginDialog$3.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/ui/users/CustomerLoginDialog$3.class -------------------------------------------------------------------------------- /bin/cafeteria/ui/users/CustomerSignUpDialog.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/ui/users/CustomerSignUpDialog.class -------------------------------------------------------------------------------- /bin/cafeteria/ui/orders/OrderHistoryTableModel.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/ui/orders/OrderHistoryTableModel.class -------------------------------------------------------------------------------- /bin/cafeteria/ui/users/CustomerSignUpDialog$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/ui/users/CustomerSignUpDialog$1.class -------------------------------------------------------------------------------- /bin/cafeteria/ui/users/CustomerSignUpDialog$2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/ui/users/CustomerSignUpDialog$2.class -------------------------------------------------------------------------------- /bin/cafeteria/ui/users/CustomerSignUpDialog$3.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/ui/users/CustomerSignUpDialog$3.class -------------------------------------------------------------------------------- /bin/cafeteria/ui/users/CustomerSignUpDialog$4.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nirzaf/Cafeteria-Billing-System/HEAD/bin/cafeteria/ui/users/CustomerSignUpDialog$4.class -------------------------------------------------------------------------------- /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Cafe Billing system 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /src/cafeteria/core/Order.java: -------------------------------------------------------------------------------- 1 | package cafeteria.core; 2 | 3 | 4 | public class Order{ 5 | private int orderId; 6 | private int customerId; 7 | private int orderTotal; 8 | 9 | public Order(int id, int cust_id, int total){ 10 | orderId = id; 11 | customerId = cust_id; 12 | orderTotal = total; 13 | } 14 | 15 | public int getOrderId(){ 16 | return orderId; 17 | } 18 | public void setOrderId(int id){ 19 | orderId = id; 20 | } 21 | 22 | public int getCustomerId(){ 23 | return customerId; 24 | } 25 | public void setCustomerId(int id){ 26 | customerId = id; 27 | } 28 | 29 | public int getOrderTotal(){ 30 | return orderTotal; 31 | } 32 | public void setOrderTotal(int total){ 33 | orderTotal = total; 34 | } 35 | 36 | public String toString(){ 37 | return String.format("Order [id= %s, Customer id= %s, Order Total= %s]", 38 | orderId, customerId, orderTotal); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/cafeteria/util/PasswordUtils.java: -------------------------------------------------------------------------------- 1 | package cafeteria.util; 2 | 3 | 4 | import org.jasypt.util.password.StrongPasswordEncryptor; 5 | 6 | public class PasswordUtils { 7 | 8 | private static StrongPasswordEncryptor passwordEncryptor = new StrongPasswordEncryptor(); 9 | 10 | /** 11 | * Encrypts (digests) a password. 12 | * 13 | * @param data - the password to be encrypted. 14 | * @return 15 | */ 16 | public static String encryptPassword(String data) { 17 | 18 | String result = passwordEncryptor.encryptPassword(data); 19 | 20 | return result; 21 | } 22 | 23 | /** 24 | * Checks an unencrypted (plain) password against an encrypted one (a digest) to see if they match. 25 | * 26 | * @param plainText 27 | * @param encryptedPassword 28 | * @return true if passwords match, false if not. 29 | */ 30 | public static boolean checkPassword(String plainText, String encryptedPassword) { 31 | 32 | return passwordEncryptor.checkPassword(plainText, encryptedPassword); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/cafeteria/ui/orders/OrderHistoryTableModel.java: -------------------------------------------------------------------------------- 1 | package cafeteria.ui.orders; 2 | import java.util.List; 3 | 4 | import javax.swing.table.AbstractTableModel; 5 | 6 | import cafeteria.core.Order; 7 | 8 | @SuppressWarnings("serial") 9 | public class OrderHistoryTableModel extends AbstractTableModel { 10 | 11 | private static final int OBJECT_COL = -1; 12 | private static final int ORDER_ID_COL = 0; 13 | private static final int ORDER_TOTAL_COL = 1; 14 | 15 | private String columnNames[] = {"Order Id", "Order Total"}; 16 | 17 | private List orderHistory; 18 | 19 | public OrderHistoryTableModel(List list){ 20 | orderHistory = list; 21 | } 22 | 23 | public int getRowCount(){ 24 | return orderHistory.size(); 25 | } 26 | 27 | public int getColumnCount(){ 28 | return columnNames.length; 29 | } 30 | 31 | public String getColumnName(int col){ 32 | return columnNames[col]; 33 | } 34 | 35 | public Object getValueAt(int row, int col){ 36 | Order temp = orderHistory.get(row); 37 | switch(col){ 38 | case ORDER_ID_COL: 39 | return temp.getOrderId(); 40 | case ORDER_TOTAL_COL: 41 | return temp.getOrderTotal(); 42 | case OBJECT_COL: 43 | return temp; 44 | default: 45 | return temp.getOrderId(); 46 | } 47 | } 48 | 49 | @SuppressWarnings({ "unchecked", "rawtypes" }) 50 | public Class getColumnClass(int col){ 51 | return getValueAt(0, col).getClass(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/cafeteria/core/Customer.java: -------------------------------------------------------------------------------- 1 | package cafeteria.core; 2 | 3 | 4 | 5 | public class Customer { 6 | 7 | private int id; 8 | private String firstName; 9 | private String lastName; 10 | private String email; 11 | private String password; 12 | 13 | public Customer(String fname, String lname, String mail, String pwd){ 14 | firstName = fname; 15 | lastName = lname; 16 | email = mail; 17 | password = pwd; 18 | } 19 | 20 | public Customer(int id, String fname, String lname, String mail, String pwd){ 21 | this(fname, lname, mail, pwd); 22 | this.id = id; 23 | } 24 | 25 | public int getId(){ 26 | return id; 27 | } 28 | public void setId(int id){ 29 | this.id = id; 30 | } 31 | 32 | public String getLastName(){ 33 | return lastName; 34 | } 35 | public void setLastName(String name){ 36 | lastName = name; 37 | } 38 | 39 | public String getFirstName(){ 40 | return firstName; 41 | } 42 | public void setFirstName(String name){ 43 | firstName = name; 44 | } 45 | 46 | public String getEmail(){ 47 | return email; 48 | } 49 | public void setEmail(String mail){ 50 | email = mail; 51 | } 52 | 53 | public String getPassword(){ 54 | return password; 55 | } 56 | public void setPassword(String pwd){ 57 | password = pwd; 58 | } 59 | 60 | public String toString(){ 61 | return String.format("Customer [Id= %s, First name= %s, Last name= %s, E-mail= %s]", 62 | id, firstName, lastName, email); 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Cafeteria Billing System 2 | 3 | *Cafeteria Billing System is a simple desktop application for calculating the bill of food products ordered by the customers and store the order details in database. The objective of this project was just to understand and implementing the concept of databases in Java. It is written is `Java`, along with `Java-Swing`, `jasypt` (for password encryption) and `sql`.* 4 | 5 | ## Running the application 6 | 7 | ### Steps 8 | * Install `MYSQL Server` and `MYSQL Workbench` (not necessary) in your system. 9 | * To create the database, switch to `bin` directory of `MYSQL Server` and execute the following commands: 10 | 11 | ```` 12 | mysql -u user -p -h hostname; 13 | create database cafe; 14 | use cafe; 15 | source table-setup.sql; 16 | ```` 17 | `table-setup.sql` is present in `sql` directory. It is preferred to provide the full path of the sql file in import command (i.e, *source path\table-setup.sql*). 18 | 19 | **Note** 20 | 21 | *`cafe` is the schema name used in the codebase. So, if you want to give your own schema name, then you need to provide that name is `cafe.properties` also.* 22 | 23 | `Cafeteria Billing System` has a few dependencies: 24 | * jasypt 25 | * jdbc-connector 26 | 27 | The above libraries have been provided in the `lib` directory. 28 | 29 | * Import the project in eclipse or any other IDE. 30 | * Add `jasypt` and `mysql-connector` jar files provided in `lib` directory to build path of the project. 31 | * Run src/cafeteria/ui/BillingApp.java. 32 | -------------------------------------------------------------------------------- /src/cafeteria/core/FoodItem.java: -------------------------------------------------------------------------------- 1 | package cafeteria.core; 2 | 3 | 4 | public class FoodItem { 5 | private int productCode; 6 | private String productName; 7 | private String productCategory; 8 | private int price; 9 | private int quantity; 10 | 11 | public FoodItem(int productCode, String productName, String productCategory, int price, int quantity){ 12 | 13 | this.productCode = productCode; 14 | this.productName = productName; 15 | this.productCategory = productCategory; 16 | this.price = price; 17 | this.quantity = quantity; 18 | } 19 | 20 | public int getProductCode(){ 21 | return productCode; 22 | } 23 | 24 | public void setProductCode(int productCode){ 25 | this.productCode = productCode; 26 | } 27 | 28 | public void setQuantity(int quantity){ 29 | this.quantity = quantity; 30 | } 31 | 32 | public int getQuantity(){ 33 | return quantity; 34 | } 35 | 36 | public String getProductName(){ 37 | return productName; 38 | } 39 | 40 | public void setProductName(String productName){ 41 | this.productName = productName; 42 | } 43 | 44 | public String getProductCategory(){ 45 | return productCategory; 46 | } 47 | 48 | public void setProductCateory(String productCateory){ 49 | this.productCategory = productCateory; 50 | } 51 | 52 | public int getPrice(){ 53 | return price; 54 | } 55 | 56 | public void setPrice(int price){ 57 | this.price = price; 58 | } 59 | 60 | @Override 61 | public String toString(){ 62 | return String.format("Product [Code= %s, Name= %s, Category= %s, Price= %s]", 63 | productCode, productName, productCategory, price); 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /src/cafeteria/ui/orders/FoodTabelModel.java: -------------------------------------------------------------------------------- 1 | package cafeteria.ui.orders; 2 | import java.util.List; 3 | 4 | import javax.swing.table.AbstractTableModel; 5 | 6 | import cafeteria.core.FoodItem; 7 | 8 | public class FoodTabelModel extends AbstractTableModel { 9 | 10 | public static final int OBJECT_COL = -1; 11 | private static final int PRODUCT_CODE_COL = 0; 12 | private static final int PRODUCT_NAME_COL = 1; 13 | private static final int PRODUCT_CATEGORY_COL = 2; 14 | private static final int PRICE_COL = 3; 15 | private static final int QUANTITY_COL = 4; 16 | 17 | private String columnNames [] = { "Product Code", "Product Name", "Product Category", 18 | "Price", "Quantity" }; 19 | private List foodItems; 20 | 21 | public FoodTabelModel(List foodItems){ 22 | 23 | this.foodItems = foodItems; 24 | } 25 | 26 | public int getColumnCount(){ 27 | return columnNames.length; 28 | } 29 | 30 | public int getRowCount(){ 31 | return foodItems.size(); 32 | } 33 | 34 | public String getColumnName(int col){ 35 | return columnNames[col]; 36 | } 37 | 38 | public Object getValueAt(int row, int col){ 39 | FoodItem tempFood = foodItems.get(row); 40 | switch(col){ 41 | case PRODUCT_CODE_COL: 42 | return tempFood.getProductCode(); 43 | case PRODUCT_NAME_COL: 44 | return tempFood.getProductName(); 45 | case PRODUCT_CATEGORY_COL: 46 | return tempFood.getProductCategory(); 47 | case PRICE_COL: 48 | return tempFood.getPrice(); 49 | case QUANTITY_COL: 50 | return tempFood.getQuantity(); 51 | case OBJECT_COL: 52 | return tempFood; 53 | default: 54 | return tempFood.getProductName(); 55 | } 56 | } 57 | 58 | public Class getColumnClass(int col){ 59 | return getValueAt(0, col).getClass(); 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /src/cafeteria/ui/orders/CartTableModel.java: -------------------------------------------------------------------------------- 1 | package cafeteria.ui.orders; 2 | import java.util.List; 3 | 4 | import javax.swing.table.AbstractTableModel; 5 | 6 | import cafeteria.core.FoodItem; 7 | 8 | @SuppressWarnings("serial") 9 | public class CartTableModel extends AbstractTableModel { 10 | 11 | public static final int OBJECT_COL = -1; 12 | private static final int PRODUCT_SERIAL = 0; 13 | private static final int PRODUCT_CODE_COL = 1; 14 | private static final int PRODUCT_NAME_COL = 2; 15 | private static final int QUANTITY_COL = 3; 16 | private static final int PRODUCT_SUB_TOTAL = 4; 17 | 18 | private String columnNames[] = {"Product Serial", "Product Code", "Product Name", "Quantity", "Product SubTotal"}; 19 | 20 | private List cartItems; 21 | 22 | public CartTableModel(List list){ 23 | cartItems = list; 24 | } 25 | 26 | public int getColumnCount(){ 27 | return columnNames.length; 28 | } 29 | 30 | public int getRowCount(){ 31 | return cartItems.size(); 32 | } 33 | 34 | public String getColumnName(int col){ 35 | return columnNames[col]; 36 | } 37 | 38 | public Object getValueAt(int row, int col){ 39 | FoodItem tempFood = cartItems.get(row); 40 | switch(col){ 41 | case PRODUCT_SERIAL: 42 | return row+1; 43 | case PRODUCT_CODE_COL: 44 | return tempFood.getProductCode(); 45 | case PRODUCT_NAME_COL: 46 | return tempFood.getProductName(); 47 | case QUANTITY_COL: 48 | return tempFood.getQuantity(); 49 | case PRODUCT_SUB_TOTAL: 50 | return tempFood.getQuantity() * tempFood.getPrice(); 51 | case OBJECT_COL: 52 | return tempFood; 53 | default: 54 | return tempFood.getProductName(); 55 | } 56 | } 57 | 58 | @SuppressWarnings({ "unchecked", "rawtypes" }) 59 | public Class getColumnClass(int col){ 60 | return getValueAt(0, col).getClass(); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/cafeteria/dao/OrderDAO.java: -------------------------------------------------------------------------------- 1 | package cafeteria.dao; 2 | import java.io.FileInputStream; 3 | import java.sql.Connection; 4 | import java.sql.DriverManager; 5 | import java.sql.PreparedStatement; 6 | import java.sql.ResultSet; 7 | import java.sql.SQLException; 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | import java.util.Properties; 11 | 12 | import cafeteria.core.Customer; 13 | import cafeteria.core.Order; 14 | 15 | public class OrderDAO { 16 | 17 | private Connection myConn; 18 | 19 | public OrderDAO() throws Exception{ 20 | 21 | //get db properties 22 | Properties props = new Properties(); 23 | props.load(new FileInputStream("cafe.properties")); 24 | 25 | String user = props.getProperty("user"); 26 | String password = props.getProperty("password"); 27 | String dburl = props.getProperty("dburl"); 28 | 29 | myConn = DriverManager.getConnection(dburl, user, password); 30 | 31 | System.out.println("Order DAO - DB connection succesful to " + dburl); 32 | 33 | } 34 | 35 | private Order convertRowToOrder(ResultSet myRs) throws SQLException{ 36 | int id = myRs.getInt("id"); 37 | int customerId = myRs.getInt("customer_id"); 38 | int orderTotal = myRs.getInt("order_total"); 39 | Order tempOrder = new Order(id, customerId, orderTotal); 40 | return tempOrder; 41 | 42 | } 43 | 44 | public List getOrderHistory(Customer customer) throws SQLException{ 45 | List list = new ArrayList(); 46 | PreparedStatement myStmt = null; 47 | ResultSet myRs = null; 48 | try{ 49 | String query = "select * from orders where orders.customer_id=?"; 50 | myStmt = myConn.prepareStatement(query); 51 | 52 | myStmt.setInt(1, customer.getId()); 53 | myRs = myStmt.executeQuery(); 54 | while(myRs.next()){ 55 | Order temp = convertRowToOrder(myRs); 56 | list.add(temp); 57 | } 58 | return list; 59 | } 60 | finally{ 61 | if(myRs != null) 62 | myRs.close(); 63 | if(myStmt != null) 64 | myStmt.close(); 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/cafeteria/ui/orders/OrderHistoryDialog.java: -------------------------------------------------------------------------------- 1 | package cafeteria.ui.orders; 2 | import java.awt.BorderLayout; 3 | import javax.swing.JButton; 4 | import javax.swing.JDialog; 5 | import javax.swing.JPanel; 6 | import javax.swing.table.DefaultTableCellRenderer; 7 | 8 | import cafeteria.core.Customer; 9 | import cafeteria.core.Order; 10 | import cafeteria.dao.OrderDAO; 11 | import cafeteria.ui.BillingApp; 12 | 13 | import javax.swing.JLabel; 14 | import java.awt.Font; 15 | import java.sql.SQLException; 16 | import java.util.List; 17 | 18 | import javax.swing.SwingConstants; 19 | import javax.swing.JScrollPane; 20 | import javax.swing.JTable; 21 | import java.awt.event.ActionListener; 22 | import java.awt.event.ActionEvent; 23 | 24 | @SuppressWarnings("serial") 25 | public class OrderHistoryDialog extends JDialog { 26 | private BillingApp frame; 27 | 28 | private OrderDAO orderDAO; 29 | private JTable orderHistoryTable; 30 | private Customer customer; 31 | 32 | private JPanel buttonPanel; 33 | 34 | public OrderHistoryDialog(BillingApp frame, OrderDAO orderDAO, Customer customer){ 35 | this(); 36 | this.frame = frame; 37 | this.orderDAO = orderDAO; 38 | this.customer = customer; 39 | } 40 | 41 | public OrderHistoryDialog() { 42 | setTitle("Hungry Hobbit Cafeteria"); 43 | setBounds(100, 100, 450, 300); 44 | getContentPane().setLayout(new BorderLayout()); 45 | 46 | JLabel lblPastOrderDetails = new JLabel("Past Order Details"); 47 | lblPastOrderDetails.setHorizontalAlignment(SwingConstants.CENTER); 48 | lblPastOrderDetails.setFont(new Font("Sylfaen", Font.BOLD, 18)); 49 | getContentPane().add(lblPastOrderDetails, BorderLayout.NORTH); 50 | 51 | JScrollPane scrollPane = new JScrollPane(); 52 | getContentPane().add(scrollPane, BorderLayout.CENTER); 53 | 54 | orderHistoryTable = new JTable(); 55 | scrollPane.setViewportView(orderHistoryTable); 56 | 57 | buttonPanel = new JPanel(); 58 | getContentPane().add(buttonPanel, BorderLayout.SOUTH); 59 | 60 | JButton btnBack = new JButton("Back"); 61 | btnBack.addActionListener(new ActionListener() { 62 | public void actionPerformed(ActionEvent arg0) { 63 | dispose(); 64 | frame.setVisible(true); 65 | 66 | } 67 | }); 68 | buttonPanel.add(btnBack); 69 | } 70 | 71 | public void setTableModel() throws SQLException{ 72 | List list = orderDAO.getOrderHistory(customer); 73 | OrderHistoryTableModel tableModel = new OrderHistoryTableModel(list); 74 | orderHistoryTable.setModel(tableModel); 75 | alignTable(); 76 | } 77 | 78 | private void alignTable(){ 79 | DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer(); 80 | centerRenderer.setHorizontalAlignment( JLabel.CENTER ); 81 | for(int i=0; i< orderHistoryTable.getColumnCount(); i++){ 82 | orderHistoryTable.getColumnModel().getColumn(i).setCellRenderer( centerRenderer ); 83 | } 84 | } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /src/cafeteria/dao/CustomerDAO.java: -------------------------------------------------------------------------------- 1 | package cafeteria.dao; 2 | import java.io.FileInputStream; 3 | import java.sql.Connection; 4 | import java.sql.DriverManager; 5 | import java.sql.PreparedStatement; 6 | import java.sql.ResultSet; 7 | import java.sql.SQLException; 8 | import java.util.Properties; 9 | 10 | import cafeteria.core.Customer; 11 | import cafeteria.util.PasswordUtils; 12 | 13 | public class CustomerDAO { 14 | 15 | private Connection myConn; 16 | 17 | public CustomerDAO() throws Exception{ 18 | 19 | //get db properties 20 | Properties props = new Properties(); 21 | props.load(new FileInputStream("cafe.properties")); 22 | 23 | String user = props.getProperty("user"); 24 | String password = props.getProperty("password"); 25 | String dburl = props.getProperty("dburl"); 26 | 27 | myConn = DriverManager.getConnection(dburl, user, password); 28 | 29 | System.out.println("Customer DAO - DB connection succesful to " + dburl); 30 | 31 | } 32 | 33 | public void addCustomer(Customer customer) throws SQLException { 34 | PreparedStatement myStmt = null; 35 | try{ 36 | 37 | myStmt = myConn.prepareStatement("insert into customers" 38 | +"(first_name, last_name, email, password)" 39 | + " values (?, ?, ?, ?)"); 40 | myStmt.setString(1, customer.getFirstName()); 41 | myStmt.setString(2, customer.getLastName()); 42 | myStmt.setString(3, customer.getEmail()); 43 | 44 | String encryptedPassword = PasswordUtils.encryptPassword(customer.getPassword()); 45 | myStmt.setString(4, encryptedPassword); 46 | 47 | myStmt.executeUpdate(); 48 | } 49 | finally{ 50 | if(myStmt!= null) 51 | myStmt.close(); 52 | } 53 | } 54 | 55 | public boolean authenticate(String plainTextPassword, Customer customer){ 56 | String encryptedPassword = customer.getPassword(); 57 | return PasswordUtils.checkPassword(plainTextPassword, encryptedPassword); 58 | } 59 | 60 | public Customer searchCustomer(String email) throws SQLException{ 61 | PreparedStatement myStmt = null; 62 | ResultSet myRs = null; 63 | try{ 64 | 65 | myStmt = myConn.prepareStatement("select * from customers where email=?"); 66 | myStmt.setString(1, email); 67 | myRs = myStmt.executeQuery(); 68 | 69 | //Statement.executeQuery() never returns null if resultset is empty. 70 | if(!myRs.next()){ 71 | return null; 72 | } 73 | else{ 74 | Customer customer = convertRowToCustomer(myRs); 75 | return customer; 76 | } 77 | } 78 | finally{ 79 | if(myRs!= null) 80 | myRs.close(); 81 | if(myStmt!= null) 82 | myStmt.close(); 83 | } 84 | } 85 | 86 | private Customer convertRowToCustomer(ResultSet myRs) throws SQLException{ 87 | int id = myRs.getInt("id"); 88 | String firstName = myRs.getString("first_name"); 89 | String lastName = myRs.getString("last_name"); 90 | String email = myRs.getString("email"); 91 | String encryptedPassword = myRs.getString("password"); 92 | Customer customer = new Customer(id, firstName, lastName, email, encryptedPassword); 93 | 94 | return customer; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /sql/table-setup.sql: -------------------------------------------------------------------------------- 1 | -- MySQL dump 10.13 Distrib 5.7.12, for Win64 (x86_64) 2 | -- 3 | -- Host: localhost Database: cafe 4 | -- ------------------------------------------------------ 5 | -- Server version 5.7.17-log 6 | 7 | /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; 8 | /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; 9 | /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; 10 | /*!40101 SET NAMES utf8 */; 11 | /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; 12 | /*!40103 SET TIME_ZONE='+00:00' */; 13 | /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; 14 | /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; 15 | /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; 16 | /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; 17 | 18 | -- 19 | -- Table structure for table `customers` 20 | -- 21 | 22 | DROP TABLE IF EXISTS `customers`; 23 | /*!40101 SET @saved_cs_client = @@character_set_client */; 24 | /*!40101 SET character_set_client = utf8 */; 25 | CREATE TABLE `customers` ( 26 | `id` int(11) NOT NULL AUTO_INCREMENT, 27 | `first_name` varchar(45) NOT NULL, 28 | `last_name` varchar(45) NOT NULL, 29 | `email` varchar(45) NOT NULL, 30 | `password` varchar(65) NOT NULL, 31 | PRIMARY KEY (`id`) 32 | ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; 33 | /*!40101 SET character_set_client = @saved_cs_client */; 34 | 35 | -- 36 | -- Dumping data for table `customers` 37 | -- 38 | 39 | LOCK TABLES `customers` WRITE; 40 | /*!40000 ALTER TABLE `customers` DISABLE KEYS */; 41 | INSERT INTO `customers` VALUES (1,'Travis','Roberts','trb.001@gmail.com','J/e8EiM0dlHB4oaAbAkDt0xFTs7l7WnzMUm9vDUfcqDW6ag6I3dF1FixnJwIPPt7'),(2,'Johny','Adams','johny.002@gmail.com','yG/oj8AKc9qymz6jpxZUbVdjcHRZzlkoWL3Sqw4hLOWrZ6UCJzTNuAGpNBL6dKvS'); 42 | /*!40000 ALTER TABLE `customers` ENABLE KEYS */; 43 | UNLOCK TABLES; 44 | 45 | -- 46 | -- Table structure for table `food` 47 | -- 48 | 49 | DROP TABLE IF EXISTS `food`; 50 | /*!40101 SET @saved_cs_client = @@character_set_client */; 51 | /*!40101 SET character_set_client = utf8 */; 52 | CREATE TABLE `food` ( 53 | `product_code` int(11) NOT NULL AUTO_INCREMENT, 54 | `product_name` varchar(45) NOT NULL, 55 | `product_category` varchar(45) NOT NULL, 56 | `price` int(11) DEFAULT NULL, 57 | `quantity` int(11) DEFAULT NULL, 58 | PRIMARY KEY (`product_code`) 59 | ) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8; 60 | /*!40101 SET character_set_client = @saved_cs_client */; 61 | 62 | -- 63 | -- Dumping data for table `food` 64 | -- 65 | 66 | LOCK TABLES `food` WRITE; 67 | /*!40000 ALTER TABLE `food` DISABLE KEYS */; 68 | INSERT INTO `food` VALUES (1,'Veg Crunchy Burger','food',88,NULL),(2,'Crunchy Chicken Burger','food',105,NULL),(3,'Garlic Bread','food',75,NULL),(4,'Tacos','food',70,NULL),(5,'Veg Sandwitch','food',110,NULL),(6,'Chicken Sandwitch','food',130,NULL),(7,'Crispy Veg Wrap','food',95,NULL),(8,'Cookies','Desserts',60,NULL),(9,'Fudge Brownie','Desserts',180,NULL),(10,'Cocoa Fantasy Cake','Desserts',220,NULL),(11,'Cafe Frappe','Beverages',145,NULL),(12,'Cappicuno','Beverages',110,NULL),(13,'Cafe Mocha','Beverages',160,NULL),(14,'Lemonade','Beverages',100,NULL),(15,'Espresso','Beverages',130,NULL),(16,'Ice Tea','Beverages',80,NULL); 69 | /*!40000 ALTER TABLE `food` ENABLE KEYS */; 70 | UNLOCK TABLES; 71 | 72 | -- 73 | -- Table structure for table `orders` 74 | -- 75 | 76 | DROP TABLE IF EXISTS `orders`; 77 | /*!40101 SET @saved_cs_client = @@character_set_client */; 78 | /*!40101 SET character_set_client = utf8 */; 79 | CREATE TABLE `orders` ( 80 | `id` int(11) NOT NULL AUTO_INCREMENT, 81 | `customer_id` int(11) NOT NULL, 82 | `order_total` int(11) NOT NULL, 83 | PRIMARY KEY (`id`), 84 | KEY `fk1_idx` (`customer_id`), 85 | CONSTRAINT `fk1` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION 86 | ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; 87 | /*!40101 SET character_set_client = @saved_cs_client */; 88 | 89 | -- 90 | -- Dumping data for table `orders` 91 | -- 92 | 93 | LOCK TABLES `orders` WRITE; 94 | /*!40000 ALTER TABLE `orders` DISABLE KEYS */; 95 | INSERT INTO `orders` VALUES (1,1,356),(2,1,350),(3,2,740),(4,2,670),(5,1,485); 96 | /*!40000 ALTER TABLE `orders` ENABLE KEYS */; 97 | UNLOCK TABLES; 98 | /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; 99 | 100 | /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; 101 | /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; 102 | /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; 103 | /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; 104 | /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; 105 | /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; 106 | /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; 107 | 108 | -- Dump completed on 2017-03-05 20:59:06 109 | -------------------------------------------------------------------------------- /src/cafeteria/ui/BillingApp.java: -------------------------------------------------------------------------------- 1 | package cafeteria.ui; 2 | import java.awt.EventQueue; 3 | 4 | import javax.swing.JFrame; 5 | import javax.swing.JPanel; 6 | import javax.swing.border.EmptyBorder; 7 | 8 | import cafeteria.core.Customer; 9 | import cafeteria.dao.CustomerDAO; 10 | import cafeteria.dao.FoodDAO; 11 | import cafeteria.dao.OrderDAO; 12 | import cafeteria.ui.orders.FoodMenuDialog; 13 | import cafeteria.ui.orders.OrderHistoryDialog; 14 | import cafeteria.ui.users.CustomerLoginDialog; 15 | 16 | import javax.swing.JLabel; 17 | import javax.swing.JOptionPane; 18 | import javax.swing.SwingConstants; 19 | import java.awt.Font; 20 | import javax.swing.JButton; 21 | import javax.swing.JDialog; 22 | 23 | import java.awt.event.ActionListener; 24 | import java.awt.event.WindowAdapter; 25 | import java.awt.event.WindowEvent; 26 | import java.sql.SQLException; 27 | import java.awt.event.ActionEvent; 28 | 29 | @SuppressWarnings("serial") 30 | public class BillingApp extends JFrame { 31 | 32 | private CustomerLoginDialog customerLoginDialog; 33 | 34 | private JPanel contentPane; 35 | private JButton btnOrderMenu; 36 | private JButton btnNewButton; 37 | 38 | private FoodDAO foodDAO; 39 | private OrderDAO orderDAO; 40 | 41 | private Customer customer; 42 | 43 | public BillingApp(CustomerLoginDialog customerLoginDialog, OrderDAO orderDAO, FoodDAO foodDAO, Customer customer) { 44 | 45 | this.customerLoginDialog = customerLoginDialog; 46 | this.orderDAO= orderDAO; 47 | this.foodDAO = foodDAO; 48 | this.customer = customer; 49 | System.out.println("Logged in as "+ this.customer); 50 | addWindowListener(new WindowAdapter() { 51 | 52 | @Override 53 | public void windowClosing(WindowEvent we) 54 | { 55 | int PromptResult = JOptionPane.showConfirmDialog(null, "Exit application ?", 56 | "Confirm exit", JOptionPane.OK_CANCEL_OPTION); 57 | if(PromptResult== JOptionPane.OK_OPTION) 58 | { 59 | System.exit(0); 60 | } 61 | } 62 | }); 63 | 64 | setTitle("Hungry Hobbit Cafeteria"); 65 | setBounds(100, 100, 550, 380); 66 | setResizable(false); 67 | contentPane = new JPanel(); 68 | contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 69 | setContentPane(contentPane); 70 | contentPane.setLayout(null); 71 | 72 | JLabel lblWelcomeToCafe = new JLabel("WELCOME TO CAFE BILLING SYSTEM"); 73 | lblWelcomeToCafe.setFont(new Font("Tahoma", Font.BOLD, 16)); 74 | lblWelcomeToCafe.setHorizontalAlignment(SwingConstants.CENTER); 75 | lblWelcomeToCafe.setBounds(75, 22, 335, 88); 76 | contentPane.add(lblWelcomeToCafe); 77 | 78 | btnOrderMenu = new JButton("ORDER MENU"); 79 | btnOrderMenu.addActionListener(new ActionListener() { 80 | public void actionPerformed(ActionEvent arg0) { 81 | 82 | //create the FoodMenuDialog and pass current dialog reference to set Visible it later 83 | FoodMenuDialog dialog = new FoodMenuDialog(BillingApp.this, foodDAO, customer); 84 | 85 | //dissolve the current dialog. 86 | dispose(); 87 | dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); 88 | dialog.setVisible(true); 89 | 90 | 91 | } 92 | }); 93 | btnOrderMenu.setFont(new Font("Tahoma", Font.BOLD, 14)); 94 | btnOrderMenu.setBounds(44, 121, 143, 67); 95 | contentPane.add(btnOrderMenu); 96 | 97 | btnNewButton = new JButton("VIEW HISTORY"); 98 | btnNewButton.addActionListener(new ActionListener() { 99 | public void actionPerformed(ActionEvent arg0) { 100 | 101 | displayOrderHistoryDialog(); 102 | } 103 | }); 104 | btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 14)); 105 | btnNewButton.setBounds(249, 121, 161, 67); 106 | contentPane.add(btnNewButton); 107 | 108 | JButton btnLogOut = new JButton("Log Out"); 109 | btnLogOut.addActionListener(new ActionListener() { 110 | public void actionPerformed(ActionEvent arg0) { 111 | dispose(); 112 | customerLoginDialog.setVisible(true); 113 | System.out.println("Customer logged out."); 114 | } 115 | }); 116 | btnLogOut.setBounds(455, 0, 89, 25); 117 | contentPane.add(btnLogOut); 118 | 119 | JLabel customerLabel = new JLabel(); 120 | customerLabel.setFont(new Font("Myriad Pro", Font.PLAIN, 14)); 121 | customerLabel.setBounds(10, 5, 250, 14); 122 | 123 | String firstName = customer.getFirstName(); 124 | String lastName = customer.getLastName(); 125 | customerLabel.setText("Logged in as: " + firstName + " " + lastName); 126 | contentPane.add(customerLabel); 127 | } 128 | 129 | private void displayOrderHistoryDialog(){ 130 | OrderHistoryDialog dialog = new OrderHistoryDialog(BillingApp.this, orderDAO, customer); 131 | dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 132 | try { 133 | dialog.setTableModel(); 134 | dialog.setVisible(true); 135 | } 136 | catch (SQLException e) { 137 | JOptionPane.showMessageDialog(BillingApp.this, "Error retrieving Order History", 138 | "Error", JOptionPane.ERROR_MESSAGE); 139 | e.printStackTrace(); 140 | } 141 | } 142 | 143 | public static void main(String[] args) { 144 | EventQueue.invokeLater(new Runnable() { 145 | public void run() { 146 | try { 147 | 148 | CustomerDAO customerDAO = new CustomerDAO(); 149 | FoodDAO foodDAO = new FoodDAO(); 150 | OrderDAO orderDAO = new OrderDAO(); 151 | 152 | CustomerLoginDialog dialog = new CustomerLoginDialog(customerDAO, foodDAO, orderDAO); 153 | dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); 154 | dialog.setVisible(true); 155 | 156 | } catch (Exception e) { 157 | e.printStackTrace(); 158 | } 159 | } 160 | }); 161 | } 162 | } 163 | -------------------------------------------------------------------------------- /src/cafeteria/ui/users/CustomerLoginDialog.java: -------------------------------------------------------------------------------- 1 | package cafeteria.ui.users; 2 | import java.awt.BorderLayout; 3 | import java.awt.FlowLayout; 4 | 5 | import javax.swing.JButton; 6 | import javax.swing.JDialog; 7 | import javax.swing.JPanel; 8 | import javax.swing.border.EmptyBorder; 9 | 10 | import cafeteria.core.Customer; 11 | import cafeteria.dao.CustomerDAO; 12 | import cafeteria.dao.FoodDAO; 13 | import cafeteria.dao.OrderDAO; 14 | import cafeteria.ui.BillingApp; 15 | 16 | import javax.swing.JTextField; 17 | import javax.swing.JLabel; 18 | import javax.swing.JOptionPane; 19 | 20 | import java.awt.Font; 21 | import java.awt.event.ActionListener; 22 | import java.awt.event.WindowAdapter; 23 | import java.awt.event.WindowEvent; 24 | import java.sql.SQLException; 25 | import java.awt.event.ActionEvent; 26 | import javax.swing.JPasswordField; 27 | 28 | 29 | @SuppressWarnings("serial") 30 | public class CustomerLoginDialog extends JDialog { 31 | 32 | private FoodDAO foodDAO; 33 | private CustomerDAO customerDAO; 34 | private OrderDAO orderDAO; 35 | 36 | private final JPanel contentPanel = new JPanel(); 37 | private JLabel lblWelcomeToCafeteria; 38 | private JPanel credentialpanel; 39 | private JTextField emailTextField; 40 | private JPasswordField passwordField; 41 | 42 | public CustomerLoginDialog(CustomerDAO customerDAO, FoodDAO foodDAO, OrderDAO orderDAO){ 43 | this(); 44 | this.customerDAO = customerDAO; 45 | this.foodDAO = foodDAO; 46 | this.orderDAO = orderDAO; 47 | 48 | addWindowListener(new WindowAdapter() { 49 | 50 | @Override 51 | public void windowClosing(WindowEvent we) 52 | { 53 | int PromptResult = JOptionPane.showConfirmDialog(null, "Exit application ?", 54 | "Confirm exit", JOptionPane.OK_CANCEL_OPTION); 55 | if(PromptResult== JOptionPane.OK_OPTION) 56 | { 57 | System.exit(0); 58 | } 59 | } 60 | }); 61 | 62 | } 63 | 64 | public CustomerLoginDialog() { 65 | //this.setResizable(false); 66 | 67 | setTitle("Hungry Hobbit Cafeteria - Log In"); 68 | setBounds(100, 100, 450, 300); 69 | getContentPane().setLayout(new BorderLayout()); 70 | contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); 71 | getContentPane().add(contentPanel, BorderLayout.CENTER); 72 | contentPanel.setLayout(new BorderLayout(0, 0)); 73 | { 74 | JPanel lblpanel = new JPanel(); 75 | FlowLayout flowLayout = (FlowLayout) lblpanel.getLayout(); 76 | flowLayout.setVgap(10); 77 | contentPanel.add(lblpanel, BorderLayout.NORTH); 78 | { 79 | lblWelcomeToCafeteria = new JLabel("Welcome to Cafeteria Billing System"); 80 | lblWelcomeToCafeteria.setFont(new Font("Sylfaen", Font.BOLD, 16)); 81 | lblpanel.add(lblWelcomeToCafeteria); 82 | } 83 | } 84 | { 85 | credentialpanel = new JPanel(); 86 | contentPanel.add(credentialpanel, BorderLayout.CENTER); 87 | credentialpanel.setLayout(null); 88 | 89 | JLabel lblEmail = new JLabel("E-mail"); 90 | lblEmail.setFont(new Font("Sylfaen", Font.PLAIN, 15)); 91 | lblEmail.setBounds(21, 19, 46, 14); 92 | credentialpanel.add(lblEmail); 93 | 94 | emailTextField = new JTextField(); 95 | emailTextField.setBounds(86, 16, 180, 20); 96 | credentialpanel.add(emailTextField); 97 | emailTextField.setColumns(30); 98 | 99 | JLabel lblPassword = new JLabel("Password"); 100 | lblPassword.setFont(new Font("Sylfaen", Font.PLAIN, 15)); 101 | lblPassword.setBounds(10, 50, 70, 14); 102 | credentialpanel.add(lblPassword); 103 | 104 | JButton btnLogIn = new JButton("Log in"); 105 | btnLogIn.addActionListener(new ActionListener() { 106 | public void actionPerformed(ActionEvent e) { 107 | 108 | performLogin(); 109 | } 110 | }); 111 | btnLogIn.setBounds(177, 78, 89, 23); 112 | credentialpanel.add(btnLogIn); 113 | 114 | JLabel lblNewUser = new JLabel("New user ?"); 115 | lblNewUser.setFont(new Font("Sylfaen", Font.PLAIN, 15)); 116 | lblNewUser.setBounds(34, 121, 80, 14); 117 | credentialpanel.add(lblNewUser); 118 | 119 | JButton btnSignUp = new JButton("Sign up"); 120 | btnSignUp.addActionListener(new ActionListener() { 121 | public void actionPerformed(ActionEvent e) { 122 | CustomerSignUpDialog dialog = new CustomerSignUpDialog(CustomerLoginDialog.this, customerDAO); 123 | dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); 124 | 125 | //dissolve current dialog and create new dialog 126 | dispose(); 127 | //setVisible(false); can use this also but dispose() is preferred to release memory 128 | dialog.setVisible(true); 129 | 130 | } 131 | }); 132 | btnSignUp.setBounds(110, 117, 89, 23); 133 | credentialpanel.add(btnSignUp); 134 | 135 | passwordField = new JPasswordField(); 136 | passwordField.setBounds(86, 47, 180, 20); 137 | credentialpanel.add(passwordField); 138 | 139 | } 140 | } 141 | 142 | private void performLogin(){ 143 | String email = emailTextField.getText(); 144 | String plainTextPassword = new String(passwordField.getPassword()); 145 | 146 | try { 147 | Customer customer = customerDAO.searchCustomer(email); //if not NULL, customer records found in database 148 | if(customer == null){ 149 | JOptionPane.showMessageDialog(CustomerLoginDialog.this, "Customer not found", "OOPS!", 150 | JOptionPane.INFORMATION_MESSAGE); 151 | return; 152 | } 153 | 154 | //Authentication check 155 | boolean check = customerDAO.authenticate(plainTextPassword, customer); 156 | if(check){ 157 | System.out.println("Customer authenticated"); 158 | emailTextField.setText(""); 159 | passwordField.setText(""); 160 | BillingApp frame = new BillingApp(CustomerLoginDialog.this, orderDAO, foodDAO, customer); 161 | frame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);; 162 | dispose(); 163 | frame.setVisible(true); 164 | } 165 | else{ 166 | JOptionPane.showMessageDialog(CustomerLoginDialog.this, "Invalid password!", "Invalid login", 167 | JOptionPane.ERROR_MESSAGE); 168 | return; 169 | } 170 | } 171 | catch (SQLException e) { 172 | JOptionPane.showMessageDialog(CustomerLoginDialog.this, "Error logging in: " 173 | + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); 174 | } 175 | } 176 | } 177 | -------------------------------------------------------------------------------- /src/cafeteria/dao/FoodDAO.java: -------------------------------------------------------------------------------- 1 | package cafeteria.dao; 2 | import java.io.FileInputStream; 3 | import java.sql.Connection; 4 | import java.sql.DriverManager; 5 | import java.sql.PreparedStatement; 6 | import java.sql.ResultSet; 7 | import java.sql.SQLException; 8 | import java.sql.Statement; 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | import java.util.Properties; 12 | 13 | import cafeteria.core.Customer; 14 | import cafeteria.core.FoodItem; 15 | 16 | public class FoodDAO { 17 | 18 | private Connection myConn; 19 | 20 | public FoodDAO() throws Exception{ 21 | 22 | //get db properties 23 | Properties props = new Properties(); 24 | props.load(new FileInputStream("cafe.properties")); 25 | 26 | String user = props.getProperty("user"); 27 | String password = props.getProperty("password"); 28 | String dburl = props.getProperty("dburl"); 29 | 30 | myConn = DriverManager.getConnection(dburl, user, password); 31 | 32 | System.out.println("Food DAO - DB connection succesful to " + dburl); 33 | 34 | } 35 | 36 | //qantity column keep tracks of the list of items selected by the user. If not null, the corresponding 37 | //food item is selected by the customer. 38 | public void updateQuantity(FoodItem temp, int val) throws SQLException{ 39 | 40 | PreparedStatement myStmt = null; 41 | try{ 42 | myStmt = myConn.prepareStatement("update food set quantity=? where product_code=?"); 43 | myStmt.setInt(1, val); 44 | myStmt.setInt(2, temp.getProductCode()); 45 | myStmt.executeUpdate(); 46 | } 47 | finally{ 48 | if(myStmt != null) 49 | myStmt.close(); 50 | } 51 | 52 | } 53 | 54 | //will be used to delete the item from cart, i.e, set quantity to null & refresh the cart table view. 55 | public void setQuantityToNull(FoodItem foodItem) throws SQLException{ 56 | PreparedStatement myStmt = null; 57 | try{ 58 | myStmt = myConn.prepareStatement("update food set quantity = null where product_code =?"); 59 | myStmt.setInt(1, foodItem.getProductCode()); 60 | myStmt.executeUpdate(); 61 | } 62 | finally{ 63 | if(myStmt != null) 64 | myStmt.close(); 65 | } 66 | } 67 | 68 | public int getNetAmount() throws SQLException{ 69 | Statement myStmt = null; 70 | ResultSet myRs = null; 71 | try{ 72 | myStmt = myConn.createStatement(); 73 | myRs = myStmt.executeQuery("select sum(price*quantity) as sum from food where quantity is not null"); 74 | myRs.next(); 75 | int sum = myRs.getInt("sum"); 76 | return sum; 77 | } 78 | finally{ 79 | if(myRs != null) 80 | myRs.close(); 81 | if(myStmt != null) 82 | myStmt.close(); 83 | } 84 | } 85 | 86 | public int addOrder(Customer customer, int netAmount) throws SQLException{ 87 | PreparedStatement myStmt = null; 88 | Statement myStmt1 = null; 89 | ResultSet myRs = null; 90 | try{ 91 | myStmt = myConn.prepareStatement("insert into orders (customer_id, order_total) " 92 | + "values(?, ?)"); 93 | myStmt.setInt(1, customer.getId()); 94 | myStmt.setInt(2, netAmount); 95 | myStmt.executeUpdate(); 96 | 97 | myStmt1 = myConn.createStatement(); 98 | //get the last auto incremented value (i.e, order id). 99 | myRs = myStmt.executeQuery("select last_insert_id() as order_id"); 100 | myRs.next(); 101 | int orderId = myRs.getInt("order_id"); 102 | System.out.println(orderId); 103 | return orderId; 104 | } 105 | finally{ 106 | if(myStmt != null) 107 | myStmt.close(); 108 | if(myStmt1 != null) 109 | myStmt1.close(); 110 | if(myRs != null) 111 | myRs.close(); 112 | } 113 | 114 | } 115 | 116 | public void vacateQuantityColumn(){ 117 | 118 | Statement myStmt = null; 119 | try { 120 | myStmt = myConn.createStatement(); 121 | myStmt.executeUpdate("update food set quantity= null where quantity is not null"); 122 | } 123 | catch (SQLException e) { 124 | System.out.println("Error while vacating Quantity column."); 125 | e.printStackTrace(); 126 | } 127 | } 128 | 129 | public boolean isEmptyQuantityColumn() throws SQLException{ 130 | ResultSet myRs = null; 131 | Statement myStmt = null; 132 | try { 133 | myStmt = myConn.createStatement(); 134 | myRs = myStmt.executeQuery("select count(quantity) as count from food"); 135 | myRs.next(); 136 | int count = myRs.getInt("count"); 137 | return count > 0 ? true : false ; 138 | } 139 | finally{ 140 | if(myRs != null) 141 | myRs.close(); 142 | if(myStmt != null) 143 | myStmt.close(); 144 | } 145 | } 146 | 147 | private FoodItem convertRowToFoodItem(ResultSet myRs) throws SQLException{ 148 | int code = myRs.getInt("product_code"); 149 | String name = myRs.getString("product_name"); 150 | String category = myRs.getString("product_category"); 151 | int price = myRs.getInt("price"); 152 | int quantity = myRs.getInt("quantity"); 153 | 154 | FoodItem tempFood = new FoodItem(code, name, category, price, quantity); 155 | 156 | return tempFood; 157 | } 158 | 159 | public List getAllFoodItems() throws Exception{ 160 | List list = new ArrayList(); 161 | Statement myStmt = null; 162 | ResultSet myRes = null; 163 | try{ 164 | myStmt = myConn.createStatement(); 165 | myRes = myStmt.executeQuery("select * from food order by product_code"); 166 | 167 | while(myRes.next()){ 168 | FoodItem temp = convertRowToFoodItem(myRes); 169 | list.add(temp); 170 | } 171 | return list; 172 | } 173 | finally{ 174 | if(myRes != null) 175 | myRes.close(); 176 | if(myStmt != null) 177 | myStmt.close(); 178 | } 179 | 180 | } 181 | 182 | public List getCartItems() throws Exception{ 183 | List list = new ArrayList(); 184 | Statement myStmt = null; 185 | ResultSet myRes = null; 186 | try{ 187 | myStmt = myConn.createStatement(); 188 | myRes= myStmt.executeQuery("select * from food where quantity is not null"); 189 | 190 | while(myRes.next()){ 191 | FoodItem temp = convertRowToFoodItem(myRes); 192 | list.add(temp); 193 | } 194 | return list; 195 | } 196 | finally{ 197 | if(myRes != null) 198 | myRes.close(); 199 | if(myStmt != null) 200 | myStmt.close(); 201 | } 202 | } 203 | 204 | public static void main(String [] args){ 205 | try { 206 | FoodDAO foodDAO = new FoodDAO(); 207 | List list = foodDAO.getAllFoodItems(); 208 | for(FoodItem temp : list){ 209 | System.out.println(temp); 210 | } 211 | int totalAmt = foodDAO.getNetAmount(); 212 | System.out.println(totalAmt); 213 | } catch (Exception e) { 214 | // TODO Auto-generated catch block 215 | e.printStackTrace(); 216 | } 217 | } 218 | } 219 | -------------------------------------------------------------------------------- /src/cafeteria/ui/users/CustomerSignUpDialog.java: -------------------------------------------------------------------------------- 1 | package cafeteria.ui.users; 2 | import java.awt.BorderLayout; 3 | import javax.swing.JButton; 4 | import javax.swing.JDialog; 5 | import javax.swing.JPanel; 6 | import javax.swing.border.EmptyBorder; 7 | 8 | import cafeteria.core.Customer; 9 | import cafeteria.dao.CustomerDAO; 10 | 11 | import javax.swing.JLabel; 12 | import javax.swing.JOptionPane; 13 | 14 | import java.awt.Font; 15 | import javax.swing.SwingConstants; 16 | import javax.swing.JTextField; 17 | import javax.swing.JPasswordField; 18 | import java.awt.event.ActionListener; 19 | import java.awt.event.WindowAdapter; 20 | import java.awt.event.WindowEvent; 21 | import java.sql.SQLException; 22 | import java.awt.event.ActionEvent; 23 | 24 | @SuppressWarnings("serial") 25 | public class CustomerSignUpDialog extends JDialog { 26 | 27 | private CustomerLoginDialog loginDialog; 28 | 29 | private CustomerDAO customerDAO; 30 | 31 | private final JPanel contentPanel = new JPanel(); 32 | private JTextField firstNameTextField; 33 | private JTextField lastNameTextField; 34 | private JTextField emailTextField; 35 | private JLabel lblPassword; 36 | private JPasswordField passwordField; 37 | private JPasswordField cnfPasswordField; 38 | private JButton btnCreateAccount; 39 | 40 | public CustomerSignUpDialog(CustomerLoginDialog dialog, CustomerDAO customerDAO){ 41 | this(); 42 | this.customerDAO = customerDAO; 43 | 44 | //It"ll be used to make the dialog visible again 45 | this.loginDialog = dialog; 46 | } 47 | 48 | public CustomerSignUpDialog() { 49 | addWindowListener(new WindowAdapter() { 50 | 51 | @Override 52 | public void windowClosing(WindowEvent we) 53 | { 54 | int PromptResult = JOptionPane.showConfirmDialog(null, "Exit application ?", 55 | "Confirm exit", JOptionPane.OK_CANCEL_OPTION); 56 | if(PromptResult== JOptionPane.OK_OPTION) 57 | { 58 | System.exit(0); 59 | } 60 | } 61 | }); 62 | 63 | setTitle("Hungry Hobbit Cafeteria - Sign Up"); 64 | setBounds(100, 100, 450, 300); 65 | getContentPane().setLayout(new BorderLayout()); 66 | contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); 67 | getContentPane().add(contentPanel, BorderLayout.CENTER); 68 | contentPanel.setLayout(null); 69 | 70 | JLabel lblFirstName = new JLabel("First Name"); 71 | lblFirstName.setHorizontalAlignment(SwingConstants.CENTER); 72 | lblFirstName.setFont(new Font("Tahoma", Font.PLAIN, 12)); 73 | lblFirstName.setBounds(21, 11, 70, 14); 74 | contentPanel.add(lblFirstName); 75 | 76 | 77 | firstNameTextField = new JTextField(); 78 | firstNameTextField.setBounds(112, 9, 190, 20); 79 | contentPanel.add(firstNameTextField); 80 | firstNameTextField.setColumns(10); 81 | 82 | 83 | JLabel lblLastName = new JLabel("Last Name"); 84 | lblLastName.setHorizontalAlignment(SwingConstants.CENTER); 85 | lblLastName.setFont(new Font("Tahoma", Font.PLAIN, 12)); 86 | lblLastName.setBounds(21, 42, 70, 14); 87 | contentPanel.add(lblLastName); 88 | 89 | 90 | lastNameTextField = new JTextField(); 91 | lastNameTextField.setColumns(10); 92 | lastNameTextField.setBounds(112, 40, 190, 20); 93 | contentPanel.add(lastNameTextField); 94 | 95 | 96 | JLabel lblEmail = new JLabel("E-mail"); 97 | lblEmail.setHorizontalAlignment(SwingConstants.CENTER); 98 | lblEmail.setFont(new Font("Tahoma", Font.PLAIN, 12)); 99 | lblEmail.setBounds(21, 73, 70, 14); 100 | contentPanel.add(lblEmail); 101 | 102 | 103 | emailTextField = new JTextField(); 104 | emailTextField.setColumns(10); 105 | emailTextField.setBounds(112, 71, 190, 20); 106 | contentPanel.add(emailTextField); 107 | 108 | lblPassword = new JLabel("Password"); 109 | lblPassword.setHorizontalAlignment(SwingConstants.CENTER); 110 | lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 12)); 111 | lblPassword.setBounds(21, 106, 70, 14); 112 | contentPanel.add(lblPassword); 113 | 114 | 115 | passwordField = new JPasswordField(); 116 | passwordField.setBounds(112, 102, 190, 20); 117 | contentPanel.add(passwordField); 118 | 119 | JLabel lblConfirmPassword = new JLabel("Confirm Password"); 120 | lblConfirmPassword.setHorizontalAlignment(SwingConstants.CENTER); 121 | lblConfirmPassword.setFont(new Font("Tahoma", Font.PLAIN, 12)); 122 | lblConfirmPassword.setBounds(10, 135, 100, 14); 123 | contentPanel.add(lblConfirmPassword); 124 | 125 | cnfPasswordField = new JPasswordField(); 126 | cnfPasswordField.setBounds(112, 133, 190, 20); 127 | contentPanel.add(cnfPasswordField); 128 | 129 | btnCreateAccount = new JButton("Create Account"); 130 | btnCreateAccount.addActionListener(new ActionListener() { 131 | public void actionPerformed(ActionEvent arg0) { 132 | 133 | createCustomer(); 134 | } 135 | }); 136 | btnCreateAccount.setFont(new Font("Lucida Fax", Font.BOLD, 13)); 137 | btnCreateAccount.setBounds(21, 176, 158, 32); 138 | contentPanel.add(btnCreateAccount); 139 | 140 | JButton btnBack = new JButton("Back"); 141 | btnBack.addActionListener(new ActionListener() { 142 | public void actionPerformed(ActionEvent arg0) { 143 | dispose(); 144 | loginDialog.setVisible(true); 145 | } 146 | }); 147 | btnBack.setFont(new Font("Lucida Fax", Font.BOLD, 13)); 148 | btnBack.setBounds(213, 176, 89, 29); 149 | contentPanel.add(btnBack); 150 | 151 | //Adding window event to handle the operations performed as the signUo Dialog is closed 152 | addWindowListener(new WindowAdapter(){ 153 | public void windowClosing(WindowEvent e){ 154 | 155 | //display the login Dialog. 156 | loginDialog.setVisible(true); 157 | } 158 | }); 159 | } 160 | 161 | private void createCustomer(){ 162 | String firstName = firstNameTextField.getText(); 163 | String lastName = lastNameTextField.getText(); 164 | String email = emailTextField.getText(); 165 | String password = new String(passwordField.getPassword()); 166 | String cnfPassword = new String(cnfPasswordField.getPassword()); 167 | 168 | if (!password.equals(cnfPassword)) { 169 | JOptionPane.showMessageDialog(this, 170 | "Passwords do not match.", "Error", 171 | JOptionPane.ERROR_MESSAGE); 172 | return; 173 | } 174 | 175 | Customer customer = new Customer(firstName, lastName, email, password); 176 | 177 | try { 178 | customerDAO.addCustomer(customer); 179 | JOptionPane.showMessageDialog(loginDialog, "Customer created successfully", 180 | "Success!", JOptionPane.INFORMATION_MESSAGE); 181 | setVisible(false); 182 | loginDialog.setVisible(true); 183 | } catch (SQLException e) { 184 | JOptionPane.showMessageDialog(CustomerSignUpDialog.this, 185 | "Error creating account" + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); 186 | } 187 | 188 | } 189 | } 190 | -------------------------------------------------------------------------------- /src/cafeteria/ui/orders/CheckoutDialog.java: -------------------------------------------------------------------------------- 1 | package cafeteria.ui.orders; 2 | import java.awt.BorderLayout; 3 | import javax.swing.JButton; 4 | import javax.swing.JDialog; 5 | import javax.swing.JPanel; 6 | import javax.swing.border.EmptyBorder; 7 | import javax.swing.table.DefaultTableCellRenderer; 8 | 9 | import cafeteria.core.Customer; 10 | import cafeteria.core.FoodItem; 11 | import cafeteria.dao.FoodDAO; 12 | import cafeteria.ui.BillingApp; 13 | 14 | import javax.swing.JLabel; 15 | import javax.swing.JOptionPane; 16 | import javax.swing.SwingConstants; 17 | import java.awt.Font; 18 | import java.awt.Component; 19 | import javax.swing.Box; 20 | import javax.swing.JScrollPane; 21 | import javax.swing.JTable; 22 | import java.sql.SQLException; 23 | import java.awt.event.ActionListener; 24 | import java.awt.event.WindowAdapter; 25 | import java.awt.event.WindowEvent; 26 | import java.awt.event.ActionEvent; 27 | 28 | @SuppressWarnings("serial") 29 | public class CheckoutDialog extends JDialog { 30 | 31 | private FoodMenuDialog foodMenuDialog; 32 | private BillingApp frame; 33 | 34 | private Customer customer; 35 | private FoodDAO foodDAO; 36 | 37 | private final JPanel contentPanel = new JPanel(); 38 | private JTable cartTable; 39 | private JButton btnDelete; 40 | private JButton btnBack; 41 | private JButton btnProceed; 42 | private JLabel netAmountLabel; 43 | private int netAmount; 44 | 45 | private CartTableModel tableModel ; 46 | 47 | 48 | public CheckoutDialog(FoodMenuDialog foodMenuDialog, BillingApp frame, FoodDAO foodDAO, Customer customer) { 49 | 50 | this.foodMenuDialog = foodMenuDialog; 51 | this.frame = frame; 52 | this.foodDAO = foodDAO; 53 | this.customer = customer; 54 | 55 | addWindowListener(new WindowAdapter() { 56 | 57 | @Override 58 | public void windowClosing(WindowEvent we) 59 | { 60 | int PromptResult = JOptionPane.showConfirmDialog(null, "Exit application ?", 61 | "Confirm exit", JOptionPane.OK_CANCEL_OPTION); 62 | if(PromptResult== JOptionPane.OK_OPTION) 63 | { 64 | foodDAO.vacateQuantityColumn(); 65 | System.exit(0); 66 | } 67 | } 68 | }); 69 | 70 | setTitle("Hungry Hobbit Cafeteria - Checkout"); 71 | setBounds(100, 100, 640, 380); 72 | getContentPane().setLayout(new BorderLayout()); 73 | contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); 74 | getContentPane().add(contentPanel, BorderLayout.CENTER); 75 | contentPanel.setLayout(new BorderLayout(0, 0)); 76 | 77 | JPanel topLabelPanel = new JPanel(); 78 | contentPanel.add(topLabelPanel, BorderLayout.NORTH); 79 | topLabelPanel.setLayout(new BorderLayout(0, 0)); 80 | 81 | JLabel lblNewLabel = new JLabel("Checkout Section"); 82 | lblNewLabel.setVerticalAlignment(SwingConstants.BOTTOM); 83 | lblNewLabel.setFont(new Font("Sylfaen", Font.BOLD, 16)); 84 | lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER); 85 | topLabelPanel.add(lblNewLabel, BorderLayout.NORTH); 86 | 87 | JLabel lblNewLabel_1 = new JLabel("Please review your order"); 88 | lblNewLabel_1.setFont(new Font("Californian FB", Font.PLAIN, 14)); 89 | topLabelPanel.add(lblNewLabel_1, BorderLayout.SOUTH); 90 | 91 | Component verticalStrut = Box.createVerticalStrut(5); 92 | topLabelPanel.add(verticalStrut, BorderLayout.CENTER); 93 | 94 | JPanel centerPanel = new JPanel(); 95 | contentPanel.add(centerPanel, BorderLayout.CENTER); 96 | centerPanel.setLayout(new BorderLayout(0, 0)); 97 | 98 | JScrollPane scrollPane = new JScrollPane(); 99 | centerPanel.add(scrollPane, BorderLayout.CENTER); 100 | 101 | cartTable = new JTable(); 102 | setCartTableModel(); 103 | scrollPane.setViewportView(cartTable); 104 | 105 | JPanel deletePanel = new JPanel(); 106 | centerPanel.add(deletePanel, BorderLayout.SOUTH); 107 | deletePanel.setLayout(new BorderLayout(0, 20)); 108 | 109 | JLabel lblSelectTheProduct = new JLabel("Select the product and click Delete button to delete from cart"); 110 | deletePanel.add(lblSelectTheProduct, BorderLayout.WEST); 111 | 112 | btnDelete = new JButton("Delete"); 113 | btnDelete.addActionListener(new ActionListener() { 114 | public void actionPerformed(ActionEvent arg0) { 115 | deleteItem(); 116 | } 117 | }); 118 | deletePanel.add(btnDelete, BorderLayout.EAST); 119 | 120 | JPanel bottomPanel = new JPanel(); 121 | contentPanel.add(bottomPanel, BorderLayout.SOUTH); 122 | bottomPanel.setLayout(new BorderLayout(0, 0)); 123 | 124 | JPanel bottomButtonPanel = new JPanel(); 125 | bottomPanel.add(bottomButtonPanel, BorderLayout.SOUTH); 126 | bottomButtonPanel.setLayout(new BorderLayout(0, 0)); 127 | btnBack = new JButton("Back"); 128 | btnBack.addActionListener(new ActionListener() { 129 | public void actionPerformed(ActionEvent e) { 130 | dispose(); 131 | foodMenuDialog.refreshFoodItemView(); 132 | foodMenuDialog.setVisible(true); 133 | } 134 | }); 135 | bottomButtonPanel.add(btnBack, BorderLayout.WEST); 136 | 137 | btnProceed = new JButton("Place Order"); 138 | btnProceed.addActionListener(new ActionListener() { 139 | public void actionPerformed(ActionEvent e) { 140 | placeOrder(); 141 | } 142 | }); 143 | bottomButtonPanel.add(btnProceed, BorderLayout.EAST); 144 | 145 | JPanel netAmountPanel = new JPanel(); 146 | bottomPanel.add(netAmountPanel, BorderLayout.NORTH); 147 | netAmountPanel.setLayout(new BorderLayout(0, 0)); 148 | 149 | netAmountLabel = new JLabel("Net Amount: "); 150 | netAmountLabel.setFont(new Font("Garamond", Font.BOLD, 16)); 151 | netAmountPanel.add(netAmountLabel, BorderLayout.EAST); 152 | 153 | Component verticalStrut1; 154 | verticalStrut1 = Box.createVerticalStrut(20); 155 | netAmountPanel.add(verticalStrut1, BorderLayout.SOUTH); 156 | 157 | Component verticalStrut_1 = Box.createVerticalStrut(20); 158 | netAmountPanel.add(verticalStrut_1, BorderLayout.NORTH); 159 | 160 | } 161 | 162 | private void alignTable(){ 163 | DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer(); 164 | centerRenderer.setHorizontalAlignment( JLabel.CENTER ); 165 | for(int i=0; i< cartTable.getColumnCount(); i++){ 166 | cartTable.getColumnModel().getColumn(i).setCellRenderer( centerRenderer ); 167 | } 168 | } 169 | 170 | public void setNetAmountLabel(){ 171 | try { 172 | netAmount = foodDAO.getNetAmount(); 173 | netAmountLabel.setText("Net Amount: " + netAmount); 174 | } 175 | catch (SQLException e) { 176 | JOptionPane.showMessageDialog(CheckoutDialog.this, "Error setting net Amount", 177 | "Error", JOptionPane.ERROR_MESSAGE); 178 | } 179 | } 180 | 181 | private void setCartTableModel(){ 182 | try { 183 | tableModel = new CartTableModel(foodDAO.getCartItems()); 184 | cartTable.setModel(tableModel); 185 | alignTable(); 186 | } 187 | catch (Exception e) { 188 | // TODO Auto-generated catch block 189 | JOptionPane.showMessageDialog(CheckoutDialog.this, "Error setting Table Model: " + e.getMessage(), 190 | "Error", JOptionPane.ERROR_MESSAGE); 191 | e.printStackTrace(); 192 | } 193 | } 194 | 195 | private void deleteItem(){ 196 | int row = cartTable.getSelectedRow(); 197 | if(row<0){ 198 | JOptionPane.showMessageDialog(CheckoutDialog.this, "You must select an item before deleting.", 199 | "OOPS!", JOptionPane.ERROR_MESSAGE); 200 | return; 201 | } 202 | 203 | FoodItem temp = (FoodItem) cartTable.getValueAt(row, CartTableModel.OBJECT_COL); 204 | try { 205 | 206 | foodDAO.setQuantityToNull(temp); 207 | setCartTableModel(); 208 | setNetAmountLabel(); 209 | JOptionPane.showMessageDialog(CheckoutDialog.this, "Item removed from cart.", 210 | "Cart", JOptionPane.INFORMATION_MESSAGE); 211 | } 212 | catch (SQLException e) { 213 | // TODO Auto-generated catch block 214 | JOptionPane.showMessageDialog(CheckoutDialog.this, "Error deleteing item from cart: " + e.getMessage(), 215 | "Error", JOptionPane.ERROR_MESSAGE); 216 | e.printStackTrace(); 217 | } 218 | } 219 | 220 | private void placeOrder(){ 221 | if(cartTable.getRowCount()<1){ 222 | JOptionPane.showMessageDialog(CheckoutDialog.this, "Your cart is empty", 223 | "Can't place order", JOptionPane.ERROR_MESSAGE); 224 | return; 225 | } 226 | 227 | try { 228 | int orderId = foodDAO.addOrder(customer, netAmount); //insert the order details in order menu. 229 | foodDAO.vacateQuantityColumn(); 230 | JOptionPane.showMessageDialog(CheckoutDialog.this, "Order id: " +orderId, 231 | "Order placed successfully", JOptionPane.INFORMATION_MESSAGE); 232 | 233 | dispose(); //dissolve current window and show main window. 234 | frame.setVisible(true); 235 | System.out.println("Order placed successfully"); 236 | 237 | } 238 | catch (SQLException e) { 239 | JOptionPane.showMessageDialog(CheckoutDialog.this, "Error while placing order:" 240 | + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); 241 | e.printStackTrace(); 242 | } 243 | 244 | 245 | } 246 | } 247 | -------------------------------------------------------------------------------- /src/cafeteria/ui/orders/FoodMenuDialog.java: -------------------------------------------------------------------------------- 1 | package cafeteria.ui.orders; 2 | import java.awt.BorderLayout; 3 | import java.awt.FlowLayout; 4 | 5 | import javax.swing.JButton; 6 | import javax.swing.JDialog; 7 | import javax.swing.JPanel; 8 | import javax.swing.JOptionPane; 9 | import javax.swing.border.EmptyBorder; 10 | import javax.swing.table.DefaultTableCellRenderer; 11 | 12 | import cafeteria.core.Customer; 13 | import cafeteria.core.FoodItem; 14 | import cafeteria.dao.FoodDAO; 15 | import cafeteria.ui.BillingApp; 16 | 17 | import javax.swing.JLabel; 18 | import javax.swing.SwingConstants; 19 | import java.awt.Font; 20 | import javax.swing.JTextField; 21 | import java.awt.Component; 22 | import javax.swing.Box; 23 | import java.awt.Dimension; 24 | import java.awt.GridLayout; 25 | import javax.swing.JScrollPane; 26 | import javax.swing.JTable; 27 | import java.awt.event.ActionListener; 28 | import java.awt.event.WindowAdapter; 29 | import java.awt.event.WindowEvent; 30 | import java.sql.SQLException; 31 | import java.awt.event.ActionEvent; 32 | 33 | @SuppressWarnings("serial") 34 | public class FoodMenuDialog extends JDialog { 35 | 36 | private BillingApp frame; 37 | 38 | private FoodDAO foodDAO; 39 | private Customer customer; 40 | 41 | private final JPanel contentPanel = new JPanel(); 42 | JScrollPane tabelscrollPane; 43 | private JTextField textField; 44 | 45 | private JTable foodtable; 46 | private FoodTabelModel tableModel; 47 | 48 | JButton btnAddToCart; 49 | JButton btnBack; 50 | JButton btnProceedToCheckout; 51 | 52 | 53 | public FoodMenuDialog(BillingApp frame, FoodDAO foodDAO, Customer customer) { 54 | 55 | this.frame = frame; 56 | this.foodDAO = foodDAO; 57 | this.customer = customer; 58 | 59 | addWindowListener(new WindowAdapter() { 60 | 61 | @Override 62 | public void windowClosing(WindowEvent we) 63 | { 64 | int PromptResult = JOptionPane.showConfirmDialog(null, "Exit application ?", 65 | "Confirm exit", JOptionPane.OK_CANCEL_OPTION); 66 | if(PromptResult== JOptionPane.OK_OPTION) 67 | { 68 | System.exit(0); 69 | } 70 | } 71 | }); 72 | 73 | setTitle("Hungry Hobbit Cafeteria - Order Menu"); 74 | setBounds(100, 100, 680, 520); 75 | getContentPane().setLayout(new BorderLayout()); 76 | contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); 77 | getContentPane().add(contentPanel, BorderLayout.CENTER); 78 | contentPanel.setLayout(new BorderLayout(0, 0)); 79 | 80 | JPanel centerpanel = new JPanel(); 81 | contentPanel.add(centerpanel, BorderLayout.CENTER); 82 | centerpanel.setLayout(new BorderLayout(0, 0)); 83 | 84 | JPanel addpanel = new JPanel(); 85 | FlowLayout flowLayout = (FlowLayout) addpanel.getLayout(); 86 | flowLayout.setVgap(10); 87 | flowLayout.setAlignment(FlowLayout.LEFT); 88 | centerpanel.add(addpanel, BorderLayout.SOUTH); 89 | 90 | JLabel lblProductQuantity = new JLabel("Product Quantity"); 91 | lblProductQuantity.setHorizontalAlignment(SwingConstants.CENTER); 92 | addpanel.add(lblProductQuantity); 93 | 94 | Component horizontalStrut = Box.createHorizontalStrut(15); 95 | addpanel.add(horizontalStrut); 96 | 97 | textField = new JTextField(); 98 | addpanel.add(textField); 99 | textField.setColumns(10); 100 | 101 | Component horizontalStrut_2 = Box.createHorizontalStrut(35); 102 | addpanel.add(horizontalStrut_2); 103 | 104 | btnAddToCart = new JButton("Add to Cart"); 105 | btnAddToCart.addActionListener(new ActionListener() { 106 | public void actionPerformed(ActionEvent ae) { 107 | 108 | try{ 109 | int row = foodtable.getSelectedRow(); 110 | if(row<0){ 111 | JOptionPane.showMessageDialog(FoodMenuDialog.this, "You must select a food item", 112 | "Error", JOptionPane.ERROR_MESSAGE); 113 | return; 114 | } 115 | //System.out.println(row); 116 | //System.out.println(tempFoodItem); 117 | 118 | FoodItem tempFoodItem = (FoodItem)foodtable.getValueAt(row, FoodTabelModel.OBJECT_COL); 119 | String s = textField.getText(); 120 | 121 | /* 122 | if(s == "" || s.trim() == ""){ 123 | 124 | JOptionPane.showMessageDialog(FoodMenuDialog.this, "You must select a food item", 125 | "Error", JOptionPane.ERROR_MESSAGE); 126 | return; 127 | } 128 | */ 129 | 130 | int quantity = Integer.parseInt(s); 131 | //System.out.println(quantity); 132 | foodDAO.updateQuantity(tempFoodItem, quantity); 133 | 134 | refreshFoodItemView(); 135 | JOptionPane.showMessageDialog(FoodMenuDialog.this, "Food item added to cart.", 136 | "Cart", JOptionPane.INFORMATION_MESSAGE); 137 | } 138 | catch(Exception e){ 139 | JOptionPane.showMessageDialog(FoodMenuDialog.this, "Error adding product to cart: " 140 | + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); 141 | } 142 | 143 | 144 | } 145 | }); 146 | addpanel.add(btnAddToCart); 147 | 148 | JPanel toplabelpanel = new JPanel(); 149 | centerpanel.add(toplabelpanel, BorderLayout.NORTH); 150 | toplabelpanel.setLayout(new GridLayout(3, 1, 0, 0)); 151 | 152 | JLabel lblFoodOrderMenu = new JLabel("Food Order Menu"); 153 | lblFoodOrderMenu.setHorizontalAlignment(SwingConstants.CENTER); 154 | lblFoodOrderMenu.setFont(new Font("Tahoma", Font.BOLD, 15)); 155 | toplabelpanel.add(lblFoodOrderMenu); 156 | 157 | Component rigidArea = Box.createRigidArea(new Dimension(20, 5)); 158 | toplabelpanel.add(rigidArea); 159 | 160 | JLabel lblPleaseSelectThe = new JLabel("Please select the item and click Add"); 161 | lblPleaseSelectThe.setHorizontalAlignment(SwingConstants.CENTER); 162 | lblPleaseSelectThe.setFont(new Font("Tahoma", Font.BOLD, 12)); 163 | toplabelpanel.add(lblPleaseSelectThe); 164 | 165 | tabelscrollPane = new JScrollPane(); 166 | centerpanel.add(tabelscrollPane, BorderLayout.CENTER); 167 | 168 | foodtable = new JTable(); 169 | 170 | 171 | try{ 172 | tableModel = new FoodTabelModel(foodDAO.getAllFoodItems()); 173 | foodtable.setModel(tableModel); 174 | 175 | //tabelscrollPane.add(foodtable.getTableHeader(), BorderLayout.NORTH); 176 | tabelscrollPane.setViewportView(foodtable); 177 | alignTable(); 178 | 179 | 180 | } 181 | catch (Exception e) { 182 | // TODO Auto-generated catch block 183 | e.printStackTrace(); 184 | } 185 | 186 | 187 | JPanel bottombtnpanel = new JPanel(); 188 | contentPanel.add(bottombtnpanel, BorderLayout.SOUTH); 189 | bottombtnpanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5)); 190 | 191 | btnBack = new JButton("Back"); 192 | btnBack.addActionListener(new ActionListener() { 193 | public void actionPerformed(ActionEvent arg0) { 194 | dispose(); 195 | frame.setVisible(true); 196 | } 197 | }); 198 | bottombtnpanel.add(btnBack); 199 | 200 | Component horizontalStrut_1 = Box.createHorizontalStrut(180); 201 | bottombtnpanel.add(horizontalStrut_1); 202 | 203 | btnProceedToCheckout = new JButton("Proceed to Checkout"); 204 | btnProceedToCheckout.addActionListener(new ActionListener() { 205 | public void actionPerformed(ActionEvent arg0) { 206 | 207 | try { 208 | boolean check = foodDAO.isEmptyQuantityColumn(); 209 | if(!check){ 210 | JOptionPane.showMessageDialog(FoodMenuDialog.this, "Your cart is empty", 211 | "Can't proceed", JOptionPane.ERROR_MESSAGE); 212 | return; 213 | } 214 | 215 | } catch (SQLException e) { 216 | // TODO Auto-generated catch block 217 | e.printStackTrace(); 218 | } 219 | 220 | CheckoutDialog dialog = new CheckoutDialog(FoodMenuDialog.this, frame, foodDAO, customer); 221 | dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); 222 | dispose(); //dissolve the current window 223 | dialog.setVisible(true); 224 | dialog.setNetAmountLabel(); 225 | } 226 | }); 227 | bottombtnpanel.add(btnProceedToCheckout); 228 | } 229 | 230 | private void alignTable(){ 231 | //Setting renderer for table cells alignment 232 | DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer(); 233 | centerRenderer.setHorizontalAlignment( JLabel.CENTER ); 234 | 235 | //Centering all columns containing String Data 236 | foodtable.setDefaultRenderer(String.class, centerRenderer); 237 | 238 | //Centering particular columns 239 | foodtable.getColumnModel().getColumn(0).setCellRenderer( centerRenderer ); 240 | foodtable.getColumnModel().getColumn(3).setCellRenderer( centerRenderer ); 241 | foodtable.getColumnModel().getColumn(4).setCellRenderer( centerRenderer ); 242 | // can use this also 243 | //foodtable.setDefaultRenderer(foodtable.getColumnClass(0), centerRenderer); 244 | 245 | } 246 | 247 | public void refreshFoodItemView(){ 248 | try { 249 | tableModel = new FoodTabelModel(foodDAO.getAllFoodItems()); 250 | foodtable.setModel(tableModel); 251 | alignTable(); 252 | 253 | } catch (Exception e) { 254 | JOptionPane.showMessageDialog(this, "Error : " + e, "Error refreshing table view", 255 | JOptionPane.ERROR_MESSAGE); 256 | } 257 | } 258 | 259 | } 260 | --------------------------------------------------------------------------------