Skip to content

Instantly share code, notes, and snippets.

@sebastianhoitz
Created June 18, 2011 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebastianhoitz/1033343 to your computer and use it in GitHub Desktop.
Save sebastianhoitz/1033343 to your computer and use it in GitHub Desktop.
Java Eieruhr
/**
* Class Eieruhr
*
* Eigener Thread. Bekommt als Parameter Name und dauer als String.
* Gibt nach Ablaufen der Zeit aus, dass die Zutat fertig ist.
*/
class Eieruhr extends Thread
{
String time;
long startTimeMillis;
long endTimeMillis;
public Eieruhr(String name, String time)
{
this.setName(name);
this.time = time;
}
public void run()
{
this.startTimeMillis = System.currentTimeMillis();
this.endTimeMillis = this.startTimeMillis;
this.endTimeMillis += parseTimeToSeconds(this.time) * 1000;
System.out.println("Es ist "+formatTime(startTimeMillis));
System.out.println("Um "+formatTime(this.endTimeMillis)+" erfolgt die Ausgabe: "+this.getName()+" fertig.");
while(System.currentTimeMillis() < this.endTimeMillis)
{
}
System.out.println(this.getName()+" fertig.");
}
/**
* Formatiert die als millisekunden übergebene Zeit und gibt dieses als
* String zurück.
*
* @param long Millisekunden seit 1. Januar 1970
*/
public String formatTime(long ms)
{
long hours = ms / 1000 / 3600 % 24;
long minutes = ms / 1000 / 60 % 60;
long seconds = ms / 1000 % 60;
return String.format("%02d:%02d:%02d UTC", hours, minutes, seconds);
}
/**
* Parset die Dauer in einen long Wert.
* @param String timeString (xx:xx min)
*/
public int parseTimeToSeconds(String timeString)
{
String[] parts = timeString.split(":");
int min = Integer.parseInt(parts[0]);
int len = parts[1].indexOf(" ");
if(len == -1) len = parts[1].length();
int sec = Integer.parseInt(parts[1].substring(0, len));
return min*60+sec;
}
}
import java.io.*;
/**
* @author Sebastian Hoitz, 4067639
*
* Anfangsklasse, um die Eieruhr zu starten.
* Hier wird die Eieruhr gestartet.
*
*/
class Main
{
public static void main(String[] args) throws IOException
{
InputStreamReader stream = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(stream);
for(int i = 0; i < 2; i++)
{
System.out.print("Name der Zutat:");
String ingredient = reader.readLine();
System.out.print("Dauer (xx:xx min):");
String duration = reader.readLine();
Eieruhr e = new Eieruhr(ingredient, duration);
e.start();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment