Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.16 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Container;
  3. import java.awt.FlowLayout;
  4. import java.awt.GridLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.io.File;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileWriter;
  10. import java.io.IOException;
  11. import java.io.PrintWriter;
  12. import java.util.Scanner;
  13.  
  14. import javax.swing.BorderFactory;
  15. import javax.swing.BoxLayout;
  16. import javax.swing.JButton;
  17. import javax.swing.JFrame;
  18. import javax.swing.JLabel;
  19. import javax.swing.JOptionPane;
  20. import javax.swing.JPanel;
  21. import javax.swing.JTextField;
  22.  
  23. public class project_GUI extends JFrame {
  24.  
  25. private static final long serialVersionUID = 1L;
  26. private JTextField jtfCarNum, jtfCarNPlate, jtfModel, jtfCapacity, jtfDateP, jtfInsSta, jtfDateI, jtfInExDa, jtfCar,
  27. jtfBrand, jtfAvail;
  28. private JButton jbSearch, jbUpdate, jbDelete, jbClear, jbNext, jbPrevious, jbCancel, jbChoose;
  29. private Container myContents;
  30. private ActionHandler buttonHandler;
  31. private car[] bil;
  32. private int index, tracker, counter;
  33. private PrintWriter printToFile;
  34.  
  35. project_GUI() {
  36.  
  37. super("Car Rental");
  38.  
  39. configureFrame();
  40. initialiseInstances();
  41. designInterface();
  42. }
  43.  
  44. public void configureFrame() {
  45.  
  46. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  47. this.setSize(700, 500);
  48. this.setLocationRelativeTo(null);
  49. this.setResizable(false);
  50. myContents = this.getContentPane();
  51. }
  52.  
  53. public void initialiseInstances() {
  54.  
  55. jtfBrand = new JTextField(20);
  56. jtfCarNum = new JTextField(20);
  57.  
  58. jtfCarNPlate = new JTextField(20);
  59. jtfModel = new JTextField(20);
  60. jtfCapacity = new JTextField(20);
  61. jtfDateP = new JTextField(20);
  62. jtfInsSta = new JTextField(20);
  63. jtfDateI = new JTextField(20);
  64. jtfInExDa = new JTextField(20);
  65. jtfAvail = new JTextField(20);
  66. jtfCar = new JTextField(20);
  67.  
  68. buttonHandler = new ActionHandler();
  69.  
  70. jbSearch = new JButton("Search");
  71. jbSearch.addActionListener(buttonHandler);
  72.  
  73. jbUpdate = new JButton("Add Car");
  74. jbUpdate.addActionListener(buttonHandler);
  75.  
  76. jbDelete = new JButton("Delete");
  77. jbDelete.addActionListener(buttonHandler);
  78.  
  79. jbClear = new JButton("Clear");
  80. jbClear.addActionListener(buttonHandler);
  81.  
  82. jbNext = new JButton("Next >>");
  83. jbNext.addActionListener(buttonHandler);
  84.  
  85. jbPrevious = new JButton("<< Previous");
  86. jbPrevious.addActionListener(buttonHandler);
  87.  
  88. jbCancel = new JButton("CANCEL");
  89. jbCancel.addActionListener(buttonHandler);
  90.  
  91. jbChoose = new JButton("Choose This Car");
  92. jbChoose.addActionListener(buttonHandler);
  93.  
  94. bil = new car[10];
  95.  
  96. tracker = 0;
  97. counter = -1;
  98.  
  99. }
  100.  
  101. private void designInterface() {
  102.  
  103. JPanel p1 = new JPanel();
  104. p1.setLayout(new GridLayout(10, 1));
  105. p1.add(new JLabel("Car Brand"));
  106. p1.add(new JLabel("Car Number"));
  107. p1.add(new JLabel("Car Nr. Plate"));
  108. p1.add(new JLabel("Model"));
  109. p1.add(new JLabel("Capacity"));
  110. p1.add(new JLabel("Date Purchased"));
  111. p1.add(new JLabel("Insurance Status"));
  112. p1.add(new JLabel("Date Insured"));
  113. p1.add(new JLabel("Insurance Expire Date"));
  114. p1.add(new JLabel("Availability"));
  115.  
  116. JPanel p2 = new JPanel();
  117. p2.setLayout(new GridLayout(10, 1));
  118. p2.add(jtfBrand);
  119. p2.add(jtfCarNum);
  120. p2.add(jtfCarNPlate);
  121. p2.add(jtfModel);
  122. p2.add(jtfCapacity);
  123. p2.add(jtfDateP);
  124. p2.add(jtfInsSta);
  125. p2.add(jtfDateI);
  126. p2.add(jtfInExDa);
  127.  
  128. p2.add(jtfAvail);
  129.  
  130. JPanel p3 = new JPanel();
  131. p3.setLayout(new BorderLayout());
  132. p3.add(p1, BorderLayout.WEST);
  133. p3.add(p2, BorderLayout.EAST);
  134.  
  135. p3.setBorder(BorderFactory.createTitledBorder("Update Data"));
  136.  
  137. JPanel p4 = new JPanel();
  138. p4.setLayout(new GridLayout(3, 1));
  139. p4.add(jbSearch);
  140. p4.add(jtfCar);
  141. p4.add(jbUpdate);
  142.  
  143. jtfCar.setBorder(BorderFactory.createTitledBorder("Brand"));
  144.  
  145. JPanel p5 = new JPanel();
  146. p5.setLayout(new BorderLayout());
  147. p5.add(jbDelete, BorderLayout.WEST);
  148. p5.add(jbClear, BorderLayout.EAST);
  149.  
  150. JPanel p6 = new JPanel();
  151. p6.setLayout(new BorderLayout());
  152. p6.add(jbNext, BorderLayout.NORTH);
  153. p6.add(jbPrevious, BorderLayout.SOUTH);
  154.  
  155. JPanel p7 = new JPanel();
  156. p7.setLayout(new BoxLayout(p7, BoxLayout.PAGE_AXIS));
  157. p7.add(p4);
  158. p7.add(p5);
  159. p7.add(p6);
  160.  
  161. p7.setBorder(BorderFactory.createTitledBorder("Search"));
  162.  
  163. JPanel p9 = new JPanel();
  164. p9.setLayout(new FlowLayout());
  165. p9.add(jbCancel);
  166. p9.add(jbChoose);
  167.  
  168. JPanel p8 = new JPanel();
  169. p8.setLayout(new BorderLayout());
  170. p8.add(p3, BorderLayout.WEST);
  171. p8.add(p7, BorderLayout.EAST);
  172. p8.add(p9, BorderLayout.SOUTH);
  173.  
  174. myContents.add(p8);
  175. }
  176.  
  177. public int getFile() {
  178. tracker = 0;
  179.  
  180. Scanner readData = null;
  181. String getCar = "/Users/VictoriaJansson/Desktop/CarFile.txt";
  182. File myFile = new File(getCar);
  183.  
  184. try {
  185.  
  186. readData = new Scanner(myFile);
  187. while (readData.hasNextLine()) {
  188.  
  189. String[] lineCont = readData.nextLine().split(",");
  190. for(String x:lineCont){
  191. System.out.println(x);
  192. }
  193. if (lineCont.length == 10) {
  194. bil[tracker] = new car(lineCont[0], Integer.parseInt(lineCont[1]), lineCont[2], lineCont[3],
  195. Integer.parseInt(lineCont[4]), lineCont[5], lineCont[6], lineCont[7], lineCont[8],
  196. lineCont[9]);
  197.  
  198. tracker++;
  199. }
  200. }
  201.  
  202. } catch (FileNotFoundException e) {
  203.  
  204. System.out.println("File not found");
  205.  
  206. }
  207. for(int i = 0;i<tracker;i++){
  208. System.out.println(bil[i]);
  209. }
  210. return tracker;
  211. }
  212.  
  213. public int searchCar(int num) {
  214. counter = -1;
  215. for (int i = 0; i < tracker; i++) {
  216. if (bil[i].getcarNum() == num) {
  217. counter = i;
  218. }
  219. }
  220. return counter;
  221. }
  222.  
  223. public int searchForCar() {
  224. counter = -1;
  225.  
  226. String text = jtfCar.getText().toString();
  227. for (int i = 0; i < tracker; i++) {
  228.  
  229. if (text.equalsIgnoreCase(bil[i].getbrand())) {
  230. counter = i;
  231. }
  232. }
  233.  
  234. return counter;
  235. }
  236.  
  237. public int removeCar(int remove) {
  238.  
  239. if (tracker == 0) {
  240. JOptionPane.showMessageDialog(null, "There are no cars to remove", "No cars",
  241. JOptionPane.INFORMATION_MESSAGE);
  242.  
  243. } else if (specNum(remove) == -1) {
  244. JOptionPane.showMessageDialog(null, "The car was not found", "No cars Found",
  245. JOptionPane.INFORMATION_MESSAGE);
  246.  
  247. } else {
  248.  
  249. index = specNum(remove);
  250.  
  251. {
  252.  
  253. bil[index] = bil[tracker - 1];
  254. bil[tracker - 1] = new car();
  255. tracker--;
  256.  
  257. FileWriter write;
  258. File myCarFile = new File("/Users/VictoriaJansson/Desktop/CarFile.txt");
  259.  
  260. try {
  261. write = new FileWriter(myCarFile, false);
  262. printToFile = new PrintWriter(write);
  263.  
  264. for (int i = 0; i < tracker; i++) {
  265.  
  266. car x = bil[i];
  267.  
  268. printToFile.write(x.getbrand());
  269. printToFile.write(",");
  270. printToFile.write(x.getcarNum());
  271. printToFile.write(",");
  272. printToFile.write(x.getcarNPlate());
  273. printToFile.write(",");
  274. printToFile.write(x.getmodel());
  275. printToFile.write(",");
  276. printToFile.write(x.getcapasaty());
  277. printToFile.write(",");
  278. printToFile.write(x.getdateP());
  279. printToFile.write(",");
  280. printToFile.write(x.getInssta());
  281. printToFile.write(",");
  282. printToFile.write(x.getDateI());
  283. printToFile.write(",");
  284. printToFile.write(x.getInExDa());
  285. printToFile.write(",");
  286. printToFile.write(x.getavalible());
  287.  
  288. printToFile.println();
  289.  
  290. }
  291.  
  292. printToFile.close();
  293.  
  294. } catch (IOException e) {
  295. // TODO Auto-generated catch block
  296. e.printStackTrace();
  297. }
  298.  
  299. JOptionPane.showMessageDialog(null, "The Car is deleted", "No cars", JOptionPane.INFORMATION_MESSAGE);
  300. }
  301. }
  302. return tracker;
  303. }
  304.  
  305. public void writeToCarFile(JTextField Brand, JTextField carNum, JTextField carPlate, JTextField Model,
  306. JTextField cap, JTextField DateP, JTextField InsSta, JTextField DateI, JTextField InExDa,
  307. JTextField avail) {
  308.  
  309. FileWriter write;
  310. File myDriveFile = new File("/Users/VictoriaJansson/Desktop/CarFile.txt");
  311.  
  312. try {
  313. write = new FileWriter(myDriveFile, true);
  314.  
  315. printToFile = new PrintWriter(write);
  316. printToFile.write(Brand.getText());
  317. printToFile.write(",");
  318. printToFile.write(carNum.getText());
  319. printToFile.write(",");
  320. printToFile.write(carPlate.getText());
  321. printToFile.write(",");
  322. printToFile.write(Model.getText());
  323. printToFile.write(",");
  324. printToFile.write(cap.getText());
  325. printToFile.write(",");
  326. printToFile.write(DateP.getText());
  327. printToFile.write(",");
  328. printToFile.write(InsSta.getText());
  329. printToFile.write(",");
  330. printToFile.write(DateI.getText());
  331. printToFile.write(",");
  332. printToFile.write(InExDa.getText());
  333. printToFile.write(",");
  334. printToFile.write(avail.getText());
  335.  
  336. printToFile.println();
  337. printToFile.close();
  338.  
  339. } catch (IOException e) {
  340. // TODO Auto-generated catch block
  341. e.printStackTrace();
  342. }
  343. }
  344.  
  345. public int addCar() {
  346.  
  347. String brand2 = jtfBrand.getText().toString();
  348. int carNum = Integer.parseInt(jtfCarNum.getText().toString());
  349. String carNPlate = jtfCarNPlate.getText().toString();
  350. String model = jtfModel.getText().toString();
  351. int capasaty = Integer.parseInt(jtfCapacity.getText().toString());
  352. String dateP = jtfDateP.getText().toString();
  353. String Inssta = jtfInsSta.getText().toString();
  354. String DateI = jtfDateI.getText().toString();
  355. String InExDa = jtfInExDa.getText().toString();
  356. String avalible = jtfAvail.getText().toString();
  357.  
  358. bil[tracker] = new car(brand2, carNum, carNPlate, model, capasaty, dateP, Inssta, DateI, InExDa, avalible);
  359. JOptionPane.showMessageDialog(null, "You have now added a car", "New Driver", JOptionPane.INFORMATION_MESSAGE);
  360. tracker++;
  361.  
  362. return tracker;
  363.  
  364. }
  365.  
  366. public int specNum(int num) { // Byt namn på variabler
  367.  
  368. int o = -1;
  369. getFile();
  370. for (int i = 0; i < tracker; i++) {
  371. if (bil[i].getcarNum() == num) {
  372.  
  373. o = i;
  374. }
  375. }
  376.  
  377. return o;
  378. }
  379.  
  380. public int avail(int unAvail) {
  381.  
  382. int niklas = -1; // Byt namn
  383.  
  384. niklas = specNum(unAvail);
  385.  
  386. bil[niklas].setAvalible("Unavailable");
  387.  
  388. FileWriter write;
  389. File myDriveFile = new File("/Users/VictoriaJansson/Desktop/CarFile.txt");
  390.  
  391. try {
  392.  
  393. write = new FileWriter(myDriveFile, false);
  394. printToFile = new PrintWriter(write);
  395.  
  396. for (int i = 0; i < tracker; i++) {
  397.  
  398. car x = bil[i];
  399.  
  400. printToFile.write(x.getbrand());
  401. printToFile.write(",");
  402. printToFile.write(x.getcarNum());
  403. System.out.println(x.getcarNum());
  404. printToFile.write(",");
  405. printToFile.write(x.getcarNPlate());
  406. printToFile.write(",");
  407. printToFile.write(x.getmodel());
  408. printToFile.write(",");
  409. printToFile.write(x.getcapasaty());
  410. printToFile.write(",");
  411. printToFile.write(x.getdateP());
  412. printToFile.write(",");
  413. printToFile.write(x.getInssta());
  414. printToFile.write(",");
  415. printToFile.write(x.getDateI());
  416. printToFile.write(",");
  417. printToFile.write(x.getInExDa());
  418. printToFile.write(",");
  419. printToFile.write(x.getavalible());
  420.  
  421. printToFile.println();
  422.  
  423. }
  424.  
  425. printToFile.close();
  426.  
  427. } catch (IOException e) {
  428. // TODO Auto-generated catch block
  429. e.printStackTrace();
  430. }
  431. return tracker;
  432. }
  433.  
  434. public class ActionHandler implements ActionListener {
  435.  
  436. @Override
  437. public void actionPerformed(ActionEvent e) {
  438.  
  439. String strAction = e.getActionCommand().toString();
  440. switch (strAction) {
  441.  
  442. case "Search":
  443.  
  444. getFile();
  445. index = searchForCar();
  446.  
  447. if (index == -1) { // behöver hjälp, hur blir
  448. // jag av med mitt problem
  449. // att den blir error om
  450. // filen är tom.
  451.  
  452. JOptionPane.showMessageDialog(null, "There are no cars to search for", "No cars",
  453. JOptionPane.INFORMATION_MESSAGE);
  454.  
  455. } else
  456.  
  457. jtfBrand.setText(bil[index].getbrand());
  458. jtfCarNum.setText(Integer.toString(bil[index].getcarNum()));
  459. jtfCarNPlate.setText(bil[index].getcarNPlate());
  460. jtfModel.setText(bil[index].getmodel());
  461. jtfCapacity.setText(Integer.toString(bil[index].getcapasaty()));
  462. jtfDateP.setText(bil[index].getdateP());
  463. jtfInsSta.setText(bil[index].getInssta());
  464. jtfDateI.setText(bil[index].getDateI());
  465. jtfInExDa.setText(bil[index].getInExDa());
  466. jtfAvail.setText(bil[index].getavalible());
  467.  
  468. break;
  469.  
  470. case "Add Car":
  471.  
  472. String num = jtfCarNum.getText().toString();
  473. int carNumber = Integer.parseInt(num);
  474.  
  475. int n = -1;
  476.  
  477. n = specNum(carNumber);
  478.  
  479. if (n == -1) {
  480.  
  481. addCar();
  482. writeToCarFile(jtfBrand, jtfCarNum, jtfCarNPlate, jtfModel, jtfCapacity, jtfDateP, jtfInsSta,
  483. jtfDateI, jtfInExDa, jtfAvail);
  484. }
  485.  
  486. else
  487. JOptionPane.showMessageDialog(null, "The car already exists", "existing",
  488. JOptionPane.INFORMATION_MESSAGE);
  489.  
  490. break;
  491.  
  492. case "Delete":
  493.  
  494. getFile();
  495.  
  496. String brandNum = jtfCarNum.getText().toString();
  497. int hej = Integer.parseInt(brandNum); // byt namn
  498.  
  499. index = removeCar(hej);
  500.  
  501. System.out.println(tracker);
  502.  
  503. break;
  504.  
  505. case "Clear":
  506.  
  507. jtfCarNum.setText("");
  508. jtfCarNPlate.setText("");
  509. jtfModel.setText("");
  510. jtfCapacity.setText("");
  511. jtfDateP.setText("");
  512. jtfInsSta.setText("");
  513. jtfDateI.setText("");
  514. jtfInExDa.setText("");
  515. jtfAvail.setText("");
  516. jtfCar.setText("");
  517. jtfBrand.setText("");
  518.  
  519. break;
  520.  
  521. case "Next >>":
  522.  
  523. if (tracker != 0 && counter < tracker - 1) {
  524. counter++;
  525.  
  526. jtfBrand.setText(bil[counter].getbrand());
  527. jtfCarNum.setText("" + bil[counter].getcarNum());
  528. jtfCarNPlate.setText(bil[counter].getcarNPlate());
  529. jtfModel.setText(bil[counter].getmodel());
  530. jtfCapacity.setText("" + bil[counter].getcapasaty());
  531. jtfDateP.setText(bil[counter].getdateP());
  532. jtfInsSta.setText(bil[counter].getInssta());
  533. jtfDateI.setText(bil[counter].getDateI());
  534. jtfInExDa.setText(bil[counter].getInExDa());
  535. jtfAvail.setText(bil[counter].getavalible());
  536. }
  537.  
  538. break;
  539.  
  540. case "<< Previous":
  541.  
  542. if (tracker != 0 && counter > 0) {
  543. counter--;
  544.  
  545. jtfBrand.setText(bil[counter].getbrand());
  546. jtfCarNum.setText("" + bil[counter].getcarNum());
  547. jtfCarNPlate.setText(bil[counter].getcarNPlate());
  548. jtfModel.setText(bil[counter].getmodel());
  549. jtfCapacity.setText("" + bil[counter].getcapasaty());
  550. jtfDateP.setText(bil[counter].getdateP());
  551. jtfInsSta.setText(bil[counter].getInssta());
  552. jtfDateI.setText(bil[counter].getDateI());
  553. jtfInExDa.setText(bil[counter].getInExDa());
  554. jtfAvail.setText(bil[counter].getavalible());
  555. }
  556.  
  557. break;
  558.  
  559. case "CANCEL":
  560.  
  561. JOptionPane.showMessageDialog(null, "You chose to CANCEL", "Close", JOptionPane.INFORMATION_MESSAGE);
  562. System.exit(0);
  563.  
  564. break;
  565.  
  566. case "Choose This Car":
  567.  
  568. int bodil = -1;
  569.  
  570. String ava = jtfCarNum.getText().toString();
  571. int carN = Integer.parseInt(ava);
  572.  
  573. boolean check = false;
  574.  
  575. if (jtfAvail.getText().equals("Available")) {
  576.  
  577. bodil = avail(carN);
  578.  
  579. GUI_Driver myDriver = new GUI_Driver((jtfCarNum.getText()));
  580. myDriver.setVisible(true);
  581. check = true;
  582. }
  583.  
  584. else if (!check)
  585. JOptionPane.showMessageDialog(null, "The car is rented", "Close", JOptionPane.INFORMATION_MESSAGE);
  586.  
  587. break;
  588. }
  589.  
  590. }
  591. }
  592. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement