Skip to content
Snippets Groups Projects
Commit 0e5d44cd authored by leinolm's avatar leinolm
Browse files

komennot tulostuu luokan ulostulotietovirtaan

parent 4593ee21
No related branches found
No related tags found
No related merge requests found
package cowhello;
import java.util.function.Consumer;
import java.io.PrintStream;
import java.util.function.BiConsumer;
/**
* luokka, jolla saa jouhevasti komentoja kuoreen lisattya
......@@ -10,7 +11,7 @@ import java.util.function.Consumer;
*/
public class Command {
private String nimi;
private Consumer<String[]> func;
private BiConsumer<PrintStream,String[]> func;
private String ohje;
/**
* muodostaja
......@@ -18,7 +19,7 @@ public class Command {
* @param func momento itsessään
* @param ohje ohje komennon kayttoon
*/
public Command(String nimi, Consumer<String[]> func,String ohje) {
public Command(String nimi, BiConsumer<PrintStream,String[]> func,String ohje) {
this.nimi=nimi;
this.func=func;
this.ohje=ohje;
......@@ -26,10 +27,11 @@ public class Command {
/**
* suorittaa komennon argumenteilla
* @param ps tietovirta,johon komento tulostaa
* @param args argumentit
*/
public void exec(String[] args) {
func.accept(args);
public void exec(PrintStream ps,String[] args) {
func.accept(ps,args);
}
public String getNimi() {return nimi;}
......
package cowhello;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
/**
......@@ -11,11 +13,11 @@ import java.util.function.Consumer;
*/
public class Commands {
private ArrayList<Command> komennot;
private static Command unknownCommand = new Command("unused",(z) -> {System.out.printf("Tuntematon komento: %s%n", z[0]);},"");
private static Command unknownCommand = new Command("unused",(o,z) -> {o.printf("Tuntematon komento: %s%n", z[0]);},"");
public Commands() {
komennot = new ArrayList<Command>();
add("help",(z) -> {
if (z.length<2) {System.out.printf("Kaytto : help <command>%ntunnetut komennot: "); for (Command c: komennot) System.out.printf("%s ",c.getNimi()); System.out.println();}
add("help",(o,z) -> {
if (z.length<2) {o.printf("Kaytto : help <command>%ntunnetut komennot: "); for (Command c: komennot) o.printf("%s ",c.getNimi()); o.println();}
else System.out.println(get(z[1]).getOhje());
},"help <komento>");
}
......@@ -30,10 +32,10 @@ public class Commands {
return komennot.contains(cmd);
}
public void exec(String komento, String[] arg) {
public void exec(PrintStream ps,String komento, String[] arg) {
// TODO Auto-generated method stub
Command cmd = get(komento);
cmd.exec(arg);
cmd.exec(ps,arg);
}
private Command get(String komento) {
......@@ -44,7 +46,7 @@ public class Commands {
return unknownCommand;
}
public void add(String nimi, Consumer<String[]> cmd, String ohje) {
public void add(String nimi, BiConsumer<PrintStream,String[]> cmd, String ohje) {
// TODO Auto-generated method stub
if (!hasCommand(nimi)) komennot.add(new Command(nimi,cmd,ohje));
}
......
......@@ -6,6 +6,7 @@ import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
/**
* takes care of the I/O of user
......@@ -91,7 +92,7 @@ public class ConsoleProcess extends Thread{
* @param cmd komennon funktionaalinen osa
* @param ohje komennon ohje
*/
protected void lisaaKomento(String nimi, Consumer<String[]> cmd, String ohje) {
protected void lisaaKomento(String nimi, BiConsumer<PrintStream,String[]> cmd, String ohje) {
komennot.add(nimi,cmd,ohje);
}
......@@ -106,7 +107,7 @@ public class ConsoleProcess extends Thread{
//TODO: ks. rivin 19 todo
private void exec(String a) {
String[] jutut = a.split(" ");
komennot.exec(jutut[0],jutut);
komennot.exec(output,jutut[0],jutut);
/*
for (Command c: komennot) {
if (c.getNimi().equals(jutut[0])) {c.exec(jutut); return;}
......
......@@ -19,14 +19,14 @@ public class Cowhello {
ConsoleProcess cp = new ConsoleProcess(System.in,System.out);
TestProcess t = new TestProcess(cp);
t.start();
cp.lisaaKomento("hello", (arg) -> {
cp.lisaaKomento("hello", (o,arg) -> {
for (String a : arg) {
System.out.printf("%s ",a);
o.printf("%s ",a);
}
System.out.println("end");
o.println("end");
},"kaytto: hello <arg>, ...\n tulostaa argumentit");
cp.lisaaKomento("pali", (z) -> {
cp.lisaaKomento("pali", (o,z) -> {
if (z.length<1) return;
String sana = z[1];
boolean flag = true;
......@@ -37,11 +37,11 @@ public class Cowhello {
loppu--;
}
String palautus = (flag) ? "on" : "ei ole";
System.out.printf("Sana %s %s palindromi.%n",sana, palautus);
o.printf("Sana %s %s palindromi.%n",sana, palautus);
},"kaytto: pali <sana>\nkertoo, onko sana palindromi.");
cp.lisaaKomento("ystava",t.annaVaihtoOikeus(),"kaytto: ystava <numero>\n vaihtaa ystavan salaisuuden");
cp.lisaaKomento("hei",(z)->{System.out.printf("moi %s%n",z[1]);},"tervehtii");
cp.lisaaKomento("hei",(o,z)->{o.printf("moi %s%n",z[1]);},"tervehtii");
cp.start();
//cpw.start();
......
package cowhello;
import java.io.PrintStream;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
/**
......@@ -40,8 +42,8 @@ public class TestProcess extends Thread{
public int getSalaisuus() {return salaisuus;}
public Consumer<String[]> annaVaihtoOikeus(){
return (z) -> {salaisuus = Integer.parseInt(z[1]);};
public BiConsumer<PrintStream,String[]> annaVaihtoOikeus(){
return (o,z) -> {salaisuus = Integer.parseInt(z[1]);};
}
......
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