Skip to content

Commit

Permalink
Refactor getPianoPieces method
Browse files Browse the repository at this point in the history
  • Loading branch information
IzN432 committed Oct 16, 2024
1 parent 569292d commit 366f969
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 73 deletions.
12 changes: 12 additions & 0 deletions src/main/java/keycontacts/model/pianopiece/PianoPiece.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import static java.util.Objects.requireNonNull;
import static keycontacts.commons.util.AppUtil.checkArgument;

import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Represents a Piano Piece in the student directory.
* Guarantees: immutable; name is valid as declared in {@link #isValidPianoPieceName(String)}
Expand Down Expand Up @@ -64,4 +68,12 @@ public String toString() {
return pianoPieceName;
}

/**
* Returns a piano piece set containing the list of strings given.
*/
public static Set<PianoPiece> getPianoPieceSet(String... strings) {
return Arrays.stream(strings)
.map(PianoPiece::new)
.collect(Collectors.toSet());
}
}
30 changes: 11 additions & 19 deletions src/main/java/keycontacts/model/util/SampleDataUtil.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package keycontacts.model.util;

import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

import keycontacts.model.ReadOnlyStudentDirectory;
import keycontacts.model.StudentDirectory;
import keycontacts.model.pianopiece.PianoPiece;
Expand All @@ -20,17 +16,22 @@ public class SampleDataUtil {
public static Student[] getSampleStudents() {
return new Student[] {
new Student(new Name("Alex Yeoh"), new Phone("87438807"), new Address("Blk 30 Geylang Street 29, #06-40"),
new GradeLevel("ABRSM 1")),
new GradeLevel("ABRSM 1"), PianoPiece.getPianoPieceSet("Canon in D"), null),
new Student(new Name("Bernice Yu"), new Phone("99272758"),
new Address("Blk 30 Lorong 3 Serangoon Gardens, #07-18"), new GradeLevel("RSL 2")),
new Address("Blk 30 Lorong 3 Serangoon Gardens, #07-18"), new GradeLevel("RSL 2"),
PianoPiece.getPianoPieceSet("Moonlight Sonata"), null),
new Student(new Name("Charlotte Oliveiro"), new Phone("93210283"),
new Address("Blk 11 Ang Mo Kio Street 74, #11-04"), new GradeLevel("AMEB 3")),
new Address("Blk 11 Ang Mo Kio Street 74, #11-04"), new GradeLevel("AMEB 3"),
PianoPiece.getPianoPieceSet("Etude", "Moonlight Sonata"), null),
new Student(new Name("David Li"), new Phone("91031282"),
new Address("Blk 436 Serangoon Gardens Street 26, #16-43"), new GradeLevel("RCM 1")),
new Address("Blk 436 Serangoon Gardens Street 26, #16-43"), new GradeLevel("RCM 1"),
PianoPiece.getPianoPieceSet("Howl's moving castle"), null),
new Student(new Name("Irfan Ibrahim"), new Phone("92492021"),
new Address("Blk 47 Tampines Street 20, #17-35"), new GradeLevel("IMEB 2")),
new Address("Blk 47 Tampines Street 20, #17-35"), new GradeLevel("IMEB 2"),
PianoPiece.getPianoPieceSet("Moonlight Sonata"), null),
new Student(new Name("Roy Balakrishnan"), new Phone("92624417"),
new Address("Blk 45 Aljunied Street 85, #11-31"), new GradeLevel("RSL 1")),
new Address("Blk 45 Aljunied Street 85, #11-31"), new GradeLevel("RSL 1"),
PianoPiece.getPianoPieceSet("Styx Helix"), null),
};
}

Expand All @@ -42,13 +43,4 @@ public static ReadOnlyStudentDirectory getSampleStudentDirectory() {
return sampleSd;
}

/**
* Returns a piano piece set containing the list of strings given.
*/
public static Set<PianoPiece> getPianoPieceSet(String... strings) {
return Arrays.stream(strings)
.map(PianoPiece::new)
.collect(Collectors.toSet());
}

}
14 changes: 0 additions & 14 deletions src/main/resources/view/DarkTheme.css
Original file line number Diff line number Diff line change
Expand Up @@ -336,17 +336,3 @@
-fx-background-color: transparent, #383838, transparent, #383838;
-fx-background-radius: 0;
}

