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

List personloans #346

Merged
merged 37 commits into from
Nov 5, 2019
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
45d520b
Added skeleton classes for Parse
Deculsion Oct 10, 2019
907d911
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
Deculsion Oct 10, 2019
2b6e7a0
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/ma…
Deculsion Oct 12, 2019
8d3cda2
Added toString() and modified some javadocs for it.
Deculsion Oct 14, 2019
a96fe29
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
Deculsion Oct 15, 2019
e343c96
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
Deculsion Oct 16, 2019
79e4603
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
Deculsion Oct 17, 2019
d69b302
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
Deculsion Oct 17, 2019
5a7d1a4
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
Deculsion Oct 17, 2019
fd55fe5
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
Deculsion Oct 18, 2019
f5af5d3
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
Deculsion Oct 24, 2019
a4a3f6c
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
Deculsion Oct 25, 2019
7413e74
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
Deculsion Oct 29, 2019
c20a03f
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
Deculsion Oct 29, 2019
c3009c5
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
Deculsion Oct 30, 2019
88da583
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
Deculsion Oct 31, 2019
7f81e83
docs/User Guide.md created from https://stackedit.io/
Deculsion Oct 31, 2019
dd6c48d
Add UG markdown
Deculsion Oct 31, 2019
0c519e2
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
Deculsion Oct 31, 2019
bf78ffc
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
Deculsion Nov 1, 2019
acbf4f4
Merge remote-tracking branch 'self_origin/master'
Deculsion Nov 1, 2019
e620f8a
LoanList is now more OOP and optimised.
Deculsion Nov 1, 2019
3fd25c7
Check whether sufficient stock before adding.
Deculsion Nov 1, 2019
d87a245
Checkstyle
Deculsion Nov 1, 2019
f2d6103
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/ma…
Deculsion Nov 1, 2019
770feb7
Implement backend to list all of one person's loan
Deculsion Nov 1, 2019
898941e
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/ma…
Deculsion Nov 1, 2019
7c5c6e3
Add logic and parser for list specific person loans
Deculsion Nov 1, 2019
eb83660
Checkstyle fixes.
Deculsion Nov 1, 2019
d35bf7b
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/ma…
Deculsion Nov 4, 2019
2c1e807
Fix basic adding of new loan
Deculsion Nov 5, 2019
ca78761
Add Loan works full now
Deculsion Nov 5, 2019
7f10d2a
Add Loan for single person works now
Deculsion Nov 5, 2019
0f062bf
Add Loan for single person works now
Deculsion Nov 5, 2019
a3de754
temp fix for listloan
Deculsion Nov 5, 2019
e9f9be4
Delete saved_stocks.csv
Deculsion Nov 5, 2019
9ae17d1
Merge branch 'master' into list-personloans
Deculsion Nov 5, 2019
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
6 changes: 0 additions & 6 deletions data/saved_stocks.csv

This file was deleted.

