Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- public class Test {
- public static void main(String[] args) {
- Bufor b = new Bufor();
- b.add(new Value("test1", 1));
- b.add(new Value("test2", 2));
- b.add(new Value("test3", 3));
- System.out.print(b.toString());
- b.get();
- System.out.println("After using get");
- System.out.print(b.toString());
- if(b.isEmpty())
- System.out.println("List is empty");
- else
- System.out.println("List is not empty");
- if(b.contains(new Value("test3", 3)))
- System.out.println("List contains element with str = test3 and val = 3");
- else
- System.out.println("List doesn't contain element with str = test3 and val = 3");
- if(b.find("test5", 5) != null)
- System.out.println("List contains element with str = test5 and val = 5");
- else
- System.out.println("List doesn't contain element with str = test5 and val = 5");
- }
- }
- class Bufor{
- private ArrayList<Value> list;
- public Bufor(){
- list = new ArrayList<>();
- }
- @Override
- public String toString(){
- StringBuffer b = new StringBuffer();
- int i = 1;
- for(Value v : list) {
- b.append(i + ".\nstr = " + v.getStr() + "\nval = " + v.getVal() + "\n\n");
- i++;
- }
- return b.toString();
- }
- public void add(Value v){
- list.add(v);
- }
- public Value get(){
- Value v = list.get(0);
- list.remove(0);
- return v;
- }
- public boolean isEmpty(){
- return list.isEmpty();
- }
- public boolean contains(Value v){
- return list.contains(v);
- }
- public Value find(String str, int val){
- for(Value v : list){
- if(v.equals(new Value(str, val)))
- return v;
- }
- return null;
- }
- }
- class Value{
- private String str;
- private int val;
- public Value(String str, int val){
- this.str = str;
- this.val = val;
- }
- @Override
- public boolean equals(Object o){
- Value v = (Value)o;
- if(v.str.equals(str) && v.val == val)
- return true;
- return false;
- }
- public String getStr() {
- return str;
- }
- public void setStr(String str) {
- this.str = str;
- }
- public int getVal() {
- return val;
- }
- public void setVal(int val) {
- this.val = val;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement