-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathRetailImpl.java
168 lines (149 loc) · 6.26 KB
/
RetailImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.everyday.dao;
import com.everyday.helpers.PostgresConnHelper;
import com.everyday.models.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
public class RetailImpl implements RetailDao {
private Connection conn;
private PreparedStatement pre,epre,fpre,apre;
private Statement statement;
private ResultSet resultSet;
private ResourceBundle resourceBundle;
private FoodItems foodItem;
private Apparel apparel;
private Electronics electronics;
public RetailImpl(){
conn= PostgresConnHelper.getConnection();
if(conn!=null)
System.out.println("Connection ready...");
else
System.out.println("Connection has issue...");
resourceBundle=ResourceBundle.getBundle("db");
}
@Override
public void addCategory(Category category) throws SQLException {
String addCategory=resourceBundle.getString("addcategory");
String addFoodItem=resourceBundle.getString("addfooditems");
String addApparel=resourceBundle.getString("addapparel");
String addElectronics=resourceBundle.getString("addelectronics");
try {
pre=conn.prepareStatement(addCategory);
epre=conn.prepareStatement(addElectronics);
fpre=conn.prepareStatement(addFoodItem);
apre=conn.prepareStatement(addApparel);
pre.setLong(1,category.getItemCode());
pre.setString(2,category.getItemName());
pre.setLong(3,category.getUnitPrice());
pre.setInt(4,category.getQuantity());
pre.executeUpdate();
if(category instanceof FoodItems){
fpre.setDate(1, Date.valueOf(((FoodItems) category).getDoe()));
fpre.setDate(2,Date.valueOf(((FoodItems) category).getDom()));
fpre.setLong(3,category.getItemCode());
fpre.setBoolean(4,((FoodItems) category).isVegetarian());
fpre.executeUpdate();
}
if(category instanceof Apparel){
apparel= (Apparel) category;
apre.setInt(1, apparel.getSize());
if(apparel.getMaterial().equals(Material.Cotton))
apre.setInt(2,1);
else
apre.setInt(2,2);
apre.setLong(3,category.getItemCode());
apre.executeUpdate();
}
if(category instanceof Electronics){
electronics= (Electronics) category;
epre.setInt(1, electronics.getWarranty());
epre.setLong(2,category.getItemCode());
epre.executeUpdate();
}
conn.commit();
} catch (SQLException exception) {
System.out.println(exception.getMessage());
conn.rollback();
}
finally {
conn.close();
}
}
@Override
public List<? extends Category> getAllCategories(int type) throws SQLException {
conn=PostgresConnHelper.getConnection();
List<Category> categoryList=null;
List<FoodItems> foodItemsList=new ArrayList<FoodItems>();
List<Apparel> apparelList=new ArrayList<Apparel>();
List<Electronics> electronicsList=new ArrayList<Electronics>();
FoodItems foodItems=null;
Apparel apparel=null;
Electronics electronics=null;
String query=null;
switch(type){
case 1:
categoryList=new ArrayList<Category>();
query=resourceBundle.getString("selectfooditems");
statement=conn.createStatement();
resultSet=statement.executeQuery(query);
while(resultSet.next()){
foodItems=new FoodItems();
foodItems.setItemCode(resultSet.getLong(1));
foodItems.setItemName(resultSet.getString(2));
foodItems.setQuantity(resultSet.getInt(3));
foodItems.setVegetarian(resultSet.getBoolean(4));
foodItemsList.add(foodItems);
}
categoryList.addAll(foodItemsList);
break;
case 2:
categoryList=new ArrayList<Category>();
query=resourceBundle.getString("selectapparels");
statement=conn.createStatement();
resultSet=statement.executeQuery(query);
while(resultSet.next()){
apparel=new Apparel();
apparel.setItemCode(resultSet.getLong(1));
apparel.setItemName(resultSet.getString(2));
apparel.setQuantity(resultSet.getInt(3));
apparel.setSize(resultSet.getInt(4));
if(resultSet.getInt(5)==1)
apparel.setMaterial(Material.Cotton);
else
apparel.setMaterial(Material.Woolen);
apparelList.add(apparel);
}
categoryList.addAll(apparelList);
break;
case 3:
categoryList=new ArrayList<Category>();
query=resourceBundle.getString("selectelectronics");
statement=conn.createStatement();
resultSet=statement.executeQuery(query);
while(resultSet.next()){
electronics=new Electronics();
electronics.setItemCode(resultSet.getLong(1));
electronics.setItemName(resultSet.getString(2));
electronics.setQuantity(resultSet.getInt(3));
electronics.setWarranty(resultSet.getByte(4));
electronicsList.add(electronics);
}
categoryList.addAll(electronicsList);
break;
}
return categoryList;
}
@Override
public Category getCategoryById(long categoryId) {
return null;
}
@Override
public boolean deleteCategory(long categoryId) {
return false;
}
@Override
public Category updateCategory(Category category) {
return null;
}
}