Advertisement
dzocesrce

[NP] Container

Apr 16th, 2025
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.60 KB | None | 0 0
  1. import java.util.List;
  2. import java.util.Scanner;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.stream.Collectors;
  6.  
  7. public class Container<T extends Weightable> {
  8.  
  9.     List<T> elements;
  10.  
  11.     public Container() {
  12.         this.elements= new ArrayList<>();
  13.     }
  14.  
  15.     public double getTotalWeight(){
  16.         return elements.stream().mapToDouble(i->i.getWeight()).sum();
  17.     }
  18.  
  19.     public void addElement(T elem){
  20.         elements.add(elem);
  21.     }
  22.  
  23.     public List<T> lighterThan(T elem){
  24.         return elements.stream().filter(i->i.getWeight()<elem.getWeight())
  25.                 .collect(Collectors.toList());
  26.     }
  27.  
  28.     public List<T> between(T a, T b){
  29.         return elements.stream().filter(i->i.getWeight()>a.getWeight()&&i.getWeight()<b.getWeight())
  30.                 .collect(Collectors.toList());
  31.     }
  32.  
  33.     public boolean compare(Container<? extends Weightable> container){
  34.         return this.getTotalWeight()>container.getTotalWeight();
  35.     }
  36.  
  37.  
  38. }
  39. public interface Weightable<T> extends Comparable<Weightable<T>> {
  40.  
  41.     double getWeight();
  42. }
  43. public class WeightableString implements Weightable<WeightableString> {
  44.  
  45.     private String word;
  46.  
  47.     public WeightableString(String word) {
  48.         this.word = word;
  49.     }
  50.  
  51.  
  52.     @Override
  53.     public double getWeight(){
  54.         return word.length();
  55.     }
  56.  
  57.  
  58.     @Override
  59.     public int compareTo(Weightable o) {
  60.         return (int)Double.compare(this.getWeight(),o.getWeight());
  61.     }
  62. }
  63. public class WeightableDouble implements Weightable<WeightableDouble>{
  64.  
  65.     private double weight;
  66.  
  67.     public WeightableDouble(double weight) {
  68.         this.weight = weight;
  69.     }
  70.  
  71.     @Override
  72.     public int compareTo(Weightable o) {
  73.         return (int)Double.compare(this.getWeight(),o.getWeight());
  74.     }
  75.  
  76.     @Override
  77.     public double getWeight(){
  78.         return weight;
  79.     }
  80.  
  81. }
  82.  
  83. public class ContainerTest {
  84.     public static void main(String[] args) {
  85.  
  86.         Container<WeightableDouble> container1 = new Container<>();
  87.         Container<WeightableDouble> container2 = new Container<>();
  88.         Container<WeightableString> container3 = new Container<>();
  89.         Scanner sc = new Scanner(System.in);
  90.         int n = sc.nextInt();
  91.         int m = sc.nextInt();
  92.         int p = sc.nextInt();
  93.         double a = sc.nextDouble();
  94.         double b = sc.nextDouble();
  95.         WeightableDouble wa = new WeightableDouble(a);
  96.         WeightableDouble wb = new WeightableDouble(b);
  97.  
  98.         for(int i=0;i<n;i++){
  99.             double weight = sc.nextDouble();
  100.             container1.addElement(new WeightableDouble(weight));
  101.         }
  102.         for(int i=0;i<m;i++){
  103.             double weight = sc.nextDouble();
  104.             container2.addElement(new WeightableDouble(weight));
  105.         }
  106.         for(int i=0;i<p;i++){
  107.             String s = sc.next();
  108.             container3.addElement(new WeightableString(s));
  109.         }
  110.         List<WeightableDouble> resultSmaller = container1.lighterThan(wa);
  111.         List<WeightableDouble> resultBetween = container1.between(wa,wb);
  112.         System.out.println("Lighter than "+wa.getWeight()+"!");
  113.         for(WeightableDouble wd : resultSmaller){
  114.             System.out.println(wd.getWeight());
  115.         }
  116.         System.out.println("Between "+wa.getWeight()+" and "+wb.getWeight()+"!");
  117.         for(WeightableDouble wd : resultBetween){
  118.             System.out.println(wd.getWeight());
  119.         }
  120.         System.out.println("Comparison: ");
  121.         System.out.println(container1.compare(container2));
  122.         System.out.println(container1.compare(container3));
  123.  
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement