Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package ppa2_cv04_4_a14b0537p;
- import java.util.Scanner;
- /**
- *
- * @author Milan
- */
- public class Ppa2_cv04_4_A14B0537P
- {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args)
- {
- Scanner sc = new Scanner(System.in);
- System.out.println("Zadejte pocet nenazrancu");
- int pocetLidu = sc.nextInt();
- System.out.println("Zadejte mnozstvi jidla");
- int pocetZradla = sc.nextInt();
- Fronta fronta = new Fronta();
- sc.nextLine();
- for(int i=0; i<pocetLidu;i++)
- {
- System.out.println("zadejte jmeno");
- String jmeno = sc.next();
- System.out.println("zadejte jeho hlad");
- int hlad = sc.nextInt();
- Stravnik stravnik = new Stravnik(jmeno, hlad);
- fronta.push(stravnik);
- }
- while(!fronta.isEmpty())
- {
- Stravnik pepa = fronta.pop();
- pepa.snizHlad();
- pocetZradla--;
- if(pepa.getHlad()==0)
- {
- System.out.println(pepa.getJmeno());
- continue;
- }
- fronta.push(pepa);
- }
- if(fronta.isEmpty())
- {
- System.out.println("Zbylo " + pocetZradla + " chutovek");
- System.exit(0);
- }
- System.out.println("Hladovi");
- while(true)
- {
- Stravnik pepa = fronta.pop();
- System.out.println(""+ pepa.getJmeno() + " ma hlad na " + pepa.getHlad() + " chutovek");
- if(fronta.isEmpty())
- {
- break;
- }
- }
- }
- }
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package ppa2_cv04_4_a14b0537p;
- /**
- *
- * @author Milan
- */
- public class Stravnik
- {
- String jmeno;
- int hlad;
- // Stravnik predchozi;
- public Stravnik dalsi;
- public Stravnik(String meno, int hlad)
- {
- this.jmeno=meno;
- this.hlad = hlad;
- }
- public String getJmeno()
- {
- return this.jmeno;
- }
- public int getHlad()
- {
- return this.hlad;
- }
- public void setHlad(int hlad)
- {
- this.hlad=hlad;
- }
- public void snizHlad()
- {
- this.hlad -=1;
- }
- }
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package ppa2_cv04_4_a14b0537p;
- /**
- *
- * @author Milan
- */
- public class Fronta
- {
- Stravnik frontaStravniku = null;
- public void push(Stravnik stravnik)
- {
- if(isEmpty())
- {
- frontaStravniku=stravnik;
- return;
- }
- Stravnik temp = frontaStravniku;
- while(temp!= null)
- {
- temp=temp.dalsi;
- if(temp==null)
- {
- temp = stravnik;
- return;
- }
- }
- }
- public Stravnik pop()
- {
- Stravnik temp = frontaStravniku;
- if(frontaStravniku !=null)
- {
- frontaStravniku = frontaStravniku.dalsi;
- }
- return temp ;
- }
- public boolean isEmpty()
- {
- return(frontaStravniku==null);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement