public class MyDate
{
private int year,month,day;
private static int thisYear;
static
{
thisYear=2018;
}
public MyDate(int year,int month,int day)
{
this.set(year,month,day);
}
public MyDate()
{
this(1970,1,1);
}
public MyDate(MyDate date)
{
this.set(date);
}
public void set(int year,int month,int day)
{
this.year=year;
this.month=(month>=1&&month<=12)?month:1;
this.day=(day>=1&&day<=31)?day:1;
}
public void set(MyDate date)
{
this.set(date.year,date.month,date.day);
}
public int getYear()
{
return this.year;
}
public int getMonth()
{
return this.month;
}
public int getDay()
{
return this.day;
}
public String toString()
{
return year+"年"+String.format("%02d",month)+"月"+String.format("%02d",day)+"日";
}
public static int getThisYear()
{
return thisYear;
}
public static boolean isLeapYear(int year)
{
return year%400==0||year%100!=0&&year%4==0;
}
public boolean isLeapYear()
{
return isLeapYear(this.year);
}
public boolean equals(Object obj)
{
if(this==obj)
return true;
if(obj instanceof MyDate)
{
MyDate p=(MyDate)obj;
return this.year==(p.year)&&this.month==(p.month)&&this.day==(p.day);
}
return false;
}
public static int daysOfMonth(int year,int month)
{
switch(month)
{
case 1:case 3:case 5:case 7:case 8:case 10:case 12:return 31;
case 4:case 6:case 9:case 11:return 30;
case 2:return MyDate.isLeapYear(year)?29:28;
default:return 0;
}
}
public int daysOfMonth()
{
return daysOfMonth(this.year,this.month);
}
public void tomorrow()
{
MyDate date=new MyDate(this);
this.day=this.day%this.daysOfMonth()+1;
if(this.day==1)
{
this.month=this.month%12+1;
if(this.month==1)
date.year++;
}
}
public MyDate yesterday()
{
MyDate date=new MyDate(this);
date.day--;
if(date.day==0)
{
date.month=(date.month-2+12)%12+1;
if(date.month==12)
date.year--;
date.day=daysOfMonth(date.year,date.month);
}
return date;
}
}
class MyDate_ex
{
public static void main(String args[]){
System.out.println("今年是"+MyDate.getThisYear()+",闰年?"+MyDate.isLeapYear(MyDate.getThisYear()));
MyDate d1=new MyDate(2017,12,31);
System.out.println(d1.getYear()+"年,闰年?"+d1.isLeapYear());
MyDate d2=new MyDate(d1);
System.out.println("d1:"+d1+",d2:"+d2+",d1==d2?"+(d1==d2)+",d1.equals(d2)?"+d1.equals(d2));
System.out.print(d1+"的明天是");
d1.tomorrow();
System.out.println(d1+"\n"+d2+"的昨天是"+(d2=d1.yesterday()));
}
}
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.DefaultTableModel;
public class LoanJFrame extends JFrame implements ActionListener{
private JTextField[] texts;
private JSpinner spin_year,spin_month;
private MyDate paydate;
private JButton button;
private DefaultTableModel tablemodel;
public LoanJFrame()
{
super("计算银行贷款,按月还本付息");
this.setBounds(300,240,800,360);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel cmdpanel=new JPanel();
this.getContentPane().add(cmdpanel,"North");
String[] str={"贷款金额","元 贷款利率","%/月 贷款年限","年 还款起始","年","月"};
String[] str_text={"100000","0.5025","1"};
this.texts=new JTextField[str_text.length];
int i=0;
for(i=0;i<str_text.length;i++)
{
cmdpanel.add(new JLabel(str[i]));
cmdpanel.add(this.texts[i]=new JTextField(str_text[i],6));
}
for(;i<str.length;i++)
cmdpanel.add(new JLabel(str[i]));
Calendar start=Calendar.getInstance();
int year=start.get(Calendar.YEAR);
int month=start.get(Calendar.MONTH)+1;
month=month%12+1;
if(month==1)
year++;
int day=MyDate.daysOfMonth(year,month);
this.paydate=new MyDate(year,month,day);
this.spin_year=new JSpinner(new SpinnerNumberModel(year,year,year+2,year+1));
cmdpanel.add(this.spin_year,7);
this.spin_month=new JSpinner(new SpinnerNumberModel(month,1,12,1));
cmdpanel.add(this.spin_month,9);
cmdpanel.add(this.button=new JButton("计算"));
this.button.addActionListener(this);
String[] titles={"还款日期","本金余额(元)","月还本金(元)","月还利息(元)","月还本息(元)"};
this.tablemodel=new DefaultTableModel(titles,1);
JTable jtable=new JTable(this.tablemodel);
this.getContentPane().add(new JScrollPane(jtable));
this.setVisible(true);
}
public void actionPerformed(ActionEvent event){
double leavings=Double.parseDouble(""+texts[0].getText());
double rate=Double.parseDouble(""+texts[01].getText());
int months=Integer.parseInt(texts[2].getText())*12;
double pay=leavings/months;
this.tablemodel.setRowCount(months);
int year=(Integer)this.spin_year.getValue();
int month=(Integer)this.spin_month.getValue();
MyDate alterdate=new MyDate(year,month,MyDate.daysOfMonth(year,month));
if(alterdate.compareTo(this.paydate)<0)
{
JOptionPane.showMessageDialog(this,"设置还款日期为"+alterdate.toString()+",月份错误,早于下月。");
return;
}
for(int row=0;row<months;row++)
{
this.tablemodel.setValueAt(alterdate.toString(),row,0);
this.tablemodel.setValueAt(String.format("%9.2f",leavings),row,1);
this.tablemodel.setValueAt(String.format("%9.2f",pay),row,2);
this.tablemodel.setValueAt(String.format("%9.2f",leavings*rate*0.01),row,3);
this.tablemodel.setValueAt(String.format("%9.2f",pay+leavings*rate*0.01),row,4);
month=month%12+1;
if(month==1)
year++;
alterdate=new MyDate(year,month,MyDate.daysOfMonth(year,month));
leavings-=pay;
}
}
public static void main(String args[]){
new LoanJFrame();
}
}