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>{ ...@@ -17,7 +17,6 @@ public abstract class Session implements Comparable<Session>{
private String number; private String number;
private List<Student> students = new ArrayList<>(); private List<Student> students = new ArrayList<>();
private Date startDate; private Date startDate;
private static int count = 0;
private int numberOfCredits; private int numberOfCredits;
protected Session(String department, String number, Date startDate) { protected Session(String department, String number, Date startDate) {
......
...@@ -9,6 +9,7 @@ import java.util.List; ...@@ -9,6 +9,7 @@ import java.util.List;
class Student { class Student {
private String name; private String name;
private String firstName = "", middleName = "", lastName;
private int credits; private int credits;
static final int CREDITS_REQUIRED_FOR_FULL_TIME = 12; static final int CREDITS_REQUIRED_FOR_FULL_TIME = 12;
static final String IN_STATE = "CO"; static final String IN_STATE = "CO";
...@@ -27,37 +28,52 @@ class Student { ...@@ -27,37 +28,52 @@ class Student {
} }
} }
Student(String name) { Student(String fullName) {
this.name = name; setNames(fullName);
credits = 0; credits = 0;
} }
String getName() { void addCredits(int credits) {
return name; this.credits += credits;
} }
boolean isFullTime() { void addGrade(Grade grade) {
return credits >= Student.CREDITS_REQUIRED_FOR_FULL_TIME; grades.add(grade);
} }
int getCredits() { private void setNames(String fullName) {
return credits; 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) { void setState(String state) {
this.credits += credits; this.state = state.toUpperCase();
} }
boolean isInState() { void setGradingStrategy(HonorsGradingStrategy gradingStrategy) {
return state.equals(Student.IN_STATE); this.gradingStrategy = gradingStrategy;
} }
void setState(String state) { boolean isFullTime() {
this.state = state.toUpperCase(); return credits >= Student.CREDITS_REQUIRED_FOR_FULL_TIME;
} }
void addGrade(Grade grade) { boolean isInState() {
grades.add(grade); return state.equals(Student.IN_STATE);
} }
double getGpa() { double getGpa() {
...@@ -69,7 +85,23 @@ class Student { ...@@ -69,7 +85,23 @@ class Student {
return total/grades.size(); return total/grades.size();
} }
void setGradingStrategy(HonorsGradingStrategy gradingStrategy) { int getCredits() {
this.gradingStrategy = gradingStrategy; return credits;
}
String getName() {
return name;
}
String getFirstName() {
return firstName;
}
String getLastName() {
return lastName;
}
String getMiddleName() {
return middleName;
} }
} }
package com.example.paktalin.agilejava; package com.example.paktalin.agilejava;
import com.example.paktalin.agilejava.language_tests.LanguageTests; 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.language_tests.SuperClassTest;
import com.example.paktalin.agilejava.summer.SummerCourseSession; import com.example.paktalin.agilejava.summer.SummerCourseSession;
...@@ -28,6 +29,7 @@ public class AllTests extends TestSuite { ...@@ -28,6 +29,7 @@ public class AllTests extends TestSuite {
suite.addTestSuite(LanguageTests.class); suite.addTestSuite(LanguageTests.class);
suite.addTestSuite(SuperClassTest.class); suite.addTestSuite(SuperClassTest.class);
suite.addTestSuite(LoopTest.class);
return suite; return suite;
} }
} }
...@@ -10,13 +10,15 @@ import junit.framework.TestCase; ...@@ -10,13 +10,15 @@ import junit.framework.TestCase;
public class StudentTest extends TestCase { public class StudentTest extends TestCase {
public void testCreate() { public void testCreate() {
final String firstStudentName = "Jane Doe"; Student student = new Student("Jane Doe");
Student student = new Student(firstStudentName); verifyNamesCorrect(student, "Jane Doe", "Jane", "Doe", "");
assertEquals(firstStudentName, student.getName());
final String secondStudentName = "Joe Blow"; student = new Student("Blow");
Student secondStudent = new Student(secondStudentName); verifyNamesCorrect(student, "Blow", "", "Blow", "");
assertEquals(secondStudentName, secondStudent.getName());
student = new Student("Raymond Douglas Davies");
verifyNamesCorrect(student, "Raymond Douglas Davies",
"Raymond", "Davies", "Douglas");
} }
public void testStudentStatus() { public void testStudentStatus() {
...@@ -88,4 +90,12 @@ public class StudentTest extends TestCase { ...@@ -88,4 +90,12 @@ public class StudentTest extends TestCase {
student.setGradingStrategy(new HonorsGradingStrategy()); student.setGradingStrategy(new HonorsGradingStrategy());
return student; 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