Skip to content

Commit 4f07872

Browse files
committed
Fixed Iterator
1 parent 396a476 commit 4f07872

File tree

11 files changed

+59
-12
lines changed

11 files changed

+59
-12
lines changed

src/BehavioralPatterns/Iterator/CollegeIterator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public boolean isLastStudent() {
3333

3434
@Override
3535
public Student currentStudent() {
36-
if (index < students.size()){
36+
for (; index < students.size(); index++) {
3737
if (students.get(index).getType().equals("College")){
3838
return students.get(index);
3939
}

src/BehavioralPatterns/Iterator/HighSchoolIterator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public boolean isLastStudent() {
3333

3434
@Override
3535
public Student currentStudent() {
36-
if (index < students.size()){
36+
for (; index < students.size(); index++) {
3737
if (students.get(index).getType().equals("High School")){
3838
return students.get(index);
3939
}

src/BehavioralPatterns/Iterator/Main.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ public static void main(String[] args){
88
school.addStudent(new Student("Tom", "High School"));
99
school.addStudent(new Student("Jack", "High School"));
1010
school.addStudent(new Student("Maor", "College"));
11+
1112
Iterator highSchoolIterator = school.createIterator("High School");
12-
System.out.println("First Student: " + highSchoolIterator.currentStudent().getName());
13+
System.out.println("First High School Student: " + highSchoolIterator.currentStudent().getName());
1314
while (!highSchoolIterator.isLastStudent()){
1415
System.out.println("High School Student: " + highSchoolIterator.nextStudent().getName());
1516
}
17+
1618
Iterator collegeIterator = school.createIterator("College");
19+
System.out.println("First College Student: " + collegeIterator.currentStudent().getName());
1720
while (!collegeIterator.isLastStudent()){
1821
System.out.println("College Student: " + collegeIterator.nextStudent().getName());
1922
}

src/BehavioralPatterns/Iterator/School.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
public class School {
77
private List<Student> students = new ArrayList<>();
88

9-
public List<Student> getStudents(){return students;}
9+
public List<Student> getStudents(){ return students; }
1010

11-
public void addStudent(Student student){students.add(student);}
11+
public void addStudent(Student student){ students.add(student); }
1212

13-
public void removeStudent(Student student){students.remove(student);}
13+
public void removeStudent(Student student){ students.remove(student); }
1414

1515
public Iterator createIterator(String type) {
1616
if (type.equals("High School")){

src/BehavioralPatterns/Memento/Main.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ public static void main(String[] args) {
1212
memento = originator.createMemento();
1313
caretaker.addMemento(memento);
1414
originator.setState("Elephant");
15-
System.out .println("Originator Current State: " + originator.getState());
15+
System.out.println("Originator Current State: " + originator.getState());
1616
System.out.println("Originator restoring to previous state...");
1717
memento = caretaker.getMemento(1);
1818
originator.setMemento(memento);
19-
System.out .println("Originator Current State: " + originator.getState());
19+
System.out.println("Originator Current State: " + originator.getState());
2020
System.out.println("Again restoring to previous state...");
2121
memento = caretaker.getMemento(0);
2222
originator.setMemento(memento);
23-
System.out .println("Originator Current State: " + originator.getState());
23+
System.out.println("Originator Current State: " + originator.getState());
2424
}
2525
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package BehavioralPatterns.StateDP;
2+
3+
public class AlertStateContext {
4+
private MobileAlertState currentState;
5+
public AlertStateContext() {
6+
currentState = new Vibration(); }
7+
public void setState(MobileAlertState state) {
8+
currentState = state; }
9+
public void alert() { currentState.alert(this); }
10+
}
11+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package BehavioralPatterns.StateDP;
2+
3+
public class Main {
4+
public static void main(String[] args){
5+
AlertStateContext stateContext = new AlertStateContext();
6+
stateContext.alert();
7+
stateContext.alert();
8+
stateContext.setState(new Silent());
9+
stateContext.alert();
10+
stateContext.alert();
11+
stateContext.alert();
12+
}
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package BehavioralPatterns.StateDP;
2+
3+
public interface MobileAlertState {
4+
void alert(AlertStateContext ctx);
5+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package BehavioralPatterns.StateDP;
2+
3+
public class Silent implements MobileAlertState{
4+
@Override
5+
public void alert(AlertStateContext ctx) {
6+
System.out.println("silent...");
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package BehavioralPatterns.StateDP;
2+
3+
public class Vibration implements MobileAlertState{
4+
@Override
5+
public void alert(AlertStateContext ctx) {
6+
System.out.println("vibration...");
7+
}
8+
}

0 commit comments

Comments
 (0)