源代码:
package 排序;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Account {
String zhanghao,xingming,kahushijian,shenfenzhenghao;
double yue;
Scanner myb=new Scanner (System.in);
private String kaihushijian;
public void kaihu() {
System.out.println("请输入开户账号、姓名、身份证号码、开户金额");
zhanghao=myb.next();
xingming=myb.next();
shenfenzhenghao=myb.next();
yue=myb.nextLong();
System.out.println("账户建立成功!");
}
public long cunkuan() {
System.out.println("请输入存款金额:");
long shue=myb.nextLong();
yue=yue+shue;
System.out.println("存款成功!");
System.out.println("余额:"+yue);
return (long) yue;
}
public long qukuan() {
System.out.println("请输入取款金额");
long jine=myb.nextLong();
yue=yue-jine;
if(yue>=0) {
System.out.println("取款成功");
System.out.println("余额:"+yue);}
else
System.out.println("对不起,您的余额不足!");
return (long) yue;
}
public String kaiHushijian(){
Date date = new Date();
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
this.kaihushijian=dateFormat.format(date);
return kaihushijian;
}
public void chaxun() {
System.out.println("账号:"+zhanghao);
System.out.println("姓名:"+xingming);
System.out.println("开户时间"+kaiHushijian());
System.out.println("身份证号码:"+shenfenzhenghao);
System.out.println("余额"+yue);
}
public void finalize()//销户
{
System.out.println("销户成功!");
}
@SuppressWarnings({ "resource" })
public static void main(String[] args)
{
Account a=new Account();
for(int i = 1;i<=6;i++) {
System.out.println("1.开户");
System.out.println("2.存款");
System.out.println("3.取款");
System.out.println("4.查询");
System.out.println("5.销户");
System.out.println("0.退出 ");
Scanner sc=new Scanner(System.in);
System.out.println("请输入您要选择操作的序号:");
int option=sc.nextInt();
if(option==0)
break;
switch(option){//选择服务
case 1 :
a.kaihu();
a.chaxun();
break;
case 2:
a.cunkuan();
break;
case 3:
a.qukuan();
break;
case 4:
a.chaxun();
break;
case 5:
a.finalize();
break;
default : System.out.println("对不起,您的输入有误");
break;
}
}
}
}
实验结果:
实验心得:
通过此次实验,我掌握了类的封装的含义,还有如何在程序中使用,但在实验过程中,还是遇到很多困难,本次程序长,编译有一些难度,但通过多次实验掌握了类的封装如何用。