diff --git a/Packliste.java b/Packliste.java new file mode 100644 index 0000000..ab44c45 --- /dev/null +++ b/Packliste.java @@ -0,0 +1,217 @@ +import java.awt.Desktop; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Scanner; + +public class Packliste { + private final static String FILETYPE = ".md"; + + private static boolean hotel; + private static boolean camping; + private static boolean camera; + private static boolean cellphone; + private static boolean beach; + private static boolean summer; + private static boolean hiking; + private static boolean lightweight; + private static boolean books; + private static int days; + private static Scanner s = new Scanner(System.in); + private static String output; + + public static void main(String[] args){ + output = ""; + System.out.println("Willkommen beim Packlisten-Konfigurator!"); + days = askIntQuestion("Wie lange soll deine Reise gehen (in Tagen)?"); + summer = askYnQuestion("Wird es ein Sommerurlaub?", true); + hotel = askYnQuestion("Bist du im Hotel?", true); + if(!hotel){ + camping = askYnQuestion("Gehst du campen?", true); + } + hiking = askYnQuestion("Gehst du wandern?", true); + camera = askYnQuestion("Möchtest du eine Kamera mitnehmen?", true); + cellphone = askYnQuestion("Möchtest du dein Handy mitnehmen?", true); + beach = askYnQuestion("Bist du am Meer oder einem Swimming-Pool?", true); + + lightweight = !askYnQuestion("Reist du mit wenig Gepäck?", false); + + books = !askYnQuestion("Möchtest du im Urlaub Bücher lesen?", false); + + //if(books && lightweight) System.out.println("Sorry, aber bei wenig Gepäck muss irgendwas zurückbleiben..."); + + addToOutput(); + writeToFile(); + } + + private static boolean askYnQuestion(String question, boolean yesAsDefault){ + if(yesAsDefault) question += " [J/n] "; + else question += " [j/N] "; + System.out.print(question); + String tmp = s.nextLine(); + tmp = tmp.toLowerCase(); + boolean trueAnswer = tmp.equals(""); + if(trueAnswer) return true; + if(yesAsDefault) trueAnswer = tmp.equals("j"); + else trueAnswer = tmp.equals("n"); + return trueAnswer; + } + + private static String askStringQuestion(String question){ + System.out.print(question + " "); + return s.nextLine(); + } + + private static int askIntQuestion(String question){ + return Integer.parseInt(askStringQuestion(question)); + } + + private static void addToOutput(){ + int packDays; + if(lightweight) packDays = (int) Math.ceil(days / 2.0); //TODO ;) + else packDays = days; + + int foo = (int) Math.ceil(days / 3.0); + if(foo == 0) foo = 1; + + //Klamotten + output += "# Klamotten\n"; + output += "- " + packDays + " T-Shirts\n"; + output += "- " + packDays + " Socken\n"; + output += "- 1 Schuhe\n"; + output += "- " + packDays + " Unterhosen\n"; + if(summer){ + output += "- 1 Hose\n"; + output += "- " + foo + " kurze Hosen\n"; + } else { + output += "- " + foo + " Hosen\n"; + output += "- " + foo + " Pullover\n"; + } + output += "- 1 Regenjacke\n"; + + if(beach){ + output += "- Badehose\n"; + output += "- Strand-Handtuch\n"; + output += "- *Schnorchel-Set*\n"; + output += "- *Badeschuhe*\n"; + } + + output += "\n"; + + //Hygiene-Kram + output += "# Hygiene-Kram\n"; + output += "- 1 Zahnbürste\n"; + output += "- 1 Zahnpasta\n"; + output += "- 1 Deo\n"; + output += "- 1 Duschgel & Shampoo\n"; + output += "- 1 Erste-Hilfe-Tasche\n"; + + output += "\n"; + + //Camping + if(camping){ + output += "# Camping\n"; + output += "- 1 Zelt\n"; + output += "- 1 Schlafsack\n"; + output += "- 1 Isomatte\n"; + output += "- 1 Stirnlampe\n"; + output += "- 1 Kocher\n"; + output += " - 1 Gas/Benzin/Spiritus\n"; + output += "- 1 Teller\n"; + output += "- 1 Besteck\n"; + output += "- 1 Tasse\n"; + output += "- 1 Becher\n"; + output += "- 1 (Microfaser)-Handtuch\n"; + } else if(hotel){ + //TODO + } + + if(hiking){ + output += "- Wanderkarten\n"; + output += "- Wanderschuhe\n"; + } + + output += "\n"; + + if(hasEntertainment()){ + output += "# Unterhaltung\n"; + if(books){ + output += "- Bücher\n"; + } + output += "\n"; + } + + if(hasElectronics()){ + output += "# Elektronik\n"; + if(camera){ + output += "## Kamera\n"; + output += "- Kamera\n"; + output += " - Objektive\n"; + if(!lightweight) output += " - Stativ\n"; + if(camera) output += "- *Action-Kamera*\n"; + output += "- Ladekabel\n"; + output += "- Akkus\n"; + output += "- Speicherkarten\n"; + } + + if(cellphone){ + output += "## Handy\n"; + output += "- Ladekabel\n"; + } + output += "\n"; + } + } + + private static void writeToFile(){ + String filename = ""; + + boolean ownName = !askYnQuestion("Möchtest du deiner Liste einen Namen geben?", false); + if(ownName){ + filename = askStringQuestion("Bitte Dateinamen eingeben:"); + } else { + SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd-HHmmss"); + filename = "packliste-" + sdf.format(new Date()); + } + filename += FILETYPE; + + PrintWriter writer = null; + File file = new File(filename); + try { + writer = new PrintWriter(file, "UTF-8"); + } catch (FileNotFoundException e) { + //TODO + e.printStackTrace(); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + + if(output.endsWith("\n\n")) output = output.substring(0, output.length()-2); + + writer.print(output); + writer.close(); + + boolean openNow = !askYnQuestion("Möchtest du die Packliste jetzt öffnen?", false); + if(openNow){ + Desktop d = Desktop.getDesktop(); + try { + d.open(file); + } catch (IOException e) { + //TODO Wann kann das passieren? + e.printStackTrace(); + } + } + System.out.println("Erhol dich gut in deinem " + days + " tägigen Urlaub und gute Reise!"); + } + + private static boolean hasEntertainment(){ + return books; + } + + private static boolean hasElectronics(){ + return camera || cellphone; + } +}