类的封装 继承和多态
2018-05-01 21:01 lang'sblog 阅读(620) 评论(0) 编辑 收藏 举报package 银行账户管理;
import java.text.SimpleDateFormat; //常用日期操作工具类
import java.util.Calendar; //使用默认时区和语言环境获得一个日历
import java.util.Date;
public class yinhang {
private String acount; //账户
private String name; //姓名
private String time; //时间
private String id; //身份证
private double num; //账户金额
public String getAcount() {
return acount;
}
public void setAcount(String a) {
acount = a;
}
public String getName() {
return name;
}
public void setName(String n) {
name = n;
}
public String getTime() {
return time;
}
public void setTime(String t) {
time = t;
}
public String getId() {
return id;
}
public void setId(String ID) {
id = ID;
}
public double getNum() {
return num;
}
public void setNum(double m) {
num = m;
}
public void cunkuan(double m){
num=num+m;
}
//取款方法
public void qukuan(double m){
if(m>num){
System.out.println("余额不足,不能取款");
}else{
num=num-m;
}
}
//显示开户时间的方法
public String kaiHuTime(){
Date currDate=Calendar.getInstance().getTime();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
this.time=sdf.format(currDate);
return time;
}
//获得账上金额的方法
public double GetAcountNum(){
return num;
}
}
package 银行账户管理;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
yinhang bank=new yinhang();
bank.setAcount("6666");
bank.setId("123456789");
bank.setName("by");
bank.setNum(10000);
Scanner sc=new Scanner(System.in);
System.out.println("请输入你的账户号");
String acount=sc.nextLine();
System.out.println("请输入密码");
String pwd=sc.nextLine();
if(pwd.equals("123456")&& acount.equals(bank.getAcount())){//如果密码正确才能进行以下操作
while(true){
System.out.println("当前你的账户金额为:"+bank.getNum()+"\n请输入以下操作:");
System.out.println("1:存款");
System.out.println("2:取款");
System.out.println("3:显示开户的时间");
System.out.println("4:获取账号的金额");
System.out.println("5:退出当前账户");
Scanner scan=new Scanner(System.in);//同一个变量sc之间可能会有影响 可以重新new 一个
String option=scan.nextLine();
switch(option) //选择服务
{
case "1" :
System.out.println("请输入你要存的钱");
double num=scan.nextDouble();
bank.cunkuan(num);
System.out.println("当前金额为"+bank.getNum());
break;
case "2":
System.out.println("请输入你要取的钱");
double num1=scan.nextDouble();
bank.qukuan(num1);
System.out.println("当前余额为"+bank.getNum());
break;
case "3":
System.out.println("开户时间为:");
System.out.println(bank.kaiHuTime());
break;
case "4":
System.out.println("当前账户金额为:"+bank.getNum());
break;
case "5":
System.out.println("退出系统");
System.exit(0);
break;
default : System.out.println("对不起,您的输入有误");
break;
}
}
}else{
System.out.println("密码或账户不正确,请重新输入");
}
}
}