Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. import java.io.*;
  2. import java.sql.*;
  3. import java.util.*;
  4.  
  5. public class GPA {
  6. private final static Map<String, Double> map = new HashMap<>();
  7. public static void main(String args[]) throws Exception {
  8. if (args.length != 1) {
  9. System.out.println("[Usage]: java GPA [query]");
  10. System.exit(0);
  11. }
  12. map.put("A+", 4.3);
  13. map.put("A", 4.0);
  14. map.put("A-", 3.7);
  15. map.put("B+", 3.3);
  16. map.put("B", 3.0);
  17. map.put("B-", 2.7);
  18. map.put("C+", 2.3);
  19. map.put("C", 2.0);
  20. map.put("C-", 1.7);
  21. map.put("F", 0.0);
  22. Class.forName("org.sqlite.JDBC");
  23. String url = "jdbc:sqlite:/Users/host/Documents/Personal/Courses.db";
  24. Connection con = DriverManager.getConnection(url);
  25. Statement stm = con.createStatement();
  26. ResultSet rs = stm.executeQuery(args[0]);
  27. double sum = 0;
  28. int credits = 0;
  29. while (rs.next()) {
  30. String grades = rs.getString("成績");
  31. if (!map.keySet().contains(grades))
  32. continue;
  33. sum += map.get(grades) * rs.getInt("學分");
  34. credits += rs.getInt("學分");
  35. }
  36. System.out.println(sum / credits);
  37. con.close();
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement