[java]代码库
package itcast;
class Bank {
static String bankName;//定义静态变量银行名称
private String name;//用户名
private String password;//密码
private double balance;//用户余额
private double turnover;//交易额
//进入银行时,欢迎语
static void welcome() {
System.out.println("欢迎来到"+bankName);
}
//通过构造方法进行开户操作
public Bank(String name,String password,double turnover) {
this.name=name;
this.password=password;
this.turnover=turnover;
this.balance=turnover-10;
System.out.println(name+"开户成功,账户余额"+balance);
}
//存款
public void deposit(double turnover) {
balance=balance+turnover;
System.out.println(name+"您好,您的账户 已存入"+turnover+
"元."+"当前账户余额"+balance+"元");
}
//取款
public void withdrawal(String password,double turnover) {
if(this.password!=password) {
System.out.println("您输入的密码错误");
return;
}
if(balance-turnover>0) {
balance=balance-turnover;
System.out.println(name+"您好,您的账户 已取出"+turnover+"元,"
+"当前余额"+balance+"元");
}else {
System.out.println("对不起,账户余额不足!");
}
}
//离开时
static void welcomNext() {
System.out.println("请携带好您的随身财物,欢迎下次光临"+bankName);
}
}
public class Trade {
public static void main(String[] args) {
Bank.bankName="中国农业银行";
Bank.welcome();
//张三去银行开户,调用构造方法
Bank bank=new Bank("张三","123456",10000.0);
//进行存款
bank.deposit(1200.00);
//取款时密码输入错误
bank.withdrawal("132456",20000.0);
//取款时密码正确,余额不足
bank.withdrawal("123456",20000.0);
//取款时密码正确,余额充足
bank.withdrawal("123456",5000.0);
//离开银行
Bank.welcomNext();
}
}
初级程序员
by: wanche12 发表于:2020-12-29 16:32:31 顶(0) | 踩(0) 回复
这也太简易了吧!!!
回复评论