-
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
8be87fe
c792975
b78d977
3bdf700
99d6b61
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 |
|---|---|---|
| @@ -1,15 +1,10 @@ | ||
| *.iml | ||
| .gradle | ||
| /local.properties | ||
| /.idea/caches | ||
| /.idea/libraries | ||
| /.idea/modules.xml | ||
| /.idea/workspace.xml | ||
| /.idea/navEditor.xml | ||
| /.idea/assetWizardSettings.xml | ||
| /.idea | ||
| .DS_Store | ||
| /build | ||
| /captures | ||
| .externalNativeBuild | ||
| .cxx | ||
| local.properties | ||
| local.properties | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,107 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
| int carQTY = 3; //количество автомобилей участвующих в гонке. | ||
| Cars cars = new Cars(); // Гараж с автомобилями. | ||
| cars.registration(carQTY); //регистрация машин. | ||
| Race race = new Race(cars); //регистрация гонки. | ||
| race.start(); //запуск гонки | ||
| } | ||
| } | ||
|
|
||
| class Car { | ||
| String name; | ||
| int speed; | ||
|
|
||
| public Car(String name, int speed) { | ||
| this.name = name; | ||
| this.speed = speed; | ||
| } | ||
| } | ||
|
|
||
| class Cars { | ||
| ArrayList<Car> cars = new ArrayList<>(); | ||
|
|
||
| public ArrayList<Car> getInfo() { | ||
| return cars; | ||
| } | ||
|
|
||
| public void registration(int qty) { | ||
| System.out.println("Добро пожаловать на 24 часа Ле-Мана!\nДавайте зарегистрируем автомобили."); | ||
| Scanner scanner = new Scanner(System.in); | ||
| while (qty > 0) { | ||
|
|
||
| String name; | ||
| String speed; | ||
| int speedInt; | ||
|
|
||
| while (true) { | ||
| System.out.format("\nВам осталось ввести %d авто.\nВведите имя машины:", qty); | ||
| name = scanner.nextLine(); | ||
| if (!this.isExist(name) && !name.trim().isEmpty()) { | ||
| break; | ||
| } | ||
| if (name.trim().isEmpty()) { | ||
| System.out.println("имя не может быть пустым.\n"); | ||
| } else { | ||
| System.out.format("'%s' уже существует, пожалуйста используйте уникальные имена.\n", name); | ||
| } | ||
| } | ||
| while (true) { | ||
| System.out.format("Введите скорость автомобиля '%s'(от 0 до 250км/ч):", name); | ||
| speed = scanner.nextLine(); | ||
| if (canConvertToInt(speed)) { | ||
| speedInt = Integer.parseInt(speed); | ||
| if (speedInt >= 0 && speedInt <= 250) { | ||
| break; | ||
| } | ||
| } | ||
| System.out.println("Вы ввели не верное значение."); | ||
| } | ||
| this.addCar(name, speedInt); | ||
| qty--; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| public boolean isExist(String name) { | ||
| for (Car existingCar : cars) { | ||
| if (existingCar.name.equals(name)) { | ||
| return true; | ||
|
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. Рекомендация: если нашли авто с таким имененм, значит, можно дальше по циклу не идти и сделать break
Owner
Author
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. если мы возвращаем true - то цикл автоматически должен прерваться, верно? тогда команда break зачем? 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. да, согласна. Здесь цикл прерывается |
||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| public void addCar(String name, int speed) { | ||
| cars.add(new Car(name, speed)); | ||
| } | ||
|
|
||
| boolean canConvertToInt(String str) { | ||
| return str.matches("-?\\d+"); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| class Race { | ||
| Cars cars; // автомобили участвующие в гонке | ||
| Car leader = new Car("", -1); // автомобиль выигравший гонку; | ||
| int time = 24; // время гонки | ||
|
|
||
| public Race(Cars cars) { | ||
| this.cars = cars; | ||
| } | ||
|
|
||
| public void start() { | ||
| System.out.println("Все автомобили зарегистрированы, начинаем гонку."); | ||
| for (Car car : cars.getInfo()) { | ||
| if (car.speed > leader.speed) { | ||
| leader = car; | ||
| } | ||
| } | ||
| System.out.format("Финиш\nПобедил автомобиль '%s'. Он проехал %dкм за 24 часа.", leader.name, time * leader.speed); | ||
| } | ||
| } | ||
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.
Комментарий про "файлы директорий .idea и build стоит вынести в .gitignore" ты понял немного не так. Наоборот , /.idea/ и /build должны обязательно присутствовать в файле .gitignore для того, чтобы вынести их из-под версионного контроля. И что бы они каждый раз при перестроении проекта не попадали в PR