Account acc= bank.getAccount("3");
System.out.println("余额=" + acc.getBalance());
acc.getMoney(100);
System.out.println("余额=" + acc.getBalance());
acc.getMoney(1000);
System.out.println("余额=" + acc.getBalance());
}
if(bank.existAccount("4")){
Account acc= bank.getAccount("4");
System.out.println("余额=" + acc.getBalance());
acc.getMoney(100);
System.out.println("余额=" + acc.getBalance());
acc.getMoney(1000);
System.out.println("余额=" + acc.getBalance());
acc.getMoney(10000);
System.out.println("余额=" + acc.getBalance());
}
System.out.println(bank.getAccountNum());
System.out.println(bank.getAllMoney());
System.out.println(bank.getAllOverdraft());
System.out.println(bank.getCreditAccountNum());
System.out.println(bank.getSavingAccountNum());
}
public static void genAccount(){
String s = "1000 0000 0000 000";
Account a = null;
for(int i = 1; i11; i ++){
if((i2) == 0){
a = new CreditAccount(s + String.valueOf(i), "账户" + String.valueOf(i));
} else {
a = new SavingAccount(s + String.valueOf(i), "账户" + String.valueOf(i));
}
bank.addAccount(a);
}
}
public static void showNowAccount(){
for(Account account : bank.getAllAccount()){
System.out.println(account);
}
}
}
// 面向对象,看多写多了就会了,简单的随便写的,有错误先不用太纠结 。
java问题,实现账户A和B之间的转账功能 , 哪里错误呢 , 求大神指教,我是小白 。//你好,你这个题目,有两天了,不太好回答,因为你这个题目有敏感字,对于你提问来说问题不大!
//对于回答者会无休止的审核,我想了一下,用馒头来模拟你的答案!其实是一样的!
//我重新写了,你看看.我注释的很详细,不懂可以继续追问!
//测试
public class TestThread {
public static void main(String[] args) {
Buns mt = new Buns();//馒头类!
new Thread(new Res(mt, 12),"张三").start();
new Thread(new Res(mt, 12),"------------------李四").start();
}
}
// 馒头类!
class Buns {
private int sum,count;// 馒头数量,两个!
private boolean flag;// 判断!
// 空参构造!
Buns() {
this(1000);
}
// 形参构造!
Buns(int sum) {
this.sum = sum;
this.count=sum;
}
// 进馒头!
synchronized void out(int n) {
while (flag)// 判断真就挂起!
try {
this.wait();
} catch (Exception e) {
}
try {Thread.sleep(100);} catch (Exception e) {}
System.out.println(Thread.currentThread().getName() + "付出馒头=" +n+"个;剩余:"+ (sum -= n));
flag = true;
this.notify();// 唤醒对方!
}
// 出馒头!
synchronized void add(int n) {
while (!flag)// 判断,假就挂起
try {
this.wait();
} catch (Exception e) {
}
try {Thread.sleep(100);} catch (Exception e) {}
System.out.println(Thread.currentThread().getName() + "进入馒头="+n+"个;剩余:" + (count += n));
flag = false;
this.notify();// 唤醒对方!
}
// 查馒头!
int Sum() {
return sum;
}
// 查馒头!
int Count() {
return count;
}
}//线程Runnable接口子类!
class Res implements Runnable {
private boolean flag;//判断!
private Buns a;//馒头类!
Res(Buns a, boolean flag) {
this.a = a;
this.flag = flag;
}
public void run() {//重写!
if (flag)//如果真就+馒头!
add();
else
out();//假就-馒头!
}
private void add() {
while (a.Sum()100) //判断不会越界!
a.out(5);//每次-5个!
推荐阅读
- 干新媒体运营收入如何,新媒体运营一个月能挣多少
- js判断类名,js判断类型为数组
- mongodb集群同步数据重复,mongodb 重复数据
- wow换服务器,wow换服务器剧情任务重置吗
- vb.net文本框字体 vb字体对话框
- redis单机与集群搭建,redis单机版搭建
- 英国模拟军事游戏手游,好玩的军事模拟游戏
- vb.net加入音乐 如何在vb中添加音乐
- 包含postgre日期取年的词条