Advertisement
Guest User

Untitled

a guest
Aug 15th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.29 KB | None | 0 0
  1. import java.sql.*;
  2. import java.applet.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.util.*;
  6. import javax.swing.*;
  7. import java.util.Date;
  8. import java.util.Calendar;
  9. import java.text.DateFormat;
  10.  
  11. public class WrigleyInc extends Applet implements ItemListener, ActionListener
  12. {
  13.  
  14. //Declare variables and assign initial value
  15. Calendar calCurrent = Calendar.getInstance();
  16. boolean blnSuccessfulOpen = false;
  17. float fltTotalCost = 0;
  18. int intDay = calCurrent.get(Calendar.DATE); //gets day of month
  19. int intMonth = calCurrent.get(Calendar.MONTH) + 1; //gets month
  20. int intYear = calCurrent.get(Calendar.YEAR); //gets year
  21. Font fntName = new Font("Cambria", Font.BOLD, 12); //sets font to Cambria and bold with 12font size
  22. Connection conCustomers;
  23. Statement cmdCustomers;
  24. ResultSet rsCustomers;
  25. Color bgColor;
  26. Label lblToday = new Label(" ");
  27. Label lblTime = new Label(" ");
  28. Label lblDifference = new Label(" ");
  29. Label lblDayOfWeek = new Label(" ");
  30. Choice chMonth1 = new Choice();
  31. Choice chMonth2 = new Choice();
  32. Choice lstNames = new Choice();
  33. TextField txtCustomerID = new TextField(15);
  34. TextField txtMonth1 = new TextField(10);
  35. TextField txtDay1 = new TextField(2);
  36. TextField txtCustomerName = new TextField(15);
  37. TextField txtAddress = new TextField(15);
  38. TextField txtCity = new TextField(15);
  39. TextField txtState = new TextField(2);
  40. TextField txtZip = new TextField(5);
  41. TextField txtPhone = new TextField(15);
  42. TextField txtProductName = new TextField(15);
  43. TextField txtOrderNumber = new TextField(15);
  44. TextField txtQuantity = new TextField(15);
  45. TextField txtUnitCost = new TextField(15);
  46. TextField txtTotalCost = new TextField(15);
  47. TextField txtID = new TextField(15);
  48. TextArea txaReport = new TextArea (" Customer Invoice ",20,30);
  49.  
  50. Button btnAdd = new Button("Add");
  51. Button btnEdit = new Button("Save");
  52. Button btnCancel = new Button("Cancel");
  53. Button btnDelete = new Button("Delete");
  54. Button btnReport = new Button("Report");
  55. Button btnNext = new Button(" > ");
  56. Button btnLast = new Button(" >>> ");
  57. Button btnPrevious = new Button(" < ");
  58. Button btnFirst = new Button(" <<< ");
  59.  
  60. JPanel pnlTxtFields = new JPanel() ;
  61.  
  62. private String dbURL = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=Customers.mdb;DriverID=22;}";
  63.  
  64. public void init()
  65. {
  66. setSize(290,780) ;
  67.  
  68.  
  69. //Set up a Calendar for the current locale and time zone
  70. Calendar calToday = Calendar.getInstance();
  71.  
  72. //Format today's date
  73. DateFormat defaultDate = DateFormat.getDateInstance();
  74. lblToday.setText(defaultDate.format(calToday.getTime()));
  75.  
  76. //Set day of week
  77. String[] strDay = {"Sunday", "Monday", "Tuesday", "Wednesday",
  78. "Thursday", "Friday", "Saturday"};
  79. int intDayOfWeek = calToday.get(calToday.DAY_OF_WEEK) - 1;
  80. lblDayOfWeek.setText(strDay[intDayOfWeek]);
  81.  
  82.  
  83. //Format time
  84. DateFormat shortTime = DateFormat.getTimeInstance(DateFormat.SHORT);
  85. lblTime.setText(shortTime.format(calToday.getTime()));
  86. pnlTxtFields.setLayout(new GridLayout(12,2)) ;
  87. add(lblDayOfWeek);
  88. add(lblToday);
  89. add(lblTime);
  90.  
  91. bgColor = Color.gray;
  92. setBackground(bgColor);
  93.  
  94. //Set up panel and load database
  95. LoadDatabase();
  96. if (blnSuccessfulOpen)
  97. {
  98. //Sets up the userinterface
  99. lstNames.insert("Select an ID",0);
  100. add(lstNames);
  101. lstNames.addItemListener(this);
  102. add(pnlTxtFields) ;
  103. pnlTxtFields.add(new Label("Customer ID"));
  104. pnlTxtFields.add(txtCustomerID);
  105. txtCustomerID.setFont(fntName);
  106. pnlTxtFields.add(new Label("Customer Name"));
  107. txtCustomerName.setFont(fntName);
  108. pnlTxtFields.add(txtCustomerName);
  109. pnlTxtFields.add(new Label("Address"));
  110. txtAddress.setFont(fntName);
  111. pnlTxtFields.add(txtAddress);
  112. pnlTxtFields.add(new Label("City"));
  113. txtCity.setFont(fntName);
  114. pnlTxtFields.add(txtCity);
  115. pnlTxtFields.add(new Label("State"));
  116. txtState.setFont(fntName);
  117. pnlTxtFields.add(txtState);
  118. pnlTxtFields.add(new Label("Zip"));
  119. txtZip.setFont(fntName);
  120. pnlTxtFields.add(txtZip);
  121. pnlTxtFields.add(new Label("Phone"));
  122. txtPhone.setFont(fntName);
  123. pnlTxtFields.add(txtPhone);
  124. pnlTxtFields.add(new Label("Product Name"));
  125. txtProductName.setFont(fntName);
  126. pnlTxtFields.add(txtProductName);
  127. pnlTxtFields.add(new Label("Order Number"));
  128. txtOrderNumber.setFont(fntName);
  129. pnlTxtFields.add(txtOrderNumber);
  130. pnlTxtFields.add(new Label("Quanity"));
  131. txtQuantity.setFont(fntName);
  132. pnlTxtFields.add(txtQuantity);
  133. pnlTxtFields.add(new Label("Unit Cost"));
  134. txtUnitCost.setFont(fntName);
  135. pnlTxtFields.add(txtUnitCost);
  136. pnlTxtFields.add(new Label ("Total Cost"));
  137. txtTotalCost.setFont(fntName);
  138. pnlTxtFields.add(txtTotalCost);
  139.  
  140.  
  141. //Add/Edit/Delete/Cance/Report buttons
  142. setTextToNotEditable();
  143. add(btnAdd);
  144. btnAdd.addActionListener(this);
  145. add(btnEdit);
  146. btnEdit.addActionListener(this);
  147. add(btnDelete);
  148. btnDelete.addActionListener(this);
  149. add(btnCancel);
  150. btnCancel.addActionListener(this);
  151. btnCancel.setEnabled(false);
  152. add(btnReport);
  153. btnReport.addActionListener(this);
  154. add(txaReport);
  155.  
  156.  
  157. //Navigate Buttons
  158. add(btnFirst);
  159. btnFirst.addActionListener(this);
  160. add(btnPrevious);
  161. btnPrevious.addActionListener(this);
  162. add(btnNext);
  163. btnNext.addActionListener(this);
  164. add(btnLast);
  165. btnLast.addActionListener(this);
  166.  
  167. }
  168. else
  169. {
  170. System.err.println("Unable to populate fields");
  171. }
  172. }
  173.  
  174. public void LoadDatabase()
  175. {
  176.  
  177. try
  178. {
  179. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//Sun driver
  180. }
  181. catch (ClassNotFoundException err)
  182. {
  183. System.err.println("Driver didn't load");
  184. }
  185.  
  186. try
  187. {
  188. //Connect to the database
  189. conCustomers = DriverManager.getConnection(dbURL);
  190. }
  191.  
  192. catch(SQLException error)
  193. {
  194. System.err.println("Unable to Connect to Database");
  195. }
  196.  
  197. try
  198. {
  199. Statement cmdCustomers = conCustomers.createStatement();
  200. //Create the ResultSet
  201. ResultSet rsCustomers = cmdCustomers.executeQuery("Select * from Customers");
  202.  
  203. loadNames(rsCustomers);
  204. blnSuccessfulOpen = true;
  205. }
  206. catch(SQLException error)
  207. {
  208. System.err.println("Error in recordset");
  209. }
  210. }
  211.  
  212.  
  213.  
  214. public void loadNames(ResultSet rsCustomers)
  215. {
  216. //Fill CustomerID list box
  217. try
  218. {
  219. while(rsCustomers.next())
  220. lstNames.add(rsCustomers.getString("CustomerID"));
  221. }
  222. catch (SQLException error)
  223. {
  224. System.err.println("Error in Display Record");
  225. }
  226. }
  227.  
  228. public void itemStateChanged(ItemEvent event)
  229. {
  230. //Retrieve and display the selected record
  231. String strCustomerID = lstNames.getSelectedItem();
  232.  
  233. showStatus(""); //Delete instructions
  234. try
  235. {
  236. Statement cmdCustomers = conCustomers.createStatement();
  237. rsCustomers = cmdCustomers.executeQuery( "Select * From Orders, Customers Where Orders.ID = \'"+strCustomerID+"\' and Customers.CustomerID = \'"+strCustomerID+"\'");
  238.  
  239. txtCustomerID.setText(strCustomerID);
  240. displayRecord(rsCustomers);
  241. setTextToEditable();
  242. }
  243. catch(SQLException error)
  244. {
  245. showStatus("Error in recordset");
  246. }
  247. }
  248.  
  249. public void displayRecord(ResultSet rsCustomers)
  250. {
  251. //Display the current record
  252. try
  253. {
  254. if(rsCustomers.next())
  255. {
  256. txtCustomerName.setText(rsCustomers.getString("CustomerName"));
  257. txtAddress.setText(rsCustomers.getString("Address"));
  258. txtCity.setText(rsCustomers.getString("City"));
  259. txtState.setText(rsCustomers.getString("State"));
  260. txtZip.setText(rsCustomers.getString("Zip"));
  261. txtPhone.setText(rsCustomers.getString("PhoneNumber"));
  262. txtProductName.setText(rsCustomers.getString("ProductName"));
  263. txtOrderNumber.setText(rsCustomers.getString("OrderNumber"));
  264. txtQuantity.setText(rsCustomers.getString("Quantity"));
  265. txtUnitCost.setText(rsCustomers.getString("UnitCost"));
  266. float fltQuantity = Float.valueOf(txtQuantity.getText()).floatValue();
  267. float fltUnitCost = Float.valueOf(txtUnitCost.getText()).floatValue();
  268. fltTotalCost = fltQuantity*fltUnitCost;
  269. txtTotalCost.setText(" " + fltTotalCost);
  270.  
  271. showStatus("");
  272. }
  273. else
  274. {
  275. showStatus("Record not found");
  276. ClearTextFields();
  277. }
  278. }
  279. catch (SQLException error)
  280. {
  281. showStatus("Error in display record");
  282. }
  283. }
  284.  
  285. public void actionPerformed(ActionEvent event)
  286. {
  287. //Test the command buttons
  288. Object objSource = event.getSource();
  289.  
  290. if(objSource == btnAdd && event.getActionCommand() == "Add")
  291. Add();//setup interface before we add...
  292. else if (objSource == btnAdd && event.getActionCommand() == "OK")//when we click the "OK" button
  293. Save();
  294. else if(objSource == btnEdit)
  295. Edit();
  296. else if(objSource == btnDelete)
  297. Delete();
  298. else if(objSource == btnCancel)
  299. Cancel();
  300. else if(objSource == btnFirst)
  301. firstRecord();
  302. else if(objSource == btnNext)
  303. nextRecord();
  304. else if(objSource == btnPrevious)
  305. previousRecord();
  306. else if(objSource == btnLast)
  307. lastRecord();
  308.  
  309. Object buttonSelected = event.getSource();
  310.  
  311. if (buttonSelected == btnReport)
  312. {
  313. txaReport.append(
  314. "\n" + "\n Personal Information" + "\n" +txtCustomerName.getText() +
  315. "\n" +txtCustomerID.getText() + "\n" +txtAddress.getText() +
  316. "\n" +txtCity.getText()+ ", " + txtState.getText()+ " " +
  317. txtZip.getText()+ "\n Order History" + "\n" +txtProductName.getText() + "\n" +
  318. txtOrderNumber.getText() + "\n" +txtQuantity.getText() + "\n$" +
  319. txtTotalCost.getText() );
  320.  
  321. }
  322.  
  323. }
  324.  
  325. public void setTextToNotEditable()
  326. {
  327. //Lock the text fields
  328. txtCustomerID.setEditable(false);
  329. txtCustomerName.setEditable(false);
  330. txtAddress.setEditable(false);
  331. txtCity.setEditable(false);
  332. txtState.setEditable(false);
  333. txtZip.setEditable(false);
  334. txtPhone.setEditable(false);
  335. txtProductName.setEditable(false);
  336. txtQuantity.setEditable(false);
  337. txtOrderNumber.setEditable(false);
  338. txtUnitCost.setEditable(false);
  339. txtTotalCost.setEditable(false);
  340.  
  341. }
  342.  
  343. public void setTextToEditable()
  344. {
  345. //Unlock the text fields
  346. txtCustomerID.setEditable(true);
  347. txtCustomerName.setEditable(true);
  348. txtAddress.setEditable(true);
  349. txtCity.setEditable(true);
  350. txtState.setEditable(true);
  351. txtZip.setEditable(true);
  352. txtPhone.setEditable(true);
  353. txtProductName.setEditable(true);
  354. txtQuantity.setEditable(true);
  355. txtOrderNumber.setEditable(true);
  356. txtUnitCost.setEditable(true);
  357. txtTotalCost.setEditable(true);
  358.  
  359. }
  360.  
  361. public void ClearTextFields()
  362. {
  363. //Clear the Text Fields
  364. txtCustomerID.setText("");
  365. txtCustomerName.setText("");
  366. txtAddress.setText("");
  367. txtCity.setText("");
  368. txtState.setText("");
  369. txtZip.setText("");
  370. txtPhone.setText("");
  371. txtProductName.setText("");
  372. txtOrderNumber.setText("");
  373. txtID.setText("");
  374. txtQuantity.setText("");
  375. txtUnitCost.setText("");
  376. txtTotalCost.setText("");
  377. }
  378.  
  379. public void Add()
  380. {
  381. //Add a new record
  382. showStatus("");
  383. //Empty the text fields
  384. setTextToEditable();
  385. ClearTextFields();
  386. txtCustomerID.requestFocus ();
  387.  
  388. //Change the button labels
  389. btnAdd.setLabel("OK");
  390.  
  391. btnCancel.setEnabled(true);
  392.  
  393. //Disable the Delete and Edit buttons
  394. btnDelete.setEnabled(false);
  395. btnEdit.setEnabled(false);
  396. }
  397.  
  398. public void Save()
  399. {
  400. String strCustomerID = lstNames.getSelectedItem();
  401.  
  402. //Save the new record
  403. // Activated when Add button has an "OK" label
  404. if (txtCustomerID.getText().length() == 0 || txtCustomerName.getText().length() == 0)
  405. showStatus("The Customer Name or ID is blank");
  406. else
  407. {
  408. try
  409. {
  410. Statement cmdCustomers = conCustomers.createStatement();
  411. cmdCustomers.executeUpdate( "Insert Into Customers "
  412. + "( CustomerName, CustomerID, PhoneNumber, Address, City, State, Zip) "
  413. + "Values('"
  414. + txtCustomerName.getText() + "', '"
  415. + txtCustomerID.getText() + "', '"
  416. + txtPhone.getText() + "', '"
  417. + txtAddress.getText() + "', '"
  418. + txtCity.getText() + "', '"
  419. + txtState.getText() + "', '"
  420. + txtZip.getText() + "')");
  421.  
  422. Statement cmdCustomers2 = conCustomers.createStatement();
  423. cmdCustomers2.executeUpdate( "Insert Into Orders "
  424. + "( ProductName, ID, OrderNumber, Quantity, UnitCost) "
  425. + "Values('"
  426. + txtProductName.getText() + "', '"
  427. + txtID.getText() + "', '"
  428. + txtOrderNumber.getText() + "', '"
  429. + txtQuantity.getText() + "', '"
  430. + txtUnitCost.getText() + "')");
  431.  
  432.  
  433.  
  434. //Add to ID list
  435. lstNames.add(txtCustomerID.getText());
  436. //Reset buttons
  437. Cancel();
  438. }
  439. catch(SQLException error)
  440. {
  441. showStatus("Error: " + error.toString());
  442. }
  443. }
  444. }
  445.  
  446. public void Delete()
  447. {
  448. //Delete the current record
  449. int intIndex = lstNames.getSelectedIndex();
  450. String strCustomerID = lstNames.getSelectedItem();
  451. if(intIndex == 0) //Make sure a record is selected
  452. //Position zero holds a message
  453. showStatus("Please select the record to be deleted");
  454. else
  455. {
  456. try
  457. {
  458. //Delete from Database
  459. Statement cmdCustomers = conCustomers.createStatement();
  460. cmdCustomers.executeUpdate("Delete from Customers Where CustomerID = '"+strCustomerID+"'");
  461.  
  462. Statement cmdCustomers2 = conCustomers.createStatement();
  463. cmdCustomers2.executeUpdate("Delete from Orders Where ID = '"+strCustomerID+"'");
  464.  
  465.  
  466. ClearTextFields(); //Delete from screen
  467. lstNames.remove(intIndex); //Delete from list
  468. showStatus("Record deleted"); //Display message
  469. }
  470. catch(SQLException error) //Exception Handling
  471. {
  472. showStatus("Error during Delete"); //Displayed if record can not be deleted
  473. }
  474. }
  475. }
  476.  
  477. public void Cancel()
  478. {
  479. //Enable the Delete and Edit buttons
  480. btnDelete.setEnabled(true);
  481. btnEdit.setEnabled(true);
  482. btnCancel.setEnabled(false);
  483.  
  484. //Change caption of button
  485. btnAdd.setLabel("Add");
  486.  
  487. //Clear the text fields and status bar
  488. ClearTextFields();
  489. showStatus("");
  490. }
  491.  
  492. public void Edit() {
  493. // Save the modified record
  494. String strCustomerID = lstNames.getSelectedItem();
  495. String updateLine = null;
  496. int intIndex = lstNames.getSelectedIndex();
  497. if (intIndex == 0) // Make sure a record is selected
  498. // Position zero holds a message
  499. showStatus("Please select the record to be changed");
  500. else {
  501. try {
  502.  
  503. Statement cmdCustomers = conCustomers.createStatement();
  504.  
  505. updateLine = String.format("UPDATE Customers SET CustomerName = '%s', PhoneNumber = '%s', Address = '%s', City = '%s', State = '%s', Zip = '%s' WHERE CustomerID = '%s'",
  506. txtCustomerName.getText(),
  507. txtPhone.getText(),
  508. txtAddress.getText(),
  509. txtCity.getText(),
  510. txtState.getText(),
  511. txtZip.getText(),
  512. strCustomerID);
  513. cmdCustomers.executeUpdate(updateLine);
  514.  
  515. String updateLine2 = String.format("UPDATE Orders SET ProductName = '%s', OrderNumber = '%s', Quantity = '%s', UnitCost = '%s' WHERE ID = '%s'",
  516. txtProductName.getText(),
  517. txtOrderNumber.getText(),
  518. txtQuantity.getText(),
  519. txtUnitCost.getText(),
  520. strCustomerID);
  521. Statement cmdCustomers2 = conCustomers.createStatement();
  522. cmdCustomers2.executeUpdate(updateLine2);
  523.  
  524. if (!strCustomerID.equals(txtCustomerID.getText())) {
  525. // Last name changed; change the list
  526. lstNames.remove(intIndex); // Remove the old entry
  527. lstNames.add(txtCustomerID.getText()); // Add the new entry
  528. }
  529. } catch (SQLException error) {
  530. showStatus(updateLine); //showStatus("Error: " + error.toString());
  531. }
  532. }
  533. }
  534. public void firstRecord() //Navigates to the first record in the listbox
  535. {
  536. String strCustomerID = lstNames.getSelectedItem();
  537. int first = 1;
  538. lstNames.select(first);
  539.  
  540. try
  541. {
  542. cmdCustomers = conCustomers.createStatement();
  543. rsCustomers = cmdCustomers.executeQuery( "Select * From Customers Where CustomerID = \'"+strCustomerID+"\'");
  544.  
  545. txtCustomerID.setText(strCustomerID);
  546. displayRecord(rsCustomers);
  547. }
  548.  
  549. catch(SQLException error) //Exception Handling
  550. {
  551. showStatus("item state try.");
  552. }
  553. }
  554.  
  555. public void previousRecord() //Navigates backwards through the records in the listbox
  556. {
  557. String strCustomerID = lstNames.getSelectedItem();
  558. int PreviousIndex = 0;
  559. PreviousIndex = lstNames.getSelectedIndex();
  560. PreviousIndex--;
  561.  
  562. if (PreviousIndex > 0)
  563. {
  564. lstNames.select(PreviousIndex);
  565.  
  566. try
  567. {
  568. Statement cmdCustomers = conCustomers.createStatement();
  569. rsCustomers = cmdCustomers.executeQuery( "Select * From Orders, Customers Where Orders.ID = \'"+strCustomerID+"\' and Customers.CustomerID = \'"+strCustomerID+"\'");
  570. txtCustomerID.setText(strCustomerID);
  571. displayRecord(rsCustomers);
  572. }
  573. catch(SQLException error)
  574. {
  575. showStatus("item state try.");
  576. }
  577. }
  578. else
  579. {
  580. showStatus("You've reached the beginning of the list.");
  581. }
  582. }
  583.  
  584. public void nextRecord() //Navigates to the next record in the listbox
  585. {
  586. String strCustomerID = lstNames.getSelectedItem();
  587. int NextIndex = 0;
  588. NextIndex = lstNames.getSelectedIndex();
  589. if(NextIndex == 0)
  590. {
  591. NextIndex = 1;
  592. }
  593. else
  594. {
  595. NextIndex++;
  596. }
  597. if (NextIndex < lstNames.getItemCount())
  598. {
  599.  
  600. lstNames.select(NextIndex);
  601.  
  602. try
  603. {
  604. Statement cmdCustomers = conCustomers.createStatement();
  605. rsCustomers = cmdCustomers.executeQuery( "Select * From Orders, Customers Where Orders.ID = \'"+strCustomerID+"\' and Customers.CustomerID = \'"+strCustomerID+"\'");
  606. txtCustomerID.setText(strCustomerID);
  607. displayRecord(rsCustomers);
  608.  
  609. }
  610.  
  611. catch(SQLException error)
  612. {
  613. showStatus("item state try.");
  614. }
  615. }
  616. else
  617. {
  618. showStatus("You've reached the end of the list.");
  619. }
  620. }
  621.  
  622. public void lastRecord() //Navigates to the last record held in the listbox
  623. {
  624. String strCustomerID = lstNames.getSelectedItem();
  625. int last = 0;
  626.  
  627. while (last < lstNames.getItemCount()-1)
  628. {
  629. last++;
  630. }
  631.  
  632. lstNames.select(last);
  633.  
  634. try
  635. {
  636.  
  637. Statement cmdCustomers = conCustomers.createStatement();
  638. rsCustomers = cmdCustomers.executeQuery( "Select * From Orders, Customers Where Orders.ID = \'"+strCustomerID+"\' and Customers.CustomerID = \'"+strCustomerID+"\'");
  639. txtCustomerID.setText(strCustomerID);
  640. displayRecord(rsCustomers);
  641. }
  642.  
  643. catch(SQLException error)
  644. {
  645. showStatus("item state try.");
  646. }
  647. }
  648.  
  649.  
  650. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement