From bb6c69aa0a6596f046ba6bdd3b964c319db5b4b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C5=A1per=20Dobrovoljc?= Date: Sun, 29 Dec 2024 14:04:25 +0100 Subject: [PATCH] Izziv 5 --- izzivi/src/izziv5/Izziv5.java | 114 ++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 izzivi/src/izziv5/Izziv5.java diff --git a/izzivi/src/izziv5/Izziv5.java b/izzivi/src/izziv5/Izziv5.java new file mode 100644 index 0000000..aa3b4c9 --- /dev/null +++ b/izzivi/src/izziv5/Izziv5.java @@ -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 { + 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; + }; + } +} \ No newline at end of file