U E D R , A S I H C RSS

만년달력/인수

Calendar.java

~cpp 
public class Calendar {
	static int DAYS_PER_MONTH[] = {31,28,31,30,31,30,31,31,30,31,30,31};
	int year, month;
	public Calendar(int year, int month) {
		set(year, month);
	}
	public void set(int year, int month) {
		this.year = year;
		this.month = month;
		DAYS_PER_MONTH[1] = isLeapYear(this.year) ? 29 : 28; 
	}
	protected int getNumOfDays() {
		return DAYS_PER_MONTH[month - 1];
	}
	public int[] getCalendar() {
		int ret[] = new int[42];
		int start = getMonthStartPoint();
		for(int i = start ; i < getNumOfDays() + start ; ++i)
			ret[i] = i - start + 1;
		return ret;
	}
	protected int getNumOfLeapYears() {
		int ret = 0;
		for(int i = 1 ; i < year ; ++i)
			if( isLeapYear(i) ) ++ret;
		return ret;
	}
	protected int getMonthStartPoint() {
		int ret = year + getNumOfLeapYears();
		for(int i = 0 ; i < month - 1 ; ++i)
			ret += DAYS_PER_MONTH[i];
		return ret % 7;
	}
	protected boolean isLeapYear() {
		return isLeapYear(this.year);
	}
	protected boolean isLeapYear(int year) {
		return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
	}
}

CalendarTestCase.java

~cpp 
import junit.framework.TestCase;

public class CalendarTestCaseTest extends TestCase {							
	Calendar calendar = new Calendar(1,1);

	public CalendarTestCaseTest(String arg) {
		super(arg);
	}
	private int[] getExpectedCalendar(int start) {
		int ret[] = new int[42];
		for(int i = start ; i < calendar.getNumOfDays() + start ; ++i)
			ret[i] = i - start + 1;
		return ret;		
	}
	private void assertEqualsArray(int [] expected) {
		int real[] = calendar.getCalendar();		
		for(int i = 0 ; i < 42 ; ++i)		
			assertEquals( expected[i], real[i] );		
	}
	public void test1Year() {
		int expectedSet[] = {1,4,4,0,2,5,0,3,6,1,4,6};
		for(int i = 0 ; i < expectedSet.length ; ++i) {
			calendar.set(1, i + 1);
			int expected[] = getExpectedCalendar(expectedSet[i]);
			assertEqualsArray(expected);
		}
	}
	public void test2Year() {
		int monthSet[] = {1,2,12};
		int expectedSet[] = {2,5,0};
		for(int i = 0 ; i < monthSet.length ; ++i) {
			calendar.set(2, monthSet[i]);
			assertEqualsArray( getExpectedCalendar(expectedSet[i]) );			
		}			
	}
	public void test4Year() {
		int expectedSet[] = {4,0,1,4,6,2,4,0,3,5,1,3};
		for(int i = 0 ; i < expectedSet.length ; ++i) {
			calendar.set(4, i + 1);
			int expected[] = getExpectedCalendar(expectedSet[i]);
			assertEqualsArray(expected);
		}			
	}
	public void test2003Year() {
		int expectedSet[] = {3,6,6,2,4,0,2,5,1,3,6,1};
		for(int i = 0 ; i < expectedSet.length ; ++i) {
			calendar.set(2003, i + 1);
			int expected[] = getExpectedCalendar(expectedSet[i]);
			assertEqualsArray(expected);
		}			
	}
	public void testMonthStartPoint() {
		int monthSet[] = {1,2,8};
		int expectedSet[] = {1,4,3};
		for(int i = 0 ; i < monthSet.length ; ++i) {
			calendar.set(1, monthSet[i]);
			assertEquals(expectedSet[i], calendar.getMonthStartPoint() );
		}	
	}
	public void testLeapYear() {
		calendar.set(2000, 1);
		assertEquals( true, calendar.isLeapYear() );
		calendar.set(1999, 1);		
		assertEquals( false, calendar.isLeapYear() );
	}
}

CalendarFrame.java for GUI

~cpp 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CalendarFrame extends JFrame {

	class CalendarPanel extends JPanel {
		JFrame parent;
		JButton dayUnit[][] = new JButton[6][7];		
		Calendar calendar;
		public CalendarPanel() {
			getContentPane().setLayout( new GridLayout(5,6) );
			for(int i = 0 ; i < 6 ; ++i) {
				for(int j = 0 ; j < 7 ; ++j) {
					dayUnit[i][j] = new JButton("    ");
					dayUnit[i][j].setBackground(Color.WHITE);
					add(dayUnit[i][j]);
				}
			}
		}
		public void set(int year, int month) {
			calendar = new Calendar(year, month);
			int monthArr[] = calendar.getCalendar();
			for(int i = 0 ; i < 6 ; ++i) {
				for(int j = 0 ; j < 7 ; ++j) {
					if( monthArr[i * 7 + j] > 0 && monthArr[i * 7 + j] < 10)
						dayUnit[i][j].setText( "  " + Integer.toString(monthArr[i * 7 + j]) );
					else if( monthArr[i * 7 + j] > 0 && monthArr[i * 7 + j] >= 10)
						dayUnit[i][j].setText( Integer.toString(monthArr[i * 7 + j]) );
					else
						dayUnit[i][j].setText( "    " );
				}
			}
		}
	}
	
	class InputPanel extends JPanel {
		JFrame parent;
		JTextField yearField = new JTextField();
		JTextField monthField = new JTextField();
		JButton submit = new JButton("보기");
		int year, month;
		public InputPanel() {
			setLayout( new GridLayout(1,3) );
			add(yearField);							
			add(monthField);
			add(submit);
			submit.addActionListener( new SubmitListener() );
		}
		
		class SubmitListener implements ActionListener {
			public void actionPerformed(ActionEvent ev) {
				year = Integer.parseInt( yearField.getText() );	
				month = Integer.parseInt( monthField.getText() );
				if( year > 0 && month > 0 )
					calendarPanel.set(year, month);
			}
		}
	}
	
	InputPanel inputPanel = new InputPanel();
	CalendarPanel calendarPanel = new CalendarPanel();

	public CalendarFrame(String arg) {
		super(arg);
		getContentPane().setLayout( new BorderLayout() );
		getContentPane().add(inputPanel, BorderLayout.NORTH);		
		getContentPane().add(calendarPanel, BorderLayout.CENTER);
		setResizable(false);
		setSize(400,260);
		setVisible(true);
	}	

	public static void main(String[] args) {
		new CalendarFrame("만년달력");
	}
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:29:25
Processing time 0.0177 sec