Skip to content
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

t9 #14

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

t9 #14

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
13 changes: 0 additions & 13 deletions README.md

This file was deleted.

1 change: 0 additions & 1 deletion koodi.rb

This file was deleted.

14 changes: 0 additions & 14 deletions testi_spec.rb

This file was deleted.

15 changes: 15 additions & 0 deletions viikko2/Ohtu-NhlStatistics1/nbactions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<actions>
<action>
<actionName>run</actionName>
<goals>
<goal>process-classes</goal>
<goal>org.codehaus.mojo:exec-maven-plugin:1.2:exec</goal>
</goals>
<properties>
<exec.args>-classpath %classpath ohtuesimerkki.Main</exec.args>
<exec.executable>java</exec.executable>
<exec.classpathScope>runtime</exec.classpathScope>
</properties>
</action>
</actions>
38 changes: 38 additions & 0 deletions viikko2/Ohtu-NhlStatistics1/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

<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>wad</groupId>
<artifactId>Ohtu-NhlStatistics1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Ohtu-NhlStatistics1</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
19 changes: 19 additions & 0 deletions viikko2/Ohtu-NhlStatistics1/src/main/java/ohtuesimerkki/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ohtuesimerkki;

public class Main {
public static void main(String[] args) {
Statistics stats = new Statistics();

System.out.println("Philadelphia Flyers");
for (Player player : stats.team("PHI") ) {
System.out.println( player );
}

System.out.println("");

System.out.println("Top scorers");
for (Player player : stats.topScorers(10) ) {
System.out.println( player );
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package ohtuesimerkki;

public class Player implements Comparable<Player> {

private String name;
private String team;
private int goals;
private int assists;

public Player(String name, String team, int goals, int assists) {
this.name = name;
this.team = team;
this.goals = goals;
this.assists = assists;
}

public int getAssists() {
return assists;
}

public void setAssists(int assists) {
this.assists = assists;
}

public int getGoals() {
return goals;
}

public void setGoals(int goals) {
this.goals = goals;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getTeam() {
return team;
}

public void setTeam(String team) {
this.team = team;
}

public int getPoints() {
return goals + assists;
}

@Override
public String toString() {
return String.format("%-20s",name) + " " + team + " " + String.format("%2d",goals) + " + "
+ String.format("%2d",assists) + " = " + getPoints();
}

public int compareTo(Player t) {
return t.getPoints()-this.getPoints();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ohtuesimerkki;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class PlayerReader {

private Scanner scanner;

public PlayerReader(String pageUrl) {
try {
URL url = new URL(pageUrl);
scanner = new Scanner(url.openStream());
} catch (Exception e) {
e.printStackTrace();
}
}

public List<Player> getPlayers() {
ArrayList<Player> players = new ArrayList<Player>();

while (scanner.hasNextLine()) {
String[] parts = scanner.nextLine().split(";");

if (parts.length > 3) {
players.add(new Player(parts[0].trim(), parts[1], extractInt(parts[3]), extractInt(parts[4])));
}
}

return players;
}

private int extractInt(String str) {
return Integer.parseInt(str.trim());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ohtuesimerkki;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class Statistics {

private List<Player> players;

public Statistics() {
PlayerReader reader = new PlayerReader("http://nhlstatistics.herokuapp.com/players.txt");
players = reader.getPlayers();
}

public Player search(String name) {
for (Player player : players) {
if (player.getName().contains(name)) {
return player;
}
}

return null;
}

public List<Player> team(String teamName) {
ArrayList<Player> playersOfTeam = new ArrayList<Player>();

for (Player player : players) {
if ( player.getTeam().equals(teamName)) {
playersOfTeam.add(player);
}
}

return playersOfTeam;
}

public List<Player> topScorers(int howMany) {
Collections.sort(players);
ArrayList<Player> topScorers = new ArrayList<Player>();
Iterator<Player> playerIterator = players.iterator();

while (howMany>=0) {
topScorers.add( playerIterator.next() );
howMany--;
}

return topScorers;
}

}
38 changes: 38 additions & 0 deletions viikko2/RiippuvuuksienInjektointi1/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

<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>ohtu</groupId>
<artifactId>RiippuvuuksienInjektointi1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>RiippuvuuksienInjektointi1</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
33 changes: 33 additions & 0 deletions viikko2/RiippuvuuksienInjektointi1/src/main/java/ohtu/Laskin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ohtu;

import java.util.Scanner;

public class Laskin {

private Scanner lukija;

public Laskin() {
lukija = new Scanner(System.in);
}

public void suorita(){
while( true ) {
System.out.print("luku 1: ");
int luku1 = lukija.nextInt();
if ( luku1==-9999 ) return;

System.out.print("luku 2: ");
int luku2 = lukija.nextInt();
if ( luku2==-9999 ) return;

int vastaus = laskeSumma(luku1, luku2);
System.out.println("summa: "+ vastaus);
}
}

private int laskeSumma(int luku1, int luku2) {
return luku1+luku2;
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ohtu;

import ohtu.laskin.KonsoliIO;

public class Main {
public static void main(String[] args) {
new ohtu.laskin.Laskin(new KonsoliIO()).suorita();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ohtu.laskin;


public interface IO {
int nextInt();
void print(String m);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

package ohtu.laskin;

import java.util.Scanner;

public class KonsoliIO implements IO {
private Scanner lukija;

public KonsoliIO() {
lukija = new Scanner(System.in);
}

public int nextInt() {
return lukija.nextInt();
}

public void print(String m) {
System.out.print(m);
}

}
Loading