Main.java (1405B)
1 import java.io.*; 2 import java.util.ArrayList; 3 4 class Main { 5 public static void main(String[] args) { 6 ArrayList<Album> albums = new ArrayList<Album>(); 7 BufferedReader br; 8 File f; 9 String fields[]; 10 String str; 11 String sortby; 12 13 try { 14 sortby = args[0]; 15 f = new File(args[1]); 16 br = new BufferedReader(new FileReader(f)); 17 18 while ((str = br.readLine()) != null) { 19 fields = str.split("\\;"); 20 albums.add(new Album(fields[0], fields[1], 21 Integer.parseInt(fields[2]), 22 Float.parseFloat(fields[3]))); 23 } 24 br.close(); 25 } catch (FileNotFoundException e) { 26 System.err.println("No such file"); 27 return; 28 } catch (IOException e) { 29 System.err.println("IO Exception"); 30 return; 31 } catch (ArrayIndexOutOfBoundsException e) { 32 System.err.println("Usage: java Main sortby file\n"); 33 System.err.println("You can sort by:\n\ttitle\n\t" + 34 "artist\n\tyear\n\tprice"); 35 return; 36 } 37 38 if (sortby.equals("title")) 39 albums.sort((o1, o2) -> o1.get_title().compareTo(o2.get_title())); 40 else if (sortby.equals("artist")) 41 albums.sort((o1, o2) -> o1.get_artist().compareTo(o2.get_artist())); 42 else if (sortby.equals("year")) 43 albums.sort((o1, o2) -> o1.get_year().compareTo(o2.get_year())); 44 else if (sortby.equals("price")) 45 albums.sort((o1, o2) -> o1.get_price().compareTo(o2.get_price())); 46 47 for (Album m : albums) 48 System.out.println(m); 49 } 50 }