本站精选了一篇相关的编程文章,网友崔安茹根据主题投稿了本篇教程内容,涉及到java、程序员、老黄历、Java实现的程序员老黄历实例相关内容,已被595网友关注,相关难点技巧可以阅读下方的电子资料。
Java实现的程序员老黄历实例
今早起来,无聊之际把程序员老黄历用Java实现了一番。
原JS版地址:程序员老黄历
尊重原作……哈哈哈(连备注都复制的人是不是很恶心的?)
代码如下:
代码段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(); } }
运行结果:
今天是:今天是2016年9月25日 星期日
座位朝向:面向西方写程序,BUG 最少。
今日宜饮:可乐,果汁
女神亲近指数:★★☆☆☆
宜:
锻炼一下身体
晚上加班:晚上是程序员精神最好的时候
浏览成人网站:重拾对生活的信心
不宜:
打DOTA:你会被虐的很惨
上AB站:满屏兄贵亮瞎你的眼
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对码农之家的支持。