From e4a20f0bc9c01814cfa8185fda281119fe1db972 Mon Sep 17 00:00:00 2001 From: Bastien Jacquelin Date: Tue, 31 Jan 2023 08:47:34 +0100 Subject: [PATCH] add Exceptions --- Source/Console/Console.html | 2 +- Source/Console/emptyParameterException.js | 5 +++++ Source/Console/{test.js => main.js} | 8 +++++++- Source/Model/Card.js | 14 +++++++++++++- Source/Model/Card5.js | 6 +++++- 5 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 Source/Console/emptyParameterException.js rename Source/Console/{test.js => main.js} (54%) diff --git a/Source/Console/Console.html b/Source/Console/Console.html index 05ac367..7e24bca 100644 --- a/Source/Console/Console.html +++ b/Source/Console/Console.html @@ -15,7 +15,7 @@ - + \ No newline at end of file diff --git a/Source/Console/emptyParameterException.js b/Source/Console/emptyParameterException.js new file mode 100644 index 0000000..f1a7346 --- /dev/null +++ b/Source/Console/emptyParameterException.js @@ -0,0 +1,5 @@ +class EmptyParamaterException extends Error{ + constructor(field){ + super(`Field ${field} missing`); + } +} \ No newline at end of file diff --git a/Source/Console/test.js b/Source/Console/main.js similarity index 54% rename from Source/Console/test.js rename to Source/Console/main.js index 8d750e4..34f3375 100644 --- a/Source/Console/test.js +++ b/Source/Console/main.js @@ -4,8 +4,14 @@ console.log("~#Test#~"); let card4 = new Card('red','2','losange','full'); +console.group('Carte 4 attributes'); console.log(`carte de 4 elements : ${card4.color}`); +console.groupEnd(); let card5 = new Card5('blue','2','losange','full','pointillet'); +console.group('Carte 5 attributes'); console.log(`carte de 5 elements : ${card5.outline}`); console.log(`carte de 5 éléments instance de 5: ${card5 instanceof Card5}`); -console.log(`carte de 5 éléments accès par méthode : ${card5.getAttributes()[0]}`); +console.log(`carte de 5 éléments accès par méthode idx 0: ${card5.getAttributes()[0]}`); +console.log(`carte de 5 éléments accès par méthode idx 4: ${card5.getAttributes()[4]}`); +let errCard = new Card5('blue','','losange','full','pointillet'); +console.groupEnd(); diff --git a/Source/Model/Card.js b/Source/Model/Card.js index 5b4aef9..d7da58b 100644 --- a/Source/Model/Card.js +++ b/Source/Model/Card.js @@ -1,5 +1,17 @@ class Card{ constructor(color, number, shape, filling){ + if(!color){ + throw new EmptyParamaterException('Color'); + } + if(!number){ + throw new EmptyParamaterException('Number'); + } + if(!shape){ + throw new EmptyParamaterException('Shape'); + } + if(!filling){ + throw new EmptyParamaterException('Filling'); + } this.color=color; this.number=number; this.shape=shape; @@ -15,7 +27,7 @@ class Card{ * @author Bastien Jacquelin */ getAttributes(){ - return [this.color,this.number,this.shape,this.filling,null]; + return [this.color,this.number,this.shape,this.filling]; } }//export {Card} \ No newline at end of file diff --git a/Source/Model/Card5.js b/Source/Model/Card5.js index fc091fe..b9d7894 100644 --- a/Source/Model/Card5.js +++ b/Source/Model/Card5.js @@ -3,6 +3,9 @@ class Card5 extends Card { constructor(color, number, shape, filling, outline){ super(color,number,shape,filling); + if(!outline){ + throw new EmptyParamaterException('Outline'); + } this.outline=outline; } /** @@ -15,7 +18,8 @@ class Card5 extends Card { * @author Bastien Jacquelin */ getAttributes(){ - return [this.color,this.number,this.shape,this.filling,this.outline]; + // return [this.color,this.number,this.shape,this.filling,this.outline]; + return super.getAttributes().concat(this.outline); } } // export {Card5}; \ No newline at end of file