#tags {
-fx-hgap: 7;
-fx-vgap: 3;
}

#tags .label {
-fx-text-fill: white;
-fx-background-color: #3e7b91;
-fx-padding: 1 3 1 3;
-fx-border-radius: 2;
-fx-background-radius: 2;
-fx-font-size: 11;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package keycontacts.logic.commands;

import static keycontacts.logic.commands.CommandTestUtil.VALID_PIANO_PIECE_BEETHOVEN;
import static keycontacts.logic.commands.CommandTestUtil.VALID_PIANO_PIECE_PACHELBEL;
import static keycontacts.testutil.Assert.assertThrows;
import static keycontacts.testutil.TypicalIndexes.INDEX_FIRST_STUDENT;
import static keycontacts.testutil.TypicalIndexes.INDEX_SECOND_STUDENT;
import static keycontacts.testutil.TypicalStudents.ALICE;
import static keycontacts.testutil.TypicalStudents.BENSON;
import static keycontacts.testutil.TypicalStudents.getTypicalStudentDirectory;
Expand All @@ -11,7 +15,6 @@
import java.util.Set;
import java.util.stream.Collectors;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import keycontacts.commons.core.index.Index;
Expand All @@ -21,92 +24,86 @@
import keycontacts.model.ModelManager;
import keycontacts.model.UserPrefs;
import keycontacts.model.pianopiece.PianoPiece;
import keycontacts.model.util.SampleDataUtil;

public class AssignPiecesCommandTest {
private static final Set<PianoPiece> VALID_PIANO_PIECES = SampleDataUtil.getPianoPieceSet(
"Testing Value 1", "Testing Value 2", "Testing Value 3");
private static final Index VALID_INDEX = Index.fromOneBased(1);
private Model model;
@BeforeEach
public void initialise() {
model = new ModelManager(getTypicalStudentDirectory(), new UserPrefs());
}

private final Set<PianoPiece> validPianoPieces = PianoPiece.getPianoPieceSet(VALID_PIANO_PIECE_BEETHOVEN,
VALID_PIANO_PIECE_PACHELBEL);

private Model model = new ModelManager(getTypicalStudentDirectory(), new UserPrefs());

@Test
public void constructor_nullIndex_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> new AssignPiecesCommand(null, VALID_PIANO_PIECES));
assertThrows(NullPointerException.class, () -> new AssignPiecesCommand(null, validPianoPieces));
}
@Test
public void constructor_nullPianoPieces_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> new AssignPiecesCommand(VALID_INDEX, null));
assertThrows(NullPointerException.class, () -> new AssignPiecesCommand(INDEX_FIRST_STUDENT, null));
}

@Test
public void execute_validIndexAndPianoPiece_success() throws Exception {
AssignPiecesCommand command = new AssignPiecesCommand(VALID_INDEX, VALID_PIANO_PIECES);
AssignPiecesCommand command = new AssignPiecesCommand(INDEX_FIRST_STUDENT, validPianoPieces);
CommandResult commandResult = command.execute(model);

assertEquals(String.format(AssignPiecesCommand.MESSAGE_SUCCESS,
Messages.format(VALID_PIANO_PIECES),
Messages.format(model.getFilteredStudentList().get(VALID_INDEX.getZeroBased()))),
Messages.format(validPianoPieces),
Messages.format(model.getFilteredStudentList().get(INDEX_FIRST_STUDENT.getZeroBased()))),
commandResult.getFeedbackToUser());
}
@Test
public void execute_indexOutOfBounds_failure() {
Index outOfBoundsIndex = Index.fromOneBased(model.getFilteredStudentList().size() + 1);
AssignPiecesCommand command = new AssignPiecesCommand(outOfBoundsIndex, VALID_PIANO_PIECES);
AssignPiecesCommand command = new AssignPiecesCommand(outOfBoundsIndex, validPianoPieces);

assertThrows(CommandException.class,
Messages.MESSAGE_INVALID_STUDENT_DISPLAYED_INDEX, () -> command.execute(model));
}
@Test
public void execute_duplicatePianoPiece_throwsCommandException() throws Exception {
AssignPiecesCommand setupCommand = new AssignPiecesCommand(VALID_INDEX, VALID_PIANO_PIECES);
AssignPiecesCommand setupCommand = new AssignPiecesCommand(INDEX_FIRST_STUDENT, validPianoPieces);
setupCommand.execute(model);

Set<PianoPiece> firstPianoPiece = VALID_PIANO_PIECES.stream().limit(1).collect(Collectors.toSet());
AssignPiecesCommand command = new AssignPiecesCommand(VALID_INDEX, firstPianoPiece);
Set<PianoPiece> firstPianoPiece = validPianoPieces.stream().limit(1).collect(Collectors.toSet());
AssignPiecesCommand command = new AssignPiecesCommand(INDEX_FIRST_STUDENT, firstPianoPiece);

assertThrows(CommandException.class,
AssignPiecesCommand.MESSAGE_DUPLICATE_PIANO_PIECE, () -> command.execute(model));
}

