Переглянути джерело

class fixes + Model methods

dominik.rieder 8 роки тому
батько
коміт
f1f983f815

BIN
bin/exceptions/newException1.class


BIN
bin/tests/Tests1.class


BIN
bin/ui/controller/IdCounter.class


BIN
bin/ui/model/Model.class


BIN
bin/ui/view/GUI$1.class


BIN
bin/ui/view/GUI$2.class


BIN
bin/ui/view/GUI.class


BIN
bin/ui/view/MyCanvas.class


+ 3 - 1
src/tests/Tests1.java

@@ -16,6 +16,8 @@ public class Tests1 {
 		HolonObject b2 = new HolonObject();
 		HolonSwitch s1 = new HolonSwitch();
 		HolonTransformer t1 = new HolonTransformer();
-		HolonElement g1 = new HolonElement();
+		b1.setPos(20, 30);
+		Position p = b1.getPos();
+		System.out.println(p.x + "," + p.y);
 	}
 }

+ 16 - 21
src/ui/controller/CpsObject.java

@@ -1,19 +1,20 @@
 package ui.controller;
 
+import java.util.ArrayList;
+
 abstract class CpsObject {
 	/* Type of the Object */
 	String objName;
 	/* Name given by the user */
 	String name;
 	/* ID of the Obj. */
-	int ID;
+	int id;
 	/* Path of the image for the Obj. */
 	String image;
 	/* Array of neighbors */
-	CpsObject[] connectedTo;
+	ArrayList<CpsObject> connectedTo;
 	/* Position with a X and Y value */
-	int xCoord;
-	int yCoord;
+	Position position;
 	/*Energy input and output of each object in the grid*/
 	float energyIn;
 	float energyOut;
@@ -22,7 +23,8 @@ abstract class CpsObject {
 	 * Constructor for an CpsObejct with an unique ID
 	 */
 	public CpsObject() {
-		ID = IdCounter.nextId();
+		connectedTo = new ArrayList<CpsObject>();
+		position = new Position();
 	}
 	
 	/* Obj type */
@@ -45,7 +47,7 @@ abstract class CpsObject {
 
 	/* Unique ID number */
 	public int getID() {
-		return ID;
+		return id;
 	}
 
 	/* Image path */
@@ -58,29 +60,22 @@ abstract class CpsObject {
 	}
 
 	/* Neighbors array */
-	public CpsObject[] getConnectedTo() {
+	public ArrayList<CpsObject> getConnectedTo() {
 		return connectedTo;
 	}
 
-	public void setConnectedTo(CpsObject[] connectedTo) {
-		this.connectedTo = connectedTo;
+	public void AddConnection(CpsObject toConnect) {
+		connectedTo.add(toConnect);
 	}
 
 	/* Position (x and Y) */
-	public int getxCoord() {
-		return xCoord;
-	}
-
-	public void setxCoord(int xCoord) {
-		this.xCoord = xCoord;
-	}
-
-	public int getyCoord() {
-		return yCoord;
+	public void setPos(int x, int y){
+		position.x = x;
+		position.y = y;
 	}
 
-	public void setyCoord(int yCoord) {
-		this.yCoord = yCoord;
+	public Position getPos(){
+		return position;
 	}
 	
 	/*Getter and Setters for the energy input and output*/

+ 6 - 6
src/ui/controller/HolonElement.java

@@ -13,12 +13,11 @@ public class HolonElement {
 	/* Total Energy */
 	float totalEnergy;
 
-	public HolonElement() {
-		setName("TV");
-		setAmount(1);
-		setEnergy(10);
+	public HolonElement(String name, int amount, float energy) {
+		this.name = name;
+		this.amount = amount;
+		this.energy = energy;
 		this.isWorking = true;
-		this.totalEnergy = getTotalEnergy();
 		System.out.println("You create some " + name + "'s. The amount is:" + amount);
 		System.out.println("It's actual status is: " + isWorking);
 	}
@@ -63,6 +62,7 @@ public class HolonElement {
 	 * @return totalEnergy (actual)
 	 */
 	public float getTotalEnergy() {
-		return 0;
+		totalEnergy = ((float)amount)*energy;
+		return totalEnergy;
 	}
 }

+ 26 - 7
src/ui/controller/HolonObject.java

@@ -1,13 +1,15 @@
 package ui.controller;
 
+import java.util.ArrayList;
+
 public class HolonObject extends CpsObject {
 	
 	/*Array of all consumers*/
-	HolonElement[] consumers;
+	private ArrayList<HolonElement> consumers;
 	/*Array of all producers*/
-	HolonElement[] producers;
+	private ArrayList<HolonElement> producers;
 	/*Total of consumption*/
-	float currentEnergy;
+	private float currentEnergy;
 	/**
 	 * State of the building: 
 	 * 0 = fully supplied (currentEnergy == 0)
@@ -21,10 +23,27 @@ public class HolonObject extends CpsObject {
 	 * Set by default the name of the object equals to the category name, until the user changes it.
 	 */
 	public HolonObject() {
-		super.setObjName("House");
-		super.name = super.getObjName();
-		System.out.println("You create a " + super.objName + " with ID " + super.ID);
-		System.out.println("It's actual name is: " + super.name);
+		super();
+	}
+	
+	public void addConsumer(HolonElement consumer){
+		consumers.add(consumer);
+	}
+	
+	public void addProducer(HolonElement producer){
+		producers.add(producer);
+	}
+	
+	public void deleteConsumer(int idx){
+		consumers.remove(idx);
+	}
+	
+	public void deleteProducer(int idx){
+		producers.remove(idx);
+	}
+	
+	public float calculateCurrentEnergy(){
+		return currentEnergy;
 	}
 	
 }

+ 5 - 7
src/ui/controller/HolonSwitch.java

@@ -5,18 +5,16 @@ public class HolonSwitch extends CpsObject {
 	boolean isWorking;
 
 	public HolonSwitch() {
-		super.setObjName("Switch");
-		super.name = super.getObjName();
-		System.out.println("You create a " + super.objName + " with ID " + super.ID);
-		System.out.println("It's actual name is: " + super.name);
+		super();
+		isWorking = false;
 	}
 	
-	public void switchState(boolean desired_state) {
-		this.isWorking = desired_state;
+	public void switchState() {
+		this.isWorking = !isWorking;
 	}
 	
 	
-	public boolean getTransformRatio() {
+	public boolean getStates() {
 		return this.isWorking;
 	}
 }

+ 1 - 5
src/ui/controller/HolonTransformer.java

@@ -10,11 +10,7 @@ public class HolonTransformer extends CpsObject {
 	 * default name ("Transformer");
 	 */
 	public HolonTransformer() {
-		super.setObjName("Transformer");
-		setTransformRatio(100);
-		super.name = super.getObjName();
-		System.out.println("You create a " + super.objName + " with ID " + super.ID);
-		System.out.println("It's actual name is: " + super.name);
+		super();
 	}
 
 	/**

+ 27 - 1
src/ui/model/Model.java

@@ -1,5 +1,31 @@
 package ui.model;
 
-public class Model {
+import java.util.ArrayList;
+
+import ui.controller.*;
 
+public class Model {
+	private ArrayList<Category> Categories;
+	private ArrayList<HolonObject> ObjectsOnCanvas;
+	
+	public Model(){
+		Categories = new ArrayList<Category>();
+		ObjectsOnCanvas = new ArrayList<HolonObject>();
+	}
+	
+	public void addCategory(Category toAdd){
+		Categories.add(toAdd);
+	}
+	
+	public void deleteCategory(int idx){
+		Categories.remove(idx);
+	}
+	
+	public void addObject(HolonObject toAdd){
+		ObjectsOnCanvas.add(toAdd);
+	}
+	
+	public void deleteObject(int idx){
+		ObjectsOnCanvas.remove(idx);
+	}
 }