Skip to content

Instantly share code, notes, and snippets.

@NicReg
Created June 13, 2017 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NicReg/c4f18a7fd343761d3160f0f19f3f21cd to your computer and use it in GitHub Desktop.
Save NicReg/c4f18a7fd343761d3160f0f19f3f21cd to your computer and use it in GitHub Desktop.
ReportCard showing data about student, date, subjects, grades and comments.
package com.example.nico.reportcard;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;
public class ReportCard {
/* Declaring Student and Date variables as part of the info to display.
Subject and Grade 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 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 "Student: " + mStudent + "\n" +
"Report Date: " + mDate + "\n" +
reportData;
/* Supposed information to be displayed;
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