Commit 58c4f184 by Paktalin

Is able to split student name into firstName, lastName and middleName

parent ce57dcf8
......@@ -17,7 +17,6 @@ public abstract class Session implements Comparable<Session>{
private String number;
private List<Student> students = new ArrayList<>();
private Date startDate;
private static int count = 0;
private int numberOfCredits;
protected Session(String department, String number, Date startDate) {
......
......@@ -9,6 +9,7 @@ import java.util.List;
class Student {
private String name;
private String firstName = "", middleName = "", lastName;
private int credits;
static final int CREDITS_REQUIRED_FOR_FULL_TIME = 12;
static final String IN_STATE = "CO";
......@@ -27,37 +28,52 @@ class Student {
}
}
Student(String name) {
this.name = name;
Student(String fullName) {
setNames(fullName);
credits = 0;
}
String getName() {
return name;
void addCredits(int credits) {
this.credits += credits;
}
boolean isFullTime() {
return credits >= Student.CREDITS_REQUIRED_FOR_FULL_TIME;
void addGrade(Grade grade) {
grades.add(grade);
}
int getCredits() {
return credits;
private void setNames(String fullName) {
this.name = fullName;
String[] nameSplit = fullName.split(" ");
switch (nameSplit.length){
case 1:
lastName = nameSplit[0];
break;
case 2:
firstName = nameSplit[0];
lastName = nameSplit[1];
break;
default:
firstName = nameSplit[0];
middleName = nameSplit[1];
lastName = nameSplit[2];
break;
}
}
void addCredits(int credits) {
this.credits += credits;
void setState(String state) {
this.state = state.toUpperCase();
}
boolean isInState() {
return state.equals(Student.IN_STATE);
void setGradingStrategy(HonorsGradingStrategy gradingStrategy) {
this.gradingStrategy = gradingStrategy;
}
void setState(String state) {
this.state = state.toUpperCase();
boolean isFullTime() {
return credits >= Student.CREDITS_REQUIRED_FOR_FULL_TIME;
}
void addGrade(Grade grade) {
grades.add(grade);
boolean isInState() {
return state.equals(Student.IN_STATE);
}
double getGpa() {
......@@ -69,7 +85,23 @@ class Student {
return total/grades.size();
}
void setGradingStrategy(HonorsGradingStrategy gradingStrategy) {
this.gradingStrategy = gradingStrategy;
int getCredits() {
return credits;
}
String getName() {
return name;
}
String getFirstName() {
return firstName;
}
String getLastName() {
return lastName;
}
String getMiddleName() {
return middleName;
}
}
package com.example.paktalin.agilejava;
import com.example.paktalin.agilejava.language_tests.LanguageTests;
import com.example.paktalin.agilejava.language_tests.LoopTest;
import com.example.paktalin.agilejava.language_tests.SuperClassTest;
import com.example.paktalin.agilejava.summer.SummerCourseSession;
......@@ -28,6 +29,7 @@ public class AllTests extends TestSuite {
suite.addTestSuite(LanguageTests.class);
suite.addTestSuite(SuperClassTest.class);
suite.addTestSuite(LoopTest.class);
return suite;
}
}
......@@ -10,13 +10,15 @@ import junit.framework.TestCase;
public class StudentTest extends TestCase {
public void testCreate() {
final String firstStudentName = "Jane Doe";
Student student = new Student(firstStudentName);
assertEquals(firstStudentName, student.getName());
Student student = new Student("Jane Doe");
verifyNamesCorrect(student, "Jane Doe", "Jane", "Doe", "");
final String secondStudentName = "Joe Blow";
Student secondStudent = new Student(secondStudentName);
assertEquals(secondStudentName, secondStudent.getName());
student = new Student("Blow");
verifyNamesCorrect(student, "Blow", "", "Blow", "");
student = new Student("Raymond Douglas Davies");
verifyNamesCorrect(student, "Raymond Douglas Davies",
"Raymond", "Davies", "Douglas");
}
public void testStudentStatus() {
......@@ -88,4 +90,12 @@ public class StudentTest extends TestCase {
student.setGradingStrategy(new HonorsGradingStrategy());
return student;
}
private void verifyNamesCorrect(
Student student, String name, String firstName, String lastName, String middleName) {
assertEquals(name, student.getName());
assertEquals(firstName, student.getFirstName());
assertEquals(lastName, student.getLastName());
assertEquals(middleName, student.getMiddleName());
}
}
package com.example.paktalin.agilejava.language_tests;
import junit.framework.TestCase;
/**
* Created by Paktalin on 01/06/2018.
*/
public class LoopTest extends TestCase {
private String[] array;
@Override
protected void setUp() throws Exception {
array = new String[]{"apple", "a", "banana", "k", "grapefruit", "pineapple"};
}
public void testMultipleConditionsLoop() {
int i, count;
for (i = 0, count = 0; i < array.length; i++)
if (array[i].length() > 1)
count++;
assertEquals(4, count);
}
public void testPalindrome() {
assertFalse(isPalindrome("abcd"));
assertTrue(isPalindrome("abba"));
assertTrue(isPalindrome(""));
assertFalse(isPalindrome("abbaa"));
}
private boolean isPalindrome(String string){
for
(int forward = 0, backward = string.length() - 1;
forward < string.length();
forward++, backward--)
if (string.charAt(forward) != string.charAt(backward))
return false;
return true;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment