Advertisement
Guest User

Untitled

a guest
May 20th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.15 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Assignment3 {
  4.  
  5.     public static void printMenu() {
  6.         System.out.println(
  7.                 "Menu: \n0. Exit. \n1. Add Flight. \n2. Remove Flight. \n3. Print DB. \n4. Find Flights. \nSelect an option: ");
  8.     }
  9.  
  10.     public static int[][] add(int[][] arr, int[] element) {
  11.         int[][] ans = new int[arr.length + 1][];
  12.         for (int i = 0; i < arr.length; i++) {
  13.             ans[i] = new int[arr[i].length];
  14.             for (int j = 0; j < arr[i].length; j++) {// רץ על אורך השורה
  15.                 ans[i][j] = arr[i][j];
  16.  
  17.             }
  18.  
  19.         }
  20.  
  21.         int lastRowIndex = ans.length - 1;
  22.         ans[lastRowIndex] = new int[element.length];
  23.  
  24.         for (int i = 0; i < element.length; i++) {
  25.             ans[lastRowIndex][i] = element[i];
  26.  
  27.         }
  28.  
  29.         return ans;
  30.     }
  31.  
  32.     public static boolean isValidFlight(int[] arr) {
  33.         if (arr == null || arr.length != 5) {
  34.             return false;
  35.         }
  36.  
  37.         int source = arr[0];
  38.         int dest = arr[1];
  39.         int timeStart = arr[2];
  40.         int timeFinish = arr[3];
  41.         int price = arr[4];
  42.  
  43.         if (source == dest)
  44.             return false;
  45.         if (timeStart >= timeFinish || timeStart < 0 || timeStart > 24)
  46.             return false;
  47.         if (timeFinish < 0 || timeFinish > 24)
  48.             return false;
  49.         if (price <= 0) // TODO <= or < ??? free flight?!
  50.             return false;
  51.  
  52.         return true;
  53.     }
  54.  
  55.     public static int[][] addFlight(int[][] flightsDB, int[] arr) {
  56.         if (isValidFlight(arr) == true) {
  57.             System.out.println("Flight added successfully!");
  58.             return add(flightsDB, arr);
  59.         } else {
  60.             System.out.println("Invalid flight!");
  61.             return flightsDB;
  62.         }
  63.  
  64.     }
  65.  
  66.     public static int[][] removeFlight(int[][] flightsDB, int index) {
  67.         // TODO: FILL YOUR CODE HERE
  68.         return null;
  69.     }
  70.  
  71.     public static String flightToString(int[] flight) {
  72.         // TODO: FILL YOUR CODE HERE
  73.         return null;
  74.     }
  75.  
  76.     public static void printDB(int[][] flightsDB) {
  77.         // TODO: FILL YOUR CODE HERE
  78.     }
  79.  
  80.     public static int[][] allFlightSequences(int[][] flightsDB, int source, int dest) {
  81.         int[][] flights = new int[0][];
  82.         for (int i = 0; i < flightsDB.length; i++) {
  83.             if (flightsDB[i][0] == source) {
  84.                 if (flightsDB[i][1] == dest) { // if found flights add it
  85.                     int[] arr2 = new int[1];
  86.                     arr2[0] = i; // flight number
  87.                     flights = add(flights, arr2);
  88.                 } else {
  89.                     for (int j = 0; j < flightsDB.length; j++) {
  90.                         if (flightsDB[i][1] == flightsDB[j][0]) { // dest of flight i == source of flight j
  91.                             if (flightsDB[j][1] == dest) {
  92.                                 int[] arr2 = new int[2];
  93.                                 arr2[0] = i; // flight number
  94.                                 arr2[1] = j;
  95.                                 flights = add(flights, arr2);
  96.                             } else {
  97.                                 for (int k = 0; k < flightsDB.length; k++) {
  98.                                     if (flightsDB[j][1] == flightsDB[k][0]) {
  99.                                         if (flightsDB[k][1] == dest) {
  100.                                             int[] arr2 = new int[3];
  101.                                             arr2[0] = i; // flight number
  102.                                             arr2[1] = j;
  103.                                             arr2[2] = k;
  104.                                             flights = add(flights, arr2);
  105.                                         }
  106.                                     }
  107.                                 }
  108.                             }
  109.                         }
  110.                     }
  111.  
  112.                 }
  113.             }
  114.         }
  115.         return flights;
  116.     }
  117.  
  118.     public static int cost(int[][] flightsDB, int[] flightSeq) {
  119.         int total = 0;
  120.  
  121.         for (int i = 0; i < flightSeq.length; i++) {
  122.             int num = flightSeq[i];// מספר טיסה
  123.             total = total + flightsDB[num][4];
  124.         }
  125.  
  126.         return total;
  127.     }
  128.  
  129.     public static int duration(int[][] flightsDB, int[] flightSeq) {
  130.         int total = 0;
  131.         for (int i = 0; i < flightSeq.length; i++) {
  132.             int num = flightSeq[i];
  133.             int time = flightsDB[num][3] - flightsDB[num][2];
  134.             total = total + time;
  135.         }
  136.         return total;
  137.     }
  138.  
  139.     public static void sort(int[][] flightsDB, int[][] allFlightSeqs) {
  140.         // Selection sort
  141.         for (int i = 0; i < allFlightSeqs.length; i++) {
  142.             // find min
  143.             int minIndex = i;
  144.             for (int j = i + 1; j < allFlightSeqs.length; j++) {
  145.                 if (cost(flightsDB, allFlightSeqs[j]) < cost(flightsDB, allFlightSeqs[minIndex]))
  146.                     minIndex = j;
  147.                 else if (cost(flightsDB, allFlightSeqs[j]) == cost(flightsDB, allFlightSeqs[minIndex])) {
  148.                     if(duration(flightsDB, allFlightSeqs[j]) < duration(flightsDB, allFlightSeqs[minIndex]))
  149.                         minIndex = j;
  150.                 }
  151.  
  152.             }
  153.  
  154.             int[] temp = allFlightSeqs[i];
  155.             allFlightSeqs[i] = allFlightSeqs[minIndex];
  156.             allFlightSeqs[minIndex] = temp;
  157.         }
  158.     }
  159.  
  160.     public static void printDealsByPrice(int[][] flightsDB, int source, int dest) {
  161.         // TODO: FILL YOUR CODE HERE
  162.     }
  163.  
  164.     public static void main(String[] args) {
  165.         Scanner sc = new Scanner(System.in);
  166.  
  167.         int input;
  168.         int[][] flightsDB = new int[0][];
  169.  
  170.         flightsDB = new int[][] { { 1, 2, 10, 16, 300 }, { 2, 4, 17, 21, 200 }, { 1, 3, 5, 8, 400 },
  171.                 { 3, 4, 12, 15, 200 }, { 1, 5, 6, 10, 150 }, { 5, 6, 12, 16, 200 }, { 6, 4, 18, 22, 150 } };
  172.         int[][] all = allFlightSequences(flightsDB, 1, 4);
  173.         sort(flightsDB, all);
  174.         for (int i = 0; i < all.length; i++) {
  175.             for (int j = 0; j < all[i].length; j++) {
  176.                 System.out.print(all[i][j] + " ");
  177.             }
  178.             System.out.println();
  179.         }
  180.  
  181.         do {
  182.             printMenu();
  183.             input = sc.nextInt();
  184.             if (input == 1) {
  185.                 System.out.println("Enter a flight:");
  186.                 int arr2[] = new int[5];
  187.                 for (int i = 0; i < 5; i++) {
  188.                     arr2[i] = sc.nextInt();
  189.                 }
  190.                 flightsDB = addFlight(flightsDB, arr2);
  191.             }
  192.         } while (input != 0);
  193.  
  194.         System.out.println("Exiting.");
  195.     }
  196.  
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement