Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @authors Adam Brickett (N0309883) and Michael Fleming (N0338124)
- */
- import java.io.*;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.StringTokenizer;
- public class SimpleServer {
- public static List<LoggedInUsers> loggedInUserList = new LinkedList<LoggedInUsers>();
- public static List<UserDetails> userList = new LinkedList<UserDetails>();
- public static List<Day> Days = new LinkedList<Day>();
- public static void main(String[] args) throws FileNotFoundException, IOException {
- if( args.length != 1 )
- {
- System.out.println("USAGE: SimpleServer <listeningPort>");
- System.exit(0);
- }
- int listeningPort = Integer.parseInt(args[0]);
- /*
- * start the server off...
- */
- //String fileName = "E:\\UNI WORK\\Systems Software\\Coursework\\Second part of Coursework\\List_of_Usernames.txt";
- String fileName = "/Volumes/KINGSTON/UNI WORK/Systems Software/Coursework/Second part of Coursework/List_of_Usernames.txt";//E:\\UNI WORK\\Systems Software\\Coursework\\Second part of Coursework\\List_of_Usernames.txt";
- FileReader fileReader = new FileReader(fileName);
- BufferedReader bufferedReader = new BufferedReader(fileReader);
- String line = null;
- while ((line = bufferedReader.readLine()) != null)
- {
- StringTokenizer tokens = new StringTokenizer(line,","); //splits it into values wherever
- //there is a comma
- String tempUser = tokens.nextToken().trim();
- String tempPass = tokens.nextToken().trim();
- UserDetails temp = new UserDetails(tempUser, tempPass);
- userList.add(temp);
- }
- //fileName = "E:\\UNI WORK\\Systems Software\\Coursework\\Second part of Coursework\\Days.txt";
- fileName = "/Volumes/KINGSTON/UNI WORK/Systems Software/Coursework/Second part of Coursework/Days_mac.txt";
- fileReader = new FileReader(fileName);
- bufferedReader = new BufferedReader(fileReader);
- line = null;
- while ((line = bufferedReader.readLine()) != null)//splits into lines
- {
- StringTokenizer tokens = new StringTokenizer(line,","); //splits it into values wherever
- //there is a comma
- String tempFilmName = tokens.nextToken().trim();
- String tempDate = tokens.nextToken().trim();
- String tempFileName = tokens.nextToken().trim();
- Day tempDay = new Day(tempFilmName,tempFileName,tempDate);
- Days.add(tempDay);
- }
- LoadDaysFile();
- Server server = new Server(listeningPort);
- server.start();
- }
- private static void SaveUserstoFile() throws IOException
- {
- String fileToWrite = "E:\\UNI WORK\\Systems Software\\Coursework\\Second part of Coursework\\List_of_Usernames.txt";
- FileWriter fileOutput = new FileWriter(fileToWrite);
- PrintWriter printWriter = new PrintWriter(fileOutput,true);
- for(int idx = 0; idx < userList.size(); idx++)
- {
- printWriter.println(userList.get(idx).username +"," + userList.get(idx).password);
- }
- }
- private static void SaveBookingsToFile() throws IOException
- {
- for (int i = 0; i < Days.size(); i++)
- {
- Days.get(i).SaveBookingsToFile();
- }
- }
- private static void LoadDaysFile() throws FileNotFoundException, IOException
- {
- for (int i = 0; i < Days.size(); i++)
- {
- Days.get(i).LoadDaysFile();
- }
- }
- public static boolean RegisterUser(String user, String pass) throws IOException
- {
- for(int idx = 0; idx < userList.size(); idx++)
- {
- if(userList.get(idx).username.equalsIgnoreCase(user))
- {
- return false;
- }
- }
- userList.add(new UserDetails(user, pass));
- SaveUserstoFile();
- return true;
- }
- public static boolean ValidateUser(String user, String pass)
- {
- for(int idx = 0; idx < userList.size(); idx++)
- {
- for(int idx2 = 0; idx2 < loggedInUserList.size(); idx2 ++)
- {
- if (loggedInUserList.get(idx2).User.username.equals(user))
- {
- return false;
- }
- }
- if(userList.get(idx).username.equals(user))
- {
- if(userList.get(idx).password.equals(pass))
- {
- return true;
- }
- }
- }
- return false;
- }
- public static String GetAvailableSeats(String Date)
- {
- String Seats = "";
- for (int seatidx = 0; seatidx < Days.size();seatidx++)
- {
- if(Days.get(seatidx).Date.equals(Date))
- {
- Seats = Days.get(seatidx).GetAvailableSeats();
- }
- }
- return Seats;
- }
- public static boolean CSVCheck(final String stringToCheck)
- {
- char[] sample = ", ".toCharArray();
- if (stringToCheck.isEmpty())
- {
- return false;
- }
- for(char c: stringToCheck.toCharArray())
- {
- if((!Character.isDigit(c)) && (Character.valueOf(c)!= sample[0]) && (Character.valueOf(c)!= sample[1]))
- {
- return false;
- }
- }
- return true;
- }
- public static boolean BookSeats(String user, String seatsRequested, String Date) throws IOException
- {
- for (int day = 0; day < Days.size();day++)
- {
- if (Date.equals(Days.get(day).Date))
- {
- if (Days.get(day).BookSeats(user, seatsRequested) == true)
- {
- return true;
- }
- }
- }
- return false;
- }
- public static int CancelBookings(String user, String seatsToCancel,String Date) throws IOException
- {
- for (int day = 0; day < Days.size();day++)
- {
- if (Date.equals(Days.get(day).Date))
- {
- return Days.get(day).CancelBookings(user, seatsToCancel);
- }
- }
- return 0;
- }
- public static void LogIP(String ip)
- {
- UserDetails temp = new UserDetails("null", "null");
- loggedInUserList.add(new LoggedInUsers(temp,ip));
- }
- public static void LogUser(String user,String ip)
- {
- for(int idx = 0; idx < loggedInUserList.size(); idx ++)
- {
- if(loggedInUserList.get(idx).IP.equals(ip))
- {
- for(int idx2 = 0; idx2 < userList.size(); idx2 ++)
- {
- if(userList.get(idx2).username.equals(user))
- {
- loggedInUserList.get(idx).User = userList.get(idx2);
- }
- }
- }
- }
- System.out.print("User logged in with: " + user + " and IP address " + ip + System.getProperty("line.separator"));
- }
- public static void LogUserOut(String ip)
- {
- UserDetails temp = new UserDetails("null", "null");
- for(int idx = 0; idx < loggedInUserList.size(); idx ++)
- {
- if(loggedInUserList.get(idx).IP.equals(ip))
- {
- loggedInUserList.get(idx).User = temp;
- }
- }
- }
- public static int getFreeSeats(String Date)
- {
- for (int day = 0; day < Days.size();day++)
- {
- if (Date.equals(Days.get(day).Date))
- {
- return Days.get(day).getFreeSeats();
- }
- }
- return 0;
- }
- public static String getFilmName(String Date)
- {
- String temp = "";
- for (int day = 0; day < Days.size();day++)
- {
- if (Date.equals(Days.get(day).Date))
- {
- return Days.get(day).FilmName;
- }
- }
- return temp;
- }
- public static String getFilmTime(String Date)
- {
- String temp = "";
- for (int day = 0; day < Days.size();day++)
- {
- if (Date.equals(Days.get(day).Date))
- {
- return Days.get(day).getShowingTime();
- }
- }
- return temp;
- }
- }
Add Comment
Please, Sign In to add comment