Java实现的程序员老黄历实例
程序员文章站
2023-12-05 17:50:22
今早起来,无聊之际把程序员老黄历用java实现了一番。
原js版地址:
尊重原作……哈哈哈(连备注都复制的人是不是很恶心的?)
代码如下:
代码段1...
今早起来,无聊之际把程序员老黄历用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站:满屏兄贵亮瞎你的眼
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。