java编程建立银行账户类_java编程问题...求速解答.....定义三个银行账号类.每个类都有账号 密码等private属...

java编程问题...求速解答.....定义三个银行账号类.每个类都有账号 密码等private属

关注:251  答案:1  mip版

解决时间 2021-02-02 06:17

e6cb1a03ad541b3098697807b7bf1798.png

提问者神经质

2021-02-01 08:49

java编程问题...求速解答.....定义三个银行账号类.每个类都有账号 密码等private属性,每个类都有存取转账三个public方法.分别为银行,工行,交行三个银行。工行、交行类要继承银行类,并实现方法重载。演示不同银行帐号之间转账,用帐号存取款,查询余额等。

最佳答案

e6cb1a03ad541b3098697807b7bf1798.png

二级知识专家蓝莓九栀

2021-02-01 08:55

稍等一会,我帮你写

银行类

public class Bank {

private double count;//余额

private static String  account="test";//账户

private static String password="test";//密码

//取款

public void getMoney(String account,String password,double num){

if(login(account,password)){

if(count

System.out.println("银行余额不足!");

}else{

count=count-num;

System.out.println("当前余额: "+count);

}

}else{

System.out.println("账号或密码错误!");

}

}

//存款

public void setMoney(String account,String password,double num){

if(login(account,password)){

count=count+num;

System.out.println("当前余额: "+count);

}else{

System.out.println("账号或密码错误!");

}

}

//查询余额

public void queryMoney(String account,String password){

if(login(account,password)){

System.out.println("当前余额: "+count);

}else{

System.out.println("账号或密码错误!");

}

}

//登录

private static boolean login(String acc,String pwd){

if(account.equals(acc)&&password.equals(pwd)){

return true;

}else{

return false;

}

}

}

工行类

public class ICBC extends Bank {

private double count;//余额

private static String  account="test";//账户

private static String password="test";//密码

//取款

public void getMoney(String account,String password,double num){

if(login(account,password)){

if(count

System.out.println("工行余额不足!");

}else{

count=count-num;

System.out.println("工行当前余额: "+count);

}

}else{

System.out.println("工行账号或密码错误!");

}

}

//存款

public void setMoney(String account,String password,double num){

if(login(account,password)){

count=count+num;

System.out.println("工行当前余额: "+count);

}else{

System.out.println("工行账号或密码错误!");

}

}

//查询余额

public void queryMoney(String account,String password){

if(login(account,password)){

System.out.println("工行当前余额: "+count);

}else{

System.out.println("工行账号或密码错误!");

}

}

//登录

private static boolean login(String acc,String pwd){

if(account.equals(acc)&&password.equals(pwd)){

return true;

}else{

return false;

}

}

}

交行类

public class COMM extends Bank {

private double count;//余额

private static String  account="test";//账户

private static String password="test";//密码

//取款

public void getMoney(String account,String password,double num){

if(login(account,password)){

if(count

System.out.println("交行余额不足!");

}else{

count=count-num;

System.out.println("交行当前余额: "+count);

}

}else{

System.out.println("交行账号或密码错误!");

}

}

//存款

public void setMoney(String account,String password,double num){

if(login(account,password)){

count=count+num;

System.out.println("交行当前余额: "+count);

}else{

System.out.println("交行账号或密码错误!");

}

}

//查询余额

public void queryMoney(String account,String password){

if(login(account,password)){

System.out.println("交行当前余额: "+count);

}else{

System.out.println("交行账号或密码错误!");

}

}

//登录

private static boolean login(String acc,String pwd){

if(account.equals(acc)&&password.equals(pwd)){

return true;

}else{

return false;

}

}

}

测试类

public class Test {

public static void main(String[] args) {

Bank b=new Bank();

ICBC i=new ICBC();

COMM c=new COMM();

//存款

b.setMoney("test", "test", 100);

i.setMoney("test", "test", 100);

c.setMoney("test", "test", 100);

//取款

b.getMoney("test", "test", 1000);

b.getMoney("test", "test", 10);

i.getMoney("test", "test", 1000);

i.getMoney("test", "test", 10);

c.getMoney("test", "test", 1000);

c.getMoney("test", "test", 10);

//查余额

b.queryMoney("test", "test");

i.queryMoney("test", "test");

c.queryMoney("test", "test");

}

}

我要举报

如以上问答内容为低俗/色情/暴力/不良/侵权的信息,可以点下面链接进行举报,我们会做出相应处理,感谢你的支持!

→点此我要举报以上信息!←

推荐资讯

大家都在看

为0;2.实现存款、取款、查询余额功能;3.考虑多线程并发操作问题。 代码如下: ``` import java.util.concurrent.locks.ReentrantLock; public class BankAccount { private String accountNumber; //账户号码 private double balance; //账户余额 private ReentrantLock lock; //重入锁 public BankAccount(String accountNumber) { this.accountNumber = accountNumber; this.balance = 0; this.lock = new ReentrantLock(); } //存款 public void deposit(double amount) { lock.lock(); //加锁 try{ balance += amount; System.out.println(Thread.currentThread().getName() + " 存款 " + amount + " 元,余额 " + balance + " 元。"); }finally { lock.unlock(); //解锁 } } //取款 public void withdraw(double amount) { lock.lock(); //加锁 try{ if(balance >= amount){ balance -= amount; System.out.println(Thread.currentThread().getName() + " 取款 " + amount + " 元,余额 " + balance + " 元。"); }else{ System.out.println(Thread.currentThread().getName() + " 余额不足,无法取款!"); } }finally { lock.unlock(); //解锁 } } //查询余额 public double getBalance() { return balance; } } ``` 主程序如下: ``` public class Main { public static void main(String[] args) { //创建账户 BankAccount account = new BankAccount("123456"); //创建存款线程 Thread depositThread1 = new Thread(() -> { account.deposit(1000); }, "存款线程1"); Thread depositThread2 = new Thread(() -> { account.deposit(2000); }, "存款线程2"); //创建取款线程 Thread withdrawThread1 = new Thread(() -> { account.withdraw(500); }, "取款线程1"); Thread withdrawThread2 = new Thread(() -> { account.withdraw(1500); }, "取款线程2"); //启动线程 depositThread1.start(); depositThread2.start(); withdrawThread1.start(); withdrawThread2.start(); //等待线程执行完毕 try { depositThread1.join(); depositThread2.join(); withdrawThread1.join(); withdrawThread2.join(); } catch (InterruptedException e) { e.printStackTrace(); } //查询余额 System.out.println("账户余额为:" + account.getBalance() + " 元。"); } } ``` 运行结果如下: ``` 存款线程1 存款 1000.0 元,余额 1000.0 元。 存款线程2 存款 2000.0 元,余额 3000.0 元。 取款线程1 取款 500.0 元,余额 2500.0 元。 取款线程2 取款 1500.0 元,余额 1000.0 元。 账户余额为:1000.0 元。 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值