This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tvidushi; | |
import static java.util.stream.Collectors.groupingBy; | |
import static java.util.stream.Collectors.mapping; | |
import static java.util.stream.Collectors.toList; | |
import java.util.Arrays; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.function.Predicate; | |
import java.util.function.Supplier; | |
import java.util.stream.Collectors; | |
import com.tvidushi._ListExample.EatingPreference; | |
/* | |
* @author takshila Vidushi | |
*/ | |
public class _ListExample { | |
/* | |
* Problem : | |
* You are given list of Passengers create List based on destinations | |
* */ | |
public enum EatingPreference {V,NV}; | |
public static List<Passenger> PassengerList ; | |
static{ | |
PassengerList = Arrays.asList( | |
new Passenger("G1110349",true,"Hong Kong","Economy","Zu","Ciana","SFO","HKG",3,72.0,false,EatingPreference.NV,"A-12",2), | |
new Passenger("P212220",true,"China","Economy","Di","Cia","SFO","HKG",1,15,false,EatingPreference.NV,"B-17",10), | |
new Passenger("Q378909",true,"India","Business","Gupta", "Rakesh", "SFO","DEL",2,52,false,EatingPreference.NV,"B-11",7), | |
new Passenger("U190909",true,"India","Business","Sharma", "Aditi", "SFO","HKG",3,75,false,EatingPreference.V,"C-12",1), | |
new Passenger("H189189",true,"India","Economy","Sharma", "Sohan", "SFO","DEL",2,14,false,EatingPreference.V,"D-03",1), | |
new Passenger("R178909",true,"India","First Class","Chattergee","Priyanshu", "SFO","DEL",2,15,false,EatingPreference.V,"G-9",1)); | |
} | |
public static void healthCheck() { | |
Predicate<Passenger> healthCheck = p->p.isHealthIssue() ==true; | |
System.out.println(PassengerList.stream().anyMatch(healthCheck)); | |
} | |
public static void listByClass() { | |
} | |
public static void eligibleForUpgrade() { | |
} | |
public static void listByNationality() { | |
/* System.out.println( | |
PassengerList.stream() | |
.collect(groupingBy(Passenger::getNationality))); */ | |
System.out.println( | |
PassengerList.stream() | |
.collect(groupingBy(Passenger::getNationality, | |
mapping(Passenger::getFirstname, toList())))); | |
} | |
public static void listByEatingPreferences() { | |
System.out.println( | |
PassengerList.stream() | |
.collect(groupingBy(Passenger::getPref, | |
mapping(Passenger::getFirstname, toList())))); | |
} | |
public static void removeHeathIssues(){ | |
Predicate<Passenger> removeHealthIssue = p->p.isHealthIssue()==true; | |
PassengerList.removeIf(removeHealthIssue); | |
PassengerList.forEach(x-> System.out.print(" "+x.getFirstname())); | |
} | |
public static void listSeatNoByMeals(){ | |
Predicate<Passenger> seatListByMeals = (p-> p.getPref() ==EatingPreference.V) ; | |
Map<Boolean, List<Passenger>>seatList =PassengerList.stream().collect(Collectors.partitioningBy(seatListByMeals)); | |
seatList.forEach((k,v)->System.out.println("Veg Meals :"+k+" "+ | |
((List<Passenger>)v).stream().map(s->s.getFirstname()).collect(Collectors.joining(",")))); | |
} | |
public static void destionationList(String destination) { | |
Predicate<Passenger> des_hkg = p->p.getDestination() ==destination; | |
List<String> destination_hkg = PassengerList.stream() | |
.filter(des_hkg) | |
.map(Passenger::getFirstname) | |
.collect(Collectors.toList()); | |
destination_hkg.stream().forEach(x->System.out.println(x)); | |
} | |
public static void groupByNationality(String nationality) { | |
Predicate<Passenger> des_hkg = p->p.getNationality() =="India"; | |
List<String> destination_hkg = PassengerList.stream() | |
.filter(des_hkg) | |
.map(Passenger::getFirstname) | |
.collect(Collectors.toList()); | |
destination_hkg.stream().forEach(x->System.out.println(x)); | |
} | |
public static void totalBaggageCount() { | |
double baggageweight = PassengerList.stream().mapToInt(p->p.getBaggageCount()).count(); | |
System.out.println(" "+baggageweight); | |
} | |
public static void totalBaggageWeight() { | |
double baggageweight = PassengerList.stream().mapToDouble(p->p.getBaggageWeight()).sum(); | |
System.out.println(" "+baggageweight); | |
} | |
public static void main(String[] args) { | |
System.out.println("List of Passengers flying to HongKong"); | |
destionationList("HKG"); | |
System.out.println("List of Passengers flying to Delhi"); | |
destionationList("DEL"); | |
System.out.println("Number of bags aircraft is carrying"); | |
totalBaggageWeight(); | |
System.out.println("Total bags weight that aircraft is carrying "); | |
totalBaggageWeight(); | |
System.out.println("Any passenger present with health issue "); | |
healthCheck(); | |
System.out.println("List People By Nationalities"); | |
listByNationality(); | |
System.out.println("List People By Eating Preferences"); | |
listByEatingPreferences(); | |
System.out.println("List seat No By meals"); | |
listSeatNoByMeals(); | |
System.out.println("Remove passenger with health issue reported"); | |
removeHeathIssues(); | |
} | |
} | |
class Passenger { | |
private String passportNo; | |
private String lastname; | |
private boolean status; | |
public String getSeatNo() { | |
return seatNo; | |
} | |
public void setSeatNo(String seatNo) { | |
this.seatNo = seatNo; | |
} | |
private String _class; | |
private String nationality; | |
private String firstname; | |
private String startingpoint; | |
private String destination; | |
private int baggageCount; | |
private double baggageWeight; | |
private Boolean healthIssue; | |
private EatingPreference pref; | |
private String seatNo; | |
private int frequentFlyer; | |
public Passenger(String passportNo,boolean status,String nationality,String lastname,String _class, String firstname,String startingpoint,String destination,int baggageCount,double baggageWeight,boolean healthIssue,EatingPreference pref,String seatNo,int frequentFlyer) { | |
this.passportNo = this.passportNo; | |
this.status = status; | |
this.nationality = nationality; | |
this._class = _class; | |
this.lastname = lastname; | |
this.firstname = firstname; | |
this.startingpoint = startingpoint; | |
this.destination = destination; | |
this.baggageCount = baggageCount; | |
this.baggageWeight = baggageWeight; | |
this.healthIssue = healthIssue; | |
this.pref=pref; | |
this.seatNo=seatNo; | |
this.frequentFlyer = frequentFlyer; | |
} | |
public int getFrequentFlyer() { | |
return frequentFlyer; | |
} | |
public void setFrequentFlyer(int frequentFlyer) { | |
this.frequentFlyer = frequentFlyer; | |
} | |
public EatingPreference getPref() { | |
return pref; | |
} | |
public void setPref(EatingPreference pref) { | |
this.pref = pref; | |
} | |
public Boolean getHealthIssue() { | |
return healthIssue; | |
} | |
public String getNationality() { | |
return nationality; | |
} | |
public void setNationality(String nationality) { | |
this.nationality = nationality; | |
} | |
public Boolean isHealthIssue() { | |
return healthIssue; | |
} | |
public void setHealthIssue(Boolean healthIssue) { | |
this.healthIssue = healthIssue; | |
} | |
public String getPassportNo() { | |
return passportNo; | |
} | |
public double getBaggageWeight() { | |
return baggageWeight; | |
} | |
public void setBaggageWeight(double baggageWeight) { | |
this.baggageWeight = baggageWeight; | |
} | |
public int getBaggageCount() { | |
return baggageCount; | |
} | |
public void setBaggageCount(int baggageCount) { | |
this.baggageCount = baggageCount; | |
} | |
public void setPassportNo(String passportNo) { | |
this.passportNo = passportNo; | |
} | |
public String getLastname() { | |
return lastname; | |
} | |
public void setLastname(String lastname) { | |
this.lastname = lastname; | |
} | |
public String getFirstname() { | |
return firstname; | |
} | |
public void setFirstname(String firstname) { | |
this.firstname = firstname; | |
} | |
public String getStartingpoint() { | |
return startingpoint; | |
} | |
public void setStartingpoint(String startingpoint) { | |
this.startingpoint = startingpoint; | |
} | |
public String getDestination() { | |
return destination; | |
} | |
public void setDestination(String destination) { | |
this.destination = destination; | |
} | |
public boolean isStatus() { | |
return status; | |
} | |
public void setStatus(boolean status) { | |
this.status = status; | |
} | |
public String get_class() { | |
return _class; | |
} | |
public void set_class(String _class) { | |
this._class = _class; | |
} | |
} |
No comments:
Post a Comment