import java.util.ArrayList; import java.util.List; import java.util.Scanner; class Person { private String name; private int age; private double income; public Person(String name, int age, double income) { this.name = name; this.age = age; this.income = income; } public String getName() { return name; } public int getAge() { return age; } public double getIncome() { return income; } } public class PersonExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); List<Person> setA = new ArrayList<>(); System.out.print("Enter the number of persons: "); int n = scanner.nextInt(); scanner.nextLine(); // consume newline character for (int i = 0; i < n; i++) { System.out.println("Enter details for person " + (i + 1) + ":"); System.out.print("Name: "); String name = scanner.nextLine(); System.out.print("Age: "); int age = scanner.nextInt(); System.out.print("Income: "); double income = scanner.nextDouble(); scanner.nextLine(); // consume newline character Person person = new Person(name, age, income); setA.add(person); } List<Person> setB = new ArrayList<>(); List<Person> setC = new ArrayList<>(); List<Person> setBC = new ArrayList<>(); for (Person person : setA) { if (person.getAge() > 60) { setB.add(person); } if (person.getIncome() < 10000) { setC.add(person); } if (person.getAge() > 60 && person.getIncome() < 10000) { setBC.add(person); } } System.out.println("Set A: "); for (Person person : setA) { System.out.println("Name: " + person.getName() + ", Age: " + person.getAge() + ", Income: " + person.getIncome()); } System.out.println("\nSet B (age > 60): "); for (Person person : setB) { System.out.println("Name: " + person.getName() + ", Age: " + person.getAge() + ", Income: " + person.getIncome()); } System.out.println("\nSet C (income < 10000): "); for (Person person : setC) { System.out.println("Name: " + person.getName() + ", Age: " + person.getAge() + ", Income: " + person.getIncome()); } System.out.println("\nSet B and C (age > 60 and income < 10000): "); for (Person person : setBC) { System.out.println("Name: " + person.getName() + ", Age: " + person.getAge() + ", Income: " + person.getIncome()); } } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter