-
Notifications
You must be signed in to change notification settings - Fork 0
Assignment02c #3
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
Open
pranet
wants to merge
12
commits into
master
Choose a base branch
from
assignment02c
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a4c6b0f
Adding input reader
pranet 81ef6aa
EventGenerator added
pranet 98d8eb0
EventGenerator finished.Added fix for water heater timeout
pranet b844aa7
EventGenerator finished.Added fix for water heater timeout
pranet 3076fca
Resolved conflicts with pom.xml
pranet 8e53fd3
Resolved conflicts with pom.xml
pranet e95e55d
Revamped design of Even, Appliance Class
pranet 829feda
Unit tests for Appliance class added
pranet abb630e
70% Coverage so far
pranet e52e37e
70% Coverage so far
pranet a75c8ce
added scheduleAndRun to main to allow testing
pranet b2e2f1c
Jobs need not be in sorted order now
pranet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| 0 5 true | ||
| 1 8 true | ||
| 1 10 false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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>com.cnu2016</groupId> | ||
| <artifactId>com.cnu2016.smartHome</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> |
34 changes: 34 additions & 0 deletions
34
smartHome/src/main/java/com/cnu2016/assignment02/Appliance.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| 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 toggle() { | ||
| state ^= true; | ||
| } | ||
| public int getID() { | ||
| return ID; | ||
| } | ||
| public Type getType() { | ||
| return type; | ||
| } | ||
| @Override | ||
| public String toString() { | ||
| return ID + " " + state; | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
smartHome/src/main/java/com/cnu2016/assignment02/BadScheduleException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
10
smartHome/src/main/java/com/cnu2016/assignment02/Event.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
64 changes: 64 additions & 0 deletions
64
smartHome/src/main/java/com/cnu2016/assignment02/EventGenerator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package com.cnu2016.assignment02; | ||
| import java.util.*; | ||
| import java.io.*; | ||
| public class EventGenerator { | ||
| static class Job { | ||
| int ID, timeStamp; | ||
| boolean state; | ||
| public Job (int ID, int timeStamp, boolean state) { | ||
| this.ID = ID; | ||
| this.timeStamp = timeStamp; | ||
| this.state = state; | ||
| } | ||
| }; | ||
| public static ArrayList<Job> getSortedJobs(String filename) throws IOException, BadScheduleException { | ||
| ArrayList<Job> jobs = new ArrayList<Job>(); | ||
| Scanner in = new Scanner(new FileReader(filename)); | ||
| while(in.hasNext()) { | ||
| int ID = in.nextInt(); | ||
| int startTime = in.nextInt(); | ||
| boolean newState = in.nextBoolean(); | ||
| jobs.add(new Job(ID, startTime, newState)); | ||
| } | ||
| in.close(); | ||
| Collections.sort(jobs, new Comparator<Job>() { | ||
| public int compare(Job a, Job b) { | ||
| return a.timeStamp - b.timeStamp; | ||
| } | ||
| }); | ||
| return jobs; | ||
| } | ||
| public static ArrayList<Event> readData(String filename, ArrayList<Appliance> appliances) throws BadScheduleException, IOException { | ||
|
|
||
| boolean state[] = new boolean[appliances.size()]; | ||
| int tim[] = new int[appliances.size()]; | ||
|
|
||
| ArrayList<Event> events = new ArrayList<Event>(); | ||
| ArrayList<Job> jobs = getSortedJobs(filename); | ||
| for (Job j : jobs) { | ||
| int ID = j.ID; | ||
| int startTime = j.timeStamp; | ||
| boolean newState = j.state; | ||
| if (state[ID] == newState) { | ||
| 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; | ||
| } | ||
| }); | ||
| return events; | ||
| } | ||
| } | ||
34 changes: 34 additions & 0 deletions
34
smartHome/src/main/java/com/cnu2016/assignment02/Main.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| 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 scheduleAndRun(ArrayList<Event> events, ArrayList<Appliance> appliances) { | ||
| Timer timer = new Timer(); | ||
| events.forEach( e -> { | ||
| timer.schedule(new TimerTask() { | ||
| @Override | ||
| public void run() { | ||
| e.appliance.toggle(); | ||
| System.out.printf("At time %d\n", e.timeStamp); | ||
| appliances.forEach( a -> { | ||
| System.out.println(a); | ||
| }); | ||
| } | ||
| }, e.timeStamp * 1000); | ||
| }); | ||
| } | ||
| public static void main(String args[]) throws InterruptedException, IOException, BadScheduleException { | ||
| ArrayList<Appliance> appliances = populate(); | ||
| ArrayList<Event> events = EventGenerator.readData("/projects/assignments/smartHome/input.txt", appliances); | ||
| scheduleAndRun(events, appliances); | ||
| Thread.sleep(10 * 1000); | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletions
21
smartHome/src/test/java/com/cnu2016/assignment02/ApplicanceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| 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.toggle(); | ||
| assertEquals(a.getState(), true); | ||
| a.toggle(); | ||
| assertEquals(a.getState(), false); | ||
| boolean check = a.toString().contains("false"); | ||
| assertEquals(check, true); | ||
| } | ||
|
|
||
| } |
45 changes: 45 additions & 0 deletions
45
smartHome/src/test/java/com/cnu2016/assignment02/EventGeneratorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| 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, BadScheduleException { | ||
| 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, BadScheduleException { | ||
| 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); | ||
| } | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
smartHome/src/test/java/com/cnu2016/assignment02/EventTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
30
smartHome/src/test/java/com/cnu2016/assignment02/MainTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package com.cnu2016.assignment02; | ||
| import java.util.*; | ||
| import java.io.*; | ||
| 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); | ||
| } | ||
| @Test | ||
| public void testScheduleAndRun() throws IOException, BadScheduleException { | ||
| ArrayList<Appliance> appliances = Main.populate(); | ||
| String filename = "/projects/assignments/smartHome/src/test/java/testData/testGoodScheduleException.txt"; | ||
| ArrayList<Event> events = EventGenerator.readData(filename, appliances); | ||
| try { | ||
| Main.scheduleAndRun(events, appliances); | ||
| Thread.sleep(10 * 1000); | ||
| assertEquals(true, true); | ||
| } | ||
| catch (Exception e) { | ||
| assertEquals(true, false); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 0 2 false |
6 changes: 6 additions & 0 deletions
6
smartHome/src/test/java/testData/testGoodScheduleException.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
1 change: 1 addition & 0 deletions
1
smartHome/src/test/java/testData/testWaterHeaterAutoShutOff.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 0 2 true |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
different methods should be used for different use-cases
use different methods for reading and sorting, say readEvents() and processEvents()