ADDRESS.JAVA:
public class Address implements Comparable<Address>{ //fill your code here private String username; private String addressLine1; private String addressLine2; private int pinCode; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getAddressLine1() { return addressLine1; } public void setAddressLine1(String addressLine1) { this.addressLine1 = addressLine1; } public String getAddressLine2() { return addressLine2; } public void setAddressLine2(String addressLine2) { this.addressLine2 = addressLine2; } public int getPinCode() { return pinCode; } public void setPinCode(int pinCode) { this.pinCode = pinCode; } public Address() { super(); // TODO Auto-generated constructor stub } public Address(String username, String addressLine1, String addressLine2, int pinCode) { super(); this.username = username; this.addressLine1 = addressLine1; this.addressLine2 = addressLine2; this.pinCode = pinCode; } public int compareTo(Address address) { if (pinCode == address.pinCode) { return (addressLine1.compareTo(address.addressLine1)); } else if (pinCode > address.pinCode) { return 1; } else { return -1; } } }
MAIN.JAVA:
import java.io.*; import java.util.*; public class Main { public static void main(String[] args){ //fill code here Scanner scan= new Scanner(System.in); System.out.println("Enter the number of users:"); int num=scan.nextInt(); scan.nextLine(); System.out.println("Enter user address in CSV(Username,AddressLine 1,AddressLine 2,PinCode)"); ArrayList<Address>address=new ArrayList<Address>(); for(int i=0;i<num;i++) { String info[]=scan.nextLine().split(","); address.add(new Address(info[0],info[1],info[2],Integer.parseInt(info[3]))); } Collections.sort(address); System.out.println("User Details:"); for(Address addr:address) { System.out.println(addr.getUsername()+","+addr.getAddressLine1()+","+addr.getAddressLine2()+","+addr.getPinCode()); } } }