DulcetAirman

sort by type (bad code)

Oct 10th, 2015
655
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.60 KB | None | 0 0
  1. package com.example.foo;
  2.  
  3. import java.time.Month;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.Random;
  7.  
  8. public class SomeClass {
  9.   public static void main(final String[] args) {
  10.     final FooContainer foos = new FooContainer();
  11.     final Random rng = new Random();
  12.     // This just adds random elements:
  13.     for (int i = 0; i < 100; i++)
  14.       foos.add(new Foo(Month.of(rng.nextInt(12) + 1).name(), rng.nextInt(3) + 1));
  15.  
  16.     final List<Foo> elements = foos.blub();
  17.     for (final Foo foo : elements)
  18.       System.out.println(foo.getName());
  19.   }
  20.  
  21.   public static class FooContainer {
  22.     private final List<Foo> list = new ArrayList<>();
  23.  
  24.     public void add(final Foo foo) {
  25.       this.list.add(foo);
  26.     }
  27.  
  28.     public List<Foo> blub() {
  29.       final List<Foo> result = new ArrayList<>();
  30.       for (int i = Foo.TYPE_A; i <= Foo.TYPE_C; i++)
  31.         for (final Foo f : this.list)
  32.           if (i == f.getType())
  33.             result.add(f);
  34.       return result;
  35.     }
  36.   }
  37.  
  38.   public static class Foo {
  39.     public static final int TYPE_A = 1;
  40.     public static final int TYPE_B = 2;
  41.     public static final int TYPE_C = 3;
  42.  
  43.     private int type;
  44.     private String name;
  45.  
  46.     public Foo(final String name, final int type) {
  47.       this.name = name;
  48.       this.type = type;
  49.     }
  50.  
  51.     public String getName() {
  52.       return this.name;
  53.     }
  54.  
  55.     public void setName(final String name) {
  56.       this.name = name;
  57.     }
  58.  
  59.     public int getType() {
  60.       return this.type;
  61.     }
  62.  
  63.     public void setType(final int type) {
  64.       this.type = type;
  65.     }
  66.   }
  67. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment