-
Notifications
You must be signed in to change notification settings - Fork 0
Проектная работа №1 #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
base: master
Are you sure you want to change the base?
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,20 @@ | ||
| public class Car { | ||
| String name; | ||
| int speed; | ||
|
|
||
| void setCarName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| void setSpeed(int speed) { | ||
| this.speed = speed; | ||
| } | ||
|
|
||
| String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| double calcKmForDay() { | ||
| return speed * 24; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,37 @@ | ||
| import java.util.InputMismatchException; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
| Race race = new Race(); | ||
| Scanner scan = new Scanner(System.in); | ||
| while (race.getCarArraySize() < 3) { | ||
| Car car = new Car(); | ||
| int number = race.getCarArraySize() + 1; | ||
| System.out.println("Введите название машины №" + number); | ||
| car.setCarName(scan.next()); | ||
| boolean validInput = false; | ||
| while (!validInput) { | ||
| try { | ||
| System.out.println("Введите скорость машины №" + number); | ||
| int speed = scan.nextInt(); | ||
| if (speed > 0 && speed <= 250) { | ||
| validInput = true; | ||
| car.setSpeed(speed); | ||
| } else { | ||
| throw new Exception(); | ||
| } | ||
|
Comment on lines
+22
to
+24
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. try/catch лучше использовать только когда что-то действительно идёт не так. В данном случае лучше использовать if/else/ |
||
| } catch (InputMismatchException e) { | ||
| System.out.println("Ошибка: это не число"); | ||
| scan.next(); | ||
| } catch (Exception e) { | ||
| System.out.println("Ошибка: введите корректное число от 0 до 250"); | ||
| } | ||
|
Comment on lines
+25
to
+30
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. Здорово, что обрабатываешь ошибки, но здесь можно было сделать проще использую метод сканнера |
||
| } | ||
| race.addCar(car); | ||
| } | ||
| Car leader = race.findLeader(); | ||
| System.out.println("Лидер гонки: " + leader.getName()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import java.util.ArrayList; | ||
|
|
||
| public class Race { | ||
| ArrayList<Car> cars = new ArrayList<>(); | ||
| Car leader = new Car(); | ||
|
|
||
| void addCar(Car car) { | ||
| cars.add(car); | ||
| } | ||
|
|
||
| Car findLeader() { | ||
| for (Car car : cars) { | ||
| if (car.calcKmForDay() > leader.calcKmForDay()) { | ||
| leader = car; | ||
| } | ||
| } | ||
| return leader; | ||
| } | ||
|
|
||
| int getCarArraySize() { | ||
| return cars.size(); | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Здорово, что вынес отдельно в класс логику.