Guest User

Untitled

a guest
Jul 29th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.52 KB | None | 0 0
  1. /**
  2.  * @authors Adam Brickett (N0309883) and Michael Fleming (N0338124)
  3.  */
  4.  
  5. import java.io.*;
  6. import java.util.LinkedList;
  7. import java.util.List;
  8. import java.util.StringTokenizer;
  9.  
  10. public class SimpleServer {
  11.  
  12.     public static List<LoggedInUsers> loggedInUserList = new LinkedList<LoggedInUsers>();
  13.     public static List<UserDetails> userList = new LinkedList<UserDetails>();
  14.     public static List<Day> Days = new LinkedList<Day>();
  15.    
  16.    
  17.     public static void main(String[] args) throws FileNotFoundException, IOException {
  18.         if( args.length != 1 )
  19.         {
  20.            System.out.println("USAGE: SimpleServer <listeningPort>");
  21.            System.exit(0);
  22.         }
  23.         int listeningPort = Integer.parseInt(args[0]);
  24.        
  25.         /*
  26.          * start the server off...
  27.          */
  28.        
  29.         //String fileName = "E:\\UNI WORK\\Systems Software\\Coursework\\Second part of Coursework\\List_of_Usernames.txt";
  30.         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";
  31.         FileReader fileReader = new FileReader(fileName);
  32.         BufferedReader bufferedReader = new BufferedReader(fileReader);
  33.        
  34.         String line = null;
  35.         while ((line = bufferedReader.readLine()) != null)
  36.         {
  37.            StringTokenizer tokens  = new StringTokenizer(line,","); //splits it into values wherever
  38.                                                        //there is a comma
  39.            String tempUser = tokens.nextToken().trim();
  40.            String tempPass = tokens.nextToken().trim();
  41.            UserDetails temp = new UserDetails(tempUser, tempPass);
  42.            userList.add(temp);
  43.         }
  44.        
  45.         //fileName = "E:\\UNI WORK\\Systems Software\\Coursework\\Second part of Coursework\\Days.txt";
  46.         fileName = "/Volumes/KINGSTON/UNI WORK/Systems Software/Coursework/Second part of Coursework/Days_mac.txt";
  47.         fileReader = new FileReader(fileName);
  48.         bufferedReader = new BufferedReader(fileReader);
  49.        
  50.         line = null;
  51.         while ((line = bufferedReader.readLine()) != null)//splits into lines
  52.         {
  53.            StringTokenizer tokens = new StringTokenizer(line,","); //splits it into values wherever
  54.                                                                    //there is a comma
  55.            
  56.            String tempFilmName = tokens.nextToken().trim();
  57.            String tempDate = tokens.nextToken().trim();
  58.            String tempFileName = tokens.nextToken().trim();
  59.            Day tempDay = new Day(tempFilmName,tempFileName,tempDate);
  60.            Days.add(tempDay);
  61.         }
  62.         LoadDaysFile();
  63.         Server server = new Server(listeningPort);
  64.         server.start();
  65. }
  66.  
  67.     private static void SaveUserstoFile() throws IOException
  68.     {
  69.         String fileToWrite = "E:\\UNI WORK\\Systems Software\\Coursework\\Second part of Coursework\\List_of_Usernames.txt";
  70.        
  71.         FileWriter fileOutput = new FileWriter(fileToWrite);
  72.         PrintWriter printWriter = new PrintWriter(fileOutput,true);
  73.        
  74.         for(int idx = 0; idx < userList.size(); idx++)
  75.         {
  76.            printWriter.println(userList.get(idx).username +"," + userList.get(idx).password);
  77.         }
  78.        
  79.     }
  80.     private static void SaveBookingsToFile() throws IOException
  81.     {
  82.         for (int i = 0; i < Days.size(); i++)
  83.         {
  84.             Days.get(i).SaveBookingsToFile();
  85.         }
  86.     }
  87.     private static void LoadDaysFile() throws FileNotFoundException, IOException
  88.     {
  89.         for (int i = 0; i < Days.size(); i++)
  90.         {
  91.             Days.get(i).LoadDaysFile();
  92.         }
  93.     }
  94.     public static boolean RegisterUser(String user, String pass) throws IOException
  95.     {
  96.         for(int idx = 0; idx < userList.size(); idx++)
  97.         {
  98.             if(userList.get(idx).username.equalsIgnoreCase(user))
  99.             {
  100.                 return false;
  101.             }
  102.         }
  103.         userList.add(new UserDetails(user, pass));
  104.        
  105.         SaveUserstoFile();
  106.         return true;
  107.     }
  108.     public static boolean ValidateUser(String user, String pass)
  109.     {
  110.         for(int idx = 0; idx < userList.size(); idx++)
  111.         {
  112.             for(int idx2 = 0; idx2 < loggedInUserList.size(); idx2 ++)
  113.             {
  114.                 if (loggedInUserList.get(idx2).User.username.equals(user))
  115.                 {
  116.                     return false;
  117.                 }
  118.             }
  119.             if(userList.get(idx).username.equals(user))
  120.             {
  121.                 if(userList.get(idx).password.equals(pass))
  122.                 {
  123.                     return true;
  124.                 }
  125.             }
  126.         }
  127.         return false;
  128.     }
  129.     public static String GetAvailableSeats(String Date)
  130.     {
  131.         String Seats = "";
  132.         for (int seatidx = 0; seatidx < Days.size();seatidx++)
  133.         {
  134.             if(Days.get(seatidx).Date.equals(Date))
  135.             {
  136.                 Seats = Days.get(seatidx).GetAvailableSeats();
  137.             }
  138.         }
  139.         return Seats;
  140.     }
  141.     public static boolean CSVCheck(final String stringToCheck)
  142.     {
  143.         char[] sample = ", ".toCharArray();
  144.         if (stringToCheck.isEmpty())
  145.         {
  146.             return false;
  147.         }
  148.         for(char c: stringToCheck.toCharArray())
  149.         {
  150.             if((!Character.isDigit(c)) && (Character.valueOf(c)!= sample[0]) && (Character.valueOf(c)!= sample[1]))
  151.             {
  152.                 return false;
  153.             }
  154.         }
  155.         return true;
  156.     }
  157.     public static boolean BookSeats(String user, String seatsRequested, String Date) throws IOException
  158.     {
  159.         for (int day = 0; day < Days.size();day++)
  160.         {
  161.             if (Date.equals(Days.get(day).Date))
  162.             {
  163.                 if (Days.get(day).BookSeats(user, seatsRequested) == true)
  164.                 {
  165.                     return true;
  166.                 }
  167.             }
  168.         }
  169.         return false;
  170.     }
  171.     public static int CancelBookings(String user, String seatsToCancel,String Date) throws IOException
  172.     {
  173.         for (int day = 0; day < Days.size();day++)
  174.         {
  175.             if (Date.equals(Days.get(day).Date))
  176.             {
  177.                 return Days.get(day).CancelBookings(user, seatsToCancel);
  178.             }
  179.         }
  180.         return 0;
  181.        
  182.     }
  183.     public static void LogIP(String ip)
  184.     {
  185.         UserDetails temp = new UserDetails("null", "null");
  186.             loggedInUserList.add(new LoggedInUsers(temp,ip));
  187.     }
  188.     public static void LogUser(String user,String ip)
  189.     {
  190.         for(int idx = 0; idx < loggedInUserList.size(); idx ++)
  191.         {
  192.             if(loggedInUserList.get(idx).IP.equals(ip))
  193.             {
  194.                 for(int idx2 = 0; idx2 < userList.size(); idx2 ++)
  195.                 {
  196.                     if(userList.get(idx2).username.equals(user))
  197.                     {
  198.                         loggedInUserList.get(idx).User = userList.get(idx2);
  199.                     }
  200.                 }
  201.                
  202.             }
  203.         }
  204.         System.out.print("User logged in with: " + user + " and IP address " + ip + System.getProperty("line.separator"));
  205.     }
  206.     public static void LogUserOut(String ip)
  207.     {
  208.         UserDetails temp = new UserDetails("null", "null");
  209.         for(int idx = 0; idx < loggedInUserList.size(); idx ++)
  210.         {
  211.             if(loggedInUserList.get(idx).IP.equals(ip))
  212.             {
  213.                 loggedInUserList.get(idx).User = temp;
  214.             }
  215.         }
  216.     }
  217.     public static int getFreeSeats(String Date)
  218.     {
  219.         for (int day = 0; day < Days.size();day++)
  220.         {
  221.             if (Date.equals(Days.get(day).Date))
  222.             {
  223.                 return Days.get(day).getFreeSeats();
  224.             }
  225.         }
  226.         return 0;
  227.     }
  228.     public static String getFilmName(String Date)
  229.     {
  230.         String temp = "";
  231.         for (int day = 0; day < Days.size();day++)
  232.         {
  233.             if (Date.equals(Days.get(day).Date))
  234.             {
  235.                 return Days.get(day).FilmName;
  236.             }
  237.         }        
  238.         return temp;
  239.     }
  240.     public static String getFilmTime(String Date)
  241.     {
  242.         String temp = "";
  243.         for (int day = 0; day < Days.size();day++)
  244.         {
  245.             if (Date.equals(Days.get(day).Date))
  246.             {
  247.                 return Days.get(day).getShowingTime();
  248.             }
  249.         }        
  250.         return temp;
  251.     }
  252. }
Add Comment
Please, Sign In to add comment