求java程序:用线程实现多人对同一银行账号的存、取款业务 这是一个作业题!
2个回答
展开全部
package demo.Threaddemo;
import java.math.BigDecimal;
public class person implements Runnable{
private String name;
private Accounts account;
private BigDecimal saveMoney;
private BigDecimal takeMoney;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Accounts getAccount() {
return account;
}
public void setAccount(Accounts account) {
this.account = account;
}
public BigDecimal getSaveMoney() {
return saveMoney;
}
public void setSaveMoney(BigDecimal saveMoney) {
this.saveMoney = saveMoney;
}
public BigDecimal getTakeMoney() {
return takeMoney;
}
public void setTakeMoney(BigDecimal takeMoney) {
this.takeMoney = takeMoney;
}
@Override
public String toString() {
return "person [account=" + account + ", name=" + name + ", saveMoney="
+ saveMoney + ", takeMoney=" + takeMoney + ", getAccount()="
+ getAccount() + ", getName()=" + getName()
+ ", getSaveMoney()=" + getSaveMoney() + ", getTakeMoney()="
+ getTakeMoney() + ", getClass()=" + getClass()
+ ", hashCode()=" + hashCode() + ", toString()="
+ super.toString() + "]";
}
public person(String name,Accounts accounts, BigDecimal saveMoney,
BigDecimal takeMoney) {
super();
this.name = name;
if(accounts==null){
account = new Accounts();
account.setToalMoney(BigDecimal.valueOf(0));
}else {
account= accounts;
}
this.saveMoney = saveMoney;
this.takeMoney = takeMoney;
}
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("正在进行存取操作......");
if(null!=saveMoney&&!"".equals(saveMoney)){
account.saveMoney(saveMoney);
}
if(null!=takeMoney&&!"".equals(takeMoney)){
account.takeMoney(takeMoney);
}
}
public static void main(String[] args) {
Accounts accounts = new Accounts();
accounts.setToalMoney(BigDecimal.valueOf(0));
person a1 = new person("pipi", accounts, BigDecimal.valueOf(50), BigDecimal.valueOf(30));
person a2 = new person("pipi", accounts, BigDecimal.valueOf(20), BigDecimal.valueOf(30));
person a3 = new person("pipi", accounts, BigDecimal.valueOf(10), BigDecimal.valueOf(80));
new Thread(a1).start();
new Thread(a2).start();
new Thread(a3).start();
}
}
package demo.Threaddemo;
import java.math.BigDecimal;
/**
* 用户的账户封装
* @author Tom
*
*/
public class Accounts {
private BigDecimal toalMoney;
public BigDecimal getToalMoney() {
return toalMoney;
}
public void setToalMoney(BigDecimal toalMoney) {
this.toalMoney = toalMoney;
}
public synchronized void saveMoney(BigDecimal money){
System.out.println("当前余额为:"+toalMoney+"正在存钱..."+money);
toalMoney = toalMoney.add(money);
System.out.println("当前余额为:"+toalMoney+"存钱结束....");
}
public synchronized void takeMoney(BigDecimal money){
System.out.println("当前余额为:"+toalMoney+"正在取钱..."+money);
if(toalMoney.compareTo(money)==-1){
System.out.println("当前余额不足");
return;
}
toalMoney = toalMoney.subtract(money);
System.out.println("当前余额为:"+toalMoney+"取钱结束....");
}
public static void main(String[] args) {
Accounts accounts = new Accounts();
accounts.setToalMoney(BigDecimal.valueOf(23.44));
accounts.takeMoney(BigDecimal.valueOf(1.44));
BigDecimal a = BigDecimal.valueOf(1.00);
BigDecimal b = BigDecimal.valueOf(2.00);
System.out.println(b.compareTo(a));
}
}
展开全部
public class Person implements Runnable{
private Account account;
private String username;
private double savemoneyn;
private double takemoneyn;
public Person(){}
public Person(String username,Account account,double savemoney,double takemoney){
this. username=username;
this. account=account;
this. savemoney=savemoney;
this. takemoney=takemoney;
}
public void run(){
System.out.println(this.username+"正在进行存取操作");
Thread.currentThread().sleep(1000);
account.saveMoney(savemoneyn);
account.takeMoney(takemoneyn);
System.out.println(this.username+"操作结束");
}
public static void main(String[] args) {
Account a=new Account ();
Person p1=new Person("p1",a,300,200);
Person p2=new Person("p1",a,500,100);
Person p3=new Person("p1",a,200,600);
new Thread(p1).start();
new Thread(p2).start();
new Thread(p3).start();
}
}
class Account {
double money;
public synchronized void saveMoney(double m){
amoney+=m;
System.out.println("存款成功,余额为:"+amoney);
}
public synchronized void takeMoney(double m){
double tmp=amoney-m;
if(tmp>0){
amoney=tmp;
System.out.println("取款成功,余额为:"+amoney);
}
else
System.out.println("超出余额");
}
}
private Account account;
private String username;
private double savemoneyn;
private double takemoneyn;
public Person(){}
public Person(String username,Account account,double savemoney,double takemoney){
this. username=username;
this. account=account;
this. savemoney=savemoney;
this. takemoney=takemoney;
}
public void run(){
System.out.println(this.username+"正在进行存取操作");
Thread.currentThread().sleep(1000);
account.saveMoney(savemoneyn);
account.takeMoney(takemoneyn);
System.out.println(this.username+"操作结束");
}
public static void main(String[] args) {
Account a=new Account ();
Person p1=new Person("p1",a,300,200);
Person p2=new Person("p1",a,500,100);
Person p3=new Person("p1",a,200,600);
new Thread(p1).start();
new Thread(p2).start();
new Thread(p3).start();
}
}
class Account {
double money;
public synchronized void saveMoney(double m){
amoney+=m;
System.out.println("存款成功,余额为:"+amoney);
}
public synchronized void takeMoney(double m){
double tmp=amoney-m;
if(tmp>0){
amoney=tmp;
System.out.println("取款成功,余额为:"+amoney);
}
else
System.out.println("超出余额");
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询