Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.example.nico.reportcard;
import java.util.ArrayList;
public class ReportCard {
// School name is a constant as this will never change
private static final String School = "Your University";
/* Declaring Student and Date variables as part of the info to display.
Subject, Grade and Comment variables will be part of an ArrayList.
*/
private String mStudent;
private String mDate;
// ArrayList holding Strings (3) arrays for Subjects, Grades and Comment only.
private ArrayList<String[]> mReportData;
// Constructor for the class ReportCard.
public ReportCard(String student, String date) {
mStudent = student;
mDate = date;
mReportData = new ArrayList<>();
}
// Getters method to access data.
public String getSchool(){
return School;
}
public String getStudent() {
return mStudent;
}
public String getDate() {
return mDate;
}
public ArrayList<String[]> getReportData() {
return mReportData;
}
// Setters methods for variables mStudent and mDate.
public void setStudent(String student) {
mStudent = student;
}
public void setDate(String date) {
mDate = date;
}
// This setter for mGrade will adds the information of subject, grade and comment into the string array.
public void setSubjectGrade(String subject, String grade, String comment) {
// String Array of 3 inputs.
String[] subjectGrade = new String[3];
// Add subject and grade Strings to the subjectGrade array.
subjectGrade[0] = subject;
subjectGrade[1] = grade;
subjectGrade[2] = comment;
// Add subjectGrade array to the mReportData ArrayList.
mReportData.add(subjectGrade);
}
// Return a particular subject report from the report card.
public String[] getReportData(int indexNumber) {
return mReportData.get(indexNumber);
}
// Remove a particular subject report from the report card.
public void removeReportData(int indexNumber) {
mReportData.remove(indexNumber);
}
// Using toString method to show the data in a proper way.
@Override
public String toString() {
/* Create subjectReportList String which will contain all subject reports to be displayed
* through the toString method.
Initialise with a new line */
String reportData = "\n";
/* Loop through the ArrayList converting all subject arrays to strings and adding them to
the subjectReportList string */
for (int index = 0; index < mReportData.size(); index++) {
String[] subjectReport = mReportData.get(index);
String subject = subjectReport[0];
String grade = subjectReport[1];
String comment = subjectReport[2];
reportData = reportData + "Subject: " + subject + ", Grade: "
+ grade + ", Comment: " + comment + "\n";
}
// Return the full report card
return "School: " + School + "\n" +
"Student: " + mStudent + "\n" +
"Report Date: " + mDate + "\n" +
reportData;
/* Supposed information to be displayed;
School: Your University
Student: Nico
Report Date: 15 Jun 2017
Subject: Physic, Grade: A, Comment: Excellent
Subject: Mathematics, Grade: B, Comment: Improving
Subject: Art, Grade: C, Comment: Need to improve
Subject: Geography, Grade: D, Comment: Study more
etc.
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.