ICS102 Final Project

ICS102 Project File

ICS102Project.java

/*|||  About The Code  |||
 * version 9.09 2009/6/7
 ************************/

 /*||||||||||||||||  INDEX  ||||||||||||||||
 *Imports.
 *		Scanner.
 *		PrintWriter.
 *		JOptionPane.
 *Public Class.
 * 		Main Method.
 * 				Accounts checker.
 * 				Commands dialog.
 * 		Dan Method.
 *		Ret Method.
 *		Add Method.
 *		Ava Method.
 *		Res Method.
 ******************************************/

// Imports.
	import java.util.Scanner;
	import java.io.*;
	import javax.swing.*;
// End: Imports.

public class ICS102Project {
// Main Method.
    public static void main(String[] args)throws Exception{
	// Accounts checker.
    	Scanner Accounts=new Scanner(new File("DATA/account.txt"));	//The file(account) contains users and passwords
    	String[] account=new String[100];
    	int g=0;
    	while(Accounts.hasNextLine()){
    		account[g]=Accounts.nextLine();	//Filling the array with the accounts, each cell will have user&password separated by (:)
    		g++;
    	}

    	Accounts.close();

    	//A welcome dialog
    	JOptionPane.showMessageDialog(null,"Welcome to the Airline Reservation System\nA project");
    	String input=null,read=null,user=null;

    	int check=0;
    	// The do-while will check if the user and password are available & comform
    	do{
    		int userCheck=0;
    		    		input=JOptionPane.showInputDialog("Administrator Control Panel\n\nPleas enter the username: ");

    		for(int i=0; i<g; i++){
    			read=account[i];
    			user=read.substring(0,read.indexOf(":"));	//because the user and the password are in one line and separated by (:)
    			if(user.equals(input)){
    				userCheck=1;
    				break;
    			}
    		}
    		if(userCheck==1){
    			input=JOptionPane.showInputDialog("Administrator Control Panel\n\nEnter Your Password: ");
    			if((read.substring(read.indexOf(":")+1)).equals(input))
    				break;
    			else{
    				JOptionPane.showMessageDialog(null,"Sorry\nWrong Password !");
    				check++;
    			}
    		}
    		else{
    			JOptionPane.showMessageDialog(null,"Sorry\nWrong Username !");
    			check++;
    		}

    	// As a seafty, the system will close after three wrong inputs or three wrong matches...
    	}while(check!=3);

    	if(check==3){
    		JOptionPane.showMessageDialog(null,"You've exeeded the limit!\nPlease contact our support team!");
    		System.exit(0);
    	}
// Emd: Accounts checker.
// Commands & commands dialog.
		String danInfo=null;
    	input=JOptionPane.showInputDialog("Administrator Control Panel\n\nEnter a command:  ");
    	String command=null;
    	String data=null;

		// the command ( q! ) will end the program
    	while(!(input.equals("q!"))){

			// To avoid getting wrong input less than 3 digits
    		if(input.length()>3)
    			{
    			command=input.substring(0,3);
    			data=input.substring(4);
    			if(command.equalsIgnoreCase("dan")){
					// airport information
    				danInfo=dan(data);
    				JOptionPane.showMessageDialog(null,"Administrator Control Panel\n\nAirport Code: "+danInfo);
    			}
    			else if(command.equalsIgnoreCase("ret")){
					// return a reservation information
    				ret(data);
    			}
    			else if(command.equalsIgnoreCase("add")){
					// add a new flight or flights
    				add(input);
    			}
    			else if(command.equalsIgnoreCase("ava")){
					// check for availability of a flight
    				ava(data);

    			}
    			else if(command.equalsIgnoreCase("res")){
					// making new reservation...
    				res(data);
    			}
    			else if(command.equalsIgnoreCase("tkt")){
					// printing a tkt data...
    				Ticket.getTKT(data);
    			}
				else if(command.equalsIgnoreCase("hlp")){
					// printing a tkt data...
    				JOptionPane.showMessageDialog(null,"Enter the command like\ndan jed\n3 leaters for the command then space rhen the data\n:)");
    			}
    			else
    				JOptionPane.showMessageDialog(null,"Administrator Control Panel\n\nNot valid command !\nEnter \"hlp me\" for Help");
    		}
    		else
    			JOptionPane.showMessageDialog(null,"Administrator Control Panel\n\nNot valid command !\nEnter \"hlp me\" for Help");
    		input=JOptionPane.showInputDialog("Administrator Control Panel\n\nEnter a command:  ");
    	}

    	JOptionPane.showMessageDialog(null,"Administrator Control Panel\n\nYou loged of !\nThank You :) ");
// End: Commands & commands dialog.
    }
// End: Main Method.
// Dan Method.
    public static String dan(String cityName)throws Exception{
		// The method dan will lock for an airport information using the code for a city.
    	String line=null;
    	Scanner in=new Scanner(new File("DATA/iata-airport-codes.txt"));
    	while(in.hasNextLine()){
    		line=in.nextLine();
    		if((line.toLowerCase()).contains(cityName.toLowerCase())){
    			in.close();
    			return line;
    		}
    	}
    		return "City not found, check the spelling!";
    }
// End: Dan Method.
// Ret Method.
	public static void ret(String fileName)throws Exception{
			// Thw method ret will return a valid reservation information using reservation number.
    		String line=null;
			String infoFull="\n";
    		Scanner out=new Scanner(new File("DATA/booking/"+fileName+".txt"));
    		while(out.hasNextLine()){
    			line=out.nextLine();
				infoFull = infoFull + line + "\n";
    		}
			JOptionPane.showMessageDialog(null,"Administrator Control Panel\n\nInformation for the reservation "+fileName+" are:"+infoFull);

    		out.close();
    }
// End: Ret Method.
// Add Method.
	public static void add(String flight)throws Exception{
			// This method will add new flights to the file ( DATA/availability.txt ).
			String line=null;

			// To mack sure that all the old data in availability.txt saved to a temp. file while adding new flights
			Scanner out=new Scanner(new File("DATA/availability.txt"));
			PrintWriter in1=new PrintWriter(new File("DATA/copy.txt"));
			while(out.hasNextLine()){
				line=out.nextLine();
				in1.println(line);
			}
			in1.close();
			out.close();

			// To return the copied flights to the origin file ( DATA/availability.text ) then to add new flights..
			Scanner out1=new Scanner(new File("DATA/copy.txt"));
			PrintWriter in=new PrintWriter(new File("DATA/availability.txt"));
			while(out1.hasNextLine()){
				line=out1.nextLine();
				in.println(line);
			}
			String month=(flight.substring(12,13)).toUpperCase()+(flight.substring(13,15)).toLowerCase();
			String dep=(flight.substring(4,7)).toUpperCase();
			String des=(flight.substring(7,10)).toUpperCase();
			String fClass=(flight.substring(15,19)).toUpperCase();
			String jClass=(flight.substring(19,23)).toUpperCase();
			String yClass=(flight.substring(23)).toUpperCase();
			if((flight.substring(10,12)).equals("**"))
			// ** means the whole month ( 30 days = 30 flights ).
				{

				for(int i=1; i<=30; i++){
					// To add a digit to the numbers less than 10..
					if(i<10)
						in.print("0");
					in.println(i+" "+month+" "+dep+" "+des+" "+fClass+" "+jClass+" "+yClass);

				}
			}
			else
				in.println(flight.substring(10,12)+" "+month+" "+dep+" "+des+" "+fClass+" "+jClass+" "+yClass);
		        JOptionPane.showMessageDialog(null,"Administrator Control Panel\n\nThe Flight:\n" +flight.substring(10,12)+" "+month+" "+dep+" "+des+" "+fClass+" "+jClass+" "+yClass + "\n" + "Added to DATA");
			out1.close();
			in.close();
		}
// End: Add Method.
// Ava Method.
		public static String ava(String check)throws Exception{
			// To check the availability of a flight.
			Scanner in=new Scanner(new File("DATA/availability.txt"));
			int count=1;
			String line=null;
			check=check.substring(0,2)+" "+check.substring(2,5)+" "+check.substring(5,8)+" "+check.substring(8);
			while(in.hasNextLine()){
				line=((in.nextLine()).toLowerCase());
				if(line.contains(check.toLowerCase())){

					JOptionPane.showMessageDialog(null,"Administrator Control Panel\n\nSearch Result:\n" + line.toUpperCase());

					return line.toUpperCase();
				}

			}
				JOptionPane.showMessageDialog(null,"Administrator Control Panel\n\nSorry, There is no available flight for\n" + check);

				return "";
    }
// End: Ava Method.
// Res Method.
		public static void res(String flight)throws Exception{

		// To book a new reservation.. and add a passengers & make tickets for them...
    	String lastName=null;
    	String givenName=null;
    	int seat=Integer.parseInt(flight.substring(12,13));

		// To check the availability of seats number
    	String classType=(flight.substring(13,14)).toUpperCase();
    	String comFlight=flight.substring(15)+"/";
    	String com2Flight=null;
    	String deleteFlight=ava(flight.substring(6,11)+flight.substring(0,6));
    	com2Flight=deleteFlight.substring(15);
    	int seats=Integer.parseInt(com2Flight.substring(com2Flight.indexOf(classType)-3,com2Flight.indexOf(classType)));
    	if(seats<seat){
    		JOptionPane.showMessageDialog(null,"Administrator Control Panel\n\nSorry, There is no seats available");
    		return;
    	}

    	seats-=seat;
    	String sseats=null;
    	if(seats<10)
    		sseats="00"+seats;
    	else if(seats<100)
    		sseats="0"+seats;
    	else
    		sseats=""+seats;
    	String newFlight=""+deleteFlight.substring(0,com2Flight.indexOf(classType)+12)+sseats+deleteFlight.substring(com2Flight.indexOf(classType)+15);

    	Scanner out=new Scanner(new File("DATA/availability.txt"));	// After booking a new reservation,, here the file availability will be updated and seats of the flight will decrease..
		PrintWriter in1=new PrintWriter(new File("DATA/copy.txt"));
		String line=null;
		while(out.hasNextLine()){
			line=out.nextLine();
			in1.println(line);
		}
		in1.close();
		out.close();

		Scanner out1=new Scanner(new File("DATA/copy.txt"));
		PrintWriter in=new PrintWriter(new File("DATA/availability.txt"));
		while(out1.hasNextLine()){

			line=out1.nextLine();
			if(line.equalsIgnoreCase(deleteFlight))
				in.println(newFlight);
			else
				in.println(line);
		}
		in.close();
		out1.close();
		Scanner out2=new Scanner(new File("DATA/res_num.txt"));	// To generate a new reservation number...
		int num=out2.nextInt();
		num++;
		out2.close();

		PrintWriter in2=new PrintWriter(new File("DATA/res_num.txt"));
		in2.print(num);
		in2.close();
		String fileName=""+num;
		PrintWriter in3=new PrintWriter(new File("DATA/booking/"+fileName+".txt"));
		int price=Integer.parseInt(JOptionPane.showInputDialog("Administrator Control Panel\n\nEnter The Price of one ticket"));
		in3.println("Departure city: "+(flight.substring(0,3)).toUpperCase());
		in3.println("Destination city: "+(flight.substring(3,6)).toUpperCase());
		in3.println("Day: "+flight.substring(6,8));
		in3.println("Months: "+flight.substring(8,11));
		in3.println("Year: 2010");// The program will be valid in 2010 because Saudi aielines will use my program for one year in 2010....<< nice comment
		in3.println("Class: "+classType);
		in3.println("Number of seats: "+seat);
		String mobile=null;
		String tktNum=null;

		for(int i=1; i<=seat; i++){// to print the passengers one by one .. depends on seats number....
			lastName=comFlight.substring(0,comFlight.indexOf(";"));
			givenName=comFlight.substring(comFlight.indexOf(";")+1,comFlight.indexOf("/"));
			mobile=JOptionPane.showInputDialog("Administrator Control Panel\n\nEnter passenger No."+i+"   phone number: ");
			tktNum="065210"+fileName+i+i;
			Ticket p_data=new Ticket(givenName,mobile,fileName,tktNum,flight.substring(0,3).toUpperCase(),flight.substring(3,6).toUpperCase(),price);	

			Scanner ticketsInformation=new Scanner(new File("DATA/tkt.txt"));
			String test1 = "";
			String test2 = "";
			while(ticketsInformation.hasNextLine()){
    		test1=ticketsInformation.nextLine();
    		if(test1.contains(tktNum)){
    			test2 = test2 + test1;
    			while(ticketsInformation.hasNextLine()){
    			test2 = test2 + "\n" + ticketsInformation.nextLine();
    			}
    			JOptionPane.showMessageDialog(null,"Administrator Control Panel\n\nTicket information\n" + test2); 

    			in.close();

    			}

    		}

			comFlight=comFlight.substring(comFlight.indexOf("/")+1);
			in3.println("Passenger "+i+":");
			in3.println("Last Name: "+lastName);
			in3.println("Given Name: "+givenName);
		}
		in3.close();
    }
// End: Res Method.
}

Ticket.java

/*|||  About The Code  |||
 * version 5.07 2009/6/8
 ************************/

// Imports.
	import java.util.Scanner;
	import java.io.*;
	import javax.swing.*;
// End: Imports.

public class Ticket {
	// The attributes
    private String passengerName;
    private String mobile;
    private String reservationNumber;
    private String tktNumber;
    private String departure;
    private String destination;
    private int price;
    // the setters and the getters
    public void setPassengerName(String passengerName){
    	this.passengerName=passengerName;
    }
    public String getPassengerName(){
    	return passengerName;
    }

    public void setMobile(String mobil){
    	this.mobile=mobile;
    }
    public String getMobile(){
    	return mobile;
    }

    public void setReservationNumber(String reservationNumber){
    	this.reservationNumber=reservationNumber;
    }
    public String getReservationNumber(){
    	return reservationNumber;
    }

    public void setTktNumber(String tktNumber){
    	this.tktNumber=tktNumber;
    }
    public String getTktNumber(){
    	return tktNumber;
    }

    public void setDeparture(String departure){
    	this.departure=departure;
    }
    public String getDeparture(){
    	return departure;
    }

    public void setDestination(String destination){
    	this.destination=destination;
    }
    public String getDestination(){
    	return destination;
    }

    public void setPrice(int price){
    	this.price=price;
    }
    public int getPrice(){
    	return price;
    }
    // This is the constructor which will take the attributes from the main method
    public Ticket(String name,String phone,String resNum,String tktNum,String dep,String des,int pr)throws Exception{
    	passengerName=name;
    	mobile=phone;
    	reservationNumber=resNum;
    	tktNumber=tktNum;
    	departure=dep;
    	destination=des;
    	price=pr;
    	storeTKT();
    	// Calling the method to store it in the file ( DATA/tkt.txt)
    }
    // This method will store the attributes in a file
    public void storeTKT()throws Exception{
    	// We need to print every thing in DATA/tkt.txt file into a copy then returning the data again.. this is how to update the file without deleting the old data..
    	Scanner out=new Scanner(new File("DATA/tkt.txt"));
    	PrintWriter in=new PrintWriter(new File("DATA/copyTKT.txt"));
		String line=null;
		while(out.hasNextLine()){
			line=out.nextLine();
			in.println(line);
		}
		in.close();
		out.close();

		Scanner out1=new Scanner(new File("DATA/copyTKT.txt"));
		PrintWriter in1=new PrintWriter(new File("DATA/tkt.txt"));
		while(out1.hasNextLine()){
			line=out1.nextLine();
			in1.println(line);
		}
		in1.println("eTKT Number: "+tktNumber);
		in1.println("reservation Number: "+reservationNumber);
		in1.println("Passenger Name: "+passengerName);
		in1.println("Phone Number: "+mobile);
		in1.println("Departure City: "+departure);
		in1.println("Destination City: "+destination);
		in1.println("Price: "+price+" SR");
		in1.println("&&&&&&&&");//this will give us an easy way to separate the tkt's ...
    	in1.close();
    	out1.close();
    }
    // this method will take number of the tkt and give it to other method to print it..
    public static void getTKT(String tktNum)throws Exception{
    		int check=0;
    	Scanner out=new Scanner(new File("DATA/tkt.txt"));
    	String line=null;
    	while(out.hasNextLine()){
    		line=out.nextLine();
    		if(line.contains(tktNum)){
    			check=1;
    			break;
    		}
    	}
    	if(check==0){
    		JOptionPane.showMessageDialog(null,"No tkt Number available...");
    		return;
    	}
    	while(!(line.equals("&&&&&&&&"))){
    		printTKT(line);
    		line=out.nextLine();
    	}
    	out.close();
    }
    // this method will print tkt data
    public static void printTKT(String line){

    	System.out.println(line);
    }

}

DB Files