Hallo, nach über 15 Jahren Abstinenz und als Bloody-Beginner hab ich mich heut mal rangesetzt und ein kleines Projekt geschrieben.
Ich möchte einen Kaffee-Simulator programmieren.
das Folgende hab ich jetzt:
und das Hauptprogramm:
Gerade ebend hab ich mir auf Oracle das Tutorial für Threads durchgelesen. Ich bin mir aber jetzt unsicher wie ich das Hauptprogramm jetzt umstelle.
Am liebsten wäre es mir wenn das Kaffeekochen eine Weile dauert und das Konsumieren des Tasseninhaltes auch eine Weile.
Was habt ihr für Vorschläge dazu?
auch wenn ihr allgemein was anmerken wollt zu meiner Grundidee und den Klassen nehme ich das gern entgegen!
Grüße aus Sachsen..
Ich möchte einen Kaffee-Simulator programmieren.
das Folgende hab ich jetzt:
Java:
public class Liquid {
public String liquidType;
public Liquid( String name )
{
liquidType = name;
}
}
Java:
import java.util.ArrayList;
public class LiquidContainer {
public String containerName;
public int capacity;
public int fillLevel;
public ArrayList filledWith = new ArrayList<>();
public LiquidContainer( String containerName, int c )
{
this.containerName = new String(containerName);
capacity = c;
}
public int fillWith( Liquid l, int mililiter )
{
int fillAmount = capacity-fillLevel;
if( mililiter < fillAmount )
fillAmount = mililiter;
int i;
boolean same = false;
for( i=0;i<filledWith.size();i+=2)
{
Liquid li = (Liquid)filledWith.get(i);
int mili = ((int)filledWith.get(i+1));
if( li.liquidType == l.liquidType )
{
mili += fillAmount;
filledWith.remove(i+1);
filledWith.add(i+1,mili);
same = true;
break;
}
}
if( !same )
{
filledWith.add(l);
filledWith.add(fillAmount);
}
System.out.println(containerName+" filled "+String.valueOf(fillAmount));
fillLevel+=fillAmount;
return fillAmount;
}
public void consume()
{
filledWith = new ArrayList<>();
fillLevel = 0;
System.out.println(containerName+" consumed..");
}
public void fillInto( LiquidContainer lc, int amount)
{
if( amount > lc.capacity )
amount = lc.capacity;
for( int i=0;i<filledWith.size();i+=2)
{
Liquid l = (Liquid)filledWith.get(i);
int mili = (int)filledWith.get(i+1);
float perc = (float)mili/fillLevel;
int fill = (int)((float)amount*perc);
lc.fillWith(l, fill);
mili -= fill;
filledWith.remove(i+1);
filledWith.add(i+1,mili);
}
fillLevel -= amount;
}
public String getFillingStr()
{
String s = new String();
if( filledWith.size()>2)
s = s.concat("mixed with:\n");
for( int i=0;i<filledWith.size();i+=2)
{
Liquid l = (Liquid)filledWith.get(i);
int ml = (int)filledWith.get(i+1);
s = s.concat(l.liquidType+" "+String.valueOf(ml)+"ml");
s = s.concat(new String("\n"));
}
if( s.equals("") )
{
s = s.concat("empty");
}
return s;
}
}
und das Hauptprogramm:
Java:
import java.time.Instant;
public class App {
public static void main(String[] args) throws Exception {
Liquid coffee = new Liquid("Coffee");
LiquidContainer coffeePot = new LiquidContainer("Pot",500);
LiquidContainer cup = new LiquidContainer("Cup",50);
coffeePot.fillWith(coffee,250);
System.out.println("Pot:"+coffeePot.getFillingStr());
coffeePot.fillInto(cup, 100);
//System.out.println("Filling:");
System.out.println("Cup:"+cup.getFillingStr());
long nowCompare = Instant.now().getEpochSecond();
do
{
long now = Instant.now().getEpochSecond();
if( now - nowCompare > 10 )
{
cup.consume();
coffeePot.fillInto(cup,50);
System.out.println("Pot contains: "+coffeePot.getFillingStr());
nowCompare = now;
}
}while(coffeePot.fillLevel>0);
System.out.println("Coffeepot is empty!");
cup.consume();
/*
Liquid milk = new Liquid("Milk");
cup.fillWith(coffee, 25);
cup.fillWith(milk, 25);
System.out.println("Cup1 contains:");
System.out.println(cup.getFillingStr());
LiquidContainer cup2 = new LiquidContainer("Cup2", 50);
cup.fillInto(cup2, 20);
System.out.println("Cup1 contains:");
System.out.println(cup.getFillingStr());
System.out.println("Cup2 contains:");
System.out.println(cup2.getFillingStr());
*/
}
}
Gerade ebend hab ich mir auf Oracle das Tutorial für Threads durchgelesen. Ich bin mir aber jetzt unsicher wie ich das Hauptprogramm jetzt umstelle.
Am liebsten wäre es mir wenn das Kaffeekochen eine Weile dauert und das Konsumieren des Tasseninhaltes auch eine Weile.
Was habt ihr für Vorschläge dazu?
auch wenn ihr allgemein was anmerken wollt zu meiner Grundidee und den Klassen nehme ich das gern entgegen!
Grüße aus Sachsen..