Advertisement
Guest User

Student

a guest
Feb 11th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. import java.util.*;
  2. import java.text.DecimalFormat;
  3.  
  4. public class Student {
  5.  
  6. String id;
  7. String first_name;
  8. String last_name;
  9. int quality_points;
  10. int gpa_hours;
  11. double gpa;
  12. List<String> stringRepresentation;
  13. DecimalFormat df = new DecimalFormat();
  14.  
  15. public Student(String id, String fn, String ln, int qp, int hours) {
  16. this.id = id;
  17. this.first_name = fn;
  18. this.last_name = ln;
  19. this.quality_points = qp;
  20. this.gpa_hours = hours;
  21. this.gpa = 1.0 * qp / hours;
  22. this.stringRepresentation = new ArrayList<String>();
  23. df.setMaximumFractionDigits(6);
  24.  
  25. stringRepresentation.add("first_name: " + fn + "\n");
  26. stringRepresentation.add("last_name: " + ln + "\n");
  27. stringRepresentation.add("quality_points: " + qp + "\n");
  28. stringRepresentation.add("gpa_hours: " + hours + "\n");
  29. stringRepresentation.add("gpa: " + df.format(gpa) + "\n");
  30. }
  31.  
  32. /**
  33. * This method takes in a list of attributes for a student and returns
  34. * the values of the attributes in a formatted String
  35. */
  36. public String format(String attributes) {
  37. String[] attributeArray = attributes.split(" ");
  38. List<String> attributeList = new ArrayList<String>();
  39. String output = "";
  40.  
  41. List<String> validAtt = new ArrayList<String>();
  42. validAtt.add("first_name");
  43. validAtt.add("last_name");
  44. validAtt.add("quality_points");
  45. validAtt.add("gpa_hours");
  46. validAtt.add("gpa");
  47. // the client adds this to the end of the message, it's okay
  48. validAtt.add("EOM");
  49.  
  50. // start at one, first attribute is the GTID
  51. for (int i = 1; i < attributeArray.length; i++) {
  52. attributeList.add((attributeArray[i]).trim());
  53. if (!(validAtt.contains(attributeArray[i].trim()))) {
  54. return "\"" + attributeArray[i] + "\" is not a valid student attribute.\n";
  55. }
  56. }
  57.  
  58. if (attributeList.contains("first_name")) {
  59. output += stringRepresentation.get(0);
  60. }
  61.  
  62. if (attributeList.contains("last_name")) {
  63. output += stringRepresentation.get(1);
  64. }
  65.  
  66. if (attributeList.contains("quality_points")) {
  67. output += stringRepresentation.get(2);
  68. }
  69.  
  70. if (attributeList.contains("gpa_hours")) {
  71. output += stringRepresentation.get(3);
  72. }
  73.  
  74. if (attributeList.contains("gpa")) {
  75. output += stringRepresentation.get(4);
  76. }
  77. return output;
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement