Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Assignment3 {
- public static void printMenu() {
- System.out.println(
- "Menu: \n0. Exit. \n1. Add Flight. \n2. Remove Flight. \n3. Print DB. \n4. Find Flights. \nSelect an option: ");
- }
- public static int[][] add(int[][] arr, int[] element) {
- int[][] ans = new int[arr.length + 1][];
- for (int i = 0; i < arr.length; i++) {
- ans[i] = new int[arr[i].length];
- for (int j = 0; j < arr[i].length; j++) {// רץ על אורך השורה
- ans[i][j] = arr[i][j];
- }
- }
- int lastRowIndex = ans.length - 1;
- ans[lastRowIndex] = new int[element.length];
- for (int i = 0; i < element.length; i++) {
- ans[lastRowIndex][i] = element[i];
- }
- return ans;
- }
- public static boolean isValidFlight(int[] arr) {
- if (arr == null || arr.length != 5) {
- return false;
- }
- int source = arr[0];
- int dest = arr[1];
- int timeStart = arr[2];
- int timeFinish = arr[3];
- int price = arr[4];
- if (source == dest)
- return false;
- if (timeStart >= timeFinish || timeStart < 0 || timeStart > 24)
- return false;
- if (timeFinish < 0 || timeFinish > 24)
- return false;
- if (price <= 0) // TODO <= or < ??? free flight?!
- return false;
- return true;
- }
- public static int[][] addFlight(int[][] flightsDB, int[] arr) {
- if (isValidFlight(arr) == true) {
- System.out.println("Flight added successfully!");
- return add(flightsDB, arr);
- } else {
- System.out.println("Invalid flight!");
- return flightsDB;
- }
- }
- public static int[][] removeFlight(int[][] flightsDB, int index) {
- // TODO: FILL YOUR CODE HERE
- return null;
- }
- public static String flightToString(int[] flight) {
- // TODO: FILL YOUR CODE HERE
- return null;
- }
- public static void printDB(int[][] flightsDB) {
- // TODO: FILL YOUR CODE HERE
- }
- public static int[][] allFlightSequences(int[][] flightsDB, int source, int dest) {
- int[][] flights = new int[0][];
- for (int i = 0; i < flightsDB.length; i++) {
- if (flightsDB[i][0] == source) {
- if (flightsDB[i][1] == dest) { // if found flights add it
- int[] arr2 = new int[1];
- arr2[0] = i; // flight number
- flights = add(flights, arr2);
- } else {
- for (int j = 0; j < flightsDB.length; j++) {
- if (flightsDB[i][1] == flightsDB[j][0]) { // dest of flight i == source of flight j
- if (flightsDB[j][1] == dest) {
- int[] arr2 = new int[2];
- arr2[0] = i; // flight number
- arr2[1] = j;
- flights = add(flights, arr2);
- } else {
- for (int k = 0; k < flightsDB.length; k++) {
- if (flightsDB[j][1] == flightsDB[k][0]) {
- if (flightsDB[k][1] == dest) {
- int[] arr2 = new int[3];
- arr2[0] = i; // flight number
- arr2[1] = j;
- arr2[2] = k;
- flights = add(flights, arr2);
- }
- }
- }
- }
- }
- }
- }
- }
- }
- return flights;
- }
- public static int cost(int[][] flightsDB, int[] flightSeq) {
- int total = 0;
- for (int i = 0; i < flightSeq.length; i++) {
- int num = flightSeq[i];// מספר טיסה
- total = total + flightsDB[num][4];
- }
- return total;
- }
- public static int duration(int[][] flightsDB, int[] flightSeq) {
- int total = 0;
- for (int i = 0; i < flightSeq.length; i++) {
- int num = flightSeq[i];
- int time = flightsDB[num][3] - flightsDB[num][2];
- total = total + time;
- }
- return total;
- }
- public static void sort(int[][] flightsDB, int[][] allFlightSeqs) {
- // Selection sort
- for (int i = 0; i < allFlightSeqs.length; i++) {
- // find min
- int minIndex = i;
- for (int j = i + 1; j < allFlightSeqs.length; j++) {
- if (cost(flightsDB, allFlightSeqs[j]) < cost(flightsDB, allFlightSeqs[minIndex]))
- minIndex = j;
- else if (cost(flightsDB, allFlightSeqs[j]) == cost(flightsDB, allFlightSeqs[minIndex])) {
- if(duration(flightsDB, allFlightSeqs[j]) < duration(flightsDB, allFlightSeqs[minIndex]))
- minIndex = j;
- }
- }
- int[] temp = allFlightSeqs[i];
- allFlightSeqs[i] = allFlightSeqs[minIndex];
- allFlightSeqs[minIndex] = temp;
- }
- }
- public static void printDealsByPrice(int[][] flightsDB, int source, int dest) {
- // TODO: FILL YOUR CODE HERE
- }
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int input;
- int[][] flightsDB = new int[0][];
- flightsDB = new int[][] { { 1, 2, 10, 16, 300 }, { 2, 4, 17, 21, 200 }, { 1, 3, 5, 8, 400 },
- { 3, 4, 12, 15, 200 }, { 1, 5, 6, 10, 150 }, { 5, 6, 12, 16, 200 }, { 6, 4, 18, 22, 150 } };
- int[][] all = allFlightSequences(flightsDB, 1, 4);
- sort(flightsDB, all);
- for (int i = 0; i < all.length; i++) {
- for (int j = 0; j < all[i].length; j++) {
- System.out.print(all[i][j] + " ");
- }
- System.out.println();
- }
- do {
- printMenu();
- input = sc.nextInt();
- if (input == 1) {
- System.out.println("Enter a flight:");
- int arr2[] = new int[5];
- for (int i = 0; i < 5; i++) {
- arr2[i] = sc.nextInt();
- }
- flightsDB = addFlight(flightsDB, arr2);
- }
- } while (input != 0);
- System.out.println("Exiting.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement