java定义存款方法代码 java存款取款
JAVA写个存款取款功能 程序
public class Bank
创新互联专注于古丈网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供古丈营销型网站建设,古丈网站制作、古丈网页设计、古丈网站官网定制、重庆小程序开发公司服务,打造古丈网络公司原创品牌,更为您提供古丈网站排名全网营销落地服务。
{
private long balance=0;
public Bank(){}
public void draw(int x) //存款
{
if(x=balance)
{
balance=balance-x;
System.out.println("Draw "+x+" Yuan success!");
}
else
{
System.out.println("The balance remain is not enough!");
}
}
public void deposit(int x) //取款
{
balance=balance+x;
System.out.println("Deposit "+x+" Yuan success!");
}
public void check() //查询
{
System.out.println("The balance remained is "+balance+" Yuan
");
}
public static void main(String[] args)
{
Bank bank1=new Bank();
bank1.deposit(10000);
bank1.check();
bank1.draw(3000);
bank1.check();
bank1.draw(8000);
bank1.deposit(5000);
bank1.check();
bank1.draw(8000);
bank1.check();
}
}
如何用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 定义一个类实现银行账户 包括变量 "帐号""存款余额",方法:"存款","取款"和"查询
private double money=0L ;
public void addMoney(double money){
this.money=this.money+ money;
System.out.println("余额"+this.money);
}
public void outMoney(double money){
if(moneythis.money){
System.out.println("余额不足!");
}else{
this.money=this.money- money;
System.out.println("取出"+money+"元,余额"+this.money);
}
}
public void getMoney(){
System.out.println("余额"+this.money);
}
文章标题:java定义存款方法代码 java存款取款
转载注明:http://azwzsj.com/article/docgjce.html