Monday, November 3, 2014

Java print pro



Print names of male members

import java.time.LocalDate;
import java.time.chrono.IsoChronology;
import java.util.ArrayList;
import java.util.List;
import java.util.function.IntConsumer;
public class Main 
{
 public static void main(String[] args) 
{
      List roster = createRoster();
                System.out.println("Male members of the collection (bulk data operations):");
      roster
          .stream()
          .filter(e -> e.getGender() == Person.Sex.Male)
          .forEach(e -> System.out.println(e.getName()));
                }
              public static List createRoster() 
              {
        
        List roster = new ArrayList<>();
        roster.add(
            new Person(
            "Fred",
            IsoChronology.Instance.date(1980, 6, 20),
            Person.Sex.Male,"fredexample"));
        roster.add(
            new Person(
            "Jane",
            IsoChronology.Instance.date(1990, 7, 15),
            Person.Sex.Female, "janeexample"));
        roster.add(
            new Person(
            "George",
            IsoChronology.Instance.date(1991, 8, 13),
            Person.Sex.Male, "georgeexample"));
        roster.add(
            new Person(
            "Bob",
            IsoChronology.Instance.date(2000, 9, 12),
            Person.Sex.Male, "bobexample"));
        return roster;
    }
}
class Averager implements IntConsumer
{
    private int total = 0;
    private int count = 0;
  public double average() 
  {
        return count > 0 ? ((double) total)/count : 0;
    }
   public void accept(int i) { total += i; count++; }
   public void combine(Averager other) 
   {
        total += other.total;
        count += other.count;
    }
}
class Person 
{
 public enum Sex 
 {
      Male, Female
  }
            String name; 
  LocalDate birthday;
  Sex gender;
  String emailAddress;
            Person(String nameArg, LocalDate birthdayArg,Sex genderArg, String emailArg) 
               {
      name = nameArg;
      birthday = birthdayArg;
      gender = genderArg;
      emailAddress = emailArg;
     }  
      public int getAge() 
     {
      return birthday
          .until(IsoChronology.Instance.dateNow())
          .getYears();
  }
             public void printPerson()  
            {
    System.out.println(name + ", " + this.getAge());
  }
  public Sex getGender() 
 {
      return gender;
  }
  public String getName() 
 {
      return name;
  }
  public String getEmailAddress() 
 {
      return emailAddress;
  }
  public LocalDate getBirthday() 
 {
      return birthday;
  }
  public static int compareByAge(Person a, Person b) 
 {
      return a.birthday.compareTo(b.birthday);
  }
          }

Update all graduates from Milan to Cambridge

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class Main 
{
 public static void main(String...args)
{
    Student raoul = new Student("Raoul", "Cambridge");
    Student mario = new Student("Mario","Milan");
    Student alan = new Student("Alan","Cambridge");
    Student brian = new Student("Brian","Cambridge");
    List transactions = Arrays.asList(
        new Graduate(brian, 2011, 300), 
        new Graduate(raoul, 2012, 1000),
        new Graduate(raoul, 2011, 400),
        new Graduate(mario, 2012, 710),  
        new Graduate(mario, 2012, 700),
        new Graduate(alan, 2012, 950)
    );  
    transactions.stream()
                .map(Graduate::getTrader)
                .filter(trader -> trader.getCity().equals("Milan"))
                .forEach(trader -> trader.setCity("Cambridge"));
    System.out.println(transactions);
    }
}
class Student
{
  private String name;
  private String city;
           public Student(String n, String c)
           {
      this.name = n;
      this.city = c;
  }
 
  public String getName()
  {
      return this.name;
   }
              public String getCity()
              {
      return this.city;
    }
               public void setCity(String newCity)
               {
      this.city = newCity;
     }
               public String toString()
              {
      return "Student:"+this.name + " in " + this.city;
  }
}
          class Graduate
           {
            private Student trader;
  private int year;
  private int value;
           public Graduate(Student trader, int year, int value)
  {
      this.trader = trader;
      this.year = year;
      this.value = value;
  }
            public Student getTrader()
            { 
      return this.trader;
  }
             public int getYear()
             {
      return this.year;
  }
             public int getValue()
             {
      return this.value;
   }
  
  public String toString()
  {
      return "{" + this.trader + ", " +
             "year: "+this.year+", " +
             "value:" + this.value +"}";
  }
}

No comments:

Post a Comment