Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added .gitignore
Empty file.
3 changes: 3 additions & 0 deletions smartHome/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0 5 true
1 8 true
1 10 false
67 changes: 67 additions & 0 deletions smartHome/pom.xml
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>
33 changes: 33 additions & 0 deletions smartHome/src/main/java/com/cnu2016/assignment02/Appliance.java
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);
}
}
10 changes: 10 additions & 0 deletions smartHome/src/main/java/com/cnu2016/assignment02/Event.java
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 {
Copy link
Copy Markdown

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


boolean state[] = new boolean[appliances.size()];
int tim[] = new int[appliances.size()];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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;
}
}
35 changes: 35 additions & 0 deletions smartHome/src/main/java/com/cnu2016/assignment02/Main.java
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);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. what if no file at given path ?
  2. what if the text inside the file doesn't have correct format

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);
});
Copy link
Copy Markdown

@maharshi95 maharshi95 Jul 9, 2016

Choose a reason for hiding this comment

The 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);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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.

}
}

}
12 changes: 12 additions & 0 deletions smartHome/src/test/java/com/cnu2016/assignment02/EventTest.java
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());
}
}
15 changes: 15 additions & 0 deletions smartHome/src/test/java/com/cnu2016/assignment02/MainTest.java
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