Skip to content

Commit

Permalink
Merge pull request #98 from yytan25/addMusicianToBand-toOneBasedIndex
Browse files Browse the repository at this point in the history
Edit AddMusiciantoBandCommand to use 1-based indices
  • Loading branch information
yytan25 authored Oct 20, 2023
2 parents dd6a662 + e272f99 commit 37f50ce
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
10 changes: 5 additions & 5 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ public static String format(Band band) {
/**
* Formats the {@code band} for display to the user.
*/
public static String format(int band, int musician) {
public static String format(Band band, Musician musician) {
final StringBuilder builder = new StringBuilder();
builder.append("Band Index: ")
.append(band)
.append("; Musician Index:")
.append(musician);
builder.append("Band Name: ")
.append(band.getName())
.append("; Musician Name: ")
.append(musician.getName());
return builder.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_BINDEX;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MINDEX;

import java.util.List;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.band.Band;
import seedu.address.model.musician.Musician;

/**
* Adds a musician to a band.
Expand All @@ -25,30 +30,32 @@ public class AddMusiciantoBandCommand extends Command {
public static final String MESSAGE_SUCCESS = "New musician added to band: %1$s";
public static final String MESSAGE_DUPLICATE_MUSICIAN = "This musician already exists in the band";

private final int toAdd;
private final int addInto;
private final Index bandToAddInto;
private final Index musicianToAdd;

/**
* Creates an AddCommand to add the specified {@code Musician}
*/
public AddMusiciantoBandCommand(int band, int musician) {
requireNonNull(musician);
requireNonNull(band);
addInto = band;
toAdd = musician;
public AddMusiciantoBandCommand(Index bandToAddInto, Index musicianToAdd) {
requireNonNull(bandToAddInto);
requireNonNull(musicianToAdd);
this.bandToAddInto = bandToAddInto;
this.musicianToAdd = musicianToAdd;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

if (model.hasMusicianInBand(addInto, toAdd)) {
if (model.hasMusicianInBand(bandToAddInto.getZeroBased(), musicianToAdd.getZeroBased())) {
throw new CommandException(MESSAGE_DUPLICATE_MUSICIAN);
}

model.addMusicianToBand(addInto, toAdd);
return new CommandResult(String.format(MESSAGE_SUCCESS, Messages.format(addInto, toAdd)));
List<Band> lastShownBandList = model.getFilteredBandList();
List<Musician> lastShownMusicianList = model.getFilteredMusicianList();
Band band = lastShownBandList.get(bandToAddInto.getZeroBased());
Musician musician = lastShownMusicianList.get(musicianToAdd.getZeroBased());
model.addMusicianToBand(bandToAddInto.getZeroBased(), musicianToAdd.getZeroBased());
return new CommandResult(String.format(MESSAGE_SUCCESS, Messages.format(band, musician)));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.util.stream.Stream;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.AddMusiciantoBandCommand;
import seedu.address.logic.parser.exceptions.ParseException;

Expand All @@ -25,9 +26,14 @@ public AddMusiciantoBandCommand parse(String args) throws ParseException {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
AddMusiciantoBandCommand.MESSAGE_USAGE));
}
int bandIndex = Integer.parseInt(argMultimap.getValue(PREFIX_BINDEX).get());
int musicianIndex = Integer.parseInt(argMultimap.getValue(PREFIX_MINDEX).get());
return new AddMusiciantoBandCommand(bandIndex, musicianIndex);
try {
Index bandIndex = ParserUtil.parseIndex(argMultimap.getValue(PREFIX_BINDEX).get());
Index musicianIndex = ParserUtil.parseIndex(argMultimap.getValue(PREFIX_MINDEX).get());
return new AddMusiciantoBandCommand(bandIndex, musicianIndex);
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddMusiciantoBandCommand.MESSAGE_USAGE), pe);
}
}

/**
Expand Down

0 comments on commit 37f50ce

Please sign in to comment.