Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.List;
- import java.util.Scanner;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.stream.Collectors;
- public class Container<T extends Weightable> {
- List<T> elements;
- public Container() {
- this.elements= new ArrayList<>();
- }
- public double getTotalWeight(){
- return elements.stream().mapToDouble(i->i.getWeight()).sum();
- }
- public void addElement(T elem){
- elements.add(elem);
- }
- public List<T> lighterThan(T elem){
- return elements.stream().filter(i->i.getWeight()<elem.getWeight())
- .collect(Collectors.toList());
- }
- public List<T> between(T a, T b){
- return elements.stream().filter(i->i.getWeight()>a.getWeight()&&i.getWeight()<b.getWeight())
- .collect(Collectors.toList());
- }
- public boolean compare(Container<? extends Weightable> container){
- return this.getTotalWeight()>container.getTotalWeight();
- }
- }
- public interface Weightable<T> extends Comparable<Weightable<T>> {
- double getWeight();
- }
- public class WeightableString implements Weightable<WeightableString> {
- private String word;
- public WeightableString(String word) {
- this.word = word;
- }
- @Override
- public double getWeight(){
- return word.length();
- }
- @Override
- public int compareTo(Weightable o) {
- return (int)Double.compare(this.getWeight(),o.getWeight());
- }
- }
- public class WeightableDouble implements Weightable<WeightableDouble>{
- private double weight;
- public WeightableDouble(double weight) {
- this.weight = weight;
- }
- @Override
- public int compareTo(Weightable o) {
- return (int)Double.compare(this.getWeight(),o.getWeight());
- }
- @Override
- public double getWeight(){
- return weight;
- }
- }
- public class ContainerTest {
- public static void main(String[] args) {
- Container<WeightableDouble> container1 = new Container<>();
- Container<WeightableDouble> container2 = new Container<>();
- Container<WeightableString> container3 = new Container<>();
- Scanner sc = new Scanner(System.in);
- int n = sc.nextInt();
- int m = sc.nextInt();
- int p = sc.nextInt();
- double a = sc.nextDouble();
- double b = sc.nextDouble();
- WeightableDouble wa = new WeightableDouble(a);
- WeightableDouble wb = new WeightableDouble(b);
- for(int i=0;i<n;i++){
- double weight = sc.nextDouble();
- container1.addElement(new WeightableDouble(weight));
- }
- for(int i=0;i<m;i++){
- double weight = sc.nextDouble();
- container2.addElement(new WeightableDouble(weight));
- }
- for(int i=0;i<p;i++){
- String s = sc.next();
- container3.addElement(new WeightableString(s));
- }
- List<WeightableDouble> resultSmaller = container1.lighterThan(wa);
- List<WeightableDouble> resultBetween = container1.between(wa,wb);
- System.out.println("Lighter than "+wa.getWeight()+"!");
- for(WeightableDouble wd : resultSmaller){
- System.out.println(wd.getWeight());
- }
- System.out.println("Between "+wa.getWeight()+" and "+wb.getWeight()+"!");
- for(WeightableDouble wd : resultBetween){
- System.out.println(wd.getWeight());
- }
- System.out.println("Comparison: ");
- System.out.println(container1.compare(container2));
- System.out.println(container1.compare(container3));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement