Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.Container;
- import java.awt.FlowLayout;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.Scanner;
- import javax.swing.BorderFactory;
- import javax.swing.BoxLayout;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- public class project_GUI extends JFrame {
- private static final long serialVersionUID = 1L;
- private JTextField jtfCarNum, jtfCarNPlate, jtfModel, jtfCapacity, jtfDateP, jtfInsSta, jtfDateI, jtfInExDa, jtfCar,
- jtfBrand, jtfAvail;
- private JButton jbSearch, jbUpdate, jbDelete, jbClear, jbNext, jbPrevious, jbCancel, jbChoose;
- private Container myContents;
- private ActionHandler buttonHandler;
- private car[] bil;
- private int index, tracker, counter;
- private PrintWriter printToFile;
- project_GUI() {
- super("Car Rental");
- configureFrame();
- initialiseInstances();
- designInterface();
- }
- public void configureFrame() {
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setSize(700, 500);
- this.setLocationRelativeTo(null);
- this.setResizable(false);
- myContents = this.getContentPane();
- }
- public void initialiseInstances() {
- jtfBrand = new JTextField(20);
- jtfCarNum = new JTextField(20);
- jtfCarNPlate = new JTextField(20);
- jtfModel = new JTextField(20);
- jtfCapacity = new JTextField(20);
- jtfDateP = new JTextField(20);
- jtfInsSta = new JTextField(20);
- jtfDateI = new JTextField(20);
- jtfInExDa = new JTextField(20);
- jtfAvail = new JTextField(20);
- jtfCar = new JTextField(20);
- buttonHandler = new ActionHandler();
- jbSearch = new JButton("Search");
- jbSearch.addActionListener(buttonHandler);
- jbUpdate = new JButton("Add Car");
- jbUpdate.addActionListener(buttonHandler);
- jbDelete = new JButton("Delete");
- jbDelete.addActionListener(buttonHandler);
- jbClear = new JButton("Clear");
- jbClear.addActionListener(buttonHandler);
- jbNext = new JButton("Next >>");
- jbNext.addActionListener(buttonHandler);
- jbPrevious = new JButton("<< Previous");
- jbPrevious.addActionListener(buttonHandler);
- jbCancel = new JButton("CANCEL");
- jbCancel.addActionListener(buttonHandler);
- jbChoose = new JButton("Choose This Car");
- jbChoose.addActionListener(buttonHandler);
- bil = new car[10];
- tracker = 0;
- counter = -1;
- }
- private void designInterface() {
- JPanel p1 = new JPanel();
- p1.setLayout(new GridLayout(10, 1));
- p1.add(new JLabel("Car Brand"));
- p1.add(new JLabel("Car Number"));
- p1.add(new JLabel("Car Nr. Plate"));
- p1.add(new JLabel("Model"));
- p1.add(new JLabel("Capacity"));
- p1.add(new JLabel("Date Purchased"));
- p1.add(new JLabel("Insurance Status"));
- p1.add(new JLabel("Date Insured"));
- p1.add(new JLabel("Insurance Expire Date"));
- p1.add(new JLabel("Availability"));
- JPanel p2 = new JPanel();
- p2.setLayout(new GridLayout(10, 1));
- p2.add(jtfBrand);
- p2.add(jtfCarNum);
- p2.add(jtfCarNPlate);
- p2.add(jtfModel);
- p2.add(jtfCapacity);
- p2.add(jtfDateP);
- p2.add(jtfInsSta);
- p2.add(jtfDateI);
- p2.add(jtfInExDa);
- p2.add(jtfAvail);
- JPanel p3 = new JPanel();
- p3.setLayout(new BorderLayout());
- p3.add(p1, BorderLayout.WEST);
- p3.add(p2, BorderLayout.EAST);
- p3.setBorder(BorderFactory.createTitledBorder("Update Data"));
- JPanel p4 = new JPanel();
- p4.setLayout(new GridLayout(3, 1));
- p4.add(jbSearch);
- p4.add(jtfCar);
- p4.add(jbUpdate);
- jtfCar.setBorder(BorderFactory.createTitledBorder("Brand"));
- JPanel p5 = new JPanel();
- p5.setLayout(new BorderLayout());
- p5.add(jbDelete, BorderLayout.WEST);
- p5.add(jbClear, BorderLayout.EAST);
- JPanel p6 = new JPanel();
- p6.setLayout(new BorderLayout());
- p6.add(jbNext, BorderLayout.NORTH);
- p6.add(jbPrevious, BorderLayout.SOUTH);
- JPanel p7 = new JPanel();
- p7.setLayout(new BoxLayout(p7, BoxLayout.PAGE_AXIS));
- p7.add(p4);
- p7.add(p5);
- p7.add(p6);
- p7.setBorder(BorderFactory.createTitledBorder("Search"));
- JPanel p9 = new JPanel();
- p9.setLayout(new FlowLayout());
- p9.add(jbCancel);
- p9.add(jbChoose);
- JPanel p8 = new JPanel();
- p8.setLayout(new BorderLayout());
- p8.add(p3, BorderLayout.WEST);
- p8.add(p7, BorderLayout.EAST);
- p8.add(p9, BorderLayout.SOUTH);
- myContents.add(p8);
- }
- public int getFile() {
- tracker = 0;
- Scanner readData = null;
- String getCar = "/Users/VictoriaJansson/Desktop/CarFile.txt";
- File myFile = new File(getCar);
- try {
- readData = new Scanner(myFile);
- while (readData.hasNextLine()) {
- String[] lineCont = readData.nextLine().split(",");
- for(String x:lineCont){
- System.out.println(x);
- }
- if (lineCont.length == 10) {
- bil[tracker] = new car(lineCont[0], Integer.parseInt(lineCont[1]), lineCont[2], lineCont[3],
- Integer.parseInt(lineCont[4]), lineCont[5], lineCont[6], lineCont[7], lineCont[8],
- lineCont[9]);
- tracker++;
- }
- }
- } catch (FileNotFoundException e) {
- System.out.println("File not found");
- }
- for(int i = 0;i<tracker;i++){
- System.out.println(bil[i]);
- }
- return tracker;
- }
- public int searchCar(int num) {
- counter = -1;
- for (int i = 0; i < tracker; i++) {
- if (bil[i].getcarNum() == num) {
- counter = i;
- }
- }
- return counter;
- }
- public int searchForCar() {
- counter = -1;
- String text = jtfCar.getText().toString();
- for (int i = 0; i < tracker; i++) {
- if (text.equalsIgnoreCase(bil[i].getbrand())) {
- counter = i;
- }
- }
- return counter;
- }
- public int removeCar(int remove) {
- if (tracker == 0) {
- JOptionPane.showMessageDialog(null, "There are no cars to remove", "No cars",
- JOptionPane.INFORMATION_MESSAGE);
- } else if (specNum(remove) == -1) {
- JOptionPane.showMessageDialog(null, "The car was not found", "No cars Found",
- JOptionPane.INFORMATION_MESSAGE);
- } else {
- index = specNum(remove);
- {
- bil[index] = bil[tracker - 1];
- bil[tracker - 1] = new car();
- tracker--;
- FileWriter write;
- File myCarFile = new File("/Users/VictoriaJansson/Desktop/CarFile.txt");
- try {
- write = new FileWriter(myCarFile, false);
- printToFile = new PrintWriter(write);
- for (int i = 0; i < tracker; i++) {
- car x = bil[i];
- printToFile.write(x.getbrand());
- printToFile.write(",");
- printToFile.write(x.getcarNum());
- printToFile.write(",");
- printToFile.write(x.getcarNPlate());
- printToFile.write(",");
- printToFile.write(x.getmodel());
- printToFile.write(",");
- printToFile.write(x.getcapasaty());
- printToFile.write(",");
- printToFile.write(x.getdateP());
- printToFile.write(",");
- printToFile.write(x.getInssta());
- printToFile.write(",");
- printToFile.write(x.getDateI());
- printToFile.write(",");
- printToFile.write(x.getInExDa());
- printToFile.write(",");
- printToFile.write(x.getavalible());
- printToFile.println();
- }
- printToFile.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- JOptionPane.showMessageDialog(null, "The Car is deleted", "No cars", JOptionPane.INFORMATION_MESSAGE);
- }
- }
- return tracker;
- }
- public void writeToCarFile(JTextField Brand, JTextField carNum, JTextField carPlate, JTextField Model,
- JTextField cap, JTextField DateP, JTextField InsSta, JTextField DateI, JTextField InExDa,
- JTextField avail) {
- FileWriter write;
- File myDriveFile = new File("/Users/VictoriaJansson/Desktop/CarFile.txt");
- try {
- write = new FileWriter(myDriveFile, true);
- printToFile = new PrintWriter(write);
- printToFile.write(Brand.getText());
- printToFile.write(",");
- printToFile.write(carNum.getText());
- printToFile.write(",");
- printToFile.write(carPlate.getText());
- printToFile.write(",");
- printToFile.write(Model.getText());
- printToFile.write(",");
- printToFile.write(cap.getText());
- printToFile.write(",");
- printToFile.write(DateP.getText());
- printToFile.write(",");
- printToFile.write(InsSta.getText());
- printToFile.write(",");
- printToFile.write(DateI.getText());
- printToFile.write(",");
- printToFile.write(InExDa.getText());
- printToFile.write(",");
- printToFile.write(avail.getText());
- printToFile.println();
- printToFile.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public int addCar() {
- String brand2 = jtfBrand.getText().toString();
- int carNum = Integer.parseInt(jtfCarNum.getText().toString());
- String carNPlate = jtfCarNPlate.getText().toString();
- String model = jtfModel.getText().toString();
- int capasaty = Integer.parseInt(jtfCapacity.getText().toString());
- String dateP = jtfDateP.getText().toString();
- String Inssta = jtfInsSta.getText().toString();
- String DateI = jtfDateI.getText().toString();
- String InExDa = jtfInExDa.getText().toString();
- String avalible = jtfAvail.getText().toString();
- bil[tracker] = new car(brand2, carNum, carNPlate, model, capasaty, dateP, Inssta, DateI, InExDa, avalible);
- JOptionPane.showMessageDialog(null, "You have now added a car", "New Driver", JOptionPane.INFORMATION_MESSAGE);
- tracker++;
- return tracker;
- }
- public int specNum(int num) { // Byt namn på variabler
- int o = -1;
- getFile();
- for (int i = 0; i < tracker; i++) {
- if (bil[i].getcarNum() == num) {
- o = i;
- }
- }
- return o;
- }
- public int avail(int unAvail) {
- int niklas = -1; // Byt namn
- niklas = specNum(unAvail);
- bil[niklas].setAvalible("Unavailable");
- FileWriter write;
- File myDriveFile = new File("/Users/VictoriaJansson/Desktop/CarFile.txt");
- try {
- write = new FileWriter(myDriveFile, false);
- printToFile = new PrintWriter(write);
- for (int i = 0; i < tracker; i++) {
- car x = bil[i];
- printToFile.write(x.getbrand());
- printToFile.write(",");
- printToFile.write(x.getcarNum());
- System.out.println(x.getcarNum());
- printToFile.write(",");
- printToFile.write(x.getcarNPlate());
- printToFile.write(",");
- printToFile.write(x.getmodel());
- printToFile.write(",");
- printToFile.write(x.getcapasaty());
- printToFile.write(",");
- printToFile.write(x.getdateP());
- printToFile.write(",");
- printToFile.write(x.getInssta());
- printToFile.write(",");
- printToFile.write(x.getDateI());
- printToFile.write(",");
- printToFile.write(x.getInExDa());
- printToFile.write(",");
- printToFile.write(x.getavalible());
- printToFile.println();
- }
- printToFile.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return tracker;
- }
- public class ActionHandler implements ActionListener {
- @Override
- public void actionPerformed(ActionEvent e) {
- String strAction = e.getActionCommand().toString();
- switch (strAction) {
- case "Search":
- getFile();
- index = searchForCar();
- if (index == -1) { // behöver hjälp, hur blir
- // jag av med mitt problem
- // att den blir error om
- // filen är tom.
- JOptionPane.showMessageDialog(null, "There are no cars to search for", "No cars",
- JOptionPane.INFORMATION_MESSAGE);
- } else
- jtfBrand.setText(bil[index].getbrand());
- jtfCarNum.setText(Integer.toString(bil[index].getcarNum()));
- jtfCarNPlate.setText(bil[index].getcarNPlate());
- jtfModel.setText(bil[index].getmodel());
- jtfCapacity.setText(Integer.toString(bil[index].getcapasaty()));
- jtfDateP.setText(bil[index].getdateP());
- jtfInsSta.setText(bil[index].getInssta());
- jtfDateI.setText(bil[index].getDateI());
- jtfInExDa.setText(bil[index].getInExDa());
- jtfAvail.setText(bil[index].getavalible());
- break;
- case "Add Car":
- String num = jtfCarNum.getText().toString();
- int carNumber = Integer.parseInt(num);
- int n = -1;
- n = specNum(carNumber);
- if (n == -1) {
- addCar();
- writeToCarFile(jtfBrand, jtfCarNum, jtfCarNPlate, jtfModel, jtfCapacity, jtfDateP, jtfInsSta,
- jtfDateI, jtfInExDa, jtfAvail);
- }
- else
- JOptionPane.showMessageDialog(null, "The car already exists", "existing",
- JOptionPane.INFORMATION_MESSAGE);
- break;
- case "Delete":
- getFile();
- String brandNum = jtfCarNum.getText().toString();
- int hej = Integer.parseInt(brandNum); // byt namn
- index = removeCar(hej);
- System.out.println(tracker);
- break;
- case "Clear":
- jtfCarNum.setText("");
- jtfCarNPlate.setText("");
- jtfModel.setText("");
- jtfCapacity.setText("");
- jtfDateP.setText("");
- jtfInsSta.setText("");
- jtfDateI.setText("");
- jtfInExDa.setText("");
- jtfAvail.setText("");
- jtfCar.setText("");
- jtfBrand.setText("");
- break;
- case "Next >>":
- if (tracker != 0 && counter < tracker - 1) {
- counter++;
- jtfBrand.setText(bil[counter].getbrand());
- jtfCarNum.setText("" + bil[counter].getcarNum());
- jtfCarNPlate.setText(bil[counter].getcarNPlate());
- jtfModel.setText(bil[counter].getmodel());
- jtfCapacity.setText("" + bil[counter].getcapasaty());
- jtfDateP.setText(bil[counter].getdateP());
- jtfInsSta.setText(bil[counter].getInssta());
- jtfDateI.setText(bil[counter].getDateI());
- jtfInExDa.setText(bil[counter].getInExDa());
- jtfAvail.setText(bil[counter].getavalible());
- }
- break;
- case "<< Previous":
- if (tracker != 0 && counter > 0) {
- counter--;
- jtfBrand.setText(bil[counter].getbrand());
- jtfCarNum.setText("" + bil[counter].getcarNum());
- jtfCarNPlate.setText(bil[counter].getcarNPlate());
- jtfModel.setText(bil[counter].getmodel());
- jtfCapacity.setText("" + bil[counter].getcapasaty());
- jtfDateP.setText(bil[counter].getdateP());
- jtfInsSta.setText(bil[counter].getInssta());
- jtfDateI.setText(bil[counter].getDateI());
- jtfInExDa.setText(bil[counter].getInExDa());
- jtfAvail.setText(bil[counter].getavalible());
- }
- break;
- case "CANCEL":
- JOptionPane.showMessageDialog(null, "You chose to CANCEL", "Close", JOptionPane.INFORMATION_MESSAGE);
- System.exit(0);
- break;
- case "Choose This Car":
- int bodil = -1;
- String ava = jtfCarNum.getText().toString();
- int carN = Integer.parseInt(ava);
- boolean check = false;
- if (jtfAvail.getText().equals("Available")) {
- bodil = avail(carN);
- GUI_Driver myDriver = new GUI_Driver((jtfCarNum.getText()));
- myDriver.setVisible(true);
- check = true;
- }
- else if (!check)
- JOptionPane.showMessageDialog(null, "The car is rented", "Close", JOptionPane.INFORMATION_MESSAGE);
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement