2009/02/22 13:11

[Java] 바이오리듬 구하는 소스

mBiorhythm 만들면서 참고한 소스

-----------------------------------

★ 핵심로직 ★

신체 : (int)(Math.sin(2.0 * Math.PI * ((double)days/23.0)) * 100 + 0.5);
감성 : (int)(Math.sin(2.0 * Math.PI * ((double)days/28.0)) * 100 + 0.5);
지성 : (int)(Math.sin(2.0 * Math.PI * ((double)days/33.0)) * 100 + 0.5);

Bean 파일

import java.text.*;
import java.util.*;

public class Bio {
private String birth;
private int body;
private int feel;
private int intel;
private int bState;
private int fState;
private int iState;

private void calBio(long days){
this.body = calBody(days);
this.feel = calFeel(days);
this.intel = calIntel(days);

if(this.body > calBody(days-1)) this.bState = 1; // 상승
else this.bState = -1; // 하강

if(this.feel > calFeel(days-1)) this.fState = 1; // 상승
else this.fState = -1; // 하강

if(this.intel > calIntel(days-1)) this.iState = 1; // 상승
else this.iState = -1; // 하강
}

private int calBody(long days){
return (int)(Math.sin(2.0 * Math.PI * ((double)days/23.0)) * 100 + 0.5);
}

private int calFeel(long days){
return (int)(Math.sin(2.0 * Math.PI * ((double)days/28.0)) * 100 + 0.5);
}

private int calIntel(long days){
return (int)(Math.sin(2.0 * Math.PI * ((double)days/33.0)) * 100 + 0.5);
}

public void setBirth(String birth) throws ParseException {
long days;
this.birth = birth;
Calendar cNow = Calendar.getInstance();
DateFormat form = DateFormat.getDateInstance(DateFormat.MEDIUM,Locale.KOREAN);
Date dNow,dBirth;

dNow = cNow.getTime();
dBirth = form.parse(birth);
days = (long)(dNow.getTime() - dBirth.getTime())/(1000*60*60*24) -1;
calBio(days);
}

public int getBody() {
return (this.body);
}

public int getFeel() {
return (this.feel);
}

public int getIntel() {
return (this.intel);
}

public int getBState() {
return (this.bState);
}

public int getFState() {
return (this.fState);
}

public int getIState() {
return (this.iState);
}
}

JSP 파일

<%@ page contentType="text/html;charset=euc-kr" %>
<%@ page import="Bio" %>
<%!
public String printState(int state){
if(state > 0) return "<font color=red>△</font>";
else return "<font color=blue>▽</font>";
}
%>
<jsp:useBean id="bio" class="Bio" scope="page">
</jsp:useBean>
<html>
<head>
<style type="text/css">
body {font-size:9pt; font-family:굴림;
</style>
<title>바이오리듬</title>
</head>
<body>
<font size=4><u><b>바이오 리듬</b></u></font>
<%
String birth;
birth = request.getParameter("birth");
if(birth == null) birth = "";
%>
<form name=f1 action="bio.jsp">
생일 : <input type=text name=birth value="<%=birth%>">
<input type=submit value="확인">&nbsp;&nbsp;&nbsp; * 예 : YYYY-MM-DD ( 양력 )
</form>
<% if(!birth.equals("")){
try {
%>
<jsp:setProperty name="bio" property="birth" value="<%=birth%>" />
신체 : <jsp:getProperty name="bio" property="body" /> (<%=printState(bio.getBState())%>)<br>
감성 : <jsp:getProperty name="bio" property="feel" /> (<%=printState(bio.getFState())%>)<br>
지성 : <jsp:getProperty name="bio" property="intel" /> (<%=printState(bio.getIState())%>)<br>
<%
} catch(Exception e){
out.println("<font color=red>날짜를 정확하게 입력하세요.");
}
}
%>
</body>
</html>

Posted by 퓨전마법사
,