摘要:
十一期间空闲时间很多,整理了一些东西。这个是很早的时候看一本书上的程序。是一个关于数据结构的。
日志ADT描述:
一个日志ADT由一组输入项组成,一个输入项对应着这个月的某一天;
在此ADT中,元素是一些与这个月份相关的输入项,并且这个结构是线性的:
输入项是按照相应的日期和相同的顺序排列的。
LogBook.java 源文件
/*
日志ADT描述:
一个日志ADT由一组输入项组成,一个输入项对应着这个月的某一天;
在此ADT中,元素是一些与这个月份相关的输入项,并且这个结构是线性的:
输入项是按照相应的日期和相同的顺序排列的。
*/
import java.io.*;
import java.util.*;
public class LogBook
{
//数据成员
private int logMonth,
logYear;
private int [] entry = new int [31];
private GregorianCalendar logCalendar; //日历类型
//构造函数
public LogBook (int month,int year)
{
int i;
if (month >= 1 && month <= 12)
{
logCalendar = new GregorianCalendar (year, month - 1, 1); //由于在GregorianCaldar中,月份代码为0-11,故要month-1
logMonth = month;
logYear = year;
for (i = 1; i < this.daysInMonth(); i++) //把此月中的输入项都设置成0;
{
entry[i - 1] = 0;
}
}
}
public LogBook ()
{
logCalendar = new GregorianCalendar (); //设置为当前月份
logMonth = logCalendar.get(Calendar.MONTH ) + 1;
logYear = logCalendar.get(Calendar.YEAR);
for (int i = 1; i < this.daysInMonth(); i++) //把此月中的输入项都设置成0;
{
entry[i - 1] = 0;
}
}
//添加输入项
//为某一天输入日志
public void putEntry (int day, int value)
{
if ((day >= 1) && (day <= this.daysInMonth()))
{
entry[day - 1] = value;
}
}
//默认为当前 天添加输入项,重载putEntry
public void putEntry (int value )
{
int day = logCalendar.get(Calendar.DAY_OF_MONTH );
entry[day - 1] = value;
}
public int getEntry(int day) //得到某一天的日志
{
int value = entry[day - 1];
if ((day >= 1) && (day <= this.daysInMonth()))
{
return value;
}
else
{
return -1;
}
}
public int year() //返回年
{
return this.logYear;
}
public int month() //返回月
{
return this.logMonth;
}
public int daysInMonth() //返回某一个月份中的天数
{
int i = this.logMonth;
if (i == 4 || i == 6 || i == 9 || i == 11 )
{
return 30;
}
else
{
if (i == 2 )
{
if (this.leapYear() )
{
return 29;
}
else
{
return 28;
}
}
else
{
return 31;
}
}
}
private boolean leapYear() //闰年?
{
return (logCalendar.isLeapYear(this.logYear));
}
/*
显示日历
首先打印出 星期 行
然后 先作判断
如果这个月的第一天不是星期天,即dayOfWeek (1 ) != 0
就打印出dayOfWeek (1 )个/t,这样使每个月的1号,和其对应的星期对应起来。
接着打印出 日期 和 其对应的输入项。
每打印到周六的时候(即dayOfWeek (day) == 6)就一个换行
*/
public void displayCalendar () //显示日历
{
System.out.println ("/tMonth/Year:" + logMonth + "/" + logYear + "/n");
System.out.println ("/tSun/tMon/tTue/tWed/tThu/tFri/tSat");
String str = "";
if (dayOfWeek (1 ) != 0 )
{
for (int i = 0; i < dayOfWeek (1); i++ )
str = str + "/t";
}
System.out.print (str );
for (int day = 1; day <= daysInMonth (); day++ )
{
System.out.print ("/t" + day + " " + entry [day - 1] );
if (dayOfWeek (day) == 6 )
{
System.out.println ();
}
}
System.out.println ();
System.out.println ();
}
private int dayOfWeek (int day ) //返回星期代码
{
logCalendar.set (logYear, logMonth - 1, day ); //设置成日历
return logCalendar.get (Calendar.DAY_OF_WEEK ) - 1; //返回星期的代码
}
}
测试文件:TestLogBook.java
public class TestLogBook
{
public static void main (String [] srgs )
{
LogBook logBook = new LogBook (10, 2005 );
for (int i = 1; i <= logBook.daysInMonth (); i++ )
{
logBook.putEntry (i, logBook.daysInMonth () - i + 1 );
}
logBook.displayCalendar ();
LogBook logBook2 = new LogBook ();
logBook2.putEntry (10 );
logBook2.displayCalendar ();
}
}