4 changes: 3 additions & 1 deletion src/main/java/eggventory/Eggventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import eggventory.logic.commands.Command;
import eggventory.commons.enums.CommandType;
import eggventory.logic.parsers.Parser;
import eggventory.model.LoanList;
import eggventory.model.PersonList;
import eggventory.model.StockList;
import eggventory.storage.Storage;
Expand All @@ -21,6 +22,7 @@ public class Eggventory {
private static Parser parser;
private static Ui ui;
private static StockList stockList;
private static LoanList loanList;
private static PersonList personList;
//private static LoanList loanList;

Expand All @@ -37,7 +39,7 @@ public static void main(String[] args) {
storage = new Storage(stockFilePath, stockTypesFilePath);
parser = new Parser();
stockList = storage.load();
//loanList = new LoanList();
loanList = new LoanList();
personList = new PersonList();

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public CommandDictionary() {
commandDict.add(new Pair<>("list template", null));
commandDict.add(new Pair<>("list lost", null));
commandDict.add(new Pair<>("list person", null));
commandDict.add(new Pair<>("list person", "<MatricNo>"));

// Find Commands
commandDict.add(new Pair<>("find stock", "<Query>"));
Expand Down
25 changes: 17 additions & 8 deletions src/main/java/eggventory/logic/commands/add/AddLoanCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,28 @@ public class AddLoanCommand extends Command {
* @param matricNo the matric number of the person making the loan.
* @param quantity the quantity loaned.
*/
public AddLoanCommand(CommandType type, String stockCode, String matricNo, int quantity) {
public AddLoanCommand(CommandType type, String matricNo, String stockCode, int quantity) {
super(type);
this.stockCode = stockCode;
this.matricNo = matricNo;
this.quantity = quantity;
}

private boolean sufficientStock() {
if (quantity - LoanList.getStockLoanedQuantity(stockCode) < 0) {
private boolean stockExists() {
if (LoanList.getStockLoanedQuantity(stockCode) == -1) {
return false;
}

return true;
}

private boolean sufficientStock() {
if (LoanList.getStockLoanedQuantity(stockCode) - quantity < 0) {
return false;
}
return true;
}

/**
* Method to execute the AddLoanCommand.
* @param list the stockList.
Expand All @@ -45,16 +52,18 @@ private boolean sufficientStock() {
*/
public String execute(StockList list, Ui ui, Storage storage) {
String output = "";
if (sufficientStock()) {
if (!stockExists()) {
output += "OOPS that stock does not exist!";
} else if (!sufficientStock()) {
output = ("OOPS there is insufficient stock to loan out!");
} else {
LoanList.addLoan(stockCode, matricNo, quantity);
output = (String.format("Nice, I have added this loan for you: \n"
+ "Stock: %s | Person: %s | Quantity: %d", stockCode, matricNo, quantity));

ui.print(output);
} else {
output = ("OOPS there is insufficient stock to loan out!");
}

ui.print(output);

return output;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eggventory.logic.commands.add;

import eggventory.model.LoanList;
import eggventory.model.StockList;
import eggventory.logic.commands.Command;
import eggventory.commons.exceptions.BadInputException;
Expand Down Expand Up @@ -69,5 +70,6 @@ public String execute(StockList list, Ui ui, Storage storage) throws BadInputExc
*/
public void execute(StockList list) {
list.addStock(stockType, stockCode, quantity, description);
LoanList.addStock(stockCode, quantity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public DeleteLoanCommand(CommandType type, String stockCode, String matricNo) {
super(type);
this.stockCode = stockCode;
this.matricNo = matricNo;
this.quantity = LoanList.getLoanQuantity(stockCode, matricNo);
this.quantity = LoanList.getPersonLoanQuantity(stockCode, matricNo);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

//@@author Deculsion
public class ListPersonCommand extends Command {

public ListPersonCommand(CommandType type) {
super(type);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package eggventory.logic.commands.list;

import eggventory.commons.enums.CommandType;
import eggventory.commons.exceptions.BadInputException;
import eggventory.logic.commands.Command;
import eggventory.model.LoanList;
import eggventory.model.PersonList;
import eggventory.model.StockList;
import eggventory.storage.Storage;
import eggventory.ui.Ui;

//@@author Deculsion
public class ListPersonLoansCommand extends Command {
String matricNo;

public ListPersonLoansCommand(CommandType type, String matricNo) {
super(type);
this.matricNo = matricNo;
}

/**
* Executes the command.
* @param list The stock list.
* @param ui The ui.
* @param storage The Storage.
*/
@Override
public String execute(StockList list, Ui ui, Storage storage) throws BadInputException {
String output = "";

output += LoanList.printPersonLoans(matricNo);

ui.drawTable(LoanList.getPersonLoansStruct(matricNo));
ui.print(output);

return output;
}
}
26 changes: 24 additions & 2 deletions src/main/java/eggventory/logic/parsers/ParseList.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import eggventory.commons.exceptions.InsufficientInfoException;
import eggventory.logic.commands.Command;
import eggventory.logic.commands.CommandDictionary;
import eggventory.logic.commands.list.ListPersonCommand;
import eggventory.logic.commands.list.ListStockCommand;
import eggventory.logic.commands.list.ListStockTypeCommand;
import eggventory.logic.commands.list.ListPersonLoansCommand;
import eggventory.logic.commands.list.ListPersonCommand;
import eggventory.commons.enums.CommandType;
import eggventory.commons.exceptions.BadInputException;

Expand All @@ -29,12 +30,28 @@ private Command processListStockType(String input) throws BadInputException {
}

private Command processListPerson(String input) throws BadInputException {
String[] inputArr = input.split(" +");
String[] inputArr = input.split(" ");
Copy link

@cyanoei cyanoei Nov 5, 2019

Choose a reason for hiding this comment

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

mm i've noticed that the predictive search (however raghav did it) prevents users from spamming spaces between their params anyway, so the multiple spaces regex has truly become obsolete


if (inputArr.length > 1) {
throw new BadInputException(CommandDictionary.getCommandUsage("list person"));
}

return new ListPersonCommand(CommandType.LIST);
}

private Command processListLoan(String input) throws BadInputException {
String[] inputArr = input.split(" +");

switch (inputArr.length) {
case 1:
throw new BadInputException(CommandDictionary.getCommandUsage("list loan"));
//return new ListLoanCommand(CommandType.LIST);
case 2:
return new ListPersonLoansCommand(CommandType.LIST, inputArr[1]);

default:
throw new BadInputException(CommandDictionary.getCommandUsage("list loan"));
}

}

Expand Down Expand Up @@ -68,11 +85,16 @@ public Command parse(String inputString) throws BadInputException, InsufficientI
listCommand = processListPerson(inputArr[0]);
break;

case "loan":
listCommand = processListLoan(inputString);
break;

default:
throw new BadInputException(CommandDictionary.getCommandUsage("list"));
}

return listCommand;
}

}
//@@author
47 changes: 41 additions & 6 deletions src/main/java/eggventory/model/LoanList.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import eggventory.model.loans.Loan;
import eggventory.model.loans.LoanPair;
import eggventory.ui.TableStruct;

import java.util.ArrayList;
import java.util.Calendar;
Expand Down Expand Up @@ -70,23 +71,37 @@ public static boolean deleteLoan(String stockCode, String matricNo) {
}

/**
* Returns the Loan quantity of a queried Loan.
* The map requires the exact same instance of the key in order to access the quantity.
* Therefore constructing the key (pair of matricNo/stockCode) cannot directly access the value (the Loan).
* Rather, we have to compare the pairs using .equals and then use the original instance to access the Loan.
* Adds a new stock to track in LoanList.
* @param stockCode The code of the stock
* @param quantity The starting quantity of the stock
*/
public static void addStock(String stockCode, int quantity) {
updateStockLoaned(stockCode, quantity);
}

/**
* Returns the quantity of a certain Stock that a Person has loaned out.
* @param stockCode the stockCode of the Stock involved.
* @param matricNo the matric number of the Person involved.
* @return the quantity.
* @return the quantity loaned out by a person.
*/
public static int getLoanQuantity(String stockCode, String matricNo) {
public static int getPersonLoanQuantity(String stockCode, String matricNo) {
Loan loan = findLoan(stockCode, matricNo);
if (loan == null) {
return -1;
}
return loan.getQuantity();
}

/**
* Returns the total quantity of a certain Stock has been loaned out.
* @param stockCode stockCode of the Stock to check
* @return The quantity loaned out
*/
public static int getStockLoanedQuantity(String stockCode) {
if (stockLoaned.get(stockCode) == null) {
return -1;
}
return stockLoaned.get(stockCode);
}

Expand Down Expand Up @@ -182,6 +197,26 @@ public static String printStockLoans(String stockCode) {
return output;
}

/**
* Returns all of a person's loans as a TableStruct.
* @param matricNo Matriculation number of person to output.
*/
public static TableStruct getPersonLoansStruct(String matricNo) {
TableStruct dataTable = new TableStruct("Loans of " + matricNo);
dataTable.setTableColumns("Stock Code", "Quantity Loaned");

ArrayList<Loan> loans = getPersonLoans(matricNo);
ArrayList<ArrayList<String>> dataList = new ArrayList<>();

for (Loan loan : loans) {
dataList.add(loan.getStockDataAsArray());
}

dataTable.setTableData(dataList);

return dataTable;
}

//@@author

}
13 changes: 13 additions & 0 deletions src/main/java/eggventory/model/loans/Loan.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eggventory.model.loans;

import java.util.ArrayList;
import java.util.Calendar;

//@@author cyanoei
Expand Down Expand Up @@ -84,6 +85,18 @@ public String toString() {
return getMatricNo() + " loaned " + getQuantity() + " of " + getStockCode() + ".";
}

/**
* Gets the data of this stock as an arraylist.
*/
public ArrayList<String> getStockDataAsArray() {
ArrayList<String> data = new ArrayList<>();

data.add(stockCode);
data.add(String.valueOf(quantity));

return data;
}

//@@author


Expand Down