From 41a1c651eec09793899490cdb8dc7214286f044b Mon Sep 17 00:00:00 2001 From: Nicolas Fley <nicolas.fley@student.ecp.fr> Date: Fri, 28 Apr 2017 18:05:18 +0200 Subject: [PATCH] command logic added --- src/Cli/Command.java | 47 ++++++++++++++++++++++++++++ src/Cli/Token.java | 50 ++++++++++++++++++++++++++++++ src/Cli/errorWrongFormatValue.java | 34 ++++++++++++++++++++ src/Core/ActiveUserContext.java | 5 +-- src/Core/MyFoodora.java | 2 +- 5 files changed, 133 insertions(+), 5 deletions(-) create mode 100644 src/Cli/Command.java create mode 100644 src/Cli/Token.java create mode 100644 src/Cli/errorWrongFormatValue.java diff --git a/src/Cli/Command.java b/src/Cli/Command.java new file mode 100644 index 0000000..00b84f6 --- /dev/null +++ b/src/Cli/Command.java @@ -0,0 +1,47 @@ +package Cli; + +import java.util.ArrayList; + +import User.User; + +public abstract class Command { + private String commandName; + private Token [] listOfArgs; + private User [] typeOfInstanceAllowed; + + public Command(String _commandName, Token [] _listOfArgs, User [] _typeOfInstanceAllowed){ + commandName = _commandName; + listOfArgs = _listOfArgs; + typeOfInstanceAllowed = _typeOfInstanceAllowed; + } + + public boolean isOneWanted(String firstToken){ + if(firstToken == commandName) + return true; + return false; + } + + public boolean isGoodArgument(ArrayList<String> arguments, User typeOfUser){ + if(arguments.size() != listOfArgs.length){ + return false; // devrait throw une erreur disant que c'est pas le bon nombre de param�tre. + } + boolean goodUserType=false; + for (User typeUser : typeOfInstanceAllowed){ + if(typeUser.getClass() == typeOfUser.getClass()){ + goodUserType=true; + } + } + if(!goodUserType) + return false; // devrait throw une erreur + int i = 0; + for (String arg : arguments) { + listOfArgs[i].isTockenCorrect(arg); // throw une erreur, faut la catch au dessus ! + i++; + } + // trow une erreur + return true; + } + + public abstract String execute(); + +} diff --git a/src/Cli/Token.java b/src/Cli/Token.java new file mode 100644 index 0000000..66b6f4e --- /dev/null +++ b/src/Cli/Token.java @@ -0,0 +1,50 @@ +package Cli; + +public class Token { + + private TypeToken typeOfToken; + private String name; + private String value; + + public Token(TypeToken _typeOfToken, String _name){ + typeOfToken = _typeOfToken; + name = _name; + value = ""; + } + + public void setValue(String _value) throws errorWrongFormatValue{ + if(isTockenCorrect(_value)){ + value = _value; + }else{ + throw new errorWrongFormatValue("Format de "+name+" incorrect !"); + /* si incorrect, throw une erreur qui doit �tre catch + par la fonction au dessus (et on doit annuler la commande */ + } + } + + public String getValue(){ + return value; + } + + public boolean isTockenCorrect(String _value){ + switch(typeOfToken){ + case date: + break; + case integer: + if(_value.matches("-?\\d+")) + return true; + break; + case decimal: + if(_value.matches("-?\\d+(\\.\\d+)?")) + return true; + break; + case str: + return true; + } + return false; + } + + static enum TypeToken{ + date,str,integer,decimal + } +} diff --git a/src/Cli/errorWrongFormatValue.java b/src/Cli/errorWrongFormatValue.java new file mode 100644 index 0000000..0959034 --- /dev/null +++ b/src/Cli/errorWrongFormatValue.java @@ -0,0 +1,34 @@ +package Cli; + +public class errorWrongFormatValue extends Exception { + + /** + * + */ + private static final long serialVersionUID = 7873997091472426895L; + + public errorWrongFormatValue() { + // TODO Auto-generated constructor stub + } + + public errorWrongFormatValue(String arg0) { + super(arg0); + // TODO Auto-generated constructor stub + } + + public errorWrongFormatValue(Throwable arg0) { + super(arg0); + // TODO Auto-generated constructor stub + } + + public errorWrongFormatValue(String arg0, Throwable arg1) { + super(arg0, arg1); + // TODO Auto-generated constructor stub + } + + public errorWrongFormatValue(String arg0, Throwable arg1, boolean arg2, boolean arg3) { + super(arg0, arg1, arg2, arg3); + // TODO Auto-generated constructor stub + } + +} diff --git a/src/Core/ActiveUserContext.java b/src/Core/ActiveUserContext.java index 497c8fb..ec5a8fa 100644 --- a/src/Core/ActiveUserContext.java +++ b/src/Core/ActiveUserContext.java @@ -5,16 +5,13 @@ import User.User; public class ActiveUserContext { User activeUser; - public ActiveUserContext(MyFoodora foodora) { // TODO Auto-generated constructor stub - } - + } public User getUser(){ return activeUser; } public void setActiveUser(User u){ activeUser = u; } - } diff --git a/src/Core/MyFoodora.java b/src/Core/MyFoodora.java index 85388fc..8db5939 100644 --- a/src/Core/MyFoodora.java +++ b/src/Core/MyFoodora.java @@ -52,7 +52,7 @@ public class MyFoodora { public MyFoodora() throws ExceptionUnknownStartegyType{ this.contextTargetProfitStrategy = new ContextTargetProfitStrategy("DeliveryCost"); - this.contextDeliveryPolicy = new ContextDeliveryStrategy("FairOccupation"); + this.contextDeliveryPolicy = new ContextDeliveryStrategy("DeliveryCost"); this.listRestaurant = new ListUser<Restaurant>(); this.listCustomer = new ListUser<Customer>(); this.listManager = new ListUser<Manager>(); -- GitLab