Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. public class Test {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         Bufor b = new Bufor();
  8.         b.add(new Value("test1", 1));
  9.         b.add(new Value("test2", 2));
  10.         b.add(new Value("test3", 3));
  11.         System.out.print(b.toString());
  12.         b.get();
  13.         System.out.println("After using get");
  14.         System.out.print(b.toString());
  15.  
  16.         if(b.isEmpty())
  17.             System.out.println("List is empty");
  18.         else
  19.             System.out.println("List is not empty");
  20.  
  21.         if(b.contains(new Value("test3", 3)))
  22.             System.out.println("List contains element with str = test3 and val = 3");
  23.         else
  24.             System.out.println("List doesn't contain element with str = test3 and val = 3");
  25.  
  26.         if(b.find("test5", 5) != null)
  27.             System.out.println("List contains element with str = test5 and val = 5");
  28.         else
  29.             System.out.println("List doesn't contain element with str = test5 and val = 5");
  30.  
  31.     }
  32.  
  33. }
  34.  
  35. class Bufor{
  36.  
  37.     private ArrayList<Value> list;
  38.  
  39.     public Bufor(){
  40.         list = new ArrayList<>();
  41.     }
  42.  
  43.     @Override
  44.     public String toString(){
  45.  
  46.         StringBuffer b = new StringBuffer();
  47.  
  48.         int i = 1;
  49.         for(Value v : list) {
  50.             b.append(i + ".\nstr = " + v.getStr() + "\nval = " + v.getVal() + "\n\n");
  51.             i++;
  52.         }
  53.  
  54.         return b.toString();
  55.     }
  56.  
  57.     public void add(Value v){
  58.         list.add(v);
  59.     }
  60.  
  61.     public Value get(){
  62.         Value v = list.get(0);
  63.         list.remove(0);
  64.         return v;
  65.     }
  66.  
  67.     public boolean isEmpty(){
  68.         return list.isEmpty();
  69.     }
  70.  
  71.     public boolean contains(Value v){
  72.         return list.contains(v);
  73.     }
  74.  
  75.     public Value find(String str, int val){
  76.         for(Value v : list){
  77.             if(v.equals(new Value(str, val)))
  78.                 return v;
  79.         }
  80.  
  81.         return null;
  82.     }
  83.  
  84. }
  85.  
  86. class Value{
  87.  
  88.     private String str;
  89.     private int val;
  90.  
  91.     public Value(String str, int val){
  92.         this.str = str;
  93.         this.val = val;
  94.     }
  95.  
  96.     @Override
  97.     public boolean equals(Object o){
  98.         Value v = (Value)o;
  99.  
  100.         if(v.str.equals(str) && v.val == val)
  101.             return true;
  102.  
  103.         return false;
  104.     }
  105.  
  106.     public String getStr() {
  107.         return str;
  108.     }
  109.  
  110.     public void setStr(String str) {
  111.         this.str = str;
  112.     }
  113.  
  114.     public int getVal() {
  115.         return val;
  116.     }
  117.  
  118.     public void setVal(int val) {
  119.         this.val = val;
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement