All pastes #19619 Raw Edit

flight information

public text v1 · immutable
#19619 ·published 2005-08-09 16:04 UTC
rendered paste body
import java.io.Serializable;
import javax.swing.*;

public class Customer implements Serializable {
static final long serialVersionUID = 5879368145892182712L;
	private String _AirCompany;
    private String _lastName;
    private String _FlightNumber;
    private String _FlyingFrom;
    private String _FlyingTo;
    private String _DateofFlight;
    private String _TimeofFlight;
    private String _FirstClassSeats;
    private String _SecondClassSeats;
    private String _ThirdClassSeats;
    private int _amount;

    Customer() {
		_AirCompany = "";
		_lastName = "";
		_amount = 0;
	}

Customer(String fn,String ln) {
		_AirCompany = fn;
		_lastName = ln;
		_amount = 0;
	}

    public String getFirstName() {
		return _AirCompany;
    }

    public String getLastName() {
        return _lastName;
    }

    public void setFirstName(String fn) {
		JOptionPane.showMessageDialog(null,"Setfirstname");
		_AirCompany = fn;
	}

public int getAmount()
{
	return _amount;
}

public void setLastName(String nm) {
		_lastName = nm;
	}

public void setAmount(int am)
{
	_amount=am;
}

/*public void display()
{
	System.out.println("Customer" +
	getFirstName()+"_"+
	getLastName()+"owes"+
getAmount()+"Euros");
}
*/
}



import java.util.*;
import javax.swing.JOptionPane;
import java.io.*;


class CustomerBase {

	private Vector _allCustomers;
	private CustomerFileReader cfr;
	private CustomerFileWriter cfw;
    public int a;
    private String helpp;
    private String help;

	CustomerBase() {
		_allCustomers = new Vector();
	}

	public int getNumberOfCustomers () {
		return _allCustomers.size();
	}

	public void addCustomer (Customer c) {
		JOptionPane.showMessageDialog(null,"mpike");
		_allCustomers.addElement(c);
	}

    public void removeCustomer (int i) {
		_allCustomers.removeElementAt(i-1);
    }

    public Customer selectCustomer (int i) {
		if (i <= _allCustomers.size() )
			return (Customer) _allCustomers.elementAt(i-1);
        else
        	return null;
    }

public Customer findbyname(String nm)
{
for(int i=0;i<getNumberOfCustomers();i++)
{
	Customer c=selectCustomer(i+1);
	JOptionPane.showMessageDialog(null,c.getFirstName()+ "_" +nm);

	if(c.getFirstName()==nm)
{
	//c=nm;
	JOptionPane.showMessageDialog(null,"FOUND");
return c;
}

}
return null;
}
//*/
	public void loadBaseFromFile(String filename) {
		Customer c = null;
		cfr = new CustomerFileReader(filename);

		try {
			do {
				c = cfr.getCustomerFromFile();
				addCustomer (c);
			}while(c != null);
		}
		catch(EOFException e) {
			JOptionPane.showMessageDialog (null, "Customers Are Loaded from File to CustomerBase", "File Loading Finished", JOptionPane.ERROR_MESSAGE );
			cfr.close();
		}

	}
	public void saveBaseToFile(String filename) {

		cfw = new CustomerFileWriter(filename);

Customer c=new Customer();
helpp="kkkk";
//help=_allCustomers.size();
JOptionPane.showMessageDialog(null,"help");
c.setFirstName(helpp);
c.setLastName("Neofytidis");

			//JOptionPane.showMessageDialog(null,c.getFirstName());

			addCustomer(c);
			JOptionPane.showMessageDialog(null,"meta");
		for(int i = 0;i <= _allCustomers.size(); i++) {
			JOptionPane.showMessageDialog(null,"help me");
			//c.setFirstName();
		//	tfFlightNumber.setText(c.getLastName());
			//JOptionPane.showMessageDialog(null,(Customer) _allCustomers.elementAt(i));

			cfw.putCustomerToFile( (Customer) _allCustomers.elementAt(i) );
			JOptionPane.showMessageDialog(null,"help me more");
			//while (_allCustomers.size()) {
			 //     System.out.println(cfw.nextElement());
			 //   }

		}
		cfw.close();

	}


}






//The Customer File Reader Manager

import java.io.*;
import javax.swing.JOptionPane;

public class CustomerFileReader {

	private ObjectInputStream _input;


    CustomerFileReader(String filename) {

		try {
        	_input = new ObjectInputStream(new FileInputStream(filename) );

        }
        catch (IOException e) {
        	JOptionPane.showMessageDialog (null, "Invalid File Name (Read)", "File " + filename + " Not Found", JOptionPane.ERROR_MESSAGE );
        }

	}

	public Customer getCustomerFromFile () throws EOFException {

		Customer c = null;
		try {
			c = (Customer) _input.readObject();
		}
		catch (ClassNotFoundException e) {
         	JOptionPane.showMessageDialog (null, "Unable to create Object while reading from file", e.getMessage(), JOptionPane.ERROR_MESSAGE );
 		}
		catch (IOException e) {
         	if (e instanceof EOFException) throw (EOFException) e;
         	else JOptionPane.showMessageDialog (null, e.getMessage(), "Abnormal situation while reading the Customers from the file", JOptionPane.ERROR_MESSAGE );
 		}
 		return c;

   	}

   	public void close() {

		try {
			_input.close();
		}
		catch(IOException e) {
			JOptionPane.showMessageDialog (null, "Error closing file", "File Error", JOptionPane.ERROR_MESSAGE );
		}
	}

}




//The Customer File Writer Manager

import java.io.*;
import javax.swing.JOptionPane;

public class CustomerFileWriter {

	private ObjectOutputStream _output;

    CustomerFileWriter(String filename) {

		try {
        	_output = new ObjectOutputStream(new FileOutputStream(filename) );

        }
        catch (IOException e) {
        	JOptionPane.showMessageDialog (null, "File " + filename + " Not Found", "Invalid File Name (Write)", JOptionPane.ERROR_MESSAGE );
        }

	}

	public void putCustomerToFile (Customer c) {

		try {
			JOptionPane.showMessageDialog(null,"xaxa putcustomertofile");
			_output.writeObject(c);
			_output.flush();
		}
		catch (IOException e) {
         	JOptionPane.showMessageDialog (null, e.getMessage(), "Unable to write Object while writing to file", JOptionPane.ERROR_MESSAGE );
 		}

   	}

   	public void close() {

		try {
			_output.close();
		}
		catch(IOException e) {
			JOptionPane.showMessageDialog (null, "Error closing file", "File Error", JOptionPane.ERROR_MESSAGE );
		}
	}


}