@Test
public void equals() {
Set<PianoPiece> pianoPieces1 = BENSON.getPianoPieces();
Set<PianoPiece> pianoPieces2 = ALICE.getPianoPieces();
Set<PianoPiece> bensonPianoPieces = BENSON.getPianoPieces();
Set<PianoPiece> alicePianoPieces = ALICE.getPianoPieces();

Index index1 = Index.fromOneBased(1);
Index index2 = Index.fromOneBased(2);

AssignPiecesCommand baseCommand = new AssignPiecesCommand(index1, pianoPieces1);
AssignPiecesCommand baseCommand = new AssignPiecesCommand(INDEX_FIRST_STUDENT, bensonPianoPieces);

// same object -> returns true
assertTrue(baseCommand.equals(baseCommand));

// same values -> returns true
AssignPiecesCommand identicalCommand = new AssignPiecesCommand(index1, pianoPieces1);
AssignPiecesCommand identicalCommand = new AssignPiecesCommand(INDEX_FIRST_STUDENT, bensonPianoPieces);
assertTrue(baseCommand.equals(identicalCommand));
assertTrue(identicalCommand.equals(baseCommand));

// different values -> returns false
AssignPiecesCommand differentIndexCommand = new AssignPiecesCommand(index2, pianoPieces1);
AssignPiecesCommand differentIndexCommand = new AssignPiecesCommand(INDEX_SECOND_STUDENT, bensonPianoPieces);
assertFalse(baseCommand.equals(differentIndexCommand));

AssignPiecesCommand differentPianoPiecesCommand = new AssignPiecesCommand(index1, pianoPieces2);
AssignPiecesCommand differentPianoPiecesCommand = new AssignPiecesCommand(INDEX_FIRST_STUDENT,
alicePianoPieces);
assertFalse(baseCommand.equals(differentPianoPiecesCommand));

// null -> returns false
assertFalse(baseCommand.equals(null));


}

