From 57cce3a1d8d45f66a6803b69aaefb9e6801cdc79 Mon Sep 17 00:00:00 2001
From: Nicolas Fley <nicolas.fley@student.ecp.fr>
Date: Wed, 5 Apr 2017 16:51:30 +0200
Subject: [PATCH] Dish and Meal logic added

---
 .gitignore                |  1 +
 README.md                 |  5 +++
 src/Cli/Confirm.java      |  2 +-
 src/Item/Dessert.java     |  8 ++++
 src/Item/Dish.java        | 29 +++++++++++--
 src/Item/FactoryDish.java | 16 ++++++++
 src/Item/FullMeal.java    | 11 +++++
 src/Item/HalfMeal.java    | 11 +++++
 src/Item/Item.java        | 20 ++++-----
 src/Item/MainDish.java    |  8 ++++
 src/Item/Meal.java        | 86 +++++++++++++++++++++++++--------------
 src/Item/MealFactory.java | 16 ++++++++
 src/Item/Starter.java     | 10 +++++
 13 files changed, 178 insertions(+), 45 deletions(-)
 create mode 100644 README.md
 create mode 100644 src/Item/Dessert.java
 create mode 100644 src/Item/FactoryDish.java
 create mode 100644 src/Item/FullMeal.java
 create mode 100644 src/Item/HalfMeal.java
 create mode 100644 src/Item/MainDish.java
 create mode 100644 src/Item/MealFactory.java
 create mode 100644 src/Item/Starter.java

diff --git a/.gitignore b/.gitignore
index 460fbf4..239de2d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
 /bin/
 /src/provisoire/
+/rapport/
 .*
 !.gitignore
\ No newline at end of file
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..d91abbe
--- /dev/null
+++ b/README.md
@@ -0,0 +1,5 @@
+# Foodora Java Project
+This is the IS1220 2017 project, sources and documentation are in french.
+## Contributors
++ Fley Nicolas
++ Ravoux Corentin
diff --git a/src/Cli/Confirm.java b/src/Cli/Confirm.java
index 3911b1d..abd707b 100644
--- a/src/Cli/Confirm.java
+++ b/src/Cli/Confirm.java
@@ -3,7 +3,7 @@ package Cli;
 import java.util.Scanner;
 
 public class Confirm {
-	Scanner reader = new Scanner(System.in);
+	static Scanner reader = new Scanner(System.in);
 	public static boolean text(String str){
 		System.out.println(str);
 		System.out.println("(Y)es/(N)o : ");
diff --git a/src/Item/Dessert.java b/src/Item/Dessert.java
new file mode 100644
index 0000000..a1e74ac
--- /dev/null
+++ b/src/Item/Dessert.java
@@ -0,0 +1,8 @@
+package Item;
+
+public class Dessert extends Dish {
+	public Dessert(String name, String typeOfFood, double _price) {
+		super(name, typeOfFood, _price);
+		this.setTypeInMeal("Dessert");
+	}
+}
diff --git a/src/Item/Dish.java b/src/Item/Dish.java
index 5df2a48..b6669d4 100644
--- a/src/Item/Dish.java
+++ b/src/Item/Dish.java
@@ -2,12 +2,33 @@ package Item;
 
 public class Dish extends Item implements VisitablePrice{
 	
-	public Dish(String name, String type) {
-		super(name, type);
-	}
-
+	private String typeInMeal;
 	private double price;
 	
+	/** CONSTURCTOR **/
+	
+	public Dish(String name, String typeOfFood, double _price) {
+		super(name, typeOfFood);
+		price = _price;
+	}
+	
+	/** GETTER AND SETTER **/
+	
+	public String getTypeInMeal() {
+		return typeInMeal;
+	}
+	protected void setTypeInMeal(String _typeInMeal) {
+		this.typeInMeal = _typeInMeal;
+	}
+	public double getPrice() {
+		return price;
+	}
+	public void setPrice(double _price) {
+		this.price = _price;
+	}
+	
+	/** VISIT LOGIC **/
+	
 	public double acceptPrice(VisitorPrice v) {
 		return v.visitPrice(this);
 	}
diff --git a/src/Item/FactoryDish.java b/src/Item/FactoryDish.java
new file mode 100644
index 0000000..e1da97d
--- /dev/null
+++ b/src/Item/FactoryDish.java
@@ -0,0 +1,16 @@
+package Item;
+
+public class FactoryDish {
+	public Dish createDishFactory(String typeInMeal, String _name, String _typeOfFood, double _price){
+		typeInMeal=typeInMeal.toLowerCase().trim();
+		switch(typeInMeal){
+		case "dessert":
+			return new Dessert(_name,_typeOfFood,_price);
+		case "maindish":
+			return new MainDish(_name,_typeOfFood,_price);
+		case "starter":
+			return new Starter(_name,_typeOfFood,_price);
+		}
+		return null;
+	}
+}
diff --git a/src/Item/FullMeal.java b/src/Item/FullMeal.java
new file mode 100644
index 0000000..04ec798
--- /dev/null
+++ b/src/Item/FullMeal.java
@@ -0,0 +1,11 @@
+package Item;
+
+public class FullMeal extends Meal {
+
+	public FullMeal(String name, String typeOfFood) {
+		super(name, typeOfFood);
+		String [][] pattern = {{"Starter","Dessert"},{"MainDish"}};
+		this.setPatternTypeInMeal(pattern);
+	}
+
+}
diff --git a/src/Item/HalfMeal.java b/src/Item/HalfMeal.java
new file mode 100644
index 0000000..d68d5c4
--- /dev/null
+++ b/src/Item/HalfMeal.java
@@ -0,0 +1,11 @@
+package Item;
+
+public class HalfMeal extends Meal {
+
+	public HalfMeal(String name, String typeOfFood) {
+		super(name, typeOfFood);
+		String [][] pattern = {{"Starter","Dessert"},{"MainDish"}};
+		this.setPatternTypeInMeal(pattern);
+	}
+
+}
diff --git a/src/Item/Item.java b/src/Item/Item.java
index 9c2b111..5801011 100644
--- a/src/Item/Item.java
+++ b/src/Item/Item.java
@@ -2,15 +2,15 @@ package Item;
 
 public abstract class Item implements VisitablePrice {
 	
-	String type;
+	String typeOfFood;
 	String name;
 	
 	/** CONSTRUCTOR **/
 	
-	public Item(String name,String type) {
+	public Item(String _name,String _typeOfFood) {
 		super();
-		this.type = type;
-		this.name = name;
+		this.typeOfFood = _typeOfFood;
+		this.name = _name;
 	}
 	
 	/** METHODS **/
@@ -18,16 +18,16 @@ public abstract class Item implements VisitablePrice {
 	
 	/** GETTER & SETTER **/
 	
-	public String getType() {
-		return type;
+	public String getTypeOfFood() {
+		return typeOfFood;
 	}
-	public void setType(String type) {
-		this.type = type;
+	public void setTypeOfFood(String _typeOfFood) {
+		this.typeOfFood = _typeOfFood;
 	}
 	public String getName() {
 		return name;
 	}
-	public void setName(String name) {
-		this.name = name;
+	public void setName(String _name) {
+		this.name = _name;
 	}
 }
diff --git a/src/Item/MainDish.java b/src/Item/MainDish.java
new file mode 100644
index 0000000..b5d8bcd
--- /dev/null
+++ b/src/Item/MainDish.java
@@ -0,0 +1,8 @@
+package Item;
+
+public class MainDish extends Dish {
+	public MainDish(String name, String typeOfFood, double _price) {
+		super(name, typeOfFood, _price);
+		this.setTypeInMeal("MainDish");
+	}
+}
diff --git a/src/Item/Meal.java b/src/Item/Meal.java
index eabbafe..e1ae4ef 100644
--- a/src/Item/Meal.java
+++ b/src/Item/Meal.java
@@ -2,48 +2,62 @@ package Item;
 
 import java.util.ArrayList;
 
-public class Meal extends Item implements VisitorPrice{
+
+/**
+ * 
+ * This Meal class is created by the use of a pattern given in the constructor of
+ * subClasses, this pattern must consist on an array, each case contain an obligatory
+ * typeOfMeal (starter,mainDish...), if the user have the choice between two typeOfMeal
+ * it can be implemented by using a subArray.
+ * For exemple, the patterns below show how a full meal may be created and then a half meal :
+ * fullMealPattern={{"Starter"},{"MainDish"},{"Dessert"}}
+ * halfMealPattern={{"Starter","Dessert"},{"MainDish"}}
+ * 
+ * We may create a children meal very easily :
+ * chlidrenMealPattern={{"MainDish"},{"Dessert"},{"Drink"}}
+ *
+ */
+public abstract class Meal extends Item implements VisitorPrice{
 	
 	ArrayList<Dish> listDish = new ArrayList<Dish>();
+	String [][] patternTypeInMeal;
 	
 	private double price=0;
 	
-	public Meal(String name,String type) {
-		super(name,type);
+	/** CONSTRUCTORS **/
+	
+	public Meal(String name,String typeOfFood) {
+		super(name,typeOfFood);
 	}
 	
-	public void addDish(ArrayList<Dish> listDishRestaurant, String name){
-		for(Dish dish: listDishRestaurant){
-			if(dish.getName()==name)
-				System.out.println("Adding "+dish.toString()+" to meal : "+this.getName());
-				if(!listDish.contains(dish)){
-					listDish.add(dish);
-					System.out.println("Dish added.");
-				}
-				else{
-					System.out.println("This dish is in this menu.");
-					if(Cli.Confirm.text("Do you really want to add "+dish.toString()+" ?")){
-						listDish.add(dish);
-						System.out.println("Dish added.");
+	/** add and remove dishes **/
+	
+	public void addDish(Dish dish){
+		if(dish.getTypeOfFood() != this.getTypeOfFood()){
+			System.out.println("Trying to add "+dish.getTypeOfFood()+" in "+this.getTypeOfFood()+" menu, resulted in error.");
+		}else{
+			int idInMenu=-1;
+			int idInMenuInc=0;
+			for(String[] tabOfType : this.getPatternTypeInMeal()){
+				for(String typeFromPatternType : tabOfType){
+					if(typeFromPatternType==dish.getTypeInMeal()){
+						idInMenu=idInMenuInc;
 					}
 				}
-		}
-	}
-	public void addDish(ArrayList<Dish> listDishRestaurant, int num){
-		Dish dish = listDishRestaurant.get(num);
-		System.out.println("Adding "+dish.toString()+" to meal : "+this.getName());
-		if(dish.getName()==name)
-			if(!listDish.contains(dish)){
-				listDish.add(dish);
-				System.out.println("Dish added.");
+				idInMenuInc+=1;
 			}
-			else{
-				System.out.println("This dish is in this menu.");
-				if(Cli.Confirm.text("Do you really want to add "+dish.toString()+" ?")){
-					listDish.add(dish);
-					System.out.println("Dish added.");
+			if(idInMenu>-1){
+				if(listDish.get(idInMenu)==null){
+					listDish.set(idInMenu, dish);
+				}
+				else{
+					if(Cli.Confirm.text("This dish : "+listDish.get(idInMenu).toString()+" exists already. Do you want to replace it ?))")){
+						listDish.remove(idInMenu);
+						listDish.add(idInMenu, dish);
+					}
 				}
 			}
+		}
 	}
 	
 	public void removeDish(String name){
@@ -69,9 +83,21 @@ public class Meal extends Item implements VisitorPrice{
 			}
 	}
 	
+	/** GETTER AND SETTER **/
+	
 	public double getPrice(){
 		return price;
 	}
+	public ArrayList<Dish> getListDish() {
+		return listDish;
+	}
+	public String[][] getPatternTypeInMeal() {
+		return patternTypeInMeal;
+	}
+
+	protected void setPatternTypeInMeal(String[][] patternTypeInMeal) {
+		this.patternTypeInMeal = patternTypeInMeal;
+	}
 
 	/** VISIT LOGIC **/
 
diff --git a/src/Item/MealFactory.java b/src/Item/MealFactory.java
new file mode 100644
index 0000000..2e78188
--- /dev/null
+++ b/src/Item/MealFactory.java
@@ -0,0 +1,16 @@
+package Item;
+
+public class MealFactory {
+
+	public Meal createMeal(String typeOfMeal, String _name, String _typeOfFood){
+		typeOfMeal=typeOfMeal.toLowerCase().trim();
+		switch(typeOfMeal){
+		case "halfmeal":
+			return new HalfMeal(_name,_typeOfFood);
+		case "fullmeal":
+			return new FullMeal(_name,_typeOfFood);
+		}
+		return null;
+	}
+
+}
diff --git a/src/Item/Starter.java b/src/Item/Starter.java
new file mode 100644
index 0000000..8c395e9
--- /dev/null
+++ b/src/Item/Starter.java
@@ -0,0 +1,10 @@
+package Item;
+
+public class Starter extends Dish {
+
+	public Starter(String name, String typeOfFood, double _price) {
+		super(name, typeOfFood, _price);
+		this.setTypeInMeal("Starter");
+	}
+
+}
-- 
GitLab