Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.25 KB | None | 0 0
  1. class Ergasia_1
  2. {
  3.     static Foititis[] pinakas = new Foititis[10];
  4.    
  5.     public static void main(String[] args)
  6.     {
  7.         pinakas[0] = new Foititis("KappaA", "KappaA", 1, 2014, 5);
  8.         pinakas[1] = new Foititis("KappaB", "KappaB", 2, 2015, 7);
  9.         pinakas[2] = new Foititis("KappaC", "KappaC", 4, 2009, 6);
  10.         pinakas[3] = new Foititis("KappaD", "KappaD", 9, 2005, 9);
  11.         pinakas[4] = new Foititis("KappaE", "KappaE", 17, 2017, 5);
  12.        
  13.         showMainMenu();
  14.     }
  15.    
  16.     public static void showMainMenu()
  17.     {
  18.         System.out.println("\n----------------------------");
  19.         System.out.println(" Lista epilogon:");
  20.         System.out.println(" 1. Eisagogi Stoixeion Foititi.");
  21.         System.out.println(" 2. Emfanisi Stoixeion Foititi.");
  22.         System.out.println(" 3. Emfanisi Oloklirou tou Pinaka.");
  23.         System.out.println(" 4. Telos.\n");
  24.        
  25.         boolean correct = false;
  26.         int choice = 0;
  27.        
  28.         while(!correct)
  29.         {
  30.             System.out.print(" Doste Epilogi (1-4): ");
  31.             choice = UserInput.getInteger();
  32.            
  33.             if(choice >= 1 && choice <= 4)
  34.             {
  35.                 correct = true;
  36.             }
  37.         }
  38.        
  39.         switch(choice)
  40.         {
  41.             case 1:
  42.             {
  43.                 int slot = -1;
  44.                
  45.                 for(int i = 0; i < pinakas.length; i++)
  46.                 {
  47.                     if(pinakas[i] == null)
  48.                     {
  49.                         slot = i;
  50.                         break;
  51.                     }
  52.                 }
  53.                
  54.                 if(slot == -1)
  55.                 {
  56.                     System.out.println(" O pinakas einai gematos.");
  57.                    
  58.                     showMainMenu();
  59.                 }
  60.                 else
  61.                 {
  62.                     showInsertData(slot);
  63.                 }
  64.                 break;
  65.             }
  66.             case 2:
  67.             {
  68.                 boolean empty = true;
  69.                
  70.                 for(int i = 0; i < pinakas.length; i++)
  71.                 {
  72.                     if(pinakas[i] != null)
  73.                     {
  74.                         empty = false;
  75.                         break;
  76.                     }
  77.                 }
  78.                
  79.                 if(empty)
  80.                 {
  81.                     System.out.println(" O pinakas einai adeios.");
  82.                    
  83.                     showMainMenu();
  84.                 }
  85.                 else
  86.                 {
  87.                     showPrintData();
  88.                 }
  89.                
  90.                 break;
  91.             }
  92.             case 3:
  93.             {
  94.                 boolean empty = true;
  95.                
  96.                 for(int i = 0; i < pinakas.length; i++)
  97.                 {
  98.                     if(pinakas[i] != null)
  99.                     {
  100.                         empty = false;
  101.                         break;
  102.                     }
  103.                 }
  104.                
  105.                 if(empty)
  106.                 {
  107.                     System.out.println(" O pinakas einai adeios.");
  108.                    
  109.                     showMainMenu();
  110.                 }
  111.                 else
  112.                 {
  113.                     showPrintTable();
  114.                 }
  115.                
  116.                 break;
  117.             }
  118.             case 4:
  119.                 closeApplication();
  120.                 break;
  121.         }
  122.     }
  123.    
  124.     public static void showInsertData(int slot)
  125.     {
  126.         System.out.println("\n----------------------------");
  127.         System.out.println(" Eisagogi Stoixeion Foititi:");
  128.        
  129.         System.out.print(" Dose Onoma: ");
  130.         String onoma = UserInput.getString();
  131.        
  132.         System.out.print(" Dose Epitheto: ");
  133.         String epitheto = UserInput.getString();
  134.        
  135.         System.out.print(" Dose Ar. Mitroou: ");
  136.         int AM = UserInput.getInteger();
  137.        
  138.         System.out.print(" Dose Etos Eisagogis: ");
  139.         int etos = UserInput.getInteger();
  140.        
  141.         System.out.print(" Dose Meso Oro Vathmologion: ");
  142.         int MO = UserInput.getInteger();
  143.        
  144.         pinakas[slot] = new Foititis(onoma, epitheto, AM, etos, MO);
  145.        
  146.         showMainMenu();
  147.     }
  148.    
  149.     public static void showPrintData()
  150.     {
  151.         System.out.println("\n----------------------------");
  152.         System.out.println(" Epilogi Pediou Anazitisis:");
  153.         System.out.println(" 1. Epitheto.");
  154.         System.out.println(" 2. Ar. Mitroou.");
  155.         System.out.println(" 3. Epistrofi stin Arxiki Lista Epilogon.\n");
  156.        
  157.         boolean correct = false;
  158.         int choice = 0;
  159.        
  160.         while(!correct)
  161.         {
  162.             System.out.print(" Doste Epilogi (1-3): ");
  163.             choice = UserInput.getInteger();
  164.            
  165.             if(choice >= 1 && choice <= 3)
  166.             {
  167.                 correct = true;
  168.             }
  169.         }
  170.        
  171.         switch(choice)
  172.         {
  173.             case 1:
  174.                 showSearchAlgo(choice);
  175.                 break;
  176.             case 2:
  177.                 showSearchAlgo(choice);
  178.                 break;
  179.             case 3:
  180.                 showMainMenu();
  181.                 break;
  182.         }
  183.     }
  184.    
  185.     public static void showSearchAlgo(int x)
  186.     {
  187.         System.out.println("\n----------------------------");
  188.         System.out.println(" Epilogi Methodou Anazitisis:");
  189.         System.out.println(" 1. Seiriaki Anazitisi.");
  190.         System.out.println(" 2. Duadiki Anazitisi (Proxosh o pinakas prepei na einai taksinomimenos.");
  191.         System.out.println(" 3. Epistrofi stin Arxiki Lista Epilogon.\n");
  192.        
  193.         boolean correct = false;
  194.         int choice = 0;
  195.        
  196.         while(!correct)
  197.         {
  198.             System.out.print(" Doste Epilogi (1-3): ");
  199.             choice = UserInput.getInteger();
  200.            
  201.             if(choice >= 1 && choice <= 3)
  202.             {
  203.                 correct = true;
  204.             }
  205.         }
  206.        
  207.         switch(choice)
  208.         {
  209.             case 1:
  210.             {
  211.                 if(x == 1)
  212.                 {
  213.                     System.out.print(" Doste Epitheto: ");
  214.                     String i = UserInput.getString();
  215.                    
  216.                     int n = MyUtils.seqSearch(pinakas, i);
  217.                    
  218.                     if(n != -1)
  219.                     {
  220.                         System.out.println(pinakas[n].toString());
  221.                         showMainMenu();
  222.                     }
  223.                     else
  224.                     {
  225.                         System.out.println(" Den uparxei afti i kataxorisi.");
  226.                         showMainMenu();
  227.                     }
  228.                 }
  229.                 else
  230.                 {
  231.                     System.out.print(" Dose Ar. Mitroou: ");
  232.                     int i = UserInput.getInteger();
  233.                    
  234.                     int n = MyUtils.seqSearch(pinakas, i);
  235.                    
  236.                     if(n != -1)
  237.                     {
  238.                         System.out.println(pinakas[n].toString());
  239.                         showMainMenu();
  240.                     }
  241.                     else
  242.                     {
  243.                         System.out.println(" Den uparxei afti i kataxorisi.");
  244.                         showMainMenu();
  245.                     }
  246.                 }
  247.                
  248.                 break;
  249.             }
  250.             case 2:
  251.             {
  252.                 if(x == 1)
  253.                 {
  254.                     System.out.print(" Doste Epitheto: ");
  255.                     String i = UserInput.getString();
  256.                    
  257.                     int n = MyUtils.binSearch(pinakas, i);
  258.                    
  259.                     if(n != -1)
  260.                     {
  261.                         System.out.println(pinakas[n].toString());
  262.                         showMainMenu();
  263.                     }
  264.                     else
  265.                     {
  266.                         System.out.println(" Den uparxei afti i kataxorisi.");
  267.                         showMainMenu();
  268.                     }
  269.                 }
  270.                 else
  271.                 {
  272.                     System.out.print(" Doste Ar. Mitroou: ");
  273.                     int i = UserInput.getInteger();
  274.                    
  275.                     int n = MyUtils.binSearch(pinakas, i);
  276.                    
  277.                     if(n != -1)
  278.                     {
  279.                         System.out.println(pinakas[n].toString());
  280.                         showMainMenu();
  281.                     }
  282.                     else
  283.                     {
  284.                         System.out.println(" Den uparxei afti i kataxorisi.");
  285.                         showMainMenu();
  286.                     }
  287.                 }
  288.                
  289.                 break;
  290.             }
  291.             case 3:
  292.                 showMainMenu();
  293.                 break;
  294.         }
  295.     }
  296.    
  297.     public static void showPrintTable()
  298.     {
  299.         for(int i = 0; i < pinakas.length; i++)
  300.         {
  301.             if(pinakas[i] != null)
  302.             {
  303.                 System.out.println(pinakas[i].toString());
  304.             }
  305.         }
  306.        
  307.         showMainMenu();
  308.     }
  309.    
  310.     public static void closeApplication()
  311.     {
  312.         System.out.println();
  313.     }
  314. }
  315.  
  316. class Foititis
  317. {
  318.     private String onoma, epitheto;
  319.     private int AM, etos, MO;
  320.    
  321.     Foititis(String onoma, String epitheto, int AM, int etos, int MO)
  322.     {
  323.         this.onoma = onoma;
  324.         this.epitheto = epitheto;
  325.         this.AM = AM;
  326.         this.etos = etos;
  327.         this.MO = MO;
  328.     }
  329.    
  330.     public String getOnoma()
  331.     {
  332.         return onoma;
  333.     }
  334.    
  335.     public String getEpitheto()
  336.     {
  337.         return epitheto;
  338.     }
  339.    
  340.     public int getAM()
  341.     {
  342.         return AM;
  343.     }
  344.    
  345.     public int getEtos()
  346.     {
  347.         return etos;
  348.     }
  349.    
  350.     public int getMO()
  351.     {
  352.         return MO;
  353.     }
  354.    
  355.     public String toString()
  356.     {
  357.         String s = "\n Onoma: " + onoma;
  358.         s += "\n Epitheto: " + epitheto;
  359.         s += "\n Ar. Mitroou: " + AM;
  360.         s += "\n Etos Eggrafis: " + etos;
  361.         s += "\n Mesos Oros: " + MO;
  362.        
  363.         return s;
  364.     }
  365. }
  366.  
  367. class MyUtils
  368. {
  369.     public static int seqSearch(Foititis[] pinakas, int x)
  370.     {
  371.         for(int i = 0; i < pinakas.length; i++)
  372.         {
  373.             if(pinakas[i].getAM() == x)
  374.             {
  375.                 return i;
  376.             }
  377.         }
  378.        
  379.         return -1;
  380.     }
  381.    
  382.     public static int seqSearch(Foititis[] pinakas, String x)
  383.     {
  384.         for(int i = 0; i < pinakas.length; i++)
  385.         {
  386.             if(pinakas[i].getEpitheto().equals(x))
  387.             {
  388.                 return i;
  389.             }
  390.         }
  391.        
  392.         return -1;
  393.     }
  394.    
  395.     public static int binSearch(Foititis[] pinakas, int x)
  396.     {
  397.         int mid, pos = -1, left = 0, right = pinakas.length - 1;
  398.        
  399.         while(pos == -1 && left <= right)
  400.         {
  401.             mid = (left + right) / 2;
  402.            
  403.             if(x < pinakas[mid].getAM())
  404.             {
  405.                 right = mid - 1;
  406.             }
  407.             else
  408.             {
  409.                 if(x > pinakas[mid].getAM())
  410.                 {
  411.                     left = mid + 1;
  412.                 }
  413.                 else
  414.                 {
  415.                     pos = mid;
  416.                 }
  417.             }
  418.         }
  419.        
  420.         return pos;
  421.     }
  422.    
  423.     public static int binSearch(Foititis[] pinakas, String x)
  424.     {
  425.         int mid, pos = -1, left = 0, right = pinakas.length - 1;
  426.        
  427.         while(pos == -1 && left <= right)
  428.         {
  429.             mid = (left + right) / 2;
  430.            
  431.             if(pinakas[mid].getEpitheto().compareTo(x) > 0)
  432.             {
  433.                 right = mid - 1;
  434.             }
  435.             else
  436.             {
  437.                 if(pinakas[mid].getEpitheto().compareTo(x) < 0)
  438.                 {
  439.                     left = mid + 1;
  440.                 }
  441.                 else
  442.                 {
  443.                     pos = mid;
  444.                 }
  445.             }
  446.         }
  447.        
  448.         return pos;
  449.     }
  450. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement