189 8069 5689

java代码活期存款 活期存期代码

如何用Java线程实现银行的存款取款问题最好能写出编出的具体程序

AccountTest.java class BankAccount //定义银行账户类BankAccount{private static int amount =2000; //账户余额最初为2000public void despoit(int m) //定义存款的方法{amount=amount+m;System.out.println("晓明存入["+m+"元]");}public void withdraw(int m) //定义取款的方法{amount=amount-m;System.out.println("张新取走["+m+"元]");if(amount0)System.out.println("***余额不足!***);public int balance() //定义得到账户余额的方法{return amount;}}classicCustomerextendsThread {String name;BankAccount bs; //定义一个具体的账户对象public Customer(BankAccount b,String s){name=s;bs=b;}public static void cus(String name,BankAccount bs) //具体的账户操作方法{if(name.equals("小明")) //判断用户是不是小明{try{for(int i=0;i6;i++) //用户晓明则向银行存款6次,每次1000元 {Thread.currentThread().sleep((int)(Math.random()*300));bs.despoit(1000);}}catch(InterruptedException e){}}else{try{for(int i=0;i6;i++) //用户不是小明则从银行取款6次,每次1000元{Thread.currentThread().sleep((int)(Math.random()*300));bs.withdraw(1000); }}catch(InterruptedException e){} }}public void run() //定义run方法}cus(name,bs); }}public classAccountTest{public static void main(String [] args) throws InterruptedException{BankAccount bs=new BankAccount();Customer customer1=new Customer(bs,"小明");Customer customer2=new Customer(bs,"张新");Thread t1=new Thread(customer1);Thread t2=new Thread(customer2);t1.Start();t2.start();Thread.currentThread().sleep(500);}}

创新互联建站是一家集网站建设,广安企业网站建设,广安品牌网站建设,网站定制,广安网站建设报价,网络营销,网络优化,广安网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

用java线程同步实现银行取款和存款。

你的代码问题很多!

帮你修改后:

class Account {

private static float Balance = 200f;//线程共享的钱

public synchronized static void deposit(float amt) {

Balance = Balance + amt;

try {

Thread.sleep(10);// 模拟其它处理所需要的时间,比如刷新数据库等

}

catch (InterruptedException e) {

}

}

public static float getBalance() {

return Balance;

}

public static void setBalance(float balance) {

Balance = balance;

}

public synchronized static void withdraw(float bmt) {

Balance -= bmt;

try {

Thread.sleep(10);// 模拟其它处理所需要的时间,比如刷新数据库等

}

catch (InterruptedException e) {

}

}

String name;

public Account(String name) {

this.name = name;

}

}

public class AccountTest {

public static void main(String[] args) {

Customer customerA = new Customer("A");

Customer customerB = new Customer("B");

customerA.start();

customerB.start();

System.out.println();

try {

customerA.join();//等待A线程执行完毕

customerB.join();//等待B线程执行完毕

}

catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println("最后总钱数:" + Account.getBalance());

}

}

class Customer extends Thread {

public Customer(String string) {

super(string);

}

@Override

public void run() {

System.out.println(currentThread().getName());

if (currentThread().getName().equals("A"))

Account.deposit(100.0f);//A线程加100

else if (currentThread().getName().equals("B"))

Account.withdraw(50.0f);//B线程减50

}

}

用java语言编写一个小型的银行系统代码

private int balance = 0;

private  String username = "A";

private  String password = "B";

public void bank() {

Scanner scan = new Scanner(System.in);

String  temp;

while (true) {

System.out.println("输入账号:");

if (scan.hasNext()) {

temp = scan.next();

if (temp.equals(username)) {

break;

} else {

System.out.println("输入错误");

}

}

}

while (true) {

System.out.println("输入密码:");

if (scan.hasNext()) {

temp = scan.next();

if (temp.equals(password)) {

break;

} else {

System.out.println("输入错误");

}

}

}

System.out.println("登录成功");

while (true) {

System.out.println("输入操作:");

if (scan.hasNext()) {

temp = scan.next();

switch (temp) {

case "存款":

int x = 0;

while (true) {

System.out.println("输入存款金额:");

if (scan.hasNextInt()) {

x = scan.nextInt();

break;

} else {

System.out.println("输入错误");

scan.next();

}

}

balance += x;

break;

case "取款":

int y = 0;

while (true) {

System.out.println("输入取款金额:");

if (scan.hasNextInt()) {

y = scan.nextInt();

if (balance  y) {

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

continue;

}

break;

} else {

System.out.println("输入错误");

scan.next();

}

}

balance -= y;

break;

case "余额":

System.out.println("余额:" + balance);

break;

case "终止":

System.exit(0);

default:

System.out.println("未知操作");

}

}

}


分享名称:java代码活期存款 活期存期代码
链接URL:http://jkwzsj.com/article/dodjjic.html

其他资讯