Advertisement
Guest User

Untitled

a guest
Mar 9th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.42 KB | None | 0 0
  1. public class PurchaseItem extends JFrame implements ActionListener {
  2.  
  3. JTextField stockNo = new JTextField (7);
  4. TextArea information = new TextArea (5, 30);
  5. JButton check = new JButton ("Check Stock");
  6. JButton Buy = new JButton ("Buy Item");
  7. DecimalFormat pounds = new DecimalFormat ("£#,##0.00");
  8. JTextField quantity = new JTextField (7);
  9.  
  10.  
  11. public PurchaseItem() {
  12. JFrame PurchaseItem = new JFrame ();
  13. PurchaseItem.setSize ( 500, 250 );
  14. PurchaseItem.setLocationRelativeTo (null);
  15. PurchaseItem.setVisible (true);
  16. PurchaseItem.setTitle("Purchase Item");
  17. PurchaseItem.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  18. PurchaseItem.setResizable(false);
  19.  
  20.  
  21. PurchaseItem.setLayout(new GridBagLayout());
  22. GridBagConstraints gbc = new GridBagConstraints();
  23.  
  24. gbc.weightx = 1;
  25. gbc.weighty = 1;
  26.  
  27. gbc.gridx = 2;
  28. gbc.gridy = 0;
  29. PurchaseItem.add(check, gbc);
  30.  
  31. gbc.gridx = 1;
  32. gbc.gridy = 2;
  33. PurchaseItem.add(information, gbc);
  34.  
  35. gbc.gridx = 1;
  36. gbc.gridy = 3;
  37. PurchaseItem.add(Buy, gbc);
  38.  
  39. gbc.anchor = GridBagConstraints.EAST;
  40. gbc.gridx = 0;
  41. gbc.gridy = 0;
  42. PurchaseItem.add(new JLabel ("Enter Stock Number: "), gbc);
  43.  
  44. gbc.gridx = 0;
  45. gbc.gridy = 1;
  46. PurchaseItem.add(new JLabel("Enter Quantity: "), gbc);
  47.  
  48. gbc.anchor = GridBagConstraints.CENTER;
  49. gbc.gridx = 1;
  50. gbc.gridy = 0;
  51. PurchaseItem.add(stockNo, gbc);
  52.  
  53. gbc.gridx = 1;
  54. gbc.gridy = 1;
  55. PurchaseItem.add(quantity, gbc);
  56.  
  57. Buy.addActionListener(this);
  58. check.addActionListener(this);
  59. }
  60.  
  61. @Override
  62.  
  63. public void actionPerformed(ActionEvent e) {
  64.  
  65. String key = stockNo.getText();
  66. String name = StockData.getName(key);
  67. String q = quantity.getText();
  68. int stock = StockData.getQuantity(key);
  69.  
  70. try {
  71. int qty = Integer.parseInt(quantity.getText());
  72. information.requestFocusInWindow();
  73.  
  74. } catch (Exception z) {
  75. if (name == null){
  76. JOptionPane.showMessageDialog(this, "Enter valid stock number!",
  77. "Error", JOptionPane.ERROR_MESSAGE);
  78. information.setText("");
  79. information.requestFocusInWindow();
  80. return;
  81. } else {
  82. JOptionPane.showMessageDialog(this, "Enter quantity using numbers!",
  83. "Error", JOptionPane.ERROR_MESSAGE);
  84. information.setText("");
  85. information.requestFocusInWindow();
  86. return;
  87. }
  88.  
  89. } if (e.getSource() == check) {
  90. int qty = Integer.parseInt(quantity.getText());
  91. double TotalPrice;
  92. TotalPrice = (StockData.getPrice(key) * qty);
  93. information.setText(name);
  94. information.append("nPrice: " + pounds.format(StockData.getPrice(key)));
  95. information.append("nTotal in stock: " + StockData.getQuantity(key));
  96. information.append("nQuantity: " + qty);
  97. information.append("nTotalPrice: " + pounds.format(TotalPrice));
  98.  
  99. } if (e.getSource() == Buy){
  100. int qty = Integer.parseInt(quantity.getText());
  101. if (qty <= stock) {
  102. double TotalPrice;
  103. TotalPrice = (StockData.getPrice(key) * qty);
  104. StockData.update(key, - qty);
  105. information.getText();
  106. information.setText("Thank you for your order!");
  107. information.append("nProduct name: " + name);
  108. information.append("nQuantity: " + qty);
  109. information.append("nTotal Price: " + pounds.format(TotalPrice));
  110. information.append("nNow in stock: " + StockData.getQuantity(key));
  111.  
  112. } else {
  113. information.setText("Not enough stock");
  114. }
  115. }
  116. }
  117. }
  118.  
  119. public class ReadDatabase {
  120.  
  121. static Connection con;
  122. static Statement st;
  123. static PreparedStatement pst;
  124. static ResultSet rs;
  125.  
  126. public static void main(String[] args)throws SQLException{
  127.  
  128. try{
  129.  
  130. String dbURL = "jdbc:ucanaccess://C:/Users/Lee/Documents/Computing - Year 1/Programming/StockData.accdb";
  131. String username = "";
  132. String password = "";
  133.  
  134. con = DriverManager.getConnection(dbURL, username, password);
  135. String query = "select * from Stock";
  136. pst = con.prepareStatement(query);
  137. rs = null;
  138.  
  139. try{
  140. rs = pst.executeQuery();
  141. while(rs.next())
  142. {
  143. System.out.println(rs.getString("StockNo") +" "+ rs.getString("StockName") +" "+rs.getDouble("Price") +" "+rs.getString("Quantity"));
  144. }
  145. }catch(SQLException sql){}
  146. pst.close();
  147. con.close();
  148.  
  149. }
  150. catch(SQLException e)
  151. {}
  152. }
  153. }
  154.  
  155. public class StockData {
  156.  
  157. private static class Item {
  158.  
  159. Item(String n, double p, int q) {
  160. name = n;
  161. price = p;
  162. quantity = q;
  163. }
  164.  
  165. // get methods
  166. public String getName() {
  167. return name;
  168. }
  169.  
  170. public double getPrice() {
  171. return price;
  172. }
  173.  
  174. public int getQuantity() {
  175. return quantity;
  176. }
  177.  
  178. // instance variables
  179. private String name;
  180. private double price;
  181. public int quantity;
  182. }
  183.  
  184. // with a Map you use put to insert a key, value pair
  185. // and get(key) to retrieve the value associated with a key
  186. // You don't need to understand how this works!
  187. private static Map<String, Item> stock = new HashMap<String, Item>();
  188.  
  189. static {
  190. // if you want to have extra stock items, put them in here
  191. // use the same style - keys should be Strings
  192. stock.put("00", new Item("Bath towel", 5.50, 10));
  193. stock.put("11", new Item("Plebney light", 20.00, 5));
  194. stock.put("22", new Item("Gorilla suit", 30.00, 7));
  195. stock.put("33", new Item("Whizz games console", 50.00, 8));
  196. stock.put("44", new Item("Oven", 200.00, 4));
  197. stock.put("55", new Item("Games Console", 320.00, 15)); //new Stock
  198. stock.put("66", new Item("Football", 5.99, 20)); //new Stock
  199. stock.put("77", new Item("Mobile Phone", 599.99, 10)); //new Stock
  200. stock.put("88", new Item ("Trainers", 99.99, 10)); //new Stock
  201. stock.put("99", new Item ("Playing Cards", 2.99, 50)); //new Stock
  202.  
  203. }
  204.  
  205. public static String getName(String key) {
  206. Item item = stock.get(key);
  207. if (item == null) {
  208. return null; // null means no such item
  209. } else {
  210. return item.getName();
  211. }
  212. }
  213.  
  214. public static double getPrice(String key) {
  215. Item item = stock.get(key);
  216. if (item == null) {
  217. return 0; // negative price means no such item
  218. } else {
  219. return item.getPrice();
  220. }
  221. }
  222.  
  223. public static int getQuantity(String key) {
  224. Item item = stock.get(key);
  225. if (item == null) {
  226. return 0; // negative quantity means no such item
  227. } else {
  228. return item.getQuantity();
  229. }
  230. }
  231.  
  232. // update stock levels
  233. // extra is +ve if adding stock
  234. // extra is -ve if selling stock
  235. public static void update(String key, int extra) {
  236. Item item = stock.get(key);
  237. if (item != null) {
  238. item.quantity += extra;
  239. }
  240. }
  241.  
  242. public static void close() {
  243. // Does nothing for this static version.
  244. // Write a statement to close the database when you are using one
  245. }
  246.  
  247. }
  248.  
  249. public class UpdateStock extends JFrame implements ActionListener {
  250.  
  251. JTextField IncQuantity = new JTextField (7);
  252. JTextField DecQuantity = new JTextField (7);
  253. JButton increase = new JButton ("+");
  254. DecimalFormat pounds = new DecimalFormat ("£#,##0.00");
  255.  
  256. JTextField stockNo = new JTextField (7);
  257. JButton check = new JButton ("Check Stock");
  258. TextArea information = new TextArea (5, 25);
  259.  
  260. public UpdateStock() {
  261. JFrame UpdateStock = new JFrame ();
  262. UpdateStock.setSize ( 500, 200 );
  263. UpdateStock.setLocationRelativeTo (null);
  264. UpdateStock.setVisible (true);
  265. UpdateStock.setTitle("Update Stock");
  266. UpdateStock.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  267. UpdateStock.setResizable(false);
  268.  
  269.  
  270. UpdateStock.setLayout(new GridBagLayout());
  271. GridBagConstraints gbc = new GridBagConstraints();
  272. gbc.weightx = 1;
  273. gbc.weighty = 1;
  274.  
  275. ////First Column////
  276. gbc.gridx = 1;
  277. gbc.gridy = 0;
  278. UpdateStock.add(new JLabel("Enter Stock Number"), gbc);
  279.  
  280.  
  281. //// Second Column /////
  282. gbc.gridx = 2;
  283. gbc.gridy = 0;
  284. UpdateStock.add(stockNo, gbc);
  285. gbc.gridy++;
  286. UpdateStock.add(information, gbc);
  287.  
  288. ////Third Column////
  289. gbc.gridx = 3;
  290. gbc.gridy = 0;
  291. UpdateStock.add(check, gbc);
  292. gbc.gridy++;
  293. UpdateStock.add(IncQuantity, gbc);
  294. gbc.gridy++;
  295. UpdateStock.add(increase, gbc);
  296.  
  297.  
  298. check.addActionListener(this);
  299. increase.addActionListener(this);
  300.  
  301. }
  302. @Override
  303. public void actionPerformed(ActionEvent e) {
  304.  
  305. String key = stockNo.getText();
  306. String name = StockData.getName(key);
  307. int stock = StockData.getQuantity(key);
  308. String I = IncQuantity.getText();
  309.  
  310. if (name == null) {
  311. JOptionPane.showMessageDialog(this, "Enter valid stock number!",
  312. "Error", JOptionPane.ERROR_MESSAGE);
  313. information.setText("");
  314. information.requestFocusInWindow();
  315. return;
  316. } else {
  317. information.setText(name);
  318. information.append("nPrice: " + pounds.format(StockData.getPrice(key)));
  319. information.append("nNumber in stock: " + StockData.getQuantity(key));
  320. } try {
  321. int Inc = Integer.parseInt(IncQuantity.getText());
  322. information.requestFocusInWindow();
  323.  
  324. } catch (Exception z) {
  325. if (I.isEmpty()){
  326. return;
  327. }
  328. JOptionPane.showMessageDialog(this, "Enter Quantity - Only numbers allowed!",
  329. "Error", JOptionPane.ERROR_MESSAGE);
  330. information.setText(name);
  331. information.append("nPrice: " + pounds.format(StockData.getPrice(key)));
  332. information.append("nNumber in stock: " + StockData.getQuantity(key));
  333. information.requestFocusInWindow();
  334. return;
  335.  
  336. } if (e.getSource() == increase) {
  337. int Inc = Integer.parseInt(IncQuantity.getText());
  338. StockData.update(key, + Inc);
  339. information.setText(name);
  340. information.append("nPrice: " + pounds.format(StockData.getPrice(key)));
  341. information.append("nNow in stock: " + StockData.getQuantity(key));
  342.  
  343. }
  344.  
  345. }
  346. }
  347.  
  348. public class UpdateDatabase extends JFrame{
  349. JLabel JL_StockName,JL_Price,JL_Quantity,JL_StockNo;
  350. JTextField JT_StockName,JT_Price,JT_Quantity,JT_StockNo;
  351. JButton btn_insert,btn_update,btn_delete;
  352.  
  353. static PreparedStatement pst;
  354. public UpdateDatabase(){
  355. super("INSERT UPDATE DELETE");
  356. JL_StockNo = new JLabel("StockNo:");
  357. JL_StockName = new JLabel("StockName:");
  358. JL_Price = new JLabel("Price:");
  359. JL_Quantity = new JLabel("Quantity:");
  360. JL_StockNo.setBounds(20, 20, 100, 20);
  361. JL_StockName.setBounds(20, 50, 100, 20);
  362. JL_Price.setBounds(20, 80, 100, 20);
  363. JL_Quantity.setBounds(20, 110, 100, 20);
  364.  
  365. JT_StockNo = new JTextField(20);
  366. JT_StockName = new JTextField(20);
  367. JT_Price = new JTextField(20);
  368. JT_Quantity = new JTextField(20);
  369. JT_StockNo.setBounds(130,20,150,20);
  370. JT_StockName.setBounds(130, 50, 150, 20);
  371. JT_Price.setBounds(130, 80, 150, 20);
  372. JT_Quantity.setBounds(130, 110, 150, 20);
  373. btn_insert = new JButton("Insert");
  374. btn_update = new JButton("Update");
  375. btn_delete = new JButton("Delete");
  376. btn_insert.setBounds(300, 50, 80, 20);
  377. btn_update.setBounds(300, 80, 80, 20);
  378. btn_delete.setBounds(300, 110, 80, 20);
  379.  
  380.  
  381. setLayout(null);
  382. add(JL_StockNo);
  383. add(JL_StockName);
  384. add(JL_Price);
  385. add(JL_Quantity);
  386. add(JT_StockNo);
  387. add(JT_StockName);
  388. add(JT_Price);
  389. add(JT_Quantity);
  390. add(btn_insert);
  391. add(btn_update);
  392. add(btn_delete);
  393.  
  394.  
  395. //button insert
  396. btn_insert.addActionListener(new ActionListener() {
  397.  
  398. public void actionPerformed(ActionEvent e) {
  399. try{
  400. theQuery("insert into Stock (StockNo,StockName,Price,Quantity) values('"+JT_StockNo.getText()+"','"+JT_StockName.getText()+"','"+JT_Price.getText()+"',"+JT_Quantity.getText()+")");
  401. }
  402. catch(Exception ex){}
  403. }
  404. });
  405.  
  406. //button update
  407. btn_update.addActionListener(new ActionListener() {
  408.  
  409. public void actionPerformed(ActionEvent e) {
  410. try{
  411.  
  412. theQuery("update Stock set StockName = '"+JT_StockName.getText()+"',Price = '"+JT_Price.getText()+"', Quantity = "+JT_Quantity.getText()+" where StockNo = "+JT_StockNo.getText());
  413. }
  414. catch(Exception ex){}
  415. }
  416. });
  417.  
  418. //button delete
  419. btn_delete.addActionListener(new ActionListener() {
  420.  
  421. public void actionPerformed(ActionEvent e) {
  422. try{
  423.  
  424. theQuery("delete from Stock where StockNo = "+JT_StockNo.getText());
  425. }
  426. catch(Exception ex){}
  427. }
  428. });
  429.  
  430.  
  431. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  432. setVisible(true);
  433. setLocationRelativeTo(null);
  434. setSize(500,200);
  435.  
  436. }
  437.  
  438. //function to execute the insert update delete query
  439. public void theQuery(String query){
  440. Connection con = null;
  441. Statement st = null;
  442. try{
  443. con = DriverManager.getConnection("jdbc:ucanaccess://C:/Users/Lee/Documents/Computing - Year 1/Programming/StockData.accdb","root","");
  444. st = con.createStatement();
  445. st.executeUpdate(query);
  446. String StockU = "Select * from stock";
  447. pst = con.prepareStatement(StockU);
  448. JOptionPane.showMessageDialog(null,"Query Executed");
  449. rs = pst.executeQuery();
  450. while(rs.next())
  451. {
  452. System.out.println(rs.getString("StockNo") +" "+ rs.getString("StockName") +" "+rs.getDouble("Price") +" "+rs.getString("Quantity"));
  453. }
  454. }catch(Exception ex){
  455. JOptionPane.showMessageDialog(null,ex.getMessage());
  456. }
  457. }
  458.  
  459.  
  460. public static void main(String[] args){
  461.  
  462. new UpdateDatabase();
  463. }
  464. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement