Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.ArrayList;
- /**
- *
- * @author admin
- */
- public class StudyHSE
- {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args)
- {
- Scanner in = new Scanner(System.in);
- /*System.out.printf("На чем добираться: ");
- int typeIndex = in.nextInt();
- System.out.printf("Откуда добираться: ");
- String home = in.next();
- System.out.printf("Куда добираться: ");
- String hse = in.next();
- String result = askYandex(home, hse, getType(typeIndex));*/
- /*int[] n = {1,2,3};
- stopsManager.AddBusStop("test", n);*/
- ArrayList<Stop> stops = new ArrayList<Stop>();
- System.out.println("Когда добавите все остановки, напишите 'конец'. Вводите остановки по порядку, как они идут по маршруту.");
- String userAddress = "";
- String destinationAddress = null;
- String type = null;
- int routesCount = 0;
- /*Stop[] stops = new Stop[100];
- for (int i = 0; i < 100; i++)
- {
- stops[i] = new Stop(userAddress, userRoutes);
- }*/
- int counter = 0;
- while (true)
- {
- System.out.println("Добавить адрес остановки: ");
- userAddress = in.next();
- if (userAddress.equals("конец")) break;
- System.out.println("Количество маршрутов на остановке: ");
- routesCount = in.nextInt();
- int[] userRoutes = new int[routesCount];
- for (int i = 0; i < routesCount; i++)
- {
- System.out.printf("Добавить маршрут остановки %s номер %d: ", userAddress, i + 1);
- userRoutes[i] = in.nextInt();
- System.out.println();
- }
- Stop stop = new Stop(userAddress, userRoutes);
- stops.add(stop);
- if (counter > 0)
- {
- for (int i = 0; i < stops.get(counter - 1).getLength(); i++)
- {
- for (int j = 0; j < stops.get(counter).getLength(); j++)
- {
- if (stops.get(counter - 1).getRoute(i) == stops.get(counter).getRoute(j))
- {
- stops.get(counter - 1).addNext(stop);
- }
- }
- }
- }
- counter++;
- }
- System.out.println("Ввод остановок закончен. добавлены следующие остановки:");
- for (int i = 0; i < counter; i++)
- {
- System.out.printf("Остановка %s, маршруты: %n", stops.get(i).getAddress());
- for (int j = 0; j < stops.get(i).getLength(); j++)
- {
- if (stops.get(i).getRoute(j) != 0)
- System.out.printf("Маршрут №%d %n", stops.get(i).getRoute(j));
- }
- }
- System.out.println("Введите название остановки, на которой вы находитесь (если вы не на остановке, напишите ваш адрес): ");
- userAddress = in.next();
- System.out.println("Введите название нужной остановки: ");
- destinationAddress = in.next();
- System.out.println("Как вы хотите добираться (пешком/авто/маршрутка):");
- type = in.next();
- System.out.println("Делаю запрос в ФСБ...");
- System.out.println(askYandex(userAddress, destinationAddress, type, stops));
- System.out.println("Приехали!");
- }
- public static String askYandex(String from, String to, String type, ArrayList<Stop> busBase)
- {
- switch (type)
- {
- case "пешком":
- return "Не прикидывайся, будто не знаешь дорогу";
- case "авто":
- return "Ну и зачем тебе машина, если ты даже не знаешь, как до Вышки доехать?";
- case "маршрутка":
- boolean toContinue = true;
- ArrayList<Stop> nextStops = null;
- int startIndex = 0;
- int lastIndex = 0;
- int[] startRoutes = new int[10];
- int[] lastRoutes = new int[10];
- int route = 0;
- for(Stop stop : busBase)
- {
- if (stop.getAddress().equals(from))
- {
- //System.out.println("S1");
- startIndex = busBase.indexOf(stop);
- for (int i = 0; i < stop.getLength(); i++)
- {
- startRoutes[i] = stop.getRoute(i);
- //System.out.println(stop.getRoute(i));
- }
- if (stop.getNextStops() != null)
- {
- nextStops = stop.getNextStops();
- }
- }
- if (stop.getAddress().equals(to))
- {
- lastIndex = busBase.indexOf(stop);
- for (int i = 0; i < stop.getLength(); i++)
- {
- lastRoutes[i] = stop.getRoute(i);
- //System.out.println(stop.getRoute(i));
- }
- }
- }
- for (int i = 0; i < 10; i++)
- for (int j = 0; j < 10; j++)
- if (startRoutes[i] == lastRoutes[j] && startRoutes[i] != 0)
- {
- route = startRoutes[i];
- break;
- }
- System.out.println("Выбран маршрут " + route);
- if (route == 0) return "Иди пешком, я ничего не нашёл.";
- else
- {
- System.out.println("Идите на остановку " + from + ", садитесь на маршрут " + route);
- while (toContinue)
- {
- Stop current = busBase.get(startIndex);
- if (current.getAddress() != to && nextStops != null)
- {
- for (Stop stop : nextStops)
- {
- for (int i = 0; i < stop.getLength(); i++)
- {
- if (stop.getRoute(i) == route)
- {
- current = stop;
- nextStops = current.getNextStops();
- System.out.println("Остановка " + current.getAddress());
- }
- }
- }
- }
- else
- {
- toContinue = false;
- return "остановка " + current.getAddress() + ". Приехали!";
- }
- }
- }
- return null;
- default: return "Обнаружен недопустимый запрос. Мне очень жаль.";
- }
- }
- public static String getType(int type)
- {
- switch (type)
- {
- case 1:
- return "пешком";
- case 2:
- return "авто";
- case 3:
- return "маршрутка";
- default: return null;
- }
- }
- }
- class Stop
- {
- private String address;
- private Bus busRoutes;
- private ArrayList<Stop> nextStop;
- public Stop(String newAddress, int[] routes)
- {
- address = newAddress;
- busRoutes = new Bus(routes);
- nextStop = new ArrayList<Stop>();
- }
- public void addNext(Stop stop)
- {
- nextStop.add(stop);
- }
- public ArrayList<Stop> getNextStops()
- {
- return nextStop;
- }
- public String getAddress()
- {
- return address;
- }
- public int getRoute(int index)
- {
- return busRoutes.getBus(index);
- }
- public int getLength()
- {
- return busRoutes.getLength();
- }
- }
- class Bus
- {
- private int[] routes;
- public Bus(int[] getRoutes)
- {
- routes = getRoutes;
- }
- public int getBus(int index)
- {
- return routes[index];
- }
- public int getLength()
- {
- return routes.length;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement