Commit 63fbf2bf authored by Jiri Sebek's avatar Jiri Sebek

3.cv

parent 7b2c66ff
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cz-cvut-fel-omo</groupId>
<artifactId>cv2_solution</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
package cz.cvut.fel.omo.trackingSystem;
import org.apache.commons.collections4.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
/**
* Created by kuki on 22/09/2017.
* GPSTrackingSystem class represents the newly introduced tool for gaining control over company car park.
*/
public class GpsTrackingSystem {
private static int counter = 0;
private List<Tracker> activeTrackers = new ArrayList<>();
public int calculateTotalMileage() {
int totalMileage = 0;
for (Tracker tracker : activeTrackers)
totalMileage += tracker.getTrackerMileage();
return totalMileage;
}
public void attachTrackingDevices(List<Vehicle> carPark) {
if (CollectionUtils.isNotEmpty(carPark)) {
for (Vehicle vehicle : carPark) {
Tracker newTracker = new Tracker(counter++);
newTracker.attachTracker(vehicle);
activeTrackers.add(newTracker);
}
}
}
public static int getCounter(){
return counter;
}
public void generateMonthlyReport(){
System.out.println("========= GPS tracking system: Monthly report ========= ");
System.out.println("Currently active devices:");
activeTrackers.forEach(tracker -> {System.out.print(tracker); tracker.resetTrackerMileage();});
System.out.println("\nThis month traveled distance:" + calculateTotalMileage());
}
}
package cz.cvut.fel.omo.trackingSystem;
import java.util.ArrayList;
import java.util.List;
/**
* Created by kuki on 22/09/2017.
*/
public class Main {
public static void main(String[] args) {
// 1. Initialize company car park
List<Vehicle> carPark = new ArrayList<>();
GpsTrackingSystem companySpy = new GpsTrackingSystem();
carPark.add(new Vehicle("Volvo", "VINOKLJ15", 879));
carPark.add(new Vehicle("Volvo", "OINNLKA48", 9080));
carPark.add(new Vehicle("Volvo", "OPWAKMKL8", 1280));
// Attaching tracking devices to the car park
companySpy.attachTrackingDevices(carPark);
// Counting initial traveled distance with trackers
companySpy.generateMonthlyReport();
// Busy month filled with business travelling
carPark.forEach(car->car.drive(100));
// End of another month, generate new report
companySpy.generateMonthlyReport();
// Busy month filled with business travelling
carPark.forEach(car->car.drive(300));
// End of another month, generate new report
companySpy.generateMonthlyReport();
}
}
package cz.cvut.fel.omo.trackingSystem;
/**
* Created by kuki on 22/09/2017.
* Tracker is device installed into company vehicles, connected to car computer in order to obtain necessary data.
*/
public class Tracker {
private final int trackerID;
private Vehicle currentVehicle;
private int innerMemory;
public Tracker(int trackerID){
this.trackerID = trackerID;
}
public int getTrackerMileage(){
return currentVehicle.getMileage() - innerMemory;
}
public void attachTracker(Vehicle vehicle){
currentVehicle = vehicle;
innerMemory = vehicle.getMileage();
}
public void resetTrackerMileage(){
this.innerMemory = currentVehicle.getMileage();
}
public Vehicle getCurrentVehicle() {
return currentVehicle;
}
public String toString(){
return getClass().getSimpleName() + "_" + trackerID + ", attached to " + currentVehicle.toString() + "\n";
}
}
package cz.cvut.fel.omo.trackingSystem;
/**
* Created by kuki on 22/09/2017.
* Class Vehicle represents a single car in company car park.
*/
public class Vehicle {
private final String manufacturer;
private final String vinCode;
private int mileage;
public Vehicle(String manufacturer, String vinCode, int mileage){
this.manufacturer = manufacturer;
this.vinCode = vinCode;
this.mileage = mileage;
}
public String getManufacturer() {
return manufacturer;
}
public String getVINCode() {
return vinCode;
}
public int getMileage() {
return mileage;
}
public void drive(int mileage){
this.mileage += mileage;
}
public String toString(){
return manufacturer + ", " + vinCode;
}
}
package cz.cvut.fel.omo.trackingSystem;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author balikm1
*/
public class GpsTrackingSystemTest {
private List<Vehicle> carPark;
private GpsTrackingSystem GpsTrackingSystem;
@Before
public void executeBeforeEach()
{
carPark = new ArrayList<>();
carPark.add(new Vehicle("Volvo", "AKLSJLJLN", 800));
carPark.add(new Vehicle("Volvo", "AKLSJLJLN", 1500));
carPark.add(new Vehicle("Volvo", "AKLSJLJLN", 420));
GpsTrackingSystem = new GpsTrackingSystem();
}
@Test
public void calculateTotalMileage_trackingDevicesAttached_zeroMileageReturned()
{
// arrange
int expectedMileage = 0;
GpsTrackingSystem.attachTrackingDevices(carPark);
// act
int actualMileage = GpsTrackingSystem.calculateTotalMileage();
// assert
assertEquals(expectedMileage, actualMileage);
}
@Test
public void calculateTotalMileage_someDistanceDriven_sumOfDrivenDistanceReturned()
{
// arrange
int mileage = 70;
GpsTrackingSystem.attachTrackingDevices(carPark);
carPark.forEach(car -> car.drive(mileage));
int expectedMileage = carPark.size() * mileage;
// act
int actualMileage = GpsTrackingSystem.calculateTotalMileage();
// assert
assertEquals(expectedMileage, actualMileage);
}
@Test
public void calculateTotalMileage_someDistanceDriven_trackingDevicesReset()
{
// arrange
int mileage = 70;
GpsTrackingSystem.attachTrackingDevices(carPark);
carPark.forEach(car -> car.drive(mileage));
int expectedMileage = 0;
// act
int previousMileage = GpsTrackingSystem.calculateTotalMileage();
GpsTrackingSystem.generateMonthlyReport();
int actualMileage = GpsTrackingSystem.calculateTotalMileage();
// assert
assertEquals(expectedMileage, actualMileage);
}
@Test
public void attachTrackingDevices_carListProvided_counterIncrementedByCountOfProvidedCars() throws Exception {
// arrange
int expectedAmount = GpsTrackingSystem.getCounter() + 3;
// act
GpsTrackingSystem.attachTrackingDevices(carPark);
int actualAmount = GpsTrackingSystem.getCounter();
// assert
assertEquals(expectedAmount, actualAmount);
}
}
\ No newline at end of file
package cz.cvut.fel.omo.trackingSystem;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author balikm1
*/
public class TrackerTest {
private Vehicle vehicle;
private Tracker tracker;
private int initialMileage;
@Before
public void executeBeforeEach()
{
initialMileage = 199049;
vehicle = new Vehicle("Jeep", "AKSJH15615", initialMileage);
tracker = new Tracker(0);
}
@Test
public void attachTracker_vehicleNotNull_currentVehicleEqualsAttached()
{
// arrange
Vehicle expectedVehicle = vehicle;
// act
tracker.attachTracker(vehicle);
Vehicle actualVehicle= tracker.getCurrentVehicle();
// assert
assertEquals(expectedVehicle, actualVehicle);
}
@Test
public void getTrackerMileage_trackerAttached_zeroMileageReturned()
{
// arrange
int expectedMileage = 0;
tracker.attachTracker(vehicle);
// act
int actualMileage = tracker.getTrackerMileage();
// assert
assertEquals(expectedMileage, actualMileage);
}
public void getTrackerMileage_someDistanceDriven_sumOfDrivenDistanceReturned()
{
// arrange
int mileage1 = 100;
int mileage2 = 200;
int expectedMileage = mileage1 + mileage2;
tracker.attachTracker(vehicle);
// act
vehicle.drive(mileage1);
vehicle.drive(mileage2);
int actualMileage = tracker.getTrackerMileage();
// assert
assertEquals(expectedMileage, actualMileage);
}
}
\ No newline at end of file
package cz.cvut.fel.omo.trackingSystem;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author balikm1
*/
public class VehicleTest {
@Test
public void drive_distanceMoreThanZero_mileageIncremetedByTraveledDistance()
{
// arrange
int initialMileage = 500;
int traveledDistance = 275;
int expectedValue = initialMileage + traveledDistance;
Vehicle vehicle = new Vehicle("Fiat", "LKASJDLK15", initialMileage);
// act
vehicle.drive(traveledDistance);
int actualMileage = vehicle.getMileage();
// assert
assertEquals(expectedValue, actualMileage);
}
}
\ 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cz.cvut.fel.omo</groupId>
<artifactId>cv3_assigment</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
package cz.cvut.fel.omo.blog;
import java.util.List;
/*
* Dashboard serves as a Blog user interface for displaying requested information to user.
*/
public class Dashboard {
/*
* Dashboard method for displaying UI context to the user. If the parameter is empty, then method displays warning line. Provided the given parameter
* is not null, the method iterates the List and delegates printing onto DisplayableComponent itself.
* @param components List of Objects implementing DisplayableComponent interface.
* @param pageHeaders Header of the dashboard page.
*/
public void display(List<DisplayableComponent>components, String pageHeader){
if (components.isEmpty())
System.out.println("There is no content to display.");
else {
System.out.println("\n" + pageHeader);
System.out.println("======================================");
components.forEach(component -> component.displayComponent());
System.out.println("======================================\n");
}
}
}
package cz.cvut.fel.omo.blog;
public class Main {
public static void main(String[] args) {
//1.1 CREATING NEW ADMIN AND USER ACCOUNT
/* UNCOMMENT ME WHEN READY
Blog blog = new Blog();
String username = "Tom_Hanks";
String password = "password";
boolean admin = true;
blog.createNewAccount(username, password, admin);
blog.createNewAccount("Nicolas_Cage", "password", false);
// 1.2 LOGGING INTO THE ADMIN ACCOUNT
AdminAccount account = (AdminAccount) blog.login(username, password);
// 1.3 CREATING NEW TOPICS
account.createNewTopic("AngularJS", "Typical issues encountered using AngularJS, also known as Angular1");
account.createNewTopic("React", "Unlocking hidden power of javascript.");
account.createNewTopic("FrontEnd development", "Various frameworks for frontend development.");
blog.displayTopics();
// 1.4 WRITING NEW POST
account.writeNewPost("Why Angular JS", "HTML is great for declaring static documents, but it falters when we try to use it for declaring dynamic views in web-applications. AngularJS lets you extend HTML vocabulary for your application. The resulting environment is extraordinarily expressive, readable, and quick to develop.\n");
account.writeNewPost("Stairway to React", "React makes it painless to create interactive UIs. Design simple views for each state in your application, and React will efficiently update and render just the right components when your data changes.\n");
// 1.5 REGISTRING POSTS TO TOPICS
Post angularPost = blog.findPost("Why Angular JS");
Post reactPost = blog.findPost("Stairway to React");
Topic angularTopic = blog.findTopic("AngularJS");
Topic reactTopic = blog.findTopic("React");
Topic frontEndTopic = blog.findTopic("FrontEnd development");
angularPost.registerToTopic(angularTopic);
angularPost.registerToTopic(frontEndTopic);
reactPost.registerToTopic(reactTopic);
reactPost.registerToTopic(frontEndTopic);
// 1.6 PUBLISHING POST
angularPost.publishPost();
reactPost.publishPost();
// 1.8 LOGGING INTO USER ACCOUNT
UserAccount userAccount = (UserAccount) blog.login("Nicolas_Cage", password);
// 1.7 READING DIFFERENT SECTIONS OF BLOG
userAccount.readBlog();
userAccount.readBlog("React");
userAccount.readBlog("AngularJS");
userAccount.readBlog("FrontEnd development");
userAccount.readBlog("BackEnd");
*/
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment