Advertisement
razvanth21

Untitled

Jan 29th, 2020
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 KB | None | 0 0
  1. import java.util.List;
  2. import java.util.ArrayList;
  3.  
  4. abstract class Nod {
  5.     protected int id;
  6.     private List<Nod> noduri;
  7.  
  8.     public Nod(int id) {
  9.         this.id = id;
  10.         noduri = new ArrayList<>();
  11.     }
  12.  
  13.     public void adauga(Nod nod) {
  14.         if (!noduri.contains(nod)) {
  15.             noduri.add(nod);
  16.         }
  17.     }
  18.  
  19.     public abstract void propaga(int id, String msg, int incercari);
  20.  
  21.     public int getID() {
  22.         return this.id;
  23.     }
  24.  
  25.     public List<Nod> getNoduri() {
  26.         return this.noduri;
  27.     }
  28.  
  29.     @Override
  30.     public boolean equals(Object o) {
  31.         return (o instanceof Nod && ((Nod)o).getID() == this.id);
  32.     }
  33. }
  34.  
  35. class NodTerminal extends Nod {
  36.     private List<String> mesaje;
  37.  
  38.     public NodTerminal(int id) {
  39.         super(id);
  40.         mesaje = new ArrayList<>();
  41.     }
  42.  
  43.     public void propaga(int id, String msg, int incercari) {
  44.         if (id == this.id) {
  45.             int len = mesaje.size();
  46.  
  47.             if (len > 0 && !mesaje.get(len - 1).equals(msg)) {
  48.                 mesaje.add(msg);
  49.             } else if (len == 0) {
  50.                 mesaje.add(msg);
  51.             }
  52.         } else {
  53.             if (incercari > 0) {
  54.                 for (Nod nod : this.getNoduri()) {
  55.                     nod.propaga(id, msg, incercari - 1);
  56.                 }
  57.             }
  58.         }
  59.     }
  60.  
  61.     public String preiaMesaj(int i) {
  62.         if (i < 0 || mesaje.get(i) == null) {
  63.             return null;
  64.         }
  65.  
  66.         return mesaje.get(i);
  67.     }
  68. }
  69.  
  70. class Filtru extends Nod {
  71.     private List<String> cuvinte;
  72.  
  73.     public Filtru(int id) {
  74.         super(id);
  75.         cuvinte = new ArrayList<>();
  76.     }
  77.  
  78.     public void propaga(int id, String msg, int incercari) {
  79.         if (id == this.id) {
  80.             if (!cuvinte.contains(msg)) {
  81.                 cuvinte.add(msg);
  82.             }
  83.         } else {
  84.             if (cuvinte.indexOf(msg) == -1) {
  85.                 if (incercari > 0) {
  86.                     for (Nod nod : this.getNoduri()) {
  87.                         nod.propaga(id, msg, incercari - 1);
  88.                     }
  89.                 }
  90.             }
  91.         }
  92.     }
  93. }
  94.  
  95. class Statistica extends Nod {
  96.     private List<Integer> noduri;
  97.     private List<Integer> mesajePrimite;
  98.  
  99.     public Statistica(int id) {
  100.         super(id);
  101.         noduri = new ArrayList<>();
  102.         mesajePrimite = new ArrayList<>();
  103.     }
  104.  
  105.     public void propaga(int id, String msg, int incercari) {
  106.         if (id == this.id) {
  107.             noduri.clear();
  108.             mesajePrimite.clear();
  109.         } else {
  110.             noduri.add(id);
  111.             mesajePrimite.add(id, mesajePrimite.get(id) + 1);
  112.  
  113.             if (incercari > 0) {
  114.                 for (Nod nod : this.getNoduri()) {
  115.                     nod.propaga(id, msg, incercari - 1);
  116.                 }
  117.             }
  118.         }
  119.     }
  120.  
  121.     public int preiaStatistica(int id) {
  122.         return mesajePrimite.get(id);
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement