-
Notifications
You must be signed in to change notification settings - Fork 0
Assignment02b #2
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
base: master
Are you sure you want to change the base?
Changes from all commits
a4c6b0f
81ef6aa
98d8eb0
b844aa7
3076fca
8e53fd3
e95e55d
829feda
abb630e
e52e37e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| 0 5 true | ||
| 1 8 true | ||
| 1 10 false |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <groupId>org.jacoco</groupId> | ||
| <artifactId>org.jacoco.examples.maven</artifactId> | ||
| <version>0.7.1.201405082137</version> | ||
| <packaging>jar</packaging> | ||
| <name>JaCoCo Maven plug-in example</name> | ||
| <url>http://www.eclemma.org/jacoco</url> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <version>4.11</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| <properties> | ||
| <maven.compiler.source>1.8</maven.compiler.source> | ||
| <maven.compiler.target>1.8</maven.compiler.target> | ||
| </properties> | ||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.jacoco</groupId> | ||
| <artifactId>jacoco-maven-plugin</artifactId> | ||
| <version>0.7.1.201405082137</version> | ||
| <executions> | ||
| <execution> | ||
| <id>default-prepare-agent</id> | ||
| <goals> | ||
| <goal>prepare-agent</goal> | ||
| </goals> | ||
| </execution> | ||
| <execution> | ||
| <id>default-report</id> | ||
| <phase>prepare-package</phase> | ||
| <goals> | ||
| <goal>report</goal> | ||
| </goals> | ||
| </execution> | ||
| <execution> | ||
| <id>default-check</id> | ||
| <goals> | ||
| <goal>check</goal> | ||
| </goals> | ||
| <configuration> | ||
| <rules> | ||
| <!-- implementation is needed only for Maven 2 --> | ||
| <rule implementation="org.jacoco.maven.RuleConfiguration"> | ||
| <element>BUNDLE</element> | ||
| <limits> | ||
| <!-- implementation is needed only for Maven 2 --> | ||
| <limit implementation="org.jacoco.report.check.Limit"> | ||
| <counter>COMPLEXITY</counter> | ||
| <value>COVEREDRATIO</value> | ||
| <minimum>0.90</minimum> | ||
| </limit> | ||
| </limits> | ||
| </rule> | ||
| </rules> | ||
| </configuration> | ||
| </execution> | ||
| </executions> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package com.cnu2016.assignment02; | ||
|
|
||
| public class Appliance { | ||
| public enum Type { | ||
| WATERHEATER, | ||
| AIRCONDITIONER, | ||
| COOKINGOVEN | ||
| }; | ||
| private Type type; | ||
| private boolean state; | ||
| private int ID; | ||
| static int cnt = 0; | ||
| public Appliance(Type type) { | ||
| this.state = false; | ||
| this.ID = cnt++; | ||
| this.type = type; | ||
| } | ||
| public boolean getState() { | ||
| return state; | ||
| } | ||
| public void start() { | ||
| state = true; | ||
| } | ||
| public void stop() { | ||
| state = false; | ||
| } | ||
| public int getID() { | ||
| return ID; | ||
| } | ||
| public Type getType() { | ||
| return type; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.cnu2016.assignment02; | ||
|
|
||
| public class BadScheduleException extends Exception { | ||
| public BadScheduleException(String msg) { | ||
| super(msg); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.cnu2016.assignment02; | ||
|
|
||
| public class Event { | ||
| int timeStamp; | ||
| Appliance appliance; | ||
| public Event(int timeStamp, Appliance appliance) { | ||
| this.timeStamp = timeStamp; | ||
| this.appliance = appliance; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package com.cnu2016.assignment02; | ||
| import java.util.*; | ||
| import java.io.*; | ||
| public class EventGenerator { | ||
| public static ArrayList<Event> readData(String filename, ArrayList<Appliance> appliances) throws IOException, BadScheduleException { | ||
|
|
||
| boolean state[] = new boolean[appliances.size()]; | ||
| int tim[] = new int[appliances.size()]; | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use Lists instead of arrays |
||
| ArrayList<Event> events = new ArrayList<Event>(); | ||
| Scanner in = new Scanner(new FileReader(filename)); | ||
| while(in.hasNext()) { | ||
| int ID = in.nextInt(); | ||
| int startTime = in.nextInt(); | ||
| boolean newState = in.nextBoolean(); | ||
| if (state[ID] == newState) { | ||
| in.close(); | ||
| throw new BadScheduleException("Please enter a valid schedule"); | ||
| } | ||
| else { | ||
| state[ID] = newState; | ||
| tim[ID] = startTime; | ||
| } | ||
| events.add(new Event(startTime, appliances.get(ID))); | ||
| } | ||
| //Explicity shut off all water heaters which were left on | ||
| for (int i = 0; i < appliances.size(); ++i) { | ||
| if (appliances.get(i).getType() == Appliance.Type.WATERHEATER && state[i] == true) { | ||
| events.add(new Event(tim[i] + 2, appliances.get(i))); | ||
| } | ||
| } | ||
| Collections.sort(events, new Comparator<Event>() { | ||
| public int compare(Event a, Event b) { | ||
| return a.timeStamp - b.timeStamp; | ||
| } | ||
| }); | ||
| in.close(); | ||
| return events; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package com.cnu2016.assignment02; | ||
| import java.io.*; | ||
| import java.util.*; | ||
| public class Main { | ||
| public static ArrayList<Appliance> populate() { | ||
| ArrayList<Appliance> appliances = new ArrayList<Appliance>(); | ||
| appliances.add(new Appliance(Appliance.Type.WATERHEATER)); | ||
| appliances.add(new Appliance(Appliance.Type.AIRCONDITIONER)); | ||
| appliances.add(new Appliance(Appliance.Type.COOKINGOVEN)); | ||
| appliances.add(new Appliance(Appliance.Type.WATERHEATER)); | ||
| return appliances; | ||
| } | ||
| public static void main(String args[]) throws IOException, BadScheduleException { | ||
| ArrayList<Appliance> appliances = populate(); | ||
| ArrayList<Event> events = EventGenerator.readData("/projects/assignments/smartHome/input.txt", appliances); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| Timer timer = new Timer(); | ||
| events.forEach( e -> { | ||
| timer.schedule(new TimerTask() { | ||
| @Override | ||
| public void run() { | ||
| if(e.appliance.getState() == true) { | ||
| e.appliance.stop(); | ||
| } | ||
| else { | ||
| e.appliance.start(); | ||
| } | ||
| System.out.printf("At time %d\n", e.timeStamp); | ||
| appliances.forEach( a -> { | ||
| System.out.printf("%d %b\n", a.getID(), a.getState()); | ||
| }); | ||
| } | ||
| }, e.timeStamp * 1000); | ||
| }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should be an Asynchronous call of a method which starts the Timer |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.cnu2016.assignment02; | ||
| import org.junit.Test; | ||
| import static org.junit.Assert.assertEquals; | ||
| public class ApplicanceTest { | ||
| @Test | ||
| public void testBasics() { | ||
| Appliance a = new Appliance(Appliance.Type.WATERHEATER); | ||
| Appliance b = new Appliance(Appliance.Type.COOKINGOVEN); | ||
| assertEquals(a.getID() + 1,b.getID()); | ||
| assertEquals(a.getType(), Appliance.Type.WATERHEATER); | ||
| assertEquals(b.getType(), Appliance.Type.COOKINGOVEN); | ||
| assertEquals(a.getState(), false); | ||
| a.start(); | ||
| assertEquals(a.getState(), true); | ||
| a.stop(); | ||
| assertEquals(a.getState(), false); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package com.cnu2016.assignment02; | ||
| import java.util.*; | ||
| import java.io.*; | ||
| import org.junit.Test; | ||
| import static org.junit.Assert.assertEquals; | ||
| public class EventGeneratorTest { | ||
| @Test | ||
| public void testWaterHeaterAutoShutOff() throws IOException, BadScheduleException { | ||
| ArrayList<Appliance> appliances = new ArrayList<Appliance>(); | ||
| appliances.add(new Appliance(Appliance.Type.WATERHEATER)); | ||
| String filename = "/projects/assignments/smartHome/src/test/java/testData/testWaterHeaterAutoShutOff.txt"; | ||
| ArrayList<Event> ret = EventGenerator.readData(filename, appliances); | ||
| assertEquals(ret.size(), 2); | ||
| assertEquals(ret.get(0).timeStamp, 2); | ||
| assertEquals(ret.get(1).timeStamp, 4); | ||
| } | ||
| @Test | ||
| public void testBadSchedule() throws IOException { | ||
| ArrayList<Appliance> appliances = new ArrayList<Appliance>(); | ||
| appliances.add(new Appliance(Appliance.Type.WATERHEATER)); | ||
| String filename = "/projects/assignments/smartHome/src/test/java/testData/testBadScheduleException.txt"; | ||
| try { | ||
| ArrayList<Event> ret = EventGenerator.readData(filename, appliances); | ||
| assertEquals(true, false); | ||
| } | ||
| catch (Exception e) { | ||
| boolean check = (e instanceof BadScheduleException); | ||
| assertEquals(check, true); | ||
| } | ||
| } | ||
| @Test | ||
| public void testGoodSchedule() throws IOException { | ||
| ArrayList<Appliance> appliances = new ArrayList<Appliance>(); | ||
| appliances.add(new Appliance(Appliance.Type.WATERHEATER)); | ||
| appliances.add(new Appliance(Appliance.Type.COOKINGOVEN)); | ||
| appliances.add(new Appliance(Appliance.Type.AIRCONDITIONER)); | ||
| String filename = "/projects/assignments/smartHome/src/test/java/testData/testGoodScheduleException.txt"; | ||
| try { | ||
| ArrayList<Event> ret = EventGenerator.readData(filename, appliances); | ||
| } | ||
| catch (Exception e) { | ||
| assertEquals(true, false); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you want to assert, then don't catch exception, test will fail automatically. |
||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.cnu2016.assignment02; | ||
| import org.junit.Test; | ||
| import static org.junit.Assert.assertEquals; | ||
| public class EventTest { | ||
| @Test | ||
| public void testBasics() { | ||
| Appliance a = new Appliance(Appliance.Type.WATERHEATER); | ||
| Event e = new Event(23, a); | ||
| assertEquals(e.timeStamp, 23); | ||
| assertEquals(e.appliance.getID(), a.getID()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package com.cnu2016.assignment02; | ||
| import java.util.*; | ||
| import org.junit.Test; | ||
| import static org.junit.Assert.assertEquals; | ||
| public class MainTest { | ||
| @Test | ||
| public void testPopulate() { | ||
| ArrayList<Appliance> ret = Main.populate(); | ||
| assertEquals(ret.size(), 4); | ||
| assertEquals(ret.get(0).getType(), Appliance.Type.WATERHEATER); | ||
| assertEquals(ret.get(1).getType(), Appliance.Type.AIRCONDITIONER); | ||
| assertEquals(ret.get(2).getType(), Appliance.Type.COOKINGOVEN); | ||
| assertEquals(ret.get(3).getType(), Appliance.Type.WATERHEATER); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 0 2 false |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| 0 2 true | ||
| 0 3 false | ||
| 0 5 true | ||
| 1 7 true | ||
| 2 9 true | ||
| 0 10 false |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 0 2 true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Commenting required for the need of passing a list of appliances in readData