Addresses

Report a typo

Our information system stores mailing addresses using the Address class. Users want to see addresses listed in alphabetical order. It means we need to sort the addresses and for this purpose we must be able to compare Address objects with each other. Make the Address class Comparable so we can do it! The full address format is defined in the toString method. Follow it when comparing addresses.

Write a program in Java 17
import java.util.*;

class Address {
private final String city;
private final String street;
private final String house;

public Address(String city, String street, String house) {
this.city = city;
this.street = street;
this.house = house;
}

@Override
public String toString() {
return "%s, %s, %s".formatted(house, street, city);
}
}

// do not change the code below
class Main {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Address> list = new ArrayList<>();

while (sc.hasNextLine()) {
String[] arguments = sc.nextLine().split(",");
list.add(new Address(arguments[0], arguments[1], arguments[2]));
}
Collections.sort(list);
Checker.check(list);
}
}
___

Create a free account to access the full topic