Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Ergasia_1
- {
- static Foititis[] pinakas = new Foititis[10];
- public static void main(String[] args)
- {
- pinakas[0] = new Foititis("KappaA", "KappaA", 1, 2014, 5);
- pinakas[1] = new Foititis("KappaB", "KappaB", 2, 2015, 7);
- pinakas[2] = new Foititis("KappaC", "KappaC", 4, 2009, 6);
- pinakas[3] = new Foititis("KappaD", "KappaD", 9, 2005, 9);
- pinakas[4] = new Foititis("KappaE", "KappaE", 17, 2017, 5);
- showMainMenu();
- }
- public static void showMainMenu()
- {
- System.out.println("\n----------------------------");
- System.out.println(" Lista epilogon:");
- System.out.println(" 1. Eisagogi Stoixeion Foititi.");
- System.out.println(" 2. Emfanisi Stoixeion Foititi.");
- System.out.println(" 3. Emfanisi Oloklirou tou Pinaka.");
- System.out.println(" 4. Telos.\n");
- boolean correct = false;
- int choice = 0;
- while(!correct)
- {
- System.out.print(" Doste Epilogi (1-4): ");
- choice = UserInput.getInteger();
- if(choice >= 1 && choice <= 4)
- {
- correct = true;
- }
- }
- switch(choice)
- {
- case 1:
- {
- int slot = -1;
- for(int i = 0; i < pinakas.length; i++)
- {
- if(pinakas[i] == null)
- {
- slot = i;
- break;
- }
- }
- if(slot == -1)
- {
- System.out.println(" O pinakas einai gematos.");
- showMainMenu();
- }
- else
- {
- showInsertData(slot);
- }
- break;
- }
- case 2:
- {
- boolean empty = true;
- for(int i = 0; i < pinakas.length; i++)
- {
- if(pinakas[i] != null)
- {
- empty = false;
- break;
- }
- }
- if(empty)
- {
- System.out.println(" O pinakas einai adeios.");
- showMainMenu();
- }
- else
- {
- showPrintData();
- }
- break;
- }
- case 3:
- {
- boolean empty = true;
- for(int i = 0; i < pinakas.length; i++)
- {
- if(pinakas[i] != null)
- {
- empty = false;
- break;
- }
- }
- if(empty)
- {
- System.out.println(" O pinakas einai adeios.");
- showMainMenu();
- }
- else
- {
- showPrintTable();
- }
- break;
- }
- case 4:
- closeApplication();
- break;
- }
- }
- public static void showInsertData(int slot)
- {
- System.out.println("\n----------------------------");
- System.out.println(" Eisagogi Stoixeion Foititi:");
- System.out.print(" Dose Onoma: ");
- String onoma = UserInput.getString();
- System.out.print(" Dose Epitheto: ");
- String epitheto = UserInput.getString();
- System.out.print(" Dose Ar. Mitroou: ");
- int AM = UserInput.getInteger();
- System.out.print(" Dose Etos Eisagogis: ");
- int etos = UserInput.getInteger();
- System.out.print(" Dose Meso Oro Vathmologion: ");
- int MO = UserInput.getInteger();
- pinakas[slot] = new Foititis(onoma, epitheto, AM, etos, MO);
- showMainMenu();
- }
- public static void showPrintData()
- {
- System.out.println("\n----------------------------");
- System.out.println(" Epilogi Pediou Anazitisis:");
- System.out.println(" 1. Epitheto.");
- System.out.println(" 2. Ar. Mitroou.");
- System.out.println(" 3. Epistrofi stin Arxiki Lista Epilogon.\n");
- boolean correct = false;
- int choice = 0;
- while(!correct)
- {
- System.out.print(" Doste Epilogi (1-3): ");
- choice = UserInput.getInteger();
- if(choice >= 1 && choice <= 3)
- {
- correct = true;
- }
- }
- switch(choice)
- {
- case 1:
- showSearchAlgo(choice);
- break;
- case 2:
- showSearchAlgo(choice);
- break;
- case 3:
- showMainMenu();
- break;
- }
- }
- public static void showSearchAlgo(int x)
- {
- System.out.println("\n----------------------------");
- System.out.println(" Epilogi Methodou Anazitisis:");
- System.out.println(" 1. Seiriaki Anazitisi.");
- System.out.println(" 2. Duadiki Anazitisi (Proxosh o pinakas prepei na einai taksinomimenos.");
- System.out.println(" 3. Epistrofi stin Arxiki Lista Epilogon.\n");
- boolean correct = false;
- int choice = 0;
- while(!correct)
- {
- System.out.print(" Doste Epilogi (1-3): ");
- choice = UserInput.getInteger();
- if(choice >= 1 && choice <= 3)
- {
- correct = true;
- }
- }
- switch(choice)
- {
- case 1:
- {
- if(x == 1)
- {
- System.out.print(" Doste Epitheto: ");
- String i = UserInput.getString();
- int n = MyUtils.seqSearch(pinakas, i);
- if(n != -1)
- {
- System.out.println(pinakas[n].toString());
- showMainMenu();
- }
- else
- {
- System.out.println(" Den uparxei afti i kataxorisi.");
- showMainMenu();
- }
- }
- else
- {
- System.out.print(" Dose Ar. Mitroou: ");
- int i = UserInput.getInteger();
- int n = MyUtils.seqSearch(pinakas, i);
- if(n != -1)
- {
- System.out.println(pinakas[n].toString());
- showMainMenu();
- }
- else
- {
- System.out.println(" Den uparxei afti i kataxorisi.");
- showMainMenu();
- }
- }
- break;
- }
- case 2:
- {
- if(x == 1)
- {
- System.out.print(" Doste Epitheto: ");
- String i = UserInput.getString();
- int n = MyUtils.binSearch(pinakas, i);
- if(n != -1)
- {
- System.out.println(pinakas[n].toString());
- showMainMenu();
- }
- else
- {
- System.out.println(" Den uparxei afti i kataxorisi.");
- showMainMenu();
- }
- }
- else
- {
- System.out.print(" Doste Ar. Mitroou: ");
- int i = UserInput.getInteger();
- int n = MyUtils.binSearch(pinakas, i);
- if(n != -1)
- {
- System.out.println(pinakas[n].toString());
- showMainMenu();
- }
- else
- {
- System.out.println(" Den uparxei afti i kataxorisi.");
- showMainMenu();
- }
- }
- break;
- }
- case 3:
- showMainMenu();
- break;
- }
- }
- public static void showPrintTable()
- {
- for(int i = 0; i < pinakas.length; i++)
- {
- if(pinakas[i] != null)
- {
- System.out.println(pinakas[i].toString());
- }
- }
- showMainMenu();
- }
- public static void closeApplication()
- {
- System.out.println();
- }
- }
- class Foititis
- {
- private String onoma, epitheto;
- private int AM, etos, MO;
- Foititis(String onoma, String epitheto, int AM, int etos, int MO)
- {
- this.onoma = onoma;
- this.epitheto = epitheto;
- this.AM = AM;
- this.etos = etos;
- this.MO = MO;
- }
- public String getOnoma()
- {
- return onoma;
- }
- public String getEpitheto()
- {
- return epitheto;
- }
- public int getAM()
- {
- return AM;
- }
- public int getEtos()
- {
- return etos;
- }
- public int getMO()
- {
- return MO;
- }
- public String toString()
- {
- String s = "\n Onoma: " + onoma;
- s += "\n Epitheto: " + epitheto;
- s += "\n Ar. Mitroou: " + AM;
- s += "\n Etos Eggrafis: " + etos;
- s += "\n Mesos Oros: " + MO;
- return s;
- }
- }
- class MyUtils
- {
- public static int seqSearch(Foititis[] pinakas, int x)
- {
- for(int i = 0; i < pinakas.length; i++)
- {
- if(pinakas[i].getAM() == x)
- {
- return i;
- }
- }
- return -1;
- }
- public static int seqSearch(Foititis[] pinakas, String x)
- {
- for(int i = 0; i < pinakas.length; i++)
- {
- if(pinakas[i].getEpitheto().equals(x))
- {
- return i;
- }
- }
- return -1;
- }
- public static int binSearch(Foititis[] pinakas, int x)
- {
- int mid, pos = -1, left = 0, right = pinakas.length - 1;
- while(pos == -1 && left <= right)
- {
- mid = (left + right) / 2;
- if(x < pinakas[mid].getAM())
- {
- right = mid - 1;
- }
- else
- {
- if(x > pinakas[mid].getAM())
- {
- left = mid + 1;
- }
- else
- {
- pos = mid;
- }
- }
- }
- return pos;
- }
- public static int binSearch(Foititis[] pinakas, String x)
- {
- int mid, pos = -1, left = 0, right = pinakas.length - 1;
- while(pos == -1 && left <= right)
- {
- mid = (left + right) / 2;
- if(pinakas[mid].getEpitheto().compareTo(x) > 0)
- {
- right = mid - 1;
- }
- else
- {
- if(pinakas[mid].getEpitheto().compareTo(x) < 0)
- {
- left = mid + 1;
- }
- else
- {
- pos = mid;
- }
- }
- }
- return pos;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement