-
Notifications
You must be signed in to change notification settings - Fork 0
Проектная работа No1 спринта No2 #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| public class Car { | ||
| String name; | ||
| int speed; | ||
|
Comment on lines
+2
to
+3
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Поля лучше пометить |
||
|
|
||
| Car(String name, int speed) { | ||
| this.name = name; | ||
| this.speed = speed; | ||
| } | ||
|
|
||
| int calcDistance(int hour) { | ||
| return speed * hour; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,48 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| Scanner scanner = new Scanner(System.in); | ||
| ArrayList<Car> cars = new ArrayList<>(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. От хранения массива машин и лишнего цикла при определении победителя можно избавиться, если при вводе данных сразу вычислять победителя и хранить его в отдельной переменной, тогда программа будет требовать меньше памяти и работать быстрее |
||
|
|
||
| for (int i = 0; i < 3; i++) { | ||
| System.out.println("Введите название автомобиля No" + (i + 1) + ":"); | ||
| String carName = scanner.nextLine(); | ||
| carName = carName.trim(); | ||
| while (carName.isEmpty()) { | ||
| System.out.println("Введена пустая строка. Введите название автомобиля No" + (i + 1) + " повторно:"); | ||
| carName = scanner.nextLine(); | ||
| carName = carName.trim(); | ||
| } | ||
|
Comment on lines
+12
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Код для считывания непустой строки с ввода лучше вынести в отдельную функцию - код, разделённый на небольшие функции, легче читать, поддерживать и переиспользовать |
||
|
|
||
| System.out.println("Введите скорость автомобиля No" + (i + 1) + ":"); | ||
| int speed; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Аналогично код для считывания скорости лучше вынести в отдельную функцию |
||
| while (true) { | ||
| String strSpeed = scanner.nextLine(); | ||
| strSpeed = strSpeed.trim(); | ||
| if (strSpeed.isEmpty()) { | ||
| System.out.println("Введена пустая строка. Введите скорость автомобиля No" + (i + 1) + " повторно:"); | ||
| continue; | ||
| } | ||
| try { | ||
| speed = Integer.parseInt(strSpeed); | ||
| } catch (NumberFormatException ex) { | ||
| System.out.println("Введено не число. Введите скорость автомобиля No" + (i + 1) + " повторно:"); | ||
| continue; | ||
| } | ||
| if (speed <= 0 || speed > 250) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Минимальную и максимальную скорости лучше вынести в константы для повышения читабельности кода |
||
| System.out.println("Скорость вне возможного диапазона (0;250]. Введите скорость автомобиля No" + (i + 1) + " повторно:"); | ||
| continue; | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| Car car = new Car(carName, speed); | ||
| cars.add(car); | ||
| } | ||
|
|
||
| Race race = new Race(); | ||
| race.calcWinner(cars); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import java.util.ArrayList; | ||
|
|
||
| public class Race { | ||
|
|
||
| void calcWinner(ArrayList<Car> cars) { | ||
| int hour = 24; | ||
| String leader = ""; | ||
| int winnerDistance = 0; | ||
| for (int i = 0; i < cars.size(); i++) { | ||
| Car car = cars.get(i); | ||
| int distance = car.calcDistance(hour); | ||
| if (distance > winnerDistance) { | ||
| winnerDistance = distance; | ||
| leader = car.name; | ||
| } | ||
| } | ||
| System.out.println("Самая быстрая машина: " + leader); | ||
| } | ||
|
|
||
| } |