GeSHi/Java

From Christoph's Personal Wiki
Revision as of 17:32, 17 December 2005 by Christoph (Talk | contribs) (Added "FlightSystemUser.java" as a test)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

<java> package flightSystem;

import java.io.*; import java.util.*;

import commandObjects.CancelObject; import commandObjects.CreateObject; import commandObjects.ExitObject; import commandObjects.FlightsObject; import commandObjects.IdentifyObject; import commandObjects.ListObject; import commandObjects.ReloadObject; import commandObjects.ReserveObject; import commandObjects.StoreObject; import exceptions.*;

/**

* This is the MAIN() class!
* 
* It provides access to all the data structures generated/created
* and provides the user with a way to interact with the application.
* 
* NOTE: The interaction is entirely from the command prompt!
* @author Christoph Champ (s051307)
*/

public class FlightSystemUser {

// Constructors // No arg Constructor public FlightSystemUser() {}

// Other Methods /** * Method that tokenizes the command argument line introduced by the * user and executes the corresponding method. * @param line * The line introduced by the user * @param in * The buffered reader from where the line was read * @throws ExpIllegalCommand * The syntax of the command introduced by the user is not correct * @throws ExpFlightNotExist * The flight name inserted by the user does not exist * @throws ExpIllegalValues * The values inserted by the user are not correct. * @throws ExpSeatsFull * There are no so many seats available * @throws ExpReserveNotExist * The booking number inserted by the user does not identify * any reservation */ public void exec(String line, BufferedReader in) throws ExpIllegalCommand, ExpIllegalValues, ExpFlightNotExist, ExpReserveNotExist, ExpSeatsFull, ExpFlightExists { // Tokenize the line read StringTokenizer st = new StringTokenizer(line); if ( st.hasMoreTokens() ) { // Get the first token, which identifies the command // to be executed String command = new String(st.nextToken().toLowerCase()); // Execute the right command depending on the data. if (command.equals("create")) new CreateObject().parse(st); else if (command.equals("reserve")) new ReserveObject().parse(st); else if (command.equals("cancel")) new CancelObject().parse(st); else if (command.equals("identify")) new IdentifyObject().execute(st); else if (command.equals("list")) new ListObject().parse(st); else if( command.equals("flights") ) new FlightsObject().parse(); else if( command.equals("store") ) new StoreObject().parse(st); else if( command.equals("reload") ) new ReloadObject().parse(st); else if (command.equals("exit")) new ExitObject().parse(st, in); else System.out.println("\tUnrecognized command " + line); } else System.out.println("\tUnrecognized command " + line); }

// STDOUT MESSAGES /** * Displays the commands that the user can execute */ private void available() { System.out.println("\tPossible Commands:\n"); System.out.println("\t-> create flightName rows rowLength"); System.out.println("\t-> reserve flightName passenger1 [passenger2 .. passengerN]"); System.out.println("\t-> cancel bookingNumber"); System.out.println("\t-> cancel bookingNumber passenger1 [passenger2 .. passengerN]"); System.out.println("\t-> identify bookingNumber"); System.out.println("\t-> list flightName"); System.out.println("\t-> flights"); System.out.println("\t-> store filename"); System.out.println("\t-> reload filename"); System.out.println("\t-> exit\n"); }

/** * Opening introduction to programme */ public void intro() { System.out.println("\n\t======================================"); System.out.println("\t| |"); System.out.println("\t| A Simple Flight Reservation System |"); System.out.println("\t| |"); System.out.println("\t| Authors: Christoph Champ |"); System.out.println("\t| Created: 6 November 2005 |"); System.out.println("\t| Subject: 02115 Java Programming |"); System.out.println("\t| |"); System.out.println("\t======================================\n");

available(); }

/** * This is the main section of the MAIN() section: */ public static void main(String[] args) { // The buffered reader from where we will read the data BufferedReader in; try { switch (args.length) { // If the number of arguments is 0, the programme // should interact with the user case 0: in = new BufferedReader(new InputStreamReader(System.in)); break;

// If the number of arguments is 1, the programme should // read the data from a text file case 1: in = new BufferedReader(new FileReader(args[0])); break;

// The programme have not been called properly default: throw new ExpInvalidArg(args); }

// Variable to store each line read String line; // Variable to handle the state of the flight system FlightSystemUser fsu = new FlightSystemUser();

// Display the introduction fsu.intro();

// While there is data available, process it // Main loop, reads the input, executes the commands, and // displays the output while ( (line = in.readLine()) != null ) { // Try to execute the command try { fsu.exec(line, in); }

// Handle illegal command exception catch(ExpIllegalCommand e) { System.out.println(e); }

// Handle illegal values exception catch(ExpIllegalValues e) { System.out.println(e); }

// Handle non-existent reservation exception catch(ExpReserveNotExist e) { System.out.println(e); }

// Handle illegal command exception catch(ExpFlightNotExist e) { System.out.println(e); }

// Handle number format exception catch(NumberFormatException e) { System.out.println("\tInvalid input value: " + e.getMessage()); System.out.println("\tInteger excepted."); }

// Handle no more seats exception catch(ExpSeatsFull e) { System.out.println(e); }

// Handle that the flight has already been created catch(ExpFlightExists e) { System.out.println(e); } } }

// Handle IOException exception catch (IOException e) { System.out.println("Can't locate file!"); }

// Handle invalid arguments exception catch (ExpInvalidArg e) { System.out.println(e); } } } </java>