Filip_Markoski

[NP] Генерички Бројач

Nov 17th, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.77 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. public class GenericCounterTest {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         int n = scanner.nextInt();
  9.         GenericCounter<Integer> counterInt = new GenericCounter<Integer>();
  10.         scanner.nextLine();
  11.         for (int i = 0; i < n; ++i) {
  12.             int x = scanner.nextInt();
  13.             counterInt.count(x);
  14.         }
  15.         System.out.println("=====INTEGERS=====");
  16.         System.out.println(counterInt);
  17.         n = scanner.nextInt();
  18.         scanner.nextLine();
  19.         GenericCounter<String> counterString = new GenericCounter<String>();
  20.         for (int i = 0; i < n; i++) {
  21.             String s = scanner.nextLine();
  22.             counterString.count(s);
  23.         }
  24.         System.out.println("=====STRINGS=====");
  25.         System.out.println(counterString);
  26.         n = scanner.nextInt();
  27.         scanner.nextLine();
  28.         GenericCounter<Float> counterFloat = new GenericCounter<Float>();
  29.         for (int i = 0; i < n; i++) {
  30.             float f = scanner.nextFloat();
  31.             counterFloat.count(f);
  32.         }
  33.         System.out.println("=====FLOATS=====");
  34.         System.out.println(counterFloat);
  35.         scanner.close();
  36.     }
  37.  
  38.  
  39. }
  40.  
  41. class CountableElement<T> {
  42.     private T element;
  43.     private int count;
  44.     private StringBuffer sb;
  45.  
  46.     public CountableElement(T element, int count) {
  47.         this.element = element;
  48.         this.count = count;
  49.         this.sb = new StringBuffer("*");
  50.     }
  51.  
  52.     public void increment(){
  53.         this.count++;
  54.         sb.append("*");
  55.     }
  56.  
  57.     public String countAsStars(){
  58.         return sb.toString();
  59.     }
  60.  
  61.     public T getElement() {
  62.         return element;
  63.     }
  64.  
  65.     public void setElement(T element) {
  66.         this.element = element;
  67.     }
  68.  
  69.  
  70. }
  71.  
  72. interface Counter<T> {
  73.     void count(T element);
  74. }
  75.  
  76. class GenericCounter<T> implements Counter<T> {
  77.  
  78.     List<CountableElement<T>> elements;
  79.  
  80.     public GenericCounter() {
  81.         this.elements = new ArrayList<>();
  82.     }
  83.  
  84.     @Override
  85.     public void count(T element) {
  86.         for (CountableElement countableElement : elements){
  87.             if (countableElement.getElement().equals(element)){
  88.                 countableElement.increment();
  89.                 return;
  90.             }
  91.         }
  92.         elements.add(new CountableElement<>(element, 1));
  93.     }
  94.  
  95.     @Override
  96.     public String toString() {
  97.         StringBuffer sb = new StringBuffer();
  98.         for (CountableElement countableElement : elements){
  99.             sb.append(String.format("%s|%s\n", countableElement.getElement().toString(), countableElement.countAsStars()));
  100.         }
  101.         return sb.toString();
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment