您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Java如何實現的程序員老黃歷,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
代碼如下:
代碼段1
package com.test; /** * activities的枚舉 * @author Xiao * */ public enum ActivitiesEnum { ACTIVITIES_1("寫單元測試", "寫單元測試將減少出錯", "寫單元測試會降低你的開發效率", false), ACTIVITIES_2("洗澡", "你幾天沒洗澡了?", "會把設計方面的靈感洗掉", true), ACTIVITIES_3("鍛煉一下身體", "", "能量沒消耗多少,吃得卻更多", true), ACTIVITIES_4("抽煙", "抽煙有利于提神,增加思維敏捷", "除非你活夠了,死得早點沒關系", true), ACTIVITIES_5("白天上線", "今天白天上線是安全的", "可能導致災難性后果", false), ACTIVITIES_6("重構", "代碼質量得到提高", "你很有可能會陷入泥潭", false), ACTIVITIES_7("使用%t", "你看起來更有品位", "別人會覺得你在裝逼", false), ACTIVITIES_8("跳槽", "該放手時就放手", "鑒于當前的經濟形勢,你的下一份工作未必比現在強", false), ACTIVITIES_9("招人", "你面前這位有成為牛人的潛質", "這人會寫程序嗎?", false), ACTIVITIES_10("面試", "面試官今天心情很好", "面試官不爽,會拿你出氣", false), ACTIVITIES_11("提交辭職申請", "公司找到了一個比你更能干的人,你趕快滾蛋", "鑒于當前的經濟形勢,你的下一份工作未必比現在強", false), ACTIVITIES_12("申請加薪", "老板今天心情很好", "公司正在考慮裁員", false), ACTIVITIES_13("晚上加班", "晚上是程序員精神最好的時候", "", true), ACTIVITIES_14("在妹子面前吹牛", "改善你矮窮挫的形象", "會被識破", true), ACTIVITIES_15("擼管", "避免緩沖區溢出", true), ACTIVITIES_16("瀏覽成人網站", "重拾對生活的信心", "你會心神不寧", true), ACTIVITIES_17("命名變量%v", "", "", false), ACTIVITIES_18("寫超過%l行的方法", "你的代碼組織的很好,長一點沒關系", "你的代碼將混亂不堪,你自己都看不懂", false), ACTIVITIES_19("提交代碼", "遇到沖突的幾率是最低的", "你遇到的一大堆沖突會讓你覺得自己是不是時間穿越了", false), ACTIVITIES_20("代碼復審", "發現重要問題的幾率大大增加", "你什么問題都發現不了,白白浪費時間", false), ACTIVITIES_21("開會", "寫代碼之余放松一下打個盹,有益健康",false), ACTIVITIES_22("打DOTA", "你將有如神助", "你會被虐的很慘", true), ACTIVITIES_23("晚上上線", "晚上是程序員精神最好的時候", "你白天已經筋疲力盡了", false), ACTIVITIES_24("修復BUG", "你今天對BUG的嗅覺大大提高", "新產生的BUG將比修復的更多", false), ACTIVITIES_25("設計評審", "設計評審會議將變成頭腦風暴", "人人筋疲力盡,評審就這么過了", false), ACTIVITIES_26("需求評審", "", "", false), ACTIVITIES_27("上微博", "今天發生的事不能錯過", "今天的微博充滿負能量", true), ACTIVITIES_28("上AB站", "還需要理由嗎?", "滿屏兄貴亮瞎你的眼", true), ACTIVITIES_29("玩FlappyBird", "今天破紀錄的幾率很高", "除非你想玩到把手機砸了", true); private String name; private String good; private String bad; private Boolean weekend; ActivitiesEnum(String name, String good, String bad, Boolean weekend){ this.setBad(bad); this.setGood(good); this.setName(name); this.setWeekend(weekend); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGood() { return good; } public void setGood(String good) { this.good = good; } public String getBad() { return bad; } public void setBad(String bad) { this.bad = bad; } public Boolean getWeekend() { return weekend; } public void setWeekend(Boolean weekend) { this.weekend = weekend; } }
代碼段2
package com.test; public enum SpecialsEnum { SPECIALS(2014, "bad", "待在男(女)友身邊", "脫團火葬場,入團保平安。"); private Integer date; private String type; private String name; private String description; SpecialsEnum (Integer date, String type, String name, String description){ this.setDate(date); this.setType(type); this.setName(name); this.setDescription(description); } public Integer getDate() { return date; } public void setDate(Integer date) { this.date = date; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
代碼段3
package com.test; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Random; import org.apache.commons.lang.StringUtils; public class ProgrammerCalendar { private Integer iday = 0; private String [] weeks = new String[]{"日", "一","二","三","四","五","六"}; private String [] directions = new String[]{"北方","東北方","東方","東南方","南方","西南方","西方","西北方"}; private String [] tools = new String[]{"Eclipse寫程序", "MSOffice寫文檔", "記事本寫程序", "Windows8", "Linux", "MacOS", "IE", "Android設備", "iOS設備"}; private String [] varNames = new String[]{"jieguo", "huodong", "pay", "expire", "zhangdan", "every", "free", "i1", "a", "virtual", "ad", "spider", "mima", "pass", "ui"}; private String [] drinks = new String[]{"水","茶","紅茶","綠茶","咖啡","奶茶","可樂","鮮奶","豆奶","果汁","果味汽水","蘇打水","運動飲料","酸奶","酒"}; public Integer getIday() { return iday; } public String[] getDirections() { return directions; } /** * 注意:本程序中的“隨機”都是偽隨機概念,以當前的天為種子。 */ public Integer random(Integer daySeed, Integer indexSeed) { Integer n = daySeed % 11117; for (int i = 0; i < 100 + indexSeed; i++) { n = n * n; n = n % 11117; //11117是個質數 } return n; } public String getTodayString (){ Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); return "今天是" + calendar.get(Calendar.YEAR) + "年" + (calendar.get(Calendar.MONTH) + 1) + "月" + calendar.get(Calendar.DAY_OF_MONTH) + "日 星期" + weeks[calendar.get(Calendar.DAY_OF_WEEK) - 1]; } public String star(Integer num) { String result = ""; int i = 0; while (i < num) { result += "★"; i++; } while(i < 5) { result += "☆"; i++; } return result; } private Boolean isWeekend() { Locale.setDefault(Locale.CHINA); Calendar calendar = Calendar.getInstance(); return calendar.get(Calendar.DAY_OF_WEEK) == 1 || calendar.get(Calendar.DAY_OF_WEEK) == 7; } public List<ActivitiesEnum> filter() { List<ActivitiesEnum> thisEnum = new ArrayList<ActivitiesEnum>(); // 周末的話,只留下 weekend = true 的事件 if(isWeekend()) { for (ActivitiesEnum e : ActivitiesEnum.values()) { if(e.getWeekend()){ thisEnum.add(e); } } return thisEnum; } return new ArrayList<ActivitiesEnum>(Arrays.asList(ActivitiesEnum.values())); } public void pickTodaysLuck() { List<ActivitiesEnum> _activities = filter(); Integer numGood = random(iday, 98) % 3 + 2; Integer numBad = random(iday, 87) % 3 + 2; List<Map<String, String>> eventArr = pickRandomActivity(_activities, numGood + numBad); Integer [] specialSize = pickSpecials(); System.out.println("宜:"); for (int i = 0; i < numGood; i++) { System.out.println(" " + eventArr.get(i).get("name") + (StringUtils.isNotBlank(eventArr.get(i).get("good")) ? ":" + eventArr.get(i).get("good") : "")); } System.out.println("不宜:"); for (int i = 0; i < numBad; i++) { System.out.println(" " + eventArr.get(numGood + i).get("name") + (StringUtils.isNotBlank(eventArr.get(numGood + i).get("bad")) ? ":" + eventArr.get(numGood + i).get("bad") : "")); } } /** * 從數組中隨機挑選 size 個 * @param size * @return */ private List<ActivitiesEnum> pickRandom(List<ActivitiesEnum> _activities, Integer size) { List<ActivitiesEnum> result = new ArrayList<ActivitiesEnum>(); for (ActivitiesEnum ae : _activities) { result.add(ae); } for (int i = 0; i < _activities.size() - size; i++) { int index = random(iday, i) % result.size(); result.remove(index); } return result; } /** * 從數組中隨機挑選 size 個 * @param size * @return */ private List<String> pickRandomDrinks(Integer size) { List<String> result = new ArrayList<String>(Arrays.asList(drinks)); for (int i = 0; i < drinks.length - size; i++) { int index = random(iday, i) % result.size(); result.remove(index); } return result; } // 枚舉 中隨機挑選 size 個 public List<Map<String, String>> pickRandomActivity(List<ActivitiesEnum> _activities, Integer size) { List<ActivitiesEnum> picked_events = pickRandom(_activities, size); List<Map<String, String>> mapList = new ArrayList<Map<String,String>>(); for (int i = 0; i < picked_events.size(); i++) { mapList.add(parse(picked_events.get(i))); } return mapList; } /** * 解析占位符并替換成隨機內容 * @param ae * @return */ public Map<String, String> parse(ActivitiesEnum ae) { Map<String, String> map = new HashMap<String, String>(); map.put("name", ae.getName()); map.put("good", ae.getGood()); map.put("bad", ae.getBad()); if(map.get("name").indexOf("%v") != -1) { map.put("name", map.get("name").replaceAll("%v", varNames[random(iday, 12) % varNames.length])); } if(map.get("name").indexOf("%t") != -1) { map.put("name", map.get("name").replaceAll("%t", tools[random(iday, 11) % tools.length])); } if(map.get("name").indexOf("%t") != -1) { map.put("name", map.get("name").replaceAll("%l", (random(iday, 12) % 247 + 30) + "")); } return map; } public Integer [] pickSpecials () { Integer [] specialSize = new Integer[]{0, 0}; for (SpecialsEnum se : SpecialsEnum.values()) { if(iday == se.getDate()){ if(se.getType().equals("good")){ specialSize[0]++; }else{ specialSize[1]++; } System.out.println("name:" + se.getName() + " description:" + se.getDescription()); } } return specialSize; } public ProgrammerCalendar() { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); try { iday = Integer.parseInt(sdf.format(new Date())); } catch (Exception e) { e.printStackTrace();//懶得引用log了 } } public static void main(String[] args) { ProgrammerCalendar hl = new ProgrammerCalendar(); System.out.println("今天是:" + hl.getTodayString()); System.out.println("座位朝向:面向" + hl.directions[hl.random(hl.getIday(), 2) % hl.getDirections().length] + "寫程序,BUG 最少。"); System.out.println("今日宜飲:" + StringUtils.join(hl.pickRandomDrinks(2), ",")); System.out.println("女神親近指數:" + hl.star(hl.random(hl.getIday(), 6) % 5 + 1)); hl.pickTodaysLuck(); } }
運行結果:
今天是:今天是2021年4月15日 星期四
座位朝向:面向西方寫程序,BUG 最少。
今日宜飲:可樂,果汁
女神親近指數:★★☆☆☆
宜:
鍛煉一下身體
晚上加班:晚上是程序員精神最好的時候
瀏覽成人網站:重拾對生活的信心
不宜:
打DOTA:你會被虐的很慘
上AB站:滿屏兄貴亮瞎你的眼
關于“Java如何實現的程序員老黃歷”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。