Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package projectresurrection; import java.io.File; import java.io.FileWriter; import java.util.*; import org.joda.time.*; import com.thoughtworks.xstream.*; import java.io.*; import com.sun.jna.platform.win32.*; public class Eve { public static final String PREFERENCE_FILE = "C:\\Eve\\preferences.xml"; public static final String PASSWORD_FILE = "C:\\Eve\\password.xml"; private static final List<String> PREFERENCES = Arrays.asList("username", "password1", "password2", "first name", "last name", "middle name", "birth date", "gender", "immortal", "address", "apt", "city", "state", "zip", "phone", "email", "time zone"); public static Listener listener; public static VoiceSynthesis voice; public static GUI gui; public static Clock clock; public static Weather weather; public static Schedule schedule; private static Queue<String> commands = new PriorityQueue<>(); private static List<String> prevCommands = new ArrayList(50); private static Map preferences = new HashMap(); private static DateTime date; private static String sb; private static final DateTime birth = new DateTime(2015, 5, 14, 21, 0, 0, DateTimeZone.forID("America/Chicago")); public static void addCommand(String text) { commands.add(text); } public static Object getPref(Object key) { return preferences.get(key); } private static Object xmlRead(String path) { Object output = null; try { String xml = ""; String line = null; XStream xstream = new XStream(); FileReader fr = new FileReader(path); BufferedReader br = new BufferedReader(fr); while ((line = br.readLine()) != null) { xml += line; } br.close(); output = xstream.fromXML(xml); } catch (Exception e) { e.printStackTrace(System.out); } return (output); } private static void xmlWrite(String path, Object input) { try { XStream xstream = new XStream(); String xml = xstream.toXML(input); FileWriter file = new FileWriter(path); file.write(xml); file.flush(); file.close(); } catch (Exception e) { e.printStackTrace(System.out); } } private static void login() { GUI login = new GUI(GUI.LOGIN); login.start(GUI.LOGIN); while (true) { try { Thread.sleep(1000); while (login.frame.isVisible()) { Thread.sleep(1000); } if (!login.getCred("username").equals(preferences.get("username")) || !login.getCred("password").equals(new String( Crypt32Util.cryptUnprotectData((byte[]) preferences.get("password1")), "UTF-8"))) { throw new Exception(); } break; } catch (Exception e) { login.frame.setVisible(true); } } } private static void init() { if (!new File(PREFERENCE_FILE).exists()) { if (!new File("C:\\Eve\\music").exists()) { new File("C:\\Eve\\music").mkdir(); } GUI pref = new GUI(GUI.PREFERENCES); pref.start(GUI.PREFERENCES); while (true) { try { Thread.sleep(1000); while (pref.frame.isVisible()) { Thread.sleep(1000); } for (int i = 0; i < PREFERENCES.size(); i++) { if (pref.getPref(PREFERENCES.get(i)).equals("") && (!PREFERENCES.get(i).equals("immortal") && !PREFERENCES.get(i).equals("apt"))) { throw new Exception(); } } if (!pref.getPref("password1").equals(pref.getPref("password2"))) { throw new Exception(); } break; } catch (Exception e) { pref.frame.setVisible(true); } } for (int i = 0; i < PREFERENCES.size(); i++) { if (PREFERENCES.get(i).equals("password1") || PREFERENCES.get(i).equals("password2")) { preferences.put(PREFERENCES.get(i), Crypt32Util.cryptProtectData(pref.getPref(PREFERENCES.get(i)).toString().getBytes())); } else { preferences.put(PREFERENCES.get(i), pref.getPref(PREFERENCES.get(i))); } } xmlWrite(PREFERENCE_FILE, preferences); } else { preferences = (Map) xmlRead(PREFERENCE_FILE); //login(); } } private static void updatePref() { GUI pref = new GUI(GUI.PREFERENCES); for (int i = 0; i < PREFERENCES.size(); i++) { try { if (PREFERENCES.get(i).equals("password1") || PREFERENCES.get(i).equals("password2")) { pref.setPref(PREFERENCES.get(i), new String( Crypt32Util.cryptUnprotectData((byte[]) preferences.get(PREFERENCES.get(i))), "UTF-8")); } else { pref.setPref(PREFERENCES.get(i), preferences.get(PREFERENCES.get(i))); } } catch (Exception e) { e.printStackTrace(System.out); } } pref.start(GUI.PREFERENCES); while (true) { try { Thread.sleep(1000); while (pref.frame.isVisible()) { Thread.sleep(1000); } for (int i = 0; i < PREFERENCES.size(); i++) { if (pref.getPref(PREFERENCES.get(i)).equals("") && (!PREFERENCES.get(i).equals("immortal") && !PREFERENCES.get(i).equals("apt"))) { throw new Exception(); } } if (!pref.getPref("password1").equals(pref.getPref("password2"))) { throw new Exception(); } break; } catch (Exception e) { pref.frame.setVisible(true); } } for (int i = 0; i < PREFERENCES.size(); i++) { if (PREFERENCES.get(i).equals("password1") || PREFERENCES.get(i).equals("password2")) { preferences.put(PREFERENCES.get(i), Crypt32Util.cryptProtectData(pref.getPref(PREFERENCES.get(i)).toString().getBytes())); } else { preferences.put(PREFERENCES.get(i), pref.getPref(PREFERENCES.get(i))); } } xmlWrite(PREFERENCE_FILE, preferences); } public static void main(String args[]) { init(); listener = new Listener(); voice = new VoiceSynthesis(); clock = new Clock(Clock.BRAIN, Clock.UPDATE, (String) preferences.get("time zone")); gui = new GUI(GUI.DEFAULT); gui.start(GUI.DEFAULT); while (true) { if (!commands.isEmpty()) { String command = commands.poll(); prevCommands.add(command); switch (command) { case "what is the current weather": Clock.addCommand("kill"); List currentWeather = Weather.getCurrent(); sb = "The Current Tempurature is " + currentWeather.get(1) + " degrees fahrenheit and " + ((currentWeather.get(2).equals("Thunderstorm In Vicinity")) ? "there is a " : "is ") + currentWeather.get(2); if (!currentWeather.get(3).equals("0")) { sb += ", with wind traveling at " + currentWeather.get(3) + " miles per hour "; switch ((String) currentWeather.get(5)) { case "N": sb += "North"; break; case "NNE": sb += "North North East"; break; case "NE": sb += "North East"; break; case "ENE": sb += "East North East"; break; case "E": sb += "East"; break; case "ESE": sb += "East South East"; break; case "SE": sb += "South East"; break; case "SSE": sb += "South South East"; break; case "S": sb += "South"; break; case "SSW": sb += "South South West"; break; case "SW": sb += "South West"; break; case "WSW": sb += "West South West"; break; case "W": sb += "West"; break; case "WNW": sb += "West North West"; break; case "NW": sb += "North West"; break; case "NNW": sb += "North North West"; break; } } voice.say(sb); break; case "what time is it": date = new DateTime(clock.getCurrent()); voice.say( "It is " + date.getHourOfDay() % 12 + ":" + ((date.getMinuteOfHour() < 10) ? "oh " : "") + date.getMinuteOfHour() + ((date.getHourOfDay() > 12) ? " P.M." : " A.M.")); break; case "what is the date": date = new DateTime(clock.getCurrent()); sb = "It is the "; switch (date.getDayOfMonth()) { case 1: sb += "first"; break; case 2: sb += "second"; break; case 3: sb += "third"; break; case 4: sb += "fourth"; break; case 5: sb += "fifth"; break; case 6: sb += "sixth"; break; case 7: sb += "seventh"; break; case 8: sb += "eighth"; break; case 9: sb += "ninth"; break; case 10: sb += "tenth"; break; case 11: sb += "eleventh"; break; case 12: sb += "twelth"; break; case 13: sb += "thirteenth"; break; case 14: sb += "fourteenth"; break; case 15: sb += "fifteenth"; break; case 16: sb += "sixteenth"; break; case 17: sb += "seventeenth"; break; case 18: sb += "eighteenth"; break; case 19: sb += "nineteenth"; break; case 20: sb += "twentieth"; break; case 21: sb += "twenty-first"; break; case 22: sb += "twenty-second"; break; case 23: sb += "twenty-third"; break; case 24: sb += "twenty-fourth"; break; case 25: sb += "twenty-fifth"; break; case 26: sb += "twenty-sixth"; break; case 27: sb += "twenty-seventh"; break; case 28: sb += "twenty-eighth"; break; case 29: sb += "twenty-ninth"; break; case 30: sb += "thirtieth"; break; case 31: sb += "thirty-first"; break; } sb += " of "; switch (date.getMonthOfYear()) { case 1: sb += "January"; break; case 2: sb += "Feburary"; break; case 3: sb += "March"; break; case 4: sb += "April"; break; case 5: sb += "May"; break; case 6: sb += "June"; break; case 7: sb += "July"; break; case 8: sb += "August"; break; case 9: sb += "September"; break; case 10: sb += "October"; break; case 11: sb += "November"; break; case 12: sb += "December"; break; } sb += ", " + date.getYear(); voice.say(sb); break; case "what day is it": date = new DateTime(clock.getCurrent()); sb = "Today is "; switch (date.getDayOfWeek()) { case (1): sb += "Mondaay"; break; case (2): sb += "Tuesdaay"; break; case (3): sb += "Wednesdaay"; break; case (4): sb += "Thursdaay"; break; case (5): sb += "Fridaay"; break; case (6): sb += "Saturdaay"; break; case (7): sb += "Sundaay"; break; } voice.say(sb); break; case "create a reminder": break; case "how old are you": sb = "I am "; DateTime current = clock.getCurrent(); Period diff = new Period(birth, current); int days = Days.daysBetween(birth, current).getDays(); int hours = Hours.hoursBetween(birth, current).getHours() % 24; int minutes = Minutes.minutesBetween(birth, current).getMinutes() % 60; int seconds = Seconds.secondsBetween(birth, current).getSeconds() % 60; switch (days) { case 0: sb += ""; break; case 1: if (hours == 0 && minutes == 0 && seconds == 0) { sb += days + " day old."; } else { sb += days + " day, "; } break; default: if (hours == 0 && minutes == 0 && seconds == 0) { sb += days + " days old."; } else { sb += days + " days, "; } break; } switch (hours) { case 0: sb += ""; break; case 1: if (minutes == 0 && seconds == 0) { sb += "and " + hours + " hour old."; } else { sb += hours + " hour, "; break; } break; default: if (minutes == 0 && seconds == 0) { sb += "and " + hours + " hours old."; } else { sb += hours + " hours, "; } break; } switch (minutes) { case 0: sb += ""; break; case 1: if (seconds == 0) { sb += "and " + minutes + " minute old."; } else { sb += minutes + " minute, "; } break; default: if (seconds == 0) { sb += "and " + minutes + " minutes old."; } else { sb += minutes + " minutes, "; } break; } switch (seconds) { case 0: sb += ""; break; case 1: sb += "and " + seconds + " second old."; default: sb += "and " + seconds + " seconds old."; break; } voice.say(sb); break; case "change preferences": updatePref(); break; case "": voice.say("Lee dul lee dul lee dul leeee"); break; case "repeat": voice.repeatSlow(); break; default: voice.say(command); break; } } else { try { Thread.sleep(1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } } }