Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.text.DecimalFormat;
- public class Student {
- String id;
- String first_name;
- String last_name;
- int quality_points;
- int gpa_hours;
- double gpa;
- List<String> stringRepresentation;
- DecimalFormat df = new DecimalFormat();
- public Student(String id, String fn, String ln, int qp, int hours) {
- this.id = id;
- this.first_name = fn;
- this.last_name = ln;
- this.quality_points = qp;
- this.gpa_hours = hours;
- this.gpa = 1.0 * qp / hours;
- this.stringRepresentation = new ArrayList<String>();
- df.setMaximumFractionDigits(6);
- stringRepresentation.add("first_name: " + fn + "\n");
- stringRepresentation.add("last_name: " + ln + "\n");
- stringRepresentation.add("quality_points: " + qp + "\n");
- stringRepresentation.add("gpa_hours: " + hours + "\n");
- stringRepresentation.add("gpa: " + df.format(gpa) + "\n");
- }
- /**
- * This method takes in a list of attributes for a student and returns
- * the values of the attributes in a formatted String
- */
- public String format(String attributes) {
- String[] attributeArray = attributes.split(" ");
- List<String> attributeList = new ArrayList<String>();
- String output = "";
- List<String> validAtt = new ArrayList<String>();
- validAtt.add("first_name");
- validAtt.add("last_name");
- validAtt.add("quality_points");
- validAtt.add("gpa_hours");
- validAtt.add("gpa");
- // the client adds this to the end of the message, it's okay
- validAtt.add("EOM");
- // start at one, first attribute is the GTID
- for (int i = 1; i < attributeArray.length; i++) {
- attributeList.add((attributeArray[i]).trim());
- if (!(validAtt.contains(attributeArray[i].trim()))) {
- return "\"" + attributeArray[i] + "\" is not a valid student attribute.\n";
- }
- }
- if (attributeList.contains("first_name")) {
- output += stringRepresentation.get(0);
- }
- if (attributeList.contains("last_name")) {
- output += stringRepresentation.get(1);
- }
- if (attributeList.contains("quality_points")) {
- output += stringRepresentation.get(2);
- }
- if (attributeList.contains("gpa_hours")) {
- output += stringRepresentation.get(3);
- }
- if (attributeList.contains("gpa")) {
- output += stringRepresentation.get(4);
- }
- return output;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement