import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int choice, chooseCar, i = 0;
String bufor;
Scanner scanner = new Scanner(System.in);
Car[] cars = null;
cars = new Car[100];
do {
System.out.println("Witaj w autolandii, co chcesz zrobić?");
System.out.println("1.Stwórz samochód...");
System.out.println("2.Usuń samochód...");
System.out.println("3.Modyfikuj samochód...");
System.out.println("4.Wyświetl samochód...");
System.out.println("0.Zakończ program...");
choice = scanner.nextInt();
bufor = scanner.nextLine();
switch (choice) {
case 1:
System.out.println("Tworzenie samochodu.");
cars[i] = new Car();
System.out.println("Podaj markę samochodu.");
cars[i].brand = scanner.nextLine();
System.out.println("Podaj model samochodu.");
cars[i].model = scanner.nextLine();
System.out.println("Podaj kraj produkcji samochodu.");
cars[i].country = scanner.nextLine();
System.out.println("Podaj rok produkcji samochodu.");
cars[i].year = scanner.nextInt();
bufor = scanner.nextLine();
System.out.println("Podaj imię aktualnego właściciela.");
cars[i].owners.firstName = scanner.nextLine();
System.out.println("Podaj nazwisko aktualnego właściciela.");
cars[i].owners.secondName = scanner.nextLine();
System.out.println("Podaj liczbę poprzednich właścicieli.");
cars[i].ownersNumber = scanner.nextInt();
bufor = scanner.nextLine();
System.out.println("Gotowe");
i++;
break;
case 2:
System.out.println("Usuwanie samochodu.");
System.out.println("Lista samochodów:");
for (int x = 0; x < i; x++) {
System.out.printf("%d. %s %s\n", x + 1, cars[x].brand, cars[x].model);
}
System.out.println("Podaj, który samochód chcesz usunąć.");
chooseCar = scanner.nextInt();
if (chooseCar - 1 <= i && chooseCar > 0) {
for (; chooseCar < i; chooseCar++) {
cars[chooseCar - 1].brand = cars[chooseCar].brand;
cars[chooseCar - 1].model = cars[chooseCar].model;
cars[chooseCar - 1].country = cars[chooseCar].country;
cars[chooseCar - 1].year = cars[chooseCar].year;
cars[chooseCar - 1].owners.firstName = cars[chooseCar].owners.firstName;
cars[chooseCar - 1].owners.secondName = cars[chooseCar].owners.secondName;
cars[chooseCar - 1].ownersNumber = cars[chooseCar].ownersNumber;
}
i--;
} else System.out.println("Samochód nie istnieje w bazie.");
System.out.println("Gotowe");
break;
case 3:
int chooseModify;
System.out.println("Modyfikacja parametrów samochodu.");
System.out.println("Lista samochodów:");
for (int x = 0; x < i; x++) {
System.out.printf("%d. %s %s\n", x+1, cars[x].brand, cars[x].model);
}
System.out.println("Podaj, który samochód chcesz zmodyfikować.");
chooseCar = scanner.nextInt();
if (chooseCar <= i && chooseCar > 0) {
chooseCar--;
System.out.println("Wybierz co chcesz zmodyfikować:");
System.out.println("1.Marka.");
System.out.println("2.Model.");
System.out.println("3.Kraj.");
System.out.println("4.Rok.");
System.out.println("5.Imię aktualnego właściciela.");
System.out.println("6.Nazwisko aktualnego właściciela.");
System.out.println("7.Liczba poprzednich właścicieli.");
chooseModify = scanner.nextInt();
bufor = scanner.nextLine();
switch (chooseModify) {
case 1:
System.out.println("Podaj markę samochodu.");
cars[chooseCar].brand = scanner.nextLine();
break;
case 2:
System.out.println("Podaj model samochodu.");
cars[chooseCar].model = scanner.nextLine();
break;
case 3:
System.out.println("Podaj kraj produkcji samochodu.");
cars[chooseCar].country = scanner.nextLine();
break;
case 4:
System.out.println("Podaj rok produkcji samochodu.");
cars[chooseCar].year = scanner.nextInt();
break;
case 5:
System.out.println("Podaj imię aktualnego właściciela.");
cars[chooseCar].owners.firstName = scanner.nextLine();
break;
case 6:
System.out.println("Podaj ilość dostępnych samochodów.");
cars[chooseCar].owners.secondName = scanner.nextLine();
break;
case 7:
System.out.println("Podaj liczbę poprzednich właścicieli.");
cars[chooseCar].ownersNumber = scanner.nextInt();
break;
default:
System.out.println("Wybierz opcję od 1 do 7.");
break;
}
} else System.out.println("Samochód nie istnieje w bazie.");
System.out.println("Gotowe");
break;
case 4:
System.out.println("Wyswietlanie samochodu");
System.out.println("Liczba samochodów w bazie: " + i);
System.out.println("Podaj, który samochód chcesz wyświetlić.");
chooseCar = scanner.nextInt();
if (chooseCar <= i && chooseCar > 0) {
chooseCar--;
System.out.println("Model: " + cars[chooseCar].brand);
System.out.println("Marka: " + cars[chooseCar].model);
System.out.println("Kraj produkcji: " + cars[chooseCar].country);
System.out.println("Rok produkcji: " + cars[chooseCar].year);
System.out.println("Wiek samochodu w latach: " + cars[chooseCar].carAge());
System.out.println("Imię i nazwisko właściciela: " + cars[chooseCar].owners.firstName +" "+ cars[chooseCar].owners.secondName);
System.out.println("Liczba poprzednich właścicieli: " + cars[chooseCar].ownersNumber);
} else System.out.println("Samochód nie istnieje w bazie.");
System.out.println("Gotowe");
break;
case 0:
break;
default:
System.out.println("Wybierz opcję od 1 do 4.");
break;
}
} while (choice != 0);
System.out.println("Żegnaj!");
}
}
------------------------------------------------------------------------------------------------------------------------------------------
import java.time.Year;
public class Car
{
int currentYear = Year.now().getValue();
int year, ownersNumber;
String brand, model, country;
Owner owners = new Owner();
int carAge()
{
int age = currentYear - year;
return age;
}
}
------------------------------------------------------------------------------------------------------------------------------------------
public class Owner
{
String firstName, secondName;
}