Skip to content
Snippets Groups Projects
Commit 127c8a57 authored by Torstein Strømme's avatar Torstein Strømme
Browse files

Etter forelesning 7. februar

parent 6d13ac25
No related branches found
No related tags found
No related merge requests found
# Created by https://www.gitignore.io/api/java,maven,eclipse,intellij+all,visualstudiocode
# Edit at https://www.gitignore.io/?templates=java,maven,eclipse,intellij+all,visualstudiocode
.metadata
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath
.recommenders
# External tool builders
.externalToolBuilders/
# Locally stored "Eclipse launch configurations"
*.launch
# PyDev specific (Python IDE for Eclipse)
*.pydevproject
# CDT-specific (C/C++ Development Tooling)
.cproject
# CDT- autotools
.autotools
# Java annotation processor (APT)
.factorypath
# PDT-specific (PHP Development Tools)
.buildpath
# sbteclipse plugin
.target
# Tern plugin
.tern-project
# TeXlipse plugin
.texlipse
# STS (Spring Tool Suite)
.springBeans
# Code Recommenders
.recommenders/
# Annotation Processing
.apt_generated/
# Scala IDE specific (Scala & Java development for Eclipse)
.cache-main
.scala_dependencies
.worksheet
# Eclipse Core
.project
# JDT-specific (Eclipse Java Development Tools)
.classpath
# Annotation Processing
.apt_generated
.sts4-cache/
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
# Generated files
.idea/**/contentModel.xml
# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
# Gradle
.idea/**/gradle.xml
.idea/**/libraries
# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr
# CMake
cmake-build-*/
# Mongo Explorer plugin
.idea/**/mongoSettings.xml
# File-based project format
*.iws
# IntelliJ
out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
# Editor-based Rest Client
.idea/httpRequests
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
# Ignores the whole .idea folder and all .iml files
# See https://github.com/joeblau/gitignore.io/issues/186 and https://github.com/joeblau/gitignore.io/issues/360
.idea/
# Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023
*.iml
modules.xml
.idea/misc.xml
*.ipr
# Sonarlint plugin
.idea/sonarlint
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
.mvn/wrapper/maven-wrapper.jar
.flattened-pom.xml
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
# Ignore all local history of files
.history
# End of https://www.gitignore.io/api/java,maven,eclipse,intellij+all,visualstudiocode
\ No newline at end of file
# trafficlight
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>inf101v22.trafficlight</groupId>
<artifactId>trafficlight</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>trafficlight</name>
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>17</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven.compiler.release>${java.version}</maven.compiler.release>
<junit>5.6.2</junit>
<!-- Plugin versions -->
<maven.shade>3.2.2</maven.shade>
<maven.clean>3.1.0</maven.clean>
<maven.resources>3.1.0</maven.resources>
<maven.compiler>3.8.1</maven.compiler>
<maven.surefire>3.0.0-M5</maven.surefire>
<maven.jar>3.2.0</maven.jar>
<maven.install>3.0.0-M1</maven.install>
</properties>
<dependencies>
<!-- Dependencies -->
<!-- Testing dependencies-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>3.0.0-M1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven.shade}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>inf101v22.trafficlight.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
package inf101v22.trafficlight;
import inf101v22.trafficlight.TrafficLight.AlreadyStartedException;
public class App {
public static void main(String[] args) throws AlreadyStartedException {
TrafficLight trafficLight = new TrafficLight();
trafficLight.turnOn();
System.out.println(trafficLight);
trafficLight.goToNextState();
trafficLight.state = TrafficLight.State.HURRY;
System.out.println(trafficLight);
trafficLight.goToRed();
System.out.println(trafficLight);
}
}
package inf101v22.trafficlight;
public interface ITrafficLight {
/**
* Change state so the light becomes green
*/
void goToGreen();
/**
* Change state so the light becomes red
*/
void goToRed();
/**
*
* @return true if green light is on, false otherwise
*/
boolean getGreen();
/**
*
* @return true if yellow light is on, false otherwise
*/
boolean getYellow();
/**
*
* @return true if red light is on, false otherwise
*/
boolean getRed();
}
package inf101v22.trafficlight;
public class TrafficLight implements ITrafficLight {
static enum State {
BLANK, STOP, GETREADY, GO, HURRY
}
static class AlreadyStartedException extends Exception { }
State state;
TrafficLight() {
this.state = State.BLANK;
}
void goToNextState() {
if (this.state == State.BLANK) {
this.state = State.BLANK;
}
else if (this.state == State.STOP) {
this.state = State.GETREADY;
}
else if (this.state == State.GETREADY) {
this.state = State.GO;
}
else if (this.state == State.GO) {
this.state = State.HURRY;
}
else if (this.state == State.HURRY) {
this.state = State.STOP;
}
}
void turnOn() throws AlreadyStartedException {
if (this.state != State.BLANK) {
throw new AlreadyStartedException();
}
this.state = State.STOP;
}
@Override
public void goToGreen() {
while (this.state != State.GO) {
this.goToNextState();
}
}
@Override
public void goToRed() {
while (this.state != State.STOP) {
this.goToNextState();
}
}
@Override
public boolean getGreen() {
return this.state == State.GO;
}
@Override
public boolean getYellow() {
return this.state == State.GETREADY || this.state == State.HURRY;
}
@Override
public boolean getRed() {
return this.state == State.STOP || this.state == State.GETREADY;
}
@Override
public String toString() {
return "TrafficLight[ " + this.state + " ]";
}
}
package inf101v22.trafficlight;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
public class AppTest {
@Test
public void shouldAnswerWithTrue() {
assertTrue(true);
}
}
package inf101v22.trafficlight;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import inf101v22.trafficlight.TrafficLight.AlreadyStartedException;
public class TrafficLightTest {
TrafficLight trafficLight;
@BeforeEach
void initializeTrafficLight() {
this.trafficLight = new TrafficLight();
}
@Test
void testStateAfterInitialization() {
assertFalse(trafficLight.getRed());
assertFalse(trafficLight.getYellow());
assertFalse(trafficLight.getGreen());
}
@Test
void testTurnOn() throws AlreadyStartedException {
this.trafficLight.turnOn();
assertTrue(trafficLight.getRed());
}
@Test
void testNextState() throws AlreadyStartedException {
this.trafficLight.turnOn();
assertTrue(trafficLight.getRed());
assertFalse(trafficLight.getGreen());
assertFalse(trafficLight.getYellow());
this.trafficLight.goToNextState();
assertTrue(trafficLight.getRed());
assertTrue(trafficLight.getYellow());
assertFalse(trafficLight.getGreen());
}
@Test
void testGoToRed() throws AlreadyStartedException {
this.trafficLight.turnOn();
assertTrue(trafficLight.getRed());
assertFalse(trafficLight.getGreen());
assertFalse(trafficLight.getYellow());
this.trafficLight.goToNextState();
assertTrue(trafficLight.getRed());
assertTrue(trafficLight.getYellow());
assertFalse(trafficLight.getGreen());
this.trafficLight.goToRed();
assertTrue(trafficLight.getRed());
assertFalse(trafficLight.getGreen());
assertFalse(trafficLight.getYellow());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment