I have included all my code below (5 Classes) for a simple database of students.
I cowrote this code with someone who believes that in the Name, Address and Student classes it is necessary to include all of the getters and setters that are currently there. I think the code will function without them and that they are not necessary and should be removed? If they are necessary could someone explain why or could someone confirm that they are not necessary?
Address Class
public class Address {
private String street, area, city, country;
public Address(String street, String area, String city, String country) {
this.street = street;
this.area = area;
this.street = city;
this.country = country;
}
public void setStreet(String street) {
this.street = street;
}
public void setArea(String area) {
this.area = area;
}
public void setCity(String city) {
this.city = city;
}
public void setCountry(String country) {
this.country = country;
}
public String street() {
return street;
}
public String area() {
return area;
}
public String city() {
return city;
}
public String country() {
return country;
}
/** Returns area, street, city and country concatenated together respectively */
public String toString() {
return area + ", " + street + ", " + city + ", " + country + ".";
}
}
Name Class
public class Name{
private String firstName, lastName;
public Name (String firstName, String lastName){
this.firstName = firstName;
this.lastName = lastName;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
/** Returns first name concatenated to last name */
public String toString() {
return firstName + " " + lastName;
}
}
Student Class
public class Student {
private Name name; // This is calling from the Name class, giving it the name 'name'
private Address address; // This calls from Address, giving it the name 'address'
private char gender;
private String course, college;
private int gradePointAverage, id, age, level;
public Student(int id, String firstName, String lastName, String street, String area, String city, String country, int age, char gender, String college, String course, int level, int gradePointAverage){ //This is the list of variables called from the Student class
this.id = id;
this.name = new Name(firstName, lastName);
this.address = new Address(street, area, city, country);
this.age = age;
this.gender = gender;
this.college = college;
this.course = course;
this.level = level;
this.gradePointAverage = gradePointAverage;
}
public int getId(){
return id;
}
public String getName(){
return name.toString();
}
public String getAddress(){
return address.toString();
}
public int getAge(){
return age;
}
public char getGender(){
return gender;
}
public String getCollege(){
return college;
}
public int getLevel() {
return level;
}
public String getCourse() {
return course;
}
public int getGradePointAverage() {
return gradePointAverage;
}
public void printStudent() {
System.out.println("The Student " + name.toString() + " is logged under the student ID number " + id + ".");
System.out.println("They live at " + address.toString() + " and their age is " + age + ".");
System.out.println("Their gender is " + gender + ".");
System.out.println("The student studies at " + college + " attending classes in " + course + ".");
System.out.println("Their level is " + level + " and the student grade average in points is " + gradePointAverage + ".");
System.out.println();
}
}
CollegeCommunity Class
import java.util.*;
public class CollegeCommunity
{
private ArrayList<Student> students; // Setting up an arraylist to store student details
public CollegeCommunity()
{
students = new ArrayList<Student>();
}
public void addStudent(Student student) // adds a student.
{
students.add(student); // .add method command.
}
public void removeStudent(int id) // deleting a student, after being passed id to locate desired student.
{
for (int i=0; i<students.size(); i++ ){ // using a loop to decide what student to remove by matching the student ID which was passed to the method with the student ID's on record. Once there's a match, the student will be removed (.remove).
if(students.get(i).getId()==id) {
students.remove(i);
}
}
}
public void showStudent(int id) // same as remove above but instead using print command to view details of particular student.
{
for (int i=0; i<students.size(); i++ ){
if(students.get(i).getId()==id) {
students.get(i).printStudent();
}
}
}
public void showAllStudents()
{
for (int i=0; i<students.size(); i++ ){ // This loop command will display ALL student details as no specific ID was passed, so it will run as long as value 'i' is less than student.size.
int id=students.get(i).getId();
showStudent(id);
}
}
public void showStudentsInCourse(String course) // This will show students in a particular course.
{
for(int i=0; i<students.size(); i++ ){ // Loop is same as remove but comparing the string course with the course of each student. .equals is used to compare strings.
if(students.get(i).getCourse().equals(course)){
int id=students.get(i).getId();
showStudent(id);
}
}
}
public int calculateGradePointAverage() // calculating the grade point average as a percentage
{
int total = 0;
for (int i = 0; i < students.size(); i++ ){
total = total + students.get(i).getGradePointAverage(); // total is calculated as it loops, each students score (getGradePointAverage).
}
total = total / students.size(); // final figure is total divided by the number of students, to give an average score.
return total;
}
}
TestSystem Class
import java.util.*;
public class TestSystem
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
Student student;
CollegeCommunity collegeCommunity = new CollegeCommunity(); // New CollegeCommunity created called 'collegeCommunity'.
int id, age, level, gradePointAverage; // ints created for student id, age, level of course, and the grade point value
String fName,lName, street, area, city, country, course, college; // first name, last name strings created
char gender; // character gender created
boolean finish = false; // boolean finish has value of False.
do
{
switch(getMenu())
{
case '1':
System.out.println();
System.out.print("Please enter a new Student ID > ");
id = sc.nextInt();
sc.nextLine();
System.out.println();
System.out.print("Please enter the Student's first name > ");
fName = sc.nextLine();
System.out.println();
System.out.print("Please enter the Student's last name > ");
lName = sc.nextLine();
System.out.println();
System.out.print("Please enter the street > ");
street = sc.nextLine();
System.out.println();
System.out.print("Please enter the area > ");
area = sc.nextLine();
System.out.println();
System.out.print("Please enter the city > ");
city = sc.nextLine();
System.out.println();
System.out.print("Please enter the country > ");
country = sc.nextLine();
System.out.println();
System.out.print("Please enter the age > ");
age = sc.nextInt();
sc.nextLine();
System.out.println();
System.out.print("Please enter the gender > ");
gender = sc.nextLine().charAt(0);
System.out.println();
System.out.print("Please enter the college > ");
college = sc.nextLine();
System.out.println();
System.out.print("Please enter the course > ");
course = sc.nextLine();
System.out.println();
System.out.print("Please enter level > ");
level = sc.nextInt();
sc.nextLine();
System.out.println();
System.out.print("Please enter the average grade in points [0-100] > ");
gradePointAverage = sc.nextInt();
sc.nextLine();
System.out.println();
student = new Student(id, fName, lName, street, area, city, country, age, gender, college, course, level, gradePointAverage);
collegeCommunity.addStudent(student);
break;
case '2':
System.out.println();
System.out.print("Please enter the student ID number > ");
id = sc.nextInt();
sc.nextLine();
// this will delete the student selected
collegeCommunity.removeStudent(id);
System.out.println();
System.out.print("Student " + id + " has been deleted.");
System.out.println();
System.out.println();
break;
case '3':
System.out.println();
System.out.print("Please enter the student ID number > ");
id = sc.nextInt();
sc.nextLine();
// This will allow for the details of the selected student to be displayed. Calls stored data from community college.
collegeCommunity.showStudent(id);
System.out.println();
System.out.println();
break;
case '4':
System.out.println();
// This will show all of the students stored in the system.
collegeCommunity.showAllStudents();
System.out.println();
break;
case '5':
System.out.println();
System.out.print("Please enter the course > ");
course = sc.nextLine();
// This will show students that are enrolled in a chosen course.
collegeCommunity.showStudentsInCourse(course);
System.out.println();
break;
case '6':
System.out.println();
System.out.print("The Average grade score is " + collegeCommunity.calculateGradePointAverage() + "%");
System.out.println();
break;
case 'x':
case 'X':
finish = true; // boolean value changes to true if X is selected
System.exit(0);
break;
default:
System.out.println();
System.out.println("That is an invalid selection.");
System.out.println(" Please try again");
break;
}
}while (!finish);
}
public static char getMenu()
{
Scanner sc = new Scanner(System.in);
System.out.println();
System.out.println("This is your community college menu");
System.out.println("Please select from the menu options below");
System.out.println();
System.out.println("1 Add a Student");
System.out.println();
System.out.println("2 Delete a Student");
System.out.println();
System.out.println("3 Show details on an individual student");
System.out.println();
System.out.println("4 Show details on all students");
System.out.println();
System.out.println("5 Shows details on all students on a course");
System.out.println();
System.out.println("6 Display the average grade (points)");
System.out.println();
System.out.println("X Exit");
System.out.println();
System.out.println();
System.out.print("Please make selection > ");
return sc.next().charAt(0);
}
}
You can always set the properties as
publicorprotectedor with default scope and access them (provided the class is in the appropiate package)That said, it is always good practice to use setters and getters. It eases control of what values are being set. For instance. You can put a check in your set so that the age is never less than 4
Setting this with properties is left to the programmer, who may miss the check somewhere in the code. Debugging that later is very tricky.
Setters and getters also help to add logs / breakpoints to check the behavior of the program.
And most decent IDEs allow you to automatically code setters and getters just from the properties, so it is not a big deal.
As a side note, I would usually recommend storing birth dates and calculating age when needed, since age is a value that changes while birth date not, and you can always calculate ages from birth dates but not the opposite.