import java.util.Date;
public class Account {
//创建一个Account类,能够建立账户、存钱、取钱、转账
private String name,Id;
private double amount;
private Date datetime;
public Account() {
this.name="";
this.Id="";
this.amount=0.0;
this.datetime=new Date();
}
public Account(String name,String Id,double amount) {
this.name=name;
this.Id=Id;
this.amount=amount;
this.datetime=new Date();
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name=name;
}
public String getId() {
return this.Id;
}
public void setId(String Id) {
this.Id=Id;
}
public double getAmount() {
return this.amount;
}
public void setAmount(double amount) {
this.amount=amount;
}
public Date getDatetime() {
return this.datetime;
}
public void setDatetime(Date datetime) {
this.datetime=datetime;
}
public void deposit(double money1) {
this.amount=this.amount+money1;
System.out.println("存入"+money1+"元钱后的账户金额"+this.amount);
}
public void withdraw(double money2) {
this.amount=this.amount-money2;
System.out.println("取出"+money2+"元钱后账户金额"+this.amount);
}
public void transfer(Account other,double money3) {
if(this.amount<money3) {
System.out.println("账户余额不足,不可转账");
}
else {
this.amount=this.amount-money3;
other.amount=other.amount+money3;
System.out.println("转出者"+this.name+"的账户余额"+this.amount);
System.out.println("转入者"+other.name+"的账户余额"+this.amount);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Account zhangsan=new Account("张三","001",1000);
Account Lisi=new Account("李四","002",300);
zhangsan.deposit(500);
Lisi.deposit(500);
zhangsan.withdraw(300);
zhangsan.transfer(Lisi, 300);
}
}
//信用卡账户
public class CraditCardAccount extends Account {
private double LineOfCradit;
public CraditCardAccount(String name,String Id,double amount,double LineOfCradit) {
super(name,Id,amount);
this.LineOfCradit=LineOfCradit;
}
public double getLineOfCradit() {
return this.LineOfCradit;
}
public void setLineOfCradit(double LineOfCradit) {
this.LineOfCradit=LineOfCradit;
}
public void withdraw(double money) {
if(money>this.getAmount()+this.getLineOfCradit()) {
System.out.println("超过额度,不可取钱");
return;
}
this.setAmount(this.getAmount()-money);
}
public void transfer(Account other,double money) {
if(money>this.getAmount()+this.getLineOfCradit()) {
System.out.println("超出额度,不可转账");
return;
}
this.setAmount(this.getAmount()-money);
other.setAmount(other.getAmount()+money);
}
}
//定期存款账户
import java.util.Date;
public class FixedAccount extends Account {
private int term;//term是存期用天数来表示
public FixedAccount(String name,String Id,double amount,int term) {
super(Id,name,amount);
this.term=term;
}
public int getTerm() {
return this.term;
}
public void setTerm(int term) {
this.term=term;
}
public void deposit(double money) {
System.out.println("不可存钱");
}
public void withdraw(double money) {
Date da=new Date();//time是存储时间差
int time=(int)(da.getTime()-this.getDatetime().getTime())/1000/3600/24;//getTime()方法是系统自带的方法,用于显示当前系统的时间,结果为毫秒进而换算成天
if(time<getTerm()) {
System.out.println("未到时间,不能取钱");
return;
}
if(money!=this.getAmount()) {
System.out.println("定期账户必须一次性取完");
return;
}
this.setAmount(0);
}
public void transfer(FixedAccount other,double money) {
System.out.println("不可转账");
}
}
//测试类
public class Bank {
public static void main(String[]args) {
FixedAccount Wangwu=new FixedAccount("001","张三",3000,3);
CraditCardAccount Zhaoliu=new CraditCardAccount("002","李四",4000,10000);
Wangwu.withdraw(200);
Wangwu.withdraw(3000);
Wangwu.deposit(5000);
Zhaoliu.withdraw(12000);
Zhaoliu.withdraw(20000);
Zhaoliu.deposit(6000);
Zhaoliu.deposit(20000);
}
}
银行账户系统——java程序
最新推荐文章于 2024-08-18 21:36:31 发布