Java实现简单的银行卡功能
package com.company;
import javax.sound.midi.Soundbank;
import java.util.Date;
public class CardInfo {
private String name;
private double balance;
private String passwd;
static String bankName="中国银行";
public CardInfo(String name,double balance,String passwd,Date date)
{
if(balance>0)
{
this.balance+=balance;
}
this.name=name;
this.balance=balance;
this.passwd=passwd;
System.out.println("恭喜你开卡成功");
System.out.println("账户信息:"+bankName+"您的余额为:"+this.balance);
System.out.println("开户时间为+"+date.toLocaleString());
}
public String getName()
{
return name;
}
public void setName(String newName)
{
this.name=newName;
}
public double getBalance()
{
return this.balance;
}
public void setBalance(double balance)
{
this.balance=balance;
}
public String getPasswd()
{
return this.passwd;
}
public void resetPasswd(CardInfo info,String newPasswd)
{
this.passwd=newPasswd;
System.out.println("当前密码已更新为"+info.getPasswd());
}
public void transform(CardInfo des,double amount)
{
if(this.balance>=amount)
{
this.balance-=amount;
des.deposit(amount);
}else
{
System.out.println(this.getName()+"账户余额不足");
}
}
public void withDraw(int amount)
{
if(amount<this.balance)
{
this.balance-=amount;
System.out.println("取款成功"+amount+"元"+"您的剩余余额为"+this.getBalance()+"元");
}else {
System.out.println("余额不足");
}
}
public void deposit(double amount)
{
if(amount<=0)
{
System.out.println("存入的钞票数额不对");
return;
}else {
this.balance += amount;
}
}
@Override
public String toString()
{
return "账户信息"+"\t"+
"银行:"+CardInfo.bankName+"\t"+
"姓名:"+this.name+"\t"+
"余额="+this.balance;
}
public void cardInfo()
{
System.out.println(this.toString());
}
}
public class Main {
public static void main(String[] args) {
System.out.printf("给张三办卡");
CardInfo zhangsan = new CardInfo("张三", 1000, "asd", new Date());
System.out.printf("给李四办卡");
CardInfo lisi=new CardInfo("李四",2000,"zxc",new Date());
System.out.println("张三给李四转账100块");
zhangsan.transform(lisi,100);
System.out.println("查询张三的余额");
System.out.println("张三的余额为"+zhangsan.getBalance());
System.out.println("查询李四的余额");
System.out.println("李四的余额为"+lisi.getBalance());
}
}
参考代码