@Test
public void toStringMethod() {
AssignPiecesCommand assignPiecesCommand = new AssignPiecesCommand(VALID_INDEX, VALID_PIANO_PIECES);
String expected = AssignPiecesCommand.class.getCanonicalName() + "{index=" + VALID_INDEX + ", "
+ "pianoPieces=" + VALID_PIANO_PIECES + "}";
AssignPiecesCommand assignPiecesCommand = new AssignPiecesCommand(INDEX_FIRST_STUDENT, validPianoPieces);
String expected = AssignPiecesCommand.class.getCanonicalName() + "{index=" + INDEX_FIRST_STUDENT + ", "
+ "pianoPieces=" + validPianoPieces + "}";
assertEquals(expected, assignPiecesCommand.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class CommandTestUtil {
public static final String VALID_GRADE_LEVEL_AMY = "RSL 2";
public static final String VALID_GRADE_LEVEL_BOB = "ABRSM 3";
public static final String VALID_PIANO_PIECE_BEETHOVEN = "Für Elise";
public static final String VALID_PIANO_PIECE_PACHELBEL = "Canon in D";

public static final String NAME_DESC_AMY = " " + PREFIX_NAME + VALID_NAME_AMY;
public static final String NAME_DESC_BOB = " " + PREFIX_NAME + VALID_NAME_BOB;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import keycontacts.commons.core.index.Index;
import keycontacts.logic.commands.AssignPiecesCommand;
import keycontacts.model.pianopiece.PianoPiece;
import keycontacts.model.util.SampleDataUtil;

public class AssignPiecesCommandParserTest {
private static final String MESSAGE_INVALID_FORMAT =
Expand Down Expand Up @@ -60,7 +59,7 @@ public void parse_invalidPianoPiece_failure() {
public void parse_multiplePianoPieces_success() {
String userInput = VALID_INDEX.getOneBased() + " " + PREFIX_PIECE_NAME + VALID_PIANO_PIECE_ONE + " "
+ PREFIX_PIECE_NAME + VALID_PIANO_PIECE_TWO;
Set<PianoPiece> pianoPieceSet = SampleDataUtil.getPianoPieceSet(VALID_PIANO_PIECE_ONE, VALID_PIANO_PIECE_TWO);
Set<PianoPiece> pianoPieceSet = PianoPiece.getPianoPieceSet(VALID_PIANO_PIECE_ONE, VALID_PIANO_PIECE_TWO);
AssignPiecesCommand expectedCommand = new AssignPiecesCommand(VALID_INDEX, pianoPieceSet);

assertParseSuccess(parser, userInput, expectedCommand);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import keycontacts.logic.commands.HelpCommand;
import keycontacts.logic.commands.ListCommand;
import keycontacts.logic.parser.exceptions.ParseException;
import keycontacts.model.pianopiece.PianoPiece;
import keycontacts.model.student.NameContainsKeywordsPredicate;
import keycontacts.model.student.Student;
import keycontacts.model.util.SampleDataUtil;
import keycontacts.testutil.EditStudentDescriptorBuilder;
import keycontacts.testutil.StudentBuilder;
import keycontacts.testutil.StudentUtil;
Expand Down Expand Up @@ -104,7 +104,7 @@ public void parseCommand_assign() throws Exception {

AssignPiecesCommand expectedCommand = new AssignPiecesCommand(
index,
SampleDataUtil.getPianoPieceSet(pianoPieces));
PianoPiece.getPianoPieceSet(pianoPieces));
assertEquals(expectedCommand, command);
}

Expand Down
3 changes: 1 addition & 2 deletions src/test/java/keycontacts/model/student/StudentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.junit.jupiter.api.Test;

import keycontacts.model.pianopiece.PianoPiece;
import keycontacts.model.util.SampleDataUtil;
import keycontacts.testutil.StudentBuilder;

public class StudentTest {
Expand Down Expand Up @@ -106,7 +105,7 @@ public void toStringMethod() {
public void withAddedPianoPieces() {
Student student = new StudentBuilder(ALICE).withPianoPieces().build();

Set<PianoPiece> pianoPieces = SampleDataUtil
Set<PianoPiece> pianoPieces = PianoPiece
.getPianoPieceSet("Für Elise", "Moonlight Sonata", "Franz Liszt – Liebestraum No. 3");
Student updatedStudent = student.withAddedPianoPieces(pianoPieces);
assertEquals(pianoPieces, updatedStudent.getPianoPieces());
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/keycontacts/testutil/StudentBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import keycontacts.model.student.Name;
import keycontacts.model.student.Phone;
import keycontacts.model.student.Student;
import keycontacts.model.util.SampleDataUtil;

/**
* A utility class to help with building Student objects.
Expand Down Expand Up @@ -87,7 +86,7 @@ public StudentBuilder withGradeLevel(String gradeLevel) {
* and set it to the {@code Student} that we are building.
*/
public StudentBuilder withPianoPieces(String ... pianoPieces) {
this.pianoPieces = SampleDataUtil.getPianoPieceSet(pianoPieces);
this.pianoPieces = PianoPiece.getPianoPieceSet(pianoPieces);
return this;
}

Expand Down

0 comments on commit 366f969

Please sign in to comment.