一个JAVA问题,急求解决!!!
1)编写一个银行帐户类Account。2)编写一个主程序类SyncDemo。3)在主程序类中模拟一个用户对帐户进行存取款操作。建一个主程序类SyncDemo,它实现Run...
1)编写一个银行帐户类Account。
2)编写一个主程序类SyncDemo。
3)在主程序类中模拟一个用户对帐户进行存取款操作。
建一个主程序类SyncDemo,它实现Runnable接口,在该类中创建两个线程,分别对帐户进行存取款操作,每次存取2元,2秒后中断它们的运行,并打印出帐户余额,存款总额,取款总额,核对数目是否一致。在代码中需要加如同步操作。 展开
2)编写一个主程序类SyncDemo。
3)在主程序类中模拟一个用户对帐户进行存取款操作。
建一个主程序类SyncDemo,它实现Runnable接口,在该类中创建两个线程,分别对帐户进行存取款操作,每次存取2元,2秒后中断它们的运行,并打印出帐户余额,存款总额,取款总额,核对数目是否一致。在代码中需要加如同步操作。 展开
3个回答
展开全部
//////////////////////Account//////////////////////////////////
public class Account {
private String name_ = null;
private Double money_ = 0.0d;
private Double saved_ = 0.0d;
private Double taken_ = 0.0d;
public Account(String name, double money) {
this.name_ = name;
this.money_ = money;
}
public synchronized void save(double money) {
this.money_ += money;
this.saved_ += money;
display("save", money, true);
}
public synchronized void take(double money) {
if (this.money_ >= money) {
this.money_ -= money;
this.taken_ += money;
display("take", money, true);
} else {
display("take", money, false);
}
}
public void displayAll() {
StringBuilder sb = new StringBuilder();
sb.append("Account:" + this.name_);
sb.append(" Saved:" + this.saved_);
sb.append(" Taken:" + this.taken_);
sb.append(" Money:" + this.money_);
if (this.money_ + this.taken_ == this.saved_) {
sb.append(" Reslut: true");
} else {
sb.append(" Reslut: false");
}
System.out.println(sb.toString());
}
public void display(String op, double money, boolean result) {
StringBuilder sb = new StringBuilder();
sb.append("Account:" + this.name_);
if (null != op) {
sb.append(" Operation:" + op);
sb.append(" " + money);
sb.append(" " + result);
}
sb.append(" Money:" + this.money_);
sb.append(" Thread:" + Thread.currentThread().getId());
System.out.println(sb.toString());
}
}
//////////////////////SyncDemo//////////////////////////////////
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class SyncDemo implements Runnable {
static boolean cancel = false;
private Account account = new Account("zhangsan", 0);
public static void main(String[] args) {
SyncDemo syn = new SyncDemo();
ExecutorService exec = Executors.newCachedThreadPool();
for (int i = 0; i < 2; i++) {
exec.execute(syn);
}
try {
Thread.sleep(2000);
syn.cancel();
exec.shutdown();
if (!exec.awaitTermination(1000, TimeUnit.MILLISECONDS)) {
System.out.println("Some tasks were not terminated!");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
syn.displayAll();
}
private void cancel() {
cancel = true;
}
private void displayAll() {
this.account.displayAll();
}
public void run() {
Random random = new Random();
while (!cancel) {
if (random.nextBoolean()) {
this.account.save(random.nextInt(100));
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
continue;
}
this.account.take(random.nextInt(100));
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Account {
private String name_ = null;
private Double money_ = 0.0d;
private Double saved_ = 0.0d;
private Double taken_ = 0.0d;
public Account(String name, double money) {
this.name_ = name;
this.money_ = money;
}
public synchronized void save(double money) {
this.money_ += money;
this.saved_ += money;
display("save", money, true);
}
public synchronized void take(double money) {
if (this.money_ >= money) {
this.money_ -= money;
this.taken_ += money;
display("take", money, true);
} else {
display("take", money, false);
}
}
public void displayAll() {
StringBuilder sb = new StringBuilder();
sb.append("Account:" + this.name_);
sb.append(" Saved:" + this.saved_);
sb.append(" Taken:" + this.taken_);
sb.append(" Money:" + this.money_);
if (this.money_ + this.taken_ == this.saved_) {
sb.append(" Reslut: true");
} else {
sb.append(" Reslut: false");
}
System.out.println(sb.toString());
}
public void display(String op, double money, boolean result) {
StringBuilder sb = new StringBuilder();
sb.append("Account:" + this.name_);
if (null != op) {
sb.append(" Operation:" + op);
sb.append(" " + money);
sb.append(" " + result);
}
sb.append(" Money:" + this.money_);
sb.append(" Thread:" + Thread.currentThread().getId());
System.out.println(sb.toString());
}
}
//////////////////////SyncDemo//////////////////////////////////
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class SyncDemo implements Runnable {
static boolean cancel = false;
private Account account = new Account("zhangsan", 0);
public static void main(String[] args) {
SyncDemo syn = new SyncDemo();
ExecutorService exec = Executors.newCachedThreadPool();
for (int i = 0; i < 2; i++) {
exec.execute(syn);
}
try {
Thread.sleep(2000);
syn.cancel();
exec.shutdown();
if (!exec.awaitTermination(1000, TimeUnit.MILLISECONDS)) {
System.out.println("Some tasks were not terminated!");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
syn.displayAll();
}
private void cancel() {
cancel = true;
}
private void displayAll() {
this.account.displayAll();
}
public void run() {
Random random = new Random();
while (!cancel) {
if (random.nextBoolean()) {
this.account.save(random.nextInt(100));
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
continue;
}
this.account.take(random.nextInt(100));
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
展开全部
账户类设定属性名称 密码 账户上的钱
主程序设定方法登陆 取款 查询 注销等
基本思路就是这样 具体写出来也不难 最好还是自己写吧
主程序设定方法登陆 取款 查询 注销等
基本思路就是这样 具体写出来也不难 最好还是自己写吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Account
{
privateString 账户ID;
private ...
private double 账户余额;
public void set账户余额(double num)
{
this.账户余额 = num;
}
public double get账户余额()
{
return 账户余额;
}
}
public class SyncDemo
{
private boolean 存钱(double num)
{
Account user1 = new Account();
try{
//锁数据
double 余额 = user1.get账户余额();
user1.set账户余额(余额 + num);
//解锁
return true;
}
catch(Exception e)
{
rollback;
return false;
}
}
private boolean 取钱(double num)
{
}
}
{
privateString 账户ID;
private ...
private double 账户余额;
public void set账户余额(double num)
{
this.账户余额 = num;
}
public double get账户余额()
{
return 账户余额;
}
}
public class SyncDemo
{
private boolean 存钱(double num)
{
Account user1 = new Account();
try{
//锁数据
double 余额 = user1.get账户余额();
user1.set账户余额(余额 + num);
//解锁
return true;
}
catch(Exception e)
{
rollback;
return false;
}
}
private boolean 取钱(double num)
{
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询