TicketDB.java (4032B)
1 import java.io.FileNotFoundException; 2 import java.io.FileWriter; 3 import java.io.IOException; 4 import java.io.File; 5 import java.util.ArrayList; 6 import java.util.Scanner; 7 8 public class TicketDB { 9 public ArrayList<Flight> AllFlights; 10 11 public ArrayList<Ticket> AllTickets; 12 13 public TicketDB() throws FileNotFoundException { 14 AllFlights = new ArrayList<>(); 15 AllTickets = new ArrayList<>(); 16 17 18 // "load" flights (they are given as constants for demo purposes) 19 20 // causing a weird bug due to all flights getting the same "pointer" for their seats 21 // so we create different object for each flight demo 22 23 Seat s1 = new Seat(false, 1); 24 Seat s2 = new Seat(false, 2); 25 Seat s3 = new Seat(false, 3); 26 27 Seat s1_2 = new Seat(false, 1); 28 Seat s2_2 = new Seat(false, 2); 29 Seat s3_2 = new Seat(false, 3); 30 31 Seat s1_3 = new Seat(false, 1); 32 Seat s2_3 = new Seat(false, 2); 33 Seat s3_3 = new Seat(false, 3); 34 35 Seat s1_4 = new Seat(false, 1); 36 Seat s2_4 = new Seat(false, 2); 37 Seat s3_4 = new Seat(false, 3); 38 39 Seat s1_5 = new Seat(false, 1); 40 Seat s2_5 = new Seat(false, 2); 41 Seat s3_5 = new Seat(false, 3); 42 43 Seat []seats1 = {s1,s2,s3}; 44 Seat []seats2 = {s1_2,s2_2,s3_2}; 45 Seat []seats3 = {s1_3,s2_3,s3_3}; 46 Seat []seats4 = {s1_4,s2_4,s3_4}; 47 Seat []seats5 = {s1_5,s2_5,s3_5}; 48 49 50 AllFlights.add(new Flight(seats1, "Monaco", "Barcelona", "05/31/22", "052A")); 51 AllFlights.add(new Flight(seats2, "Monaco", "Barcelona", "06/01/22", "053A")); 52 AllFlights.add(new Flight(seats3, "Barcelona", "Mexico", "06/05/22", "062A")); 53 AllFlights.add(new Flight(seats4, "UK", "Greece", "07/05/22", "072A")); 54 AllFlights.add(new Flight(seats5, "UK", "Greece", "07/06/22", "073A")); 55 56 // load Tickets from file 57 58 try 59 { 60 LoadDB("DataBase.db"); 61 } 62 catch (FileNotFoundException e) 63 { 64 System.out.println("! Database file not found, no tickets loaded."); 65 } 66 67 } 68 69 public void LoadDB(String path) throws FileNotFoundException { 70 File fileObj = new File(path); 71 Scanner reader = new Scanner(fileObj); 72 while (reader.hasNextLine()) { 73 String data = reader.nextLine(); 74 String []ticketData = data.split("-"); 75 ClientInfo c = new ClientInfo(ticketData[3], ticketData[4], 76 ticketData[5], ticketData[6], ticketData[7]); 77 Ticket T = new Ticket(c, ticketData[0], Integer.parseInt(ticketData[1]), ticketData[2]); 78 79 for (Flight f : AllFlights) 80 { 81 if (T.FlightID.equals(f.ID)) 82 { 83 f.Seats[T.SeatID-1].isOccupied = true; 84 break; 85 } 86 } 87 AllTickets.add(T); 88 } 89 reader.close(); 90 } 91 92 public void SaveDB(String path) throws IOException { 93 FileWriter fileObj = new FileWriter(path); 94 for (Ticket T : AllTickets) 95 fileObj.write(T.toDataString() + "\n"); 96 fileObj.close(); 97 } 98 99 public ArrayList<Seat> CheckFlight(Flight F) { 100 ArrayList<Seat> seats = new ArrayList<>(); 101 102 for (Seat s : F.Seats) { 103 if (!s.isOccupied) 104 seats.add(s); 105 } 106 if (seats.size() > 0) 107 return seats; 108 else 109 return null; 110 } 111 112 public Ticket BookSeat(ClientInfo CInfo, int SeatInd, String fID) { 113 for (Flight f : AllFlights) { 114 if (f.ID.equals(fID)) { 115 f.Seats[SeatInd-1].isOccupied = true; 116 String seatid = Integer.toString(f.Seats[SeatInd-1].ID); 117 Ticket t = new Ticket(CInfo, fID, SeatInd, fID + seatid); 118 AllTickets.add(t); 119 return t; 120 } 121 } 122 123 return null; 124 } 125 126 public boolean CheckTicket(Ticket T) { 127 for (Ticket Check : AllTickets) 128 if (Check.TicketID.equals(T.TicketID)) 129 return true; 130 return false; 131 } 132 133 }