/**
* My Tiny Company User Interface
* @author George Eleftherakis
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import javax.swing.*;

public class CustomerUI extends Frame implements ActionListener{

	CustomerBase cb;
	private TextField tfFirstName;
	private TextField tfLastName;
	private TextField tfAmount;
	//private TextField tfRecNo;
	private TextField tfAirCompany;
	private TextField tfFlightNumber;
	private TextField tfFlyingFrom;
	private TextField tfFlyingTo;
	private TextField tfDateOfFlight;
	private TextField tfTimeOfFlight;
	private TextField tfFClass;
	private TextField tfSClass;
	private TextField tfTClass;
	private int RecNo = 0;

	//The constructor
	public CustomerUI() {

		cb = new CustomerBase();


		// Create and add the menu to the frame
		MenuBar mb;
        Menu m1, m2;
        MenuItem mi1_1, mi1_2,mi1_3, mi2_1, mi2_2, mi2_3;

        mb = new MenuBar();
        setMenuBar(mb);

		//Build first menu in the menu bar.
        m1 = new Menu("File", true);
		mb.add(m1);

		mi1_1 = new MenuItem("Load");
		mi1_1.addActionListener(this);
		m1.add(mi1_1);



		mi1_2 = new MenuItem("Save");
		mi1_2.addActionListener(this);
        m1.add(mi1_2);

        //mi1_3 = new MenuItem("Close");
		//		mi1_3.addActionListener(this);
		//m1.add(mi1_3);

        //Build second menu in the menu bar.
        m2 = new Menu("Help");
        mb.setHelpMenu(m2);

        mi2_1 = new MenuItem("Contents");
		mi2_1.addActionListener(this);
        m2.add(mi2_1);

        mi2_2 = new MenuItem("-");
        m2.add(mi2_2);

        mi2_3 = new MenuItem("About");
		mi2_3.addActionListener(this);
        m2.add(mi2_3);


		//Create a panel to group the buttons, put that south
		Panel pButtons = new Panel();

        Button insertButton = new Button("Insert new flight");
		pButtons.add(insertButton);
		insertButton.addActionListener(this);

		Button nextButton = new Button("Next");
				pButtons.add(nextButton);
		nextButton.addActionListener(this);



		Button previousButton = new Button("Previous");
		pButtons.add(previousButton);
		previousButton.addActionListener(this);

		Button deleteButton = new Button("Delete");
		pButtons.add(deleteButton);
		deleteButton.addActionListener(this);

Button searchButton = new Button("Search");
		pButtons.add(searchButton);
		searchButton.addActionListener(this);

		Button clearButton = new Button("Clear");
				pButtons.add(clearButton);
		clearButton.addActionListener(this);
		Button exitButton = new Button("Exit");
				pButtons.add(exitButton);
		exitButton.addActionListener(this);
		add(pButtons, "South");

		//Create a panel to group the textFields, put that north
		Panel pTextFields = new Panel();
		pTextFields.setLayout(new GridLayout(9,2));

		//pTextFields.add(new Label("Customer First Name: "));
		//tfFirstName = new TextField("", 20);
		//pTextFields.add(tfFirstName);

		//pTextFields.add(new Label("Customer Last Name: "));
		//tfLastName = new TextField("", 20);
		//pTextFields.add(tfLastName);

		pTextFields.add(new Label("Air company: "));
		tfAirCompany = new TextField("", 10);
		pTextFields.add(tfAirCompany);

		pTextFields.add(new Label("Flight number: "));
	    tfFlightNumber = new TextField("", 3);
		pTextFields.add(tfFlightNumber);

		pTextFields.add(new Label("Flying from: "));
		tfFlyingFrom = new TextField("", 50);
		pTextFields.add(tfFlyingFrom);

		pTextFields.add(new Label("Flying to: "));
		tfFlyingTo= new TextField("", 50);
		pTextFields.add(tfFlyingTo);

		pTextFields.add(new Label("Date of flight: "));
		tfDateOfFlight= new TextField("", 50);
		pTextFields.add(tfDateOfFlight);

		pTextFields.add(new Label("Time of flight: "));
		tfTimeOfFlight= new TextField("", 50);
		pTextFields.add(tfTimeOfFlight);

		pTextFields.add(new Label("First Class Seats: "));
	    tfFClass= new TextField("", 50);
		pTextFields.add(tfFClass);
		pTextFields.add(new Label("Second Class Seats: "));
		tfSClass= new TextField("", 50);
		pTextFields.add(tfSClass);
		pTextFields.add(new Label("Third Class Seats: "));
	tfTClass= new TextField("", 50);
		pTextFields.add(tfTClass);

//Create a combo box with choices.
	//String[] Seats =
		//{	"First Class",
		//	"Business Class",
			//"Economy Class"};

//pTextFields.add(new JComboBox(Seats));
//tfClass= new JComboBox("", 50);

//JComboBox petList = new JComboBox(Seats);
//pTextFields.add(petList);
//ComboBoxRenderer renderer = new ComboBoxRenderer();
//renderer.setPreferredSize(new Dimension(200, 130));
//petList.setRenderer(renderer);
//petList.setMaximumRowCount(3);


//myChoices.setSelectedIndex(0);


		add(pTextFields, "North");

	}


    //This method handles the 'captured' events
    public void actionPerformed (ActionEvent evt) {

		String arg = evt.getActionCommand();


        if (arg.equals("Next")) {
			//JOptionPane.showMessageDialog (null,cb.getNumberOfCustomers());
			if (RecNo < cb.getNumberOfCustomers()) RecNo++; //Control the Record Number to know where I am and avoid to increase if I am at the end
			Customer c = (Customer) cb.selectCustomer (RecNo);  //Read the customer from the file
			display (c); //Display the info
		}

        else if (arg.equals("Previous")){
			if (RecNo > 1 ) RecNo--;
			Customer c = (Customer) cb.selectCustomer (RecNo);
			display (c);
		}

		else if (arg.equals("Delete")){

		}
		else if (arg.equals("Insert")){

		}
		else if (arg.equals("Save")){
		cb.saveBaseToFile("customer.dat");
		}
else if (arg.equals("Load")){
cb.loadBaseFromFile("customer.dat");
		}
		else if (arg.equals("Search"))
		{
		//cb.removeCustomer(RecNo);
		String nm=tfAirCompany.getText();
		JOptionPane.showMessageDialog(null,nm);
		Customer c = cb.findbyname(nm);
		JOptionPane.showMessageDialog(null,c+"_"+nm);
		if (c!=null)
		{
			JOptionPane.showMessageDialog(null,"FOUND");
				        	//JOptionPane.showMessageDialog (this, " FOUND", JOptionPane.INFORMATION_MESSAGE );

		//found
}
}
//*/
		else if (arg.equals("Exit")){
System.exit(0);
		}

		else if (arg.equals("Clear")){
		//cb.loadBaseFromFile("customers.dat");
		}
		else if (arg.equals("About") ){
        	JOptionPane.showMessageDialog (this, "Airflights database. \nby Stefanos Neofytidis", "About...", JOptionPane.INFORMATION_MESSAGE );
		}

	}


	public void display (Customer c) {
		if (c != null) {
			//Integer a = new Integer(c.getAmount());
			Integer b = new Integer(RecNo);
			//tfFirstName.setText(c.getFirstName());
			//tfLastName.setText(c.getLastName());
			JOptionPane.showMessageDialog (null,"gggg");
			tfAirCompany.setText(c.getFirstName());
			tfFlightNumber.setText(c.getLastName());
			//tfAirCompany.setText(b.toString());
		}
	}




	//The main function that actually creates the UI(frame) to the screen
	public static void main(String [] Args){
		Frame f = new CustomerUI();
		f.setTitle("Flights Information");
		f.setSize(440,350);
		f.setVisible(true);

		//This method handles the 'x' button that closes a frame
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
	}




}