프로그래머 노 란 달력 자바 소스 코드 구현

11790 단어 자바
오늘 아침 에 일어나 심심 할 때 프로그래머 의 황 력 을 자바 로 실현 했다.
원래 JS 버 전 주소:http://sandbox.runjs.cn/show/ydp3it7b/
원작 존중... 하하 하.
코드 는 다음 과 같 습 니 다:
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;
	}
}
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;
	}
	
}
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 filter() {
		List thisEnum = new ArrayList();
		
	    //     ,    weekend = true    
		if(isWeekend()) {
			for (ActivitiesEnum e : ActivitiesEnum.values()) {
				if(e.getWeekend()){
					thisEnum.add(e);
				}
			}
			return thisEnum;
		}
		return new ArrayList(Arrays.asList(ActivitiesEnum.values()));
	}
	
	public void pickTodaysLuck() {
		List _activities = filter();
		
		Integer numGood = random(iday, 98) % 3 + 2;
		Integer numBad = random(iday, 87) % 3 + 2;
		List> 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 pickRandom(List _activities, Integer size) {
		List result = new ArrayList();
		
		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 pickRandomDrinks(Integer size) {
		List result = new ArrayList(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> pickRandomActivity(List _activities, Integer size) {
		List picked_events = pickRandom(_activities, size);
		List> mapList = new ArrayList>();
		for (int i = 0; i < picked_events.size(); i++) {
			mapList.add(parse(picked_events.get(i)));
		}
		return mapList;
	}
	
	/**
	 *              
	 * @param ae
	 * @return
	 */
	public Map parse(ActivitiesEnum ae) {
		Map map = new HashMap();
		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 :         

좋은 웹페이지 즐겨찾기