Advertisement
Guest User

ссаные маршруты

a guest
Sep 23rd, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.25 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.ArrayList;
  3.  
  4. /**
  5.  *
  6.  * @author admin
  7.  */
  8. public class StudyHSE
  9. {
  10.  
  11.     /**
  12.      * @param args the command line arguments
  13.      */
  14.     public static void main(String[] args)
  15.     {
  16.         Scanner in = new Scanner(System.in);
  17.        
  18.         /*System.out.printf("На чем добираться: ");
  19.         int typeIndex = in.nextInt();
  20.         System.out.printf("Откуда добираться: ");
  21.         String home = in.next();
  22.         System.out.printf("Куда добираться: ");
  23.         String hse = in.next();
  24.         String result = askYandex(home, hse, getType(typeIndex));*/    
  25.         /*int[] n = {1,2,3};
  26.         stopsManager.AddBusStop("test", n);*/
  27.         ArrayList<Stop> stops = new ArrayList<Stop>();
  28.         System.out.println("Когда добавите все остановки, напишите 'конец'. Вводите остановки по порядку, как они идут по маршруту.");
  29.        
  30.         String userAddress = "";
  31.         String destinationAddress = null;
  32.         String type = null;
  33.        
  34.        
  35.         int routesCount = 0;
  36.        
  37.         /*Stop[] stops = new Stop[100];
  38.         for (int i = 0; i < 100; i++)
  39.         {
  40.             stops[i] = new Stop(userAddress, userRoutes);
  41.         }*/
  42.         int counter = 0;
  43.        
  44.         while (true)
  45.         {
  46.             System.out.println("Добавить адрес остановки: ");
  47.             userAddress = in.next();
  48.             if (userAddress.equals("конец")) break;
  49.             System.out.println("Количество маршрутов на остановке: ");
  50.             routesCount = in.nextInt();
  51.             int[] userRoutes = new int[routesCount];
  52.             for (int i = 0; i < routesCount; i++)
  53.             {
  54.                 System.out.printf("Добавить маршрут остановки %s номер %d: ", userAddress, i + 1);
  55.                 userRoutes[i] = in.nextInt();
  56.                 System.out.println();
  57.             }
  58.             Stop stop = new Stop(userAddress, userRoutes);
  59.             stops.add(stop);
  60.            
  61.             if (counter > 0)
  62.             {
  63.                 for (int i = 0; i < stops.get(counter - 1).getLength(); i++)
  64.                 {
  65.                     for (int j = 0; j < stops.get(counter).getLength(); j++)
  66.                     {
  67.                         if (stops.get(counter - 1).getRoute(i) == stops.get(counter).getRoute(j))
  68.                         {
  69.                             stops.get(counter - 1).addNext(stop);
  70.                         }
  71.                     }
  72.                 }
  73.             }
  74.            
  75.             counter++;
  76.         }
  77.        
  78.        
  79.         System.out.println("Ввод остановок закончен. добавлены следующие остановки:");
  80.         for (int i = 0; i < counter; i++)
  81.         {
  82.             System.out.printf("Остановка %s, маршруты: %n", stops.get(i).getAddress());
  83.             for (int j = 0; j < stops.get(i).getLength(); j++)
  84.             {
  85.                 if (stops.get(i).getRoute(j) != 0)
  86.                     System.out.printf("Маршрут №%d %n", stops.get(i).getRoute(j));
  87.             }
  88.         }
  89.         System.out.println("Введите название остановки, на которой вы находитесь (если вы не на остановке, напишите ваш адрес): ");
  90.         userAddress = in.next();
  91.         System.out.println("Введите название нужной остановки: ");
  92.         destinationAddress = in.next();
  93.         System.out.println("Как вы хотите добираться (пешком/авто/маршрутка):");
  94.         type = in.next();
  95.         System.out.println("Делаю запрос в ФСБ...");
  96.         System.out.println(askYandex(userAddress, destinationAddress, type, stops));
  97.         System.out.println("Приехали!");
  98.     }
  99.    
  100.     public static String askYandex(String from, String to, String type, ArrayList<Stop> busBase)
  101.     {
  102.         switch (type)
  103.         {
  104.             case "пешком":
  105.                 return "Не прикидывайся, будто не знаешь дорогу";
  106.             case "авто":
  107.                 return "Ну и зачем тебе машина, если ты даже не знаешь, как до Вышки доехать?";
  108.             case "маршрутка":
  109.                 boolean toContinue = true;
  110.                 ArrayList<Stop> nextStops = null;
  111.                 int startIndex = 0;
  112.                 int lastIndex = 0;
  113.                 int[] startRoutes = new int[10];
  114.                 int[] lastRoutes = new int[10];
  115.                 int route = 0;
  116.                 for(Stop stop : busBase)
  117.                 {
  118.                     if (stop.getAddress().equals(from))
  119.                     {
  120.                         //System.out.println("S1");
  121.                         startIndex = busBase.indexOf(stop);
  122.                         for (int i = 0; i < stop.getLength(); i++)
  123.                         {
  124.                             startRoutes[i] = stop.getRoute(i);
  125.                             //System.out.println(stop.getRoute(i));
  126.                         }
  127.                         if (stop.getNextStops() != null)
  128.                         {
  129.                             nextStops = stop.getNextStops();        
  130.                         }
  131.                     }
  132.                     if (stop.getAddress().equals(to))
  133.                     {
  134.                         lastIndex = busBase.indexOf(stop);
  135.                         for (int i = 0; i < stop.getLength(); i++)
  136.                         {
  137.                             lastRoutes[i] = stop.getRoute(i);
  138.                             //System.out.println(stop.getRoute(i));
  139.                         }
  140.                     }
  141.                 }
  142.                
  143.                 for (int i = 0; i < 10; i++)
  144.                     for (int j = 0; j < 10; j++)
  145.                         if (startRoutes[i] == lastRoutes[j] && startRoutes[i] != 0)
  146.                         {
  147.                             route = startRoutes[i];
  148.                             break;
  149.                         }
  150.                
  151.                 System.out.println("Выбран маршрут " + route);
  152.                
  153.                 if (route == 0) return "Иди пешком, я ничего не нашёл.";
  154.                 else
  155.                 {
  156.                     System.out.println("Идите на остановку " + from + ", садитесь на маршрут " + route);
  157.                     while (toContinue)
  158.                     {
  159.                         Stop current = busBase.get(startIndex);
  160.                         if (current.getAddress() != to && nextStops != null)
  161.                         {
  162.                             for (Stop stop : nextStops)
  163.                             {
  164.                                 for (int i = 0; i < stop.getLength(); i++)
  165.                                 {
  166.                                     if (stop.getRoute(i) == route)
  167.                                     {
  168.                                         current = stop;
  169.                                         nextStops = current.getNextStops();
  170.                                         System.out.println("Остановка " + current.getAddress());
  171.                                     }
  172.                                 }
  173.                             }
  174.                         }
  175.                         else
  176.                         {
  177.                             toContinue = false;
  178.                             return "остановка " + current.getAddress() + ". Приехали!";
  179.                         }
  180.                     }
  181.                 }
  182.                 return null;
  183.             default: return "Обнаружен недопустимый запрос. Мне очень жаль.";
  184.         }
  185.     }
  186.     public static String getType(int type)
  187.     {
  188.         switch (type)
  189.         {
  190.             case 1:
  191.                 return "пешком";
  192.             case 2:
  193.                 return "авто";
  194.             case 3:
  195.                 return "маршрутка";
  196.             default: return null;
  197.         }
  198.     }
  199. }
  200.  
  201. class Stop
  202. {
  203.     private String address;
  204.     private Bus busRoutes;
  205.     private ArrayList<Stop> nextStop;
  206.    
  207.     public Stop(String newAddress, int[] routes)
  208.     {
  209.         address = newAddress;
  210.         busRoutes = new Bus(routes);
  211.         nextStop = new ArrayList<Stop>();
  212.     }
  213.    
  214.     public void addNext(Stop stop)
  215.     {
  216.         nextStop.add(stop);
  217.     }
  218.    
  219.     public ArrayList<Stop> getNextStops()
  220.     {
  221.         return nextStop;
  222.     }
  223.    
  224.     public String getAddress()
  225.     {
  226.         return address;
  227.     }
  228.    
  229.     public int getRoute(int index)
  230.     {
  231.         return busRoutes.getBus(index);
  232.     }
  233.    
  234.     public int getLength()
  235.     {
  236.         return busRoutes.getLength();
  237.     }
  238. }
  239.  
  240. class Bus
  241. {
  242.     private int[] routes;
  243.     public Bus(int[] getRoutes)
  244.     {
  245.         routes = getRoutes;
  246.     }
  247.    
  248.     public int getBus(int index)
  249.     {
  250.         return routes[index];
  251.     }
  252.    
  253.     public int getLength()
  254.     {
  255.         return routes.length;
  256.     }
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement