This commit is contained in:
Gašper Dobrovoljc 2024-12-29 14:04:25 +01:00
parent 8dd164785e
commit bb6c69aa0a
No known key found for this signature in database
GPG Key ID: 0E7E037018CFA5A5

View File

@ -0,0 +1,114 @@
package izziv5;
import java.util.Scanner;
public class Izziv5 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Vnesi velikost n: ");
int n = Integer.parseInt(scanner.nextLine());
Oseba[] tt = new Oseba[n];
for (int i = 0; i < n; i++) {
tt[i] = new Oseba();
}
Oseba[] t = new Oseba[n];
while (true) {
System.arraycopy(tt, 0, t, 0, n);
print(t);
System.out.print("Vnesi atr [0 = ime, 1 = priimek, 2 = letoR]: ");
Oseba.atr = Integer.parseInt(scanner.nextLine());
System.out.print("Vnesi smer [0 = naraščujoče, 1 = padajoče]: ");
Oseba.smer = Integer.parseInt(scanner.nextLine());
sort(t);
System.out.print("Ponovitev [d/N]: ");
String s = scanner.nextLine();
if (!s.equals("D") && !s.equals("d")) {
break;
}
}
}
private static void print(Oseba[] array) {
print(array, -1);
}
private static void print(Oseba[] array, int split) {
if (array.length == 0) {
return;
}
if (split == 0) {
System.out.print("| ");
}
System.out.print(array[0]);
for (int i = 1; i < array.length; i++) {
if (i == split) {
System.out.print(" |");
}
System.out.print(" " + array[i]);
}
System.out.println();
}
private static void sort(Oseba[] array) {
for (int i = 1; i < array.length; i++) {
int swap = array.length - 1;
for (int j = array.length - 1; j >= i; j--) {
if (Oseba.smer == 0 ? array[j - 1].compareTo(array[j]) > 0 : array[j - 1].compareTo(array[j]) < 0) {
Oseba tmp = array[j];
array[j] = array[j - 1];
array[j - 1] = tmp;
swap = j;
}
}
i = swap;
print(array, i);
}
}
}
class Oseba implements Comparable<Oseba> {
private static final String[] imena = new String[]{"Franc", "Janez", "Marko", "Andrej", "Ivan", "Anton", "Luka", "Jože", "Matej", "Jožef", "Marija", "Ana", "Maja", "Irena", "Mojca", "Nina", "Mateja", "Nataša", "Eva", "Andreja"};
private static final String[] priimki = new String[]{"Novak", "Horvat", "Kovačič", "Kranjc", "Zupančič", "Kovač", "Potočnik", "Mlakar", "Vidmar", "Kos", "Golob", "Kralj", "Turk", "Božič", "Korošec", "Bizjak", "Zupan", "Kotnik", "Hribar", "Kavčič"};
static int atr = 0;
static int smer = 0;
String ime, priimek;
int letoR;
Oseba() {
this.ime = imena[(int) (Math.random() * imena.length)];
this.priimek = priimki[(int) (Math.random() * priimki.length)];
this.letoR = (int) (Math.random() * (2024 - 1960) + 1960);
}
@Override
public String toString() {
return switch (atr) {
case 0 -> ime;
case 1 -> priimek;
case 2 -> Integer.toString(letoR);
default -> "";
};
}
@Override
public int compareTo(Oseba o) {
return switch (atr) {
case 0 -> ime.compareTo(o.ime);
case 1 -> priimek.compareTo(o.priimek);
case 2 -> letoR - o.letoR;
default -> 0;
};
}
}