Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0403c52

Browse files
committed
Add StudentsVector as a sequential vector
1 parent cecfa6c commit 0403c52

File tree

6 files changed

+252
-0
lines changed

6 files changed

+252
-0
lines changed

.gitignore

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
.attach_pid*
2+
/src/target/
3+
/src/.attach_pid*
4+
!.mvn/wrapper/maven-wrapper.jar
5+
6+
### sts ###
7+
.apt_generated
8+
.classpath
9+
.factorypath
10+
.project
11+
.settings
12+
.springbeans
13+
.sts4-cache
14+
15+
### intellij idea ###
16+
.idea
17+
*.iws
18+
*.iml
19+
*.ipr
20+
21+
### netbeans ###
22+
/nbproject/private/
23+
/build/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/a
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package br.com.zevolution.datastructure.sequentialvector;
2+
3+
public class InvalidPosition extends RuntimeException {
4+
5+
private static final long serialVersionUID = -3536404610449816152L;
6+
7+
public InvalidPosition() {
8+
super("Invalid position");
9+
}
10+
11+
public InvalidPosition(String message) {
12+
super(message);
13+
}
14+
15+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package br.com.zevolution.datastructure.sequentialvector;
2+
3+
public class Student {
4+
5+
private String name;
6+
7+
public Student(String name) {
8+
this.name = name;
9+
}
10+
11+
public String getName() {
12+
return name;
13+
}
14+
15+
@Override
16+
public boolean equals(Object obj) {
17+
if (obj == null) return false;
18+
Student other = (Student) obj;
19+
return other.getName().equals(this.getName());
20+
}
21+
22+
@Override
23+
public String toString() {
24+
return this.name;
25+
}
26+
27+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package br.com.zevolution.datastructure.sequentialvector;
2+
3+
import java.util.Arrays;
4+
5+
public class StudentsVector {
6+
7+
private Student[] students = new Student[2];
8+
private int totalStudents = 0;
9+
10+
public void add(Student student) {
11+
this.grow();
12+
/*
13+
for (int i = 0; i < students.length; i++) {
14+
if (this.students[i] == null) {
15+
this.students[i] = student;
16+
break;
17+
}
18+
}*/
19+
this.students[totalStudents] = student;
20+
this.totalStudents++;
21+
}
22+
23+
public void add(int position, Student student) {
24+
this.grow();
25+
26+
if (!this.isValidPosition(position)) {
27+
throw new InvalidPosition();
28+
}
29+
30+
for (int i = this.totalStudents - 1; i >= position; i--) {
31+
this.students[i+1] = this.students[i];
32+
}
33+
this.students[position] = student;
34+
this.totalStudents++;
35+
}
36+
37+
public void remove(int position) {
38+
if (!this.isValidContentPosition(position)) {
39+
throw new UnoccupiedPosition();
40+
}
41+
42+
for (int i = position; i < this.totalStudents; i++) {
43+
this.students[i] = this.students[i+1];
44+
}
45+
46+
this.totalStudents--;
47+
this.students[this.totalStudents] = null;
48+
}
49+
50+
public Student get(int position) {
51+
if (!this.isValidContentPosition(position)) {
52+
throw new UnoccupiedPosition();
53+
}
54+
return this.students[position];
55+
}
56+
57+
public boolean has(Student student) {
58+
/*for (int i = 0; i < students.length; i++) {*/
59+
for (int i = 0; i < this.totalStudents; i++) {
60+
if (student.equals(this.students[i])) {
61+
return true;
62+
}
63+
}
64+
return false;
65+
}
66+
67+
public int size() {
68+
return this.totalStudents;
69+
}
70+
71+
public String toString() {
72+
return Arrays.toString(this.students);
73+
}
74+
75+
private boolean isValidContentPosition(int position) {
76+
return position >= 0 && position < this.totalStudents;
77+
}
78+
79+
private boolean isValidPosition(int position) {
80+
return position >= 0 && position <= this.totalStudents;
81+
}
82+
83+
private void grow() {
84+
if (this.totalStudents == this.students.length -1) {
85+
Student[] newStudentsArray = new Student[this.students.length*2];
86+
for (int i = 0; i < this.students.length; i++) {
87+
newStudentsArray[i] = this.students[i];
88+
}
89+
this.students = newStudentsArray;
90+
}
91+
}
92+
93+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package br.com.zevolution.datastructure.sequentialvector;
2+
3+
public class UnoccupiedPosition extends InvalidPosition {
4+
5+
private static final long serialVersionUID = -2300538444121627252L;
6+
7+
public UnoccupiedPosition() {
8+
super("There is no value in this position");
9+
}
10+
11+
12+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package br.com.zevolution.datastructure.sequentialvector;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
public class StudentsVectorTest {
8+
9+
@Test
10+
public void should_Contain_OneRecordInStudentsVector() {
11+
StudentsVector students = new StudentsVector();
12+
students.add(new Student("José Lucas"));
13+
14+
assertEquals(1, students.size());
15+
}
16+
17+
@Test
18+
public void should_Contain_TwoRecordsInStudentsVector() {
19+
StudentsVector students = new StudentsVector();
20+
students.add(new Student("Beatriz"));
21+
students.add(new Student("José Lucas"));
22+
23+
assertEquals(2, students.size());
24+
}
25+
26+
@Test
27+
public void should_MakeSure_StudentsPosition() {
28+
StudentsVector students = new StudentsVector();
29+
students.add(new Student("Beatriz"));
30+
students.add(new Student("José Lucas"));
31+
students.add(0, new Student("Laura"));
32+
33+
assertEquals("Laura", students.get(0).getName());
34+
assertEquals("Beatriz", students.get(1).getName());
35+
assertEquals("José Lucas", students.get(2).getName());
36+
}
37+
38+
@Test
39+
public void should_Be_Empty() {
40+
StudentsVector students = new StudentsVector();
41+
students.add(new Student("Beatriz"));
42+
students.add(new Student("José Lucas"));
43+
students.add(0, new Student("Laura"));
44+
45+
for (int i = students.size(); i > 0; i--) {
46+
students.remove(i-1);
47+
}
48+
49+
assertEquals(0, students.size());
50+
}
51+
52+
@Test
53+
public void should_Have_StudentLu() {
54+
StudentsVector students = new StudentsVector();
55+
students.add(new Student("Beatriz"));
56+
students.add(new Student("José Lucas"));
57+
students.add(0, new Student("Laura"));
58+
students.add(new Student("Lu"));
59+
60+
assertEquals(true, students.has(new Student("Lu")));
61+
}
62+
63+
@Test(expected = UnoccupiedPosition.class)
64+
public void should_ThrowException_When_UnoccupiedPosition() {
65+
StudentsVector students = new StudentsVector();
66+
students.add(new Student("zevolution"));
67+
68+
students.remove(1);
69+
}
70+
71+
@Test(expected = InvalidPosition.class)
72+
public void should_ThrowException_When_InvalidPosition() {
73+
StudentsVector students = new StudentsVector();
74+
75+
students.add(1, new Student("zevolution"));
76+
}
77+
78+
}

0 commit comments

Comments
 (0)