Question: I created the following programs for and from the Arraylist i want to extract the year 2011 and the numer of customers and print them,

I created the following programs for and from the Arraylist i want to extract the year 2011 and the numer of customers and print them, how would i extract the years and number of customers from the ArrayList?

Info.java

import java.util.ArrayList; import java.util.Collection; import java.util.Collections; class Info { public static void main(String[] args){ ArrayList agents = new ArrayList<>(); agents.add(new Agent("Peter", "Rome")); agents.add(new Agent("Ann", "Paris")); agents.add(new Agent("Mary", "Rome")); agents.add(new Agent("Paul", "Rome")); ArrayList cust = new ArrayList<>(); cust.add(new Customer("Paul", 2011, 100)); cust.add(new Customer("Peter", 2012, 150)); cust.add(new Customer("Peter", 2011, 52)); cust.add(new Customer("Ann", 2012, 85)); cust.add(new Customer("Ann", 2012, 75)); cust.add(new Customer("Mary", 2012, 950)); Collections.sort(cust); Collections.sort(agents); //Prints out the customers in sorted form by year. System.out.println("The customers(sorted): "); for(Customer cus : cust){ System.out.println(cus.agent + " " + cus.year + " " + cus.noOfCustomers + " "); } //prints out the agents System.out.println(); System.out.println("The agents: "); for(Agent age : agents){ System.out.println(age.name + " " + age.location + " "); } } } 

Agent.java

class Agent implements Comparable{ public String name; public String location; public Agent(String n, String l){ this.name = n; this.location = l; } //Setters and Getters public String getName(){ return name; } public String getLocation(){ return location; } public void setName(String n){ this.name = n; } public void setLocation(String l){ this.location = l; } public int compareTo(Agent o){ if(this.name.equals(o.name)){ return 0; } else{ return 1; } } }

Customer.java

class Customer implements Comparable { public String agent; public int year; public int noOfCustomers; public Customer(String a, int y, int noc){ this.agent = a; this.year = y; this.noOfCustomers = noc; } //Setters and getters public String getAgent(String a){ return agent; } public int getYear(int y){ return year; } public int getNoOfCustomers(int noc){ return noOfCustomers; } public void setAgent(String a){ this.agent = a; } public void setYear(int y){ this.year = y; } public void setNoOfCustomers(int noc){ this.noOfCustomers = noc; } //Sorts the cust ArrayList by year. public int compareTo(Customer o){ int compareYear = ((Customer)o).year; return this.year - compareYear; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!