Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Group Management #88

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/main/java/longah/LongAh.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ public static void main(String[] args) {

Logging.logInfo("Starting Pre-program preparations.");
try {
group = new Group("group"); // Give a temporary name for now
new PINHandler();
String groupName = UI.getGroupName();
group = new Group(groupName);
} catch (LongAhException e) {
LongAhException.printException(e);
}
Expand All @@ -46,6 +47,10 @@ public static void main(String[] args) {
Command c = InputHandler.parseInput(command);
c.execute(group);

if (c.isSwitch()) {
group = new Group(c.getTaskExpression());
UI.showMessage("Switched to group: " + group.getGroupName());
}
// Check will not be reached if exception is thrown
if (c.isExit()) {
System.exit(0);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/longah/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public boolean isExit() {
return false;
}

/**
* Returns whether the current command is a switch command or not.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ayo what

*/
public boolean isSwitch() {
return false;
}
/**
* Returns the command string.
*
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/longah/commands/SwitchCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package longah.commands;

import longah.exception.ExceptionMessage;
import longah.exception.LongAhException;
import longah.node.Group;

public class SwitchCommand extends Command {
/**
* Constructor for SwitchCommand.
*
* @param commandString The command string.
* @param taskExpression The task expression.
*/
public SwitchCommand(String commandString, String taskExpression) {
super(commandString, taskExpression);
}

/**
* Executes the switch command.
*
* @param group The group to execute the command on.
*/
public void execute(Group group) throws LongAhException {
if (this.taskExpression.isEmpty()) {
throw new LongAhException(ExceptionMessage.INVALID_SWITCH_GROUP_COMMAND);
}
}

/**
* Returns whether the current command is a switch command or not.
*
* @return True if the command is a switch command, false otherwise.
*/
@Override
public boolean isSwitch() {
return true;
}
}
4 changes: 3 additions & 1 deletion src/main/java/longah/exception/ExceptionMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ public enum ExceptionMessage {
INVALID_EXIT_COMMAND ("Invalid command format." +
" Use 'exit'", ExceptionType.INFO),
INVALID_HELP_COMMAND ("Invalid command format." +
" Use 'help'", ExceptionType.INFO);
" Use 'help'", ExceptionType.INFO),
INVALID_SWITCH_GROUP_COMMAND ("Invalid command format." +
" Use 'switchgroup GROUP_NAME'", ExceptionType.INFO);

private final String message;
private final ExceptionType type;
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/longah/handler/InputHandler.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package longah.handler;

import longah.commands.ClearCommand;
import longah.commands.Command;
import longah.commands.ExitCommand;
import longah.commands.HelpCommand;
import longah.commands.PINCommand;
import longah.commands.SettleCommand;
import longah.commands.add.AddCommand;
import longah.commands.delete.DeleteCommand;
import longah.commands.edit.EditCommand;
import longah.commands.find.FindCommand;
import longah.commands.list.ListCommand;
import longah.commands.SettleCommand;
import longah.commands.ClearCommand;
import longah.commands.ExitCommand;
import longah.commands.PINCommand;
import longah.commands.SwitchCommand;
import longah.commands.HelpCommand;
import longah.exception.ExceptionMessage;
import longah.exception.LongAhException;

Expand Down Expand Up @@ -63,6 +64,8 @@ public static Command parseCommand(String commandString, String taskExpression)
// Fallthrough
case "chart":
throw new LongAhException(ExceptionMessage.COMMAND_NOT_IMPLEMENTED);
case "switchgroup":

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wut

return new SwitchCommand(commandString, taskExpression);
default:
throw new LongAhException(ExceptionMessage.INVALID_COMMAND);
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/longah/handler/StorageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ public class StorageHandler {
/**
* Initializes a new StorageHandler instance.
* Each instance handles the data storage requirements of each group of members.
*
*
* @param members The MemberList object to store the members data
* @param transactions The TransactionList object to store the transactions data
* @param groupName The name of the group to store the data
*
* @throws LongAhException If the data files are not created
*/
public StorageHandler(MemberList members, TransactionList transactions, String groupName)
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/longah/handler/UI.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,8 @@ public static void printSeparator() {
System.out.println(SEPARATOR);
}

public static String getGroupName() {
showMessage("Create a new group or enter the name of your group to manage:");
return getUserInput();
}
}
4 changes: 2 additions & 2 deletions src/main/java/longah/node/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public boolean isLender(String memberName) {
* @param memberName String representation of member name to check
* @return a boolean value determining whether the input name is a borrower in the transaction
*/
public boolean isBorrower(String memberName) {
public boolean checkIsBorrower(String memberName) {
for (Subtransaction subtransaction : this.subtransactions) {
if (subtransaction.getBorrower().isName(memberName)) {
return true;
Expand All @@ -209,7 +209,7 @@ public boolean isBorrower(String memberName) {
* @return a boolean value determining whether the input name is involved in the transaction
*/
public boolean isInvolved(String memberName) {
return isLender(memberName) || isBorrower(memberName);
return isLender(memberName) || checkIsBorrower(memberName);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/longah/util/TransactionList.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public String findBorrower(String borrowerName) throws LongAhException {
int index = 1;
String outString = String.format("%s owns the following list of transactions.", borrowerName) + "\n";
for (Transaction transaction : this.transactions) {
if (transaction.isBorrower(borrowerName)) {
if (transaction.checkIsBorrower(borrowerName)) {
outString = outString + String.format("%d.\n%s", index, transaction) + "\n";
index++;
}
Expand Down Expand Up @@ -193,7 +193,7 @@ public String findDebts(String borrowerName) throws LongAhException {
, borrowerName) + "\n";
int index = 1;
for (Transaction transaction : this.transactions) {
if (transaction.isBorrower(borrowerName)) {
if (transaction.checkIsBorrower(borrowerName)) {
outString = outString + String.format("%d.\n%s", index, transaction) + "\n";
index++;
}
Expand Down
1 change: 1 addition & 0 deletions text-ui-test/EXPECTED.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Welcome to LongAh!
Error reading saved PIN and authentication enabled state.
Create your 6-digit PIN:
PIN saved successfully! You can enter 'pin disable' to login automatically upon startup.
Create a new group or enter the name of your group to manage:
Enter command: Invalid command. Use 'help' to see the list of commands.
Enter command: Enter command: Enter command: Amy: $0.0
Brandon: $0.0
Expand Down
3 changes: 2 additions & 1 deletion text-ui-test/input.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
123456
123456
test group
test
add member Amy
add member Brandon
list members
Expand Down
Loading