8
\$\begingroup\$

I have made a small project and I am just wondering if I could add more functionality to the project e.g more classed etc. Is a better way of displaying my results and what error handling I could do?

I was also wondering what would be the best way to add Inheritance to the this project.I was thinking a EveningCourse class or PostGrad, UnderGrad class. What would be the best way to go about it?

The user enters student name, age, year and student number.

package david;

public class Student 
{

    private String name;
    private int age;
    private int year;
    private String studentNum;



    public Student(String name, int age, int year, String studentNum) 
    {

        this.name = name;
        this.age = age;
        this.year = year;
        this.studentNum = studentNum;
    }

    // Setters and getters (Name, Age, Year and Student Number)

    public String getName() // name
    {
        return name;
    }

    public void setName(String name) 
    {
        this.name = name;
    }

    public int getAge() // age
    {
        return age;
    }

    public void setAge(int age)
    {
        this.age = age;
    }

    public int getYear() // year
    {
        return year;
    }

    public void setYear(int year) 
    {
        this.year = year;
    }

    public String getstudentNum() // studentNum
    {
    return studentNum;
    }

    public void setstudentNum(String studentNum) 
    {
        this.studentNum = studentNum;
    }

}

package david;

// David Needham - G00263842.
// Student information program.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class TestStudents 
{

    public static void main(String[] args) throws IOException 
    {

    System.out.println("============" + "=================");
    System.out.println("Students " + "Personal Details");
    System.out.println("============" + "=================");

    String name;
    int age;
    int year;
    String studentNum;

    List<Student> studentsList = new ArrayList<Student>(); 

    for (int i = 0; i < 2; i++) 
    {

    int studentNumber = (i + 1);

    System.out.println("");
    System.out.println("Please enter " + "data for student " + studentNumber);

    InputStreamReader converter = new InputStreamReader(System.in);
    BufferedReader in = new BufferedReader(converter);

    System.out.println("Enter Student "+ studentNumber + " Name:"); 
    name = in.readLine();

    System.out.println("Enter Student " + studentNumber + " Age (Integer):");
    age = Integer.valueOf(in.readLine());

    System.out.println("Enter Student " + studentNumber + " Year (Integer):");
    year = Integer.valueOf(in.readLine());

    System.out.println("Enter Student " + studentNumber + " Student Number:"); 
    studentNum = in.readLine();

    Student student = new Student(name, age, year, studentNum);

    studentsList.add(student); // add student
    }

    for (int j = 0; j < studentsList.size(); j++)
    {

        Student st = studentsList.get(j);

        System.out.println("Student : " + (j + 1));
        System.out.println("Name: " + st.getName() + " - Age: " + st.getAge() + " - Year: " + st.getYear() + " - Student Number: " + st.getstudentNum());
    }

    }
}
\$\endgroup\$
1
  • \$\begingroup\$ You could have used a single function for the name, age, year and studentnum. \$\endgroup\$
    – user30271
    Commented Oct 2, 2013 at 10:04

3 Answers 3

4
\$\begingroup\$

Yes, you can add more functionality to your project. You can take this in many different directirons such as adding courses that students and register for.

As for a error handling, you could add a check in your set functions of the Student class to check for formatting or content and return a boolean value based on if they pass the validation or not. For example, since studentnum is a string, you can check to make sure that the format of letters and numbers if correct, if there is a certain format.

You could also use a try-catch block around the code where you read in the values from the user. Or validate that the user enter an acceptable value for each entry such as checking that when the user is asked to enter the age, check to make sure that they did indeed enter a number.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks very much for this feedback, now I know where to start! \$\endgroup\$ Commented Dec 5, 2012 at 20:42
3
\$\begingroup\$

Some general stuff. Your use of whitespace and indentation is messy, fix it!

Java normally uses a modified K&R style with the opening braces on the same line, like this:

function test() {
    if (condition) {
        // Stuff
    } else {
        // Stuff
    }
}

package david;

In this case it's minor and/or unnecessary, but keep in mind that package names should give some information about the package, f.e. who wrote it.

package com.yourcompany.department.package;
package com.gmail.youremailname.package;

public String getName() // name

Whenever possible use JavaDoc, it can be consumed by various tools and IDEs and can make your life a lot easier.

/**
 * The (full, first and last) name of the Student.
 */
public String getName()

Instead of making local variables and creating the object afterwards, you could add an empty constructor and fill the object directly.

Student student = new Student();
student.setName(fromInput);
student.setAge(fromInput);
...

List<Student> studentsList = new ArrayList<Student>(); 

This variables would be better named students. From the plural you can already perceive that is some sort of collection, and looking at the definition tells you that it is a List. There's no need for any sort of hungarian notation.


You could extract the reading of the values into it's own class, something that allows you to do something like this:

InputReader reader = new InputReader(System.out, System.in);
//Inside the for
Student student = new Student();
student.setName(reader.getString("Please enter the name: "));
student.setAge(reader.getInt("Please enter the ager: "));
...

for (int j = 0; j < studentsList.size(); j++)
    {

        Student st = studentsList.get(j);

Use appropriate loops, in this case a for each:

for(Student student : studentsList) {

private String studentNum;

This variable would be better named ID, that would make the Object a lot cleaner:

student.getID();
student.setID(id);

studentsList.add(student); // add student

Comments should mostly explain why you do something the way you do, and partly how. You never need to explain "obvious" things, like incrementing an integer. In this case your comment says exactly the same thing as your code, actually, the code is more precise.


public static void main(String[] args) throws IOException 

This is baaaad. Your application should handle exceptions gracefully, main() should never throw exceptions.

\$\endgroup\$
1
\$\begingroup\$

You could make Student an abstract class, and then have UnderGrad and PostGrad classes as concrete implementations of it with their own variations on how they implement the base methods.

This would then give you the flexibility of accommodating other types of student. For example students attending a short language course before starting their undergrad.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.