Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.List;
- import java.util.ArrayList;
- import java.lang.Comparable;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.IOException;
- import java.io.PrintStream;
- import java.io.FileReader;
- import java.util.Collections;
- import java.util.Comparator;
- class Htyo {
- public static void main(String[] args) throws IOException {
- if(args.length != 1) {
- System.exit(1);
- }
- String filename = args[0];
- CityReader reader = new CityReader(filename);
- reader.read();
- CityPrinter printer = new CityPrinter(reader.getCities());
- printer.printByName(System.out);
- printer.printByPop(System.out);
- printer.print(System.out);
- }
- }
- class City {
- private String name;
- private int pop;
- private int cityLength;
- private int popLength;
- public void setName(String name) {
- this.name = name;
- }
- public City withName(String name) {
- setName(name);
- return this;
- }
- public String getName() {
- return name;
- }
- public void setPop(int pop) {
- this.pop = pop;
- }
- public City withPop(int pop) {
- setPop(pop);
- return this;
- }
- public int getPop() {
- return pop;
- }
- public void setPopLength(int popLength) {
- this.popLength = popLength;
- }
- public City withPopLength(int popLength) {
- setPopLength(popLength);
- return this;
- }
- public int getPopLength() {
- return popLength;
- }
- public void setCityLength(int cityLength) {
- this.cityLength = cityLength;
- }
- public City withCityLength(int cityLength) {
- setCityLength(cityLength);
- return this;
- }
- public int getCityLength() {
- return cityLength;
- }
- public String toString() {
- int stars = pop / 10000;
- StringBuilder starStr = new StringBuilder();
- for(int i = 0; i < stars; ++i) {
- starStr.append("*");
- if(starStr.length() >= 40) {
- starStr.append("...(");
- starStr.append(stars);
- starStr.append(")");
- break;
- }
- }
- return String.format("%1$-" + cityLength + "s: ", name) + starStr.toString();
- }
- public boolean equals(Object other) {
- if(other instanceof City) {
- City c = (City) other;
- return name.equals(c.getName()) && pop == c.getPop();
- }
- return false;
- }
- }
- class CityReader {
- private List<City> cities;
- private BufferedReader in;
- public CityReader(String filename) throws IOException {
- cities = new ArrayList<City>();
- in = new BufferedReader(new FileReader(filename));
- }
- public void read() throws IOException {
- String line = in.readLine();
- while(line != null) {
- String[] parts = line.split(", ");
- cities.add(new City().withName(parts[0]).withPop(Integer.parseInt(parts[1])));
- line = in.readLine();
- }
- }
- public List<City> getCities() {
- return cities;
- }
- }
- class CityPrinter {
- List<City> cities;
- int maxNameLength;
- public CityPrinter(List<City> cities) {
- this.cities = cities;
- maxNameLength = 0;
- for(City city : cities) {
- if(city.getName().length() > maxNameLength) {
- maxNameLength = city.getName().length();
- }
- }
- }
- private void printCity(City city, PrintStream out) {
- out.println(city.withCityLength(maxNameLength + 2).withPopLength(40));
- }
- public void print(PrintStream out) {
- for(City city : cities) {
- printCity(city, out);
- }
- }
- public void printByName(PrintStream out) {
- List<City> temp = new ArrayList<City>(cities);
- Collections.sort(temp, new Comparator<City>() {
- public int compare(City c1, City c2) {
- return c1.getName().compareTo(c2.getName());
- }
- public boolean equals(City c1, City c2) {
- return c1.equals(c2);
- }
- });
- for(City city : temp) {
- printCity(city, out);
- }
- }
- public void printByPop(PrintStream out) {
- List<City> temp = new ArrayList<City>(cities);
- Collections.sort(temp, new Comparator<City>() {
- public int compare(City c1, City c2) {
- return c2.getPop() - c1.getPop();
- }
- public boolean equals(City c1, City c2) {
- return c1.equals(c2);
- }
- });
- for(City city : temp) {
- printCity(city, out);
- }
- }
- }
Add Comment
Please, Sign In to add comment