Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package ppa2_cv04_4_a14b0537p;
  7.  
  8. import java.util.Scanner;
  9.  
  10. /**
  11. *
  12. * @author Milan
  13. */
  14. public class Ppa2_cv04_4_A14B0537P
  15. {
  16.  
  17. /**
  18. * @param args the command line arguments
  19. */
  20. public static void main(String[] args)
  21. {
  22. Scanner sc = new Scanner(System.in);
  23. System.out.println("Zadejte pocet nenazrancu");
  24. int pocetLidu = sc.nextInt();
  25. System.out.println("Zadejte mnozstvi jidla");
  26. int pocetZradla = sc.nextInt();
  27. Fronta fronta = new Fronta();
  28. sc.nextLine();
  29. for(int i=0; i<pocetLidu;i++)
  30. {
  31.  
  32. System.out.println("zadejte jmeno");
  33. String jmeno = sc.next();
  34. System.out.println("zadejte jeho hlad");
  35. int hlad = sc.nextInt();
  36. Stravnik stravnik = new Stravnik(jmeno, hlad);
  37. fronta.push(stravnik);
  38.  
  39. }
  40.  
  41. while(!fronta.isEmpty())
  42. {
  43. Stravnik pepa = fronta.pop();
  44.  
  45. pepa.snizHlad();
  46. pocetZradla--;
  47. if(pepa.getHlad()==0)
  48. {
  49. System.out.println(pepa.getJmeno());
  50. continue;
  51. }
  52. fronta.push(pepa);
  53.  
  54. }
  55. if(fronta.isEmpty())
  56. {
  57. System.out.println("Zbylo " + pocetZradla + " chutovek");
  58. System.exit(0);
  59. }
  60. System.out.println("Hladovi");
  61. while(true)
  62. {
  63.  
  64. Stravnik pepa = fronta.pop();
  65. System.out.println(""+ pepa.getJmeno() + " ma hlad na " + pepa.getHlad() + " chutovek");
  66. if(fronta.isEmpty())
  67. {
  68. break;
  69. }
  70. }
  71.  
  72.  
  73.  
  74.  
  75. }
  76.  
  77. }
  78.  
  79.  
  80. /*
  81. * To change this license header, choose License Headers in Project Properties.
  82. * To change this template file, choose Tools | Templates
  83. * and open the template in the editor.
  84. */
  85. package ppa2_cv04_4_a14b0537p;
  86.  
  87. /**
  88. *
  89. * @author Milan
  90. */
  91. public class Stravnik
  92. {
  93. String jmeno;
  94. int hlad;
  95. // Stravnik predchozi;
  96. public Stravnik dalsi;
  97.  
  98. public Stravnik(String meno, int hlad)
  99. {
  100. this.jmeno=meno;
  101. this.hlad = hlad;
  102. }
  103. public String getJmeno()
  104. {
  105. return this.jmeno;
  106. }
  107. public int getHlad()
  108. {
  109. return this.hlad;
  110. }
  111. public void setHlad(int hlad)
  112. {
  113. this.hlad=hlad;
  114. }
  115. public void snizHlad()
  116. {
  117. this.hlad -=1;
  118. }
  119. }
  120.  
  121.  
  122. /*
  123. * To change this license header, choose License Headers in Project Properties.
  124. * To change this template file, choose Tools | Templates
  125. * and open the template in the editor.
  126. */
  127. package ppa2_cv04_4_a14b0537p;
  128.  
  129. /**
  130. *
  131. * @author Milan
  132. */
  133. public class Fronta
  134. {
  135. Stravnik frontaStravniku = null;
  136.  
  137. public void push(Stravnik stravnik)
  138. {
  139. if(isEmpty())
  140. {
  141. frontaStravniku=stravnik;
  142. return;
  143. }
  144. Stravnik temp = frontaStravniku;
  145. while(temp!= null)
  146. {
  147. temp=temp.dalsi;
  148. if(temp==null)
  149. {
  150. temp = stravnik;
  151. return;
  152. }
  153. }
  154.  
  155. }
  156. public Stravnik pop()
  157. {
  158. Stravnik temp = frontaStravniku;
  159. if(frontaStravniku !=null)
  160. {
  161. frontaStravniku = frontaStravniku.dalsi;
  162.  
  163. }
  164.  
  165. return temp ;
  166. }
  167. public boolean isEmpty()
  168. {
  169. return(frontaStravniku==null);
  170.  
  171. }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement