Guest User

Untitled

a guest
Nov 28th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.78 KB | None | 0 0
  1. /************************************************************************************************
  2. * READ THE INSTRUCTIONS BEFORE STARTING THE TEST
  3. *
  4. * 1. Rename the file with your ID no. For eg. if your ID no is 2017A7PSXXXXP, rename the file as
  5. * P2017A7PSXXXX.java.
  6. * 2. Solve the questions in chronological order i.e. Q1, Q2, Q3 etc. and not by the order of their appearance.
  7. * 3. You are are NOT ALLOWED to
  8. * * Declare new instance variables
  9. * * Modify the code which is provided except those methods for which you are explicitly asked to write the code.
  10. * * Create new classes and/or methods
  11. * * Import any new class to your solution
  12. * 4. Partial code is given to you. GUI design and a video of the necessary interaction with the GUI are provided for your reference.
  13. * 5. Questions are given as comments. Write the code only for the portions that are intentionally left blank.
  14. * 6. Periodically save your work to avoid any last minute mishap.
  15. * 7. Fill in the following details before starting the test.
  16. *
  17. * ID: 2016B3A70549P
  18. * NAME: APURV BAJAJ
  19. * ROOM No.: 6114
  20. * MACHINE No.: 42
  21. ************************************************************************************************/
  22.  
  23. import java.awt.FlowLayout;
  24. import java.awt.event.ActionEvent;
  25. import java.awt.event.ActionListener;
  26. import java.awt.event.ItemEvent;
  27. import java.awt.event.ItemListener;
  28. import java.sql.Connection;
  29. import java.sql.DriverManager;
  30. import java.sql.PreparedStatement;
  31. import java.sql.ResultSet;
  32. import java.sql.SQLException;
  33. import java.sql.Statement;
  34. import java.util.logging.Level;
  35. import java.util.logging.Logger;
  36.  
  37. import javax.swing.JButton;
  38. import javax.swing.JComboBox;
  39. import javax.swing.JFrame;
  40. import javax.swing.JLabel;
  41. import javax.swing.JOptionPane;
  42. import javax.swing.JPanel;
  43. import javax.swing.JTabbedPane;
  44. import javax.swing.JTextField;
  45.  
  46. class OnlineTest {
  47.  
  48. private JFrame mainFrame;
  49. private JPanel panel1, panel2;
  50. private JTabbedPane tabPane;
  51.  
  52. private JLabel lbl_name, lbl_ID, lbl_Name, lbl_ItemName, lbl_Qty, lbl_BillAmt;
  53. private JTextField txtField1_Name, txtField2_Name, txtField_Qty, txtField_BillAmt;
  54.  
  55. private JComboBox<Integer> txtField_ID;
  56. private JComboBox<String> txtField_Item;
  57. private JButton savebtn, resetbtn, addbtn, proceedbtn ;
  58.  
  59. public static void main(String args[]) {
  60. OnlineTest pro = new OnlineTest();
  61. pro.displayAppGUI();
  62. }
  63.  
  64. OnlineTest() {
  65.  
  66. /************************************************************************************************
  67. * DONOT DELETE/MODIFY THE FOLLOWING CODE
  68. * After you run the application for the first time comment the following lines of code
  69. * This method should be called only one time when the application is executed for the first time
  70. ************************************************************************************************/
  71. try {
  72. createDatabase();
  73. } catch (ClassNotFoundException | SQLException e2) {
  74. e2.printStackTrace();
  75. }
  76. /************************************************************************************************/
  77.  
  78. /************************************************************************************************
  79. * UNCOMMENT THE FOLLOWING CODE TO DELETE ALL THE RECORDS FROM ANYONE OR ALL THE TABLES
  80. ************************************************************************************************/
  81. // try {
  82. // deleteAllRecordsFromTable("PURCHASE");
  83. // deleteAllRecordsFromTable("ITEM");
  84. // deleteAllRecordsFromTable("CUSTOMER");
  85. // } catch (ClassNotFoundException e1) {
  86. // e1.printStackTrace();
  87. // } catch (SQLException e1) {
  88. // e1.printStackTrace();
  89. // }
  90.  
  91. /************************************************************************************************
  92. * DONOT DELETE/MODIFY THE FOLLOWING CODE
  93. * After you run the application for the first time comment the following lines of code
  94. * This method should be called only one time when the application is executed for the first time
  95. ************************************************************************************************/
  96. try {
  97. insertItemInItemTable();
  98. } catch (Exception e1) {
  99. e1.printStackTrace();
  100. }
  101. /************************************************************************************************/
  102.  
  103.  
  104. /************************************************************************************************
  105. * DONOT DELETE/MODIFY THE FOLLOWING CODE
  106. * Functionon calls to initialize all the GUI components and to populate the drop down boxes.
  107. * You have to write code for these method. Unimplemented method stubs are provided below:
  108. * refer Q. 1 and Q. 6
  109. ************************************************************************************************/
  110. initializeAppGUI(); // function call
  111.  
  112. populate_txtField_ID_And_txtField_Item(); //function call
  113.  
  114. /************************************************************************************************
  115. * Q.7 WRITE CODE FOR THE ACTION LISTNER OF THE resetbtn - this should reset the txtField1
  116. ************************************************************************************************/
  117. resetbtn.addActionListener(new ActionListener() {
  118. public void actionPerformed(ActionEvent ae) {
  119. txtField1_Name.setText("");
  120. }
  121. });
  122.  
  123. /************************************************************************************************
  124. * Q.8 WRITE CODE FOR THE ACTION LISTNER OF THE savebtn - this event should automatically generates
  125. * the customer id using the getCustomerID() and inserts the customer id and name into the CUSTOMER table.
  126. ************************************************************************************************/
  127. savebtn.addActionListener(new ActionListener() {
  128. public void actionPerformed(ActionEvent ae) {
  129. Connection con;
  130. PreparedStatement st;
  131.  
  132. int customerid; // used to store the customer id
  133. String value1; //used to store the customer name
  134.  
  135. value1 = txtField1_Name.getText();
  136. try {
  137. con = getConnection();
  138. customerid = getCustomerID(con);
  139. st = con.prepareStatement("insert into CUSTOMER(CID,NAME) values(?,?)");
  140. st.setInt(1, customerid);
  141. st.setString(2, value1);
  142. st.executeUpdate();
  143. txtField_ID.addItem(customerid);
  144. JOptionPane.showMessageDialog(panel1,"Query successfully inserted into database!");
  145.  
  146. } catch(ClassNotFoundException e){
  147. JOptionPane.showMessageDialog(panel1,"Error in submitting data!");
  148. } catch (SQLException ex) {
  149. Logger.getLogger(OnlineTest.class.getName()).log(Level.SEVERE, null, ex);
  150. }
  151. }
  152. });
  153.  
  154. /************************************************************************************************
  155. * Q.9 WRITE CODE FOR THE ITEM LISTNER OF THE txtField_ID - this event is used to retrieve the customer name
  156. * from the CUSTOMER table, when the customer id is chosen from the drop down box. It should then display name
  157. * in the txtField2_Name text box.
  158. ************************************************************************************************/
  159. txtField_ID.addItemListener(new ItemListener() {
  160. public void itemStateChanged(ItemEvent ie) {
  161.  
  162. Connection con;
  163. PreparedStatement st;
  164. ResultSet res;
  165.  
  166. int value; //used to store the selected customer id
  167. value = (int)txtField_ID.getSelectedItem();
  168. try {
  169. con = getConnection();
  170. st = con.prepareStatement("select NAME from CUSTOMER where CID=?");
  171. st.setInt(1, value);
  172. res = st.executeQuery();
  173.  
  174. if(res.next())
  175. txtField2_Name.setText(res.getString(1));
  176. con.close();
  177. } catch (Exception e) {
  178. e.printStackTrace();
  179. }
  180. }
  181. });
  182.  
  183. /************************************************************************************************
  184. * Q.10 WRITE CODE FOR THE ACTION LISTNER OF THE addbtn - The record from the ITEM table is retrieved
  185. * for the item chosen from the drop down list. If required quantity is available in the ITEM table,
  186. * a record is inserted into the PURCHASE table (customer id, item id, quantity, price) and the quantity
  187. * field in the ITEM table is updated. Else, an error message is thrown. Then clear all the fields for
  188. * adding the next item.
  189. ************************************************************************************************/
  190. addbtn.addActionListener(new ActionListener() {
  191. public void actionPerformed(ActionEvent ae) {
  192. Connection con;
  193. PreparedStatement st;
  194. ResultSet res;
  195.  
  196. String value1; //used to store selected item name
  197. Integer value2; //used to store the quantity
  198.  
  199. int iid; //used to store the item id
  200.  
  201. value1 = (String)txtField_Item.getSelectedItem();
  202. value2 = Integer.parseInt(txtField_Qty.getText());
  203. try {
  204. con = getConnection();
  205. st = con.prepareStatement("select * from ITEM where NAME=?");
  206. st.setString(1, value1);
  207. res = st.executeQuery();
  208. res.next();
  209. if(res.getInt(3) >= value2){
  210. iid = res.getInt(1);
  211. st = con.prepareStatement("insert into PURCHASE(CID,IID,QTY,PRICE) values(?,?,?,?)");
  212. st.setInt(1, (int)txtField_ID.getSelectedItem());
  213. st.setInt(2, iid);
  214. st.setInt(3, value2);
  215. st.setFloat(4, res.getFloat(4));
  216. st.executeUpdate();
  217.  
  218. st = con.prepareStatement("update ITEM set QTY=" + (res.getInt(3)-value2) + "where NAME=?");
  219. st.setString(1, value1);
  220. st.executeUpdate();
  221. JOptionPane.showMessageDialog(panel2,"Item successfully added to cart!");
  222. }
  223. else {
  224. JOptionPane.showMessageDialog(panel2,"Required quantity not available!");
  225. }
  226. txtField_Item.setSelectedIndex(0);
  227. txtField_Qty.setText("");
  228. } catch (Exception e) {
  229. e.printStackTrace();
  230. }
  231.  
  232. }
  233. });
  234.  
  235. /************************************************************************************************
  236. * Q.11 WRITE CODE FOR THE ACTION LISTNER OF THE proceedbtn - All records pertaining to a customer
  237. * is retrieved from the PURCHASE table, the total bill amount is computed for all the items purchased
  238. * by a customer.
  239. ************************************************************************************************/
  240. proceedbtn.addActionListener(new ActionListener() {
  241. public void actionPerformed(ActionEvent ae) {
  242. Connection con;
  243. PreparedStatement st;
  244. ResultSet res;
  245. // * TABLE: PURCHASE, ATTRIBUTES - CID (int), IID (int), QTY (int), PRICE (float) (Customer id, item id, quantity, and price)
  246. int value; //used to store customer id
  247. float bill = 0; //used to store the bill amount
  248. value = (int) txtField_ID.getSelectedItem();
  249. try {
  250. con = getConnection();
  251. st = con.prepareStatement("select * from PURCHASE where CID=?");
  252. st.setInt(1, value);
  253. res = st.executeQuery();
  254. while(res.next()) {
  255. bill += res.getFloat(4) * res.getInt(3);
  256. }
  257. txtField_BillAmt.setText(String.valueOf(bill));
  258.  
  259. } catch (Exception e) {
  260. e.printStackTrace();
  261. }
  262.  
  263. }
  264. });
  265.  
  266. }// end of constructor
  267.  
  268. /************************************************************************************************
  269. * Q.6 WRITE CODE FOR THIS METHOD TO POPULATE txtField_ID and txtField_Item from the CUSTOMER and
  270. * ITEM tables.
  271. ************************************************************************************************/
  272. public void populate_txtField_ID_And_txtField_Item() {
  273. Connection con;
  274. Statement statement1, statement2;
  275. ResultSet rs1, rs2;
  276. String query1, query2;
  277.  
  278. try {
  279. con = getConnection();
  280. statement1 = con.createStatement();
  281. statement2 = con.createStatement();
  282. query1 = "select CID from CUSTOMER";
  283. rs1 = statement1.executeQuery(query1);
  284. while(rs1.next())
  285. txtField_ID.addItem(rs1.getInt(1));
  286.  
  287. txtField_Item.addItem("Choose the Item");
  288. query2 = "select NAME from ITEM";
  289. rs2 = statement2.executeQuery(query2);
  290. while(rs2.next())
  291. txtField_Item.addItem(rs2.getString(1));
  292. con.close();
  293. } catch (Exception e) {
  294. e.printStackTrace();
  295. }
  296. }
  297.  
  298. /************************************************************************************************
  299. * Q.1 WRITE CODE FOR THIS METHOD TO INITIALIZE APP GUI
  300. ************************************************************************************************/
  301. public void initializeAppGUI(){
  302. mainFrame = new JFrame("Online purchase");
  303. panel1 = new JPanel();
  304. panel2 = new JPanel();
  305. tabPane = new JTabbedPane();
  306. lbl_name = new JLabel("Customer Name");
  307. txtField1_Name = new JTextField(12);
  308. savebtn = new JButton("Add");
  309. resetbtn = new JButton("Reset");
  310. lbl_ID = new JLabel("Customer ID");
  311. txtField_ID = new JComboBox<Integer>();
  312. lbl_Name = new JLabel("Customer Name");
  313. txtField2_Name = new JTextField(12);
  314. lbl_ItemName = new JLabel("Item Name");
  315. txtField_Item = new JComboBox<String>();
  316. lbl_Qty = new JLabel("Quantity");
  317. txtField_Qty = new JTextField(12);
  318. lbl_BillAmt = new JLabel("Bill Amount");
  319. txtField_BillAmt = new JTextField(12);
  320. addbtn = new JButton("Add More Items");
  321. proceedbtn = new JButton("Checkout");
  322. }
  323.  
  324. /************************************************************************************************
  325. * Q.2 WRITE CODE FOR THIS METHOD TO DISPLAY APP GUI
  326. ************************************************************************************************/
  327. void displayAppGUI() {
  328. mainFrame.setSize(450,300);
  329. mainFrame.setVisible(true);
  330. mainFrame.getContentPane().add(tabPane);
  331. mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  332.  
  333. panel1.setLayout(new FlowLayout(FlowLayout.LEFT, 15,15));
  334. // Customer name 1
  335. panel1.add(lbl_name);
  336. panel1.add(txtField1_Name);
  337. panel1.add(savebtn);
  338. panel1.add(resetbtn);
  339.  
  340. tabPane.addTab("New Customer", panel1);
  341.  
  342. panel2.setLayout(new FlowLayout(FlowLayout.LEFT, 15,15));
  343. panel2.add(lbl_ID);
  344. panel2.add(txtField_ID);
  345. panel2.add(lbl_Name);
  346. panel2.add(txtField2_Name);
  347. panel2.add(lbl_ItemName);
  348. panel2.add(txtField_Item);
  349. panel2.add(lbl_Qty);
  350. panel2.add(txtField_Qty);
  351. panel2.add(lbl_BillAmt);
  352. panel2.add(txtField_BillAmt);
  353. panel2.add(addbtn);
  354. panel2.add(proceedbtn);
  355.  
  356. tabPane.addTab("Existing Customer", panel2);
  357. }
  358.  
  359. /************************************************************************************************
  360. * Q.3 WRITE CODE FOR THIS METHOD TO GET DATABASE CONNECTION
  361. ************************************************************************************************/
  362. public Connection getConnection() throws ClassNotFoundException, SQLException {
  363. Connection con;
  364. Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
  365. con = DriverManager.getConnection("jdbc:derby:cust;create=true;user=hello;password=hello");
  366. return con;
  367. }
  368.  
  369. /************************************************************************************************
  370. * Q.4 WRITE CODE FOR THIS METHOD TO CREATE CUSTOMER, ITEM, AND PURCHASE TABLES
  371. * TABLE : CUSTOMER, ATTRIBUTES - CID (int), NAME (var char) (Customer id and customer name)
  372. * TABLE: ITEM, ATTRIBUTES - IID (int), NAME (var char), QTY (int), PRICE (float) (Item id, item name, quantity and price)
  373. * TABLE: PURCHASE, ATTRIBUTES - CID (int), IID (int), QTY (int), PRICE (float) (Customer id, item id, quantity, and price)
  374. ************************************************************************************************/
  375. public void createDatabase() throws ClassNotFoundException, SQLException {
  376.  
  377. Connection con;
  378. Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
  379. con = DriverManager.getConnection("jdbc:derby:cust;create=true;user=hello;password=hello");
  380. Statement stmt;
  381. stmt = con.createStatement();
  382.  
  383. stmt.executeUpdate("create table CUSTOMER(CID integer primary key,\r\n" + "NAME varchar(20))");
  384.  
  385. stmt.executeUpdate("create table ITEM(IID integer primary key,\r\n" +
  386. "NAME varchar(20),\r\n" +
  387. "QTY integer,\r\n" +
  388. "PRICE float)");
  389.  
  390. stmt.executeUpdate("create table PURCHASE(CID integer primary key,\r\n" +
  391. "IID integer,\r\n" + "QTY integer,\r\n" + "PRICE float)");
  392. }
  393.  
  394. /************************************************************************************************
  395. * Q.5 WRITE CODE FOR THIS METHOD TO INSERT THREE ITEMS IN THE ITEM TABLE
  396. ************************************************************************************************/
  397. public void insertItemInItemTable() {
  398.  
  399. String query;
  400. Connection con;
  401. Statement stmt;
  402.  
  403. try {
  404. con = getConnection();
  405. stmt = con.createStatement();
  406.  
  407. query = "insert into ITEM(IID,NAME,QTY,PRICE) values(1,'Mobile',20,100.0)";
  408. stmt.executeUpdate(query);
  409.  
  410. query = "insert into ITEM(IID,NAME,QTY,PRICE) values(2,'Chocolates',20,100.0)";
  411. stmt.executeUpdate(query);
  412.  
  413. query = "insert into ITEM(IID,NAME,QTY,PRICE) values(3,'Notebooks',20,100.0)";
  414. stmt.executeUpdate(query);
  415.  
  416.  
  417. } catch (Exception e) {
  418. e.printStackTrace();
  419. }
  420. }
  421.  
  422. /******************************************************************************************************
  423. * HELPER METHODS - DONOT MODIFY THE CODE OF THE FOLLOWING TWO METHODS
  424. ******************************************************************************************************/
  425. public int getCustomerID(Connection con) {
  426.  
  427. int value = 0;
  428. ResultSet rs;
  429. Statement stmt;
  430.  
  431. try {
  432. stmt = con.createStatement();
  433. rs = stmt.executeQuery("Select Max(CID) from CUSTOMER");
  434. rs.next();
  435. if(rs.getInt(1) == 0) value = 100;
  436. else value = rs.getInt(1) + 1;
  437. } catch (SQLException e) {
  438. e.printStackTrace();
  439. }
  440.  
  441. return value;
  442. }
  443.  
  444. public void deleteAllRecordsFromTable(String tableName) throws ClassNotFoundException, SQLException {
  445. String query;
  446. Connection con = getConnection();
  447. Statement stmt = con.createStatement();
  448. query = "DELETE FROM " + tableName;
  449. stmt.executeUpdate(query);
  450. }
  451. }
Add Comment
Please, Sign In to add comment