news 2026/9/3 1:43:42

Java 存储钱取钱作业

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Java 存储钱取钱作业

package testthread3;

public class testwork7next4 {

public static void main(String[] args) throws InterruptedException {

Account account = new Account("张三",200);

System.out.println("初始账户信息:" + account);

Thread1 thread1 = new Thread1(account,100);

Thread2 thread2 = new Thread2(account,50);

thread1.start();

thread2.start();

thread1.join();

thread2.join();

System.out.println("最终账户信息:" + account);

System.out.println("最终账户余额应为250元:" + account.getBalance());

}

}

class Account {

private String username;

private double balance;

public Account(String username, double balance) {

this.username = username;

this.balance = balance;

}

public synchronized void cunkuan (double amount){

if (amount>0){

balance += amount;

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

}

}

public synchronized void qukuan (double amount){

if (amount > 0 && amount<= balance){

balance -= amount;

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

}else if(amount > balance){

System.out.println("取款是白!余额不足");

}

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public double getBalance() {

return balance;

}

public void setBalance(double balance) {

this.balance = balance;

}

@Override

public String toString() {

return "Account{" +

"username='" + username + '\'' +

", balance=" + balance +

'}';

}

}

class Thread1 extends Thread{ //存款线程

private Account account;

private double amount;

public Thread1(Account account,double amount){

super("存款线程1");

this.account = account;

this.amount = amount;

}

public void run(){

account.cunkuan(amount);

System.out.println("操作完成");

}

}

class Thread2 extends Thread { // 取款线程

private Account account;

private double amount;

public Thread2(Account account, double amount) {

super("取款线程B");

this.account = account;

this.amount = amount;

}

public void run() {

System.out.println(getName() + " 开始操作...");

account.qukuan(amount);

System.out.println(getName() + " 操作完成");

}

}

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/2 23:28:24

CyberdropBunkrDownloader:批量下载Cyberdrop和Bunkr文件的终极指南

CyberdropBunkrDownloader&#xff1a;批量下载Cyberdrop和Bunkr文件的终极指南 【免费下载链接】CyberdropBunkrDownloader Simple downloader for cyberdrop.me and bunkrr.sk 项目地址: https://gitcode.com/gh_mirrors/cy/CyberdropBunkrDownloader 你是否曾经需要从…

作者头像 李华
网站建设 2026/9/2 22:46:13

VSCode日志分析实战(量子算法性能瓶颈的4个信号)

第一章&#xff1a;VSCode日志分析与量子算法性能监控概述在现代软件开发与科研计算融合的背景下&#xff0c;集成开发环境&#xff08;IDE&#xff09;不仅承担代码编写任务&#xff0c;更成为复杂算法调试与系统行为分析的核心平台。Visual Studio Code&#xff08;VSCode&am…

作者头像 李华
网站建设 2026/9/2 23:31:17

NarratoAI:零基础也能制作专业级视频解说的智能工具

NarratoAI&#xff1a;零基础也能制作专业级视频解说的智能工具 【免费下载链接】NarratoAI 利用AI大模型&#xff0c;一键解说并剪辑视频&#xff1b; Using AI models to automatically provide commentary and edit videos with a single click. 项目地址: https://gitcod…

作者头像 李华
网站建设 2026/9/2 7:38:42

MikroTikPatch终极指南:轻松管理RouterOS 7.19.1 arm64版本

MikroTikPatch终极指南&#xff1a;轻松管理RouterOS 7.19.1 arm64版本 【免费下载链接】MikroTikPatch 项目地址: https://gitcode.com/gh_mirrors/mikr/MikroTikPatch MikroTikPatch是一个专注于MikroTik RouterOS系统补丁和版本管理的开源项目&#xff0c;为网络管理…

作者头像 李华
网站建设 2026/9/3 0:23:51

MediaMux视频处理终极指南:Windows平台免费全能解决方案

还在为复杂的视频格式转换而头疼吗&#xff1f;MediaMux是一款基于FFmpeg的Windows视频处理工具&#xff0c;专门解决视频格式转换、封装、分割、合并等常见需求。这款免费工具集成了专业级视频处理功能&#xff0c;却提供了极其友好的图形界面&#xff0c;让普通用户也能轻松完…

作者头像 李华
网站建设 2026/8/31 10:55:16

PS4存档管理终极解决方案:Apollo Save Tool完全指南

PS4存档管理终极解决方案&#xff1a;Apollo Save Tool完全指南 【免费下载链接】apollo-ps4 Apollo Save Tool (PS4) 项目地址: https://gitcode.com/gh_mirrors/ap/apollo-ps4 作为一名PS4玩家&#xff0c;你是否曾经历过这样的绝望时刻&#xff1f;奋战数十小时的游戏…

作者头像 李华