十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
public class ATM {
沽源网站制作公司哪家好,找成都创新互联!从网页设计、网站建设、微信开发、APP开发、自适应网站建设等网站项目制作,到程序开发,运营维护。成都创新互联从2013年创立到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联。
public static void main(String[] args) {
// 开立帐号
Account account = new Account();
// 在 account 中存 10,000 元
account.setBalance(10000);
// 检查 account 中的存款
System.out.println("帐户原始金额 : " + account.getBalance() + " 元");
// 小明, 小华与小英一起对 account 进行提款的动作
WithDraw s1 = new WithDraw("小明", account, 5000); // 小明 在 account 中提 5000 元
WithDraw s2 = new WithDraw("小华", account, 2000); // 小华 在 account 中提 2000 元
WithDraw s3 = new WithDraw("小英", account, 4000); // 小英 在 account 中提 4000 元
s1.start();
s2.start();
s3.start();
}
}
//帐户
class Account {
private int balance; // 帐户馀额
public int getBalance() { // 取得帐户馀额
return balance;
}
public void setBalance(int money) { // 设定帐户馀额
balance = money;
}
// 提款方法
public void withDraw(Account account, int withdrawMoney) {
String tName = Thread.currentThread().getName(); // tName=提款人
System.out.println(tName + " 开始提款 ... ");
boolean withDrawStatus; // 提款状态 说明:false=提款失败, true=提款成功
synchronized(ATM.class) {
int tmpBalabce = account.getBalance(); // 取得最新帐户馀额
//用 for-loop 模拟提款时系统所花的时间
for(double delay=0;delay99999999;delay++) {
// ... 提款进行中 ...
}
tmpBalabce = tmpBalabce - withdrawMoney; // 最新帐户馀额 - 欲提款金额 (用来判断是否馀额足够的依据)
if (tmpBalabce 0) { // 判断是否馀额足够
withDrawStatus = false;
System.out.println("....................");
System.out.println(" 帐户馀额不足!");
System.out.println("....................");
} else {
withDrawStatus = true;
account.setBalance(tmpBalabce); // 回存account最後剩馀金额
}
}
System.out.println(tName + "的交易单:");
System.out.println("\t欲提款金额:" + withdrawMoney + "元");
System.out.println("\t帐户馀额:" + account.getBalance());
if(withDrawStatus == true){
System.out.println(tName + " 完成提款 ... ");
} else {
System.out.println(tName + " 提款失败 ... ");
}
System.out.println("-------------------------------");
}
}
// 提款类别
class WithDraw extends Thread {
private Account account; // 帐号
private int withdrawMoney; // 欲提款的金额
// tName:执行绪名称, account:Account物件名称, withdrawMoney:欲提款金额
public WithDraw(String tName, Account account, int withdrawMoney) {
setName(tName);
this.account = account;
this.withdrawMoney= withdrawMoney;
}
public void run() {
// 执行提款动作(account:帐号, withdrawMoney 欲提款金额)
account.withDraw(account, withdrawMoney); // 执行提款动作
}
}
import java.util.Scanner;
public class ZH {
private String zh;//账户
private String password;//密码
private String openTime;//开户时间
private String sfz;//身份证号
private double je;//存款金额
public String getZh() {
return zh;
}
public void setZh(String zh) {
this.zh = zh;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getOpenTime() {
return openTime;
}
public void setOpenTime(String openTime) {
this.openTime = openTime;
}
public String getSfz() {
return sfz;
}
public void setSfz(String sfz) {
this.sfz = sfz;
}
public double getJe() {
return je;
}
public void setJe(double je) {
this.je = je;
}
//存款方法
public void ck(double je){
this.je=this.je+je;//存入的金额加上原有的金额
}
//取款方法
public void qk(double je){
if(jethis.je){//取款金额大于余额
System.out.println("存款余额不足");
}else{
this.je=this.je-je;//原有的金额减去取出的金额
}
}
public static void main(String[] args) {
ZH zh = new ZH();//初始化一个账户信息
zh.setJe(10000.0);//向zh账户添加余额
zh.setOpenTime("2013.12.3");//向zh账户添加开发时间
zh.setPassword("123456");//向zh账户添加密码
zh.setSfz("123456789");//向zh账户添加身份证
zh.setZh("zhangsan");//向zh账户添加账号
System.out.println("欢迎光临模拟银行");
Scanner scan = new Scanner(System.in);
int count=0;//记录输入错误的次数
while(1==1){//循环
System.out.println("请输入账号");
String zhm=scan.next();
System.out.println("请输入密码");
String mm=scan.next();
if(zhm.equals(zh.getZh()) mm.equals(zh.getPassword())){//输入的信息与zh账户信息的密码和账号一致
while(1==1){
System.out.println("当前余额为:"+zh.getJe()+"元。请选择操作:1.存款;2.取款;3.退出(只能输入数字)");
String cz=scan.next();
switch (Integer.parseInt(cz)) {
case 1:
System.out.println("请输入存款金额(输入小数)");
double ckje=scan.nextDouble();
zh.ck(ckje);
System.out.println("实施存款:"+ckje+"元,当前余额为"+zh.getJe()+"元");
break;
case 2:
System.out.println("请输入取款金额(输入小数)");
double qkje=scan.nextDouble();
zh.qk(qkje);
System.out.println("实施取款:"+qkje+"元,当前余额为"+zh.getJe()+"元");
break;
case 3:
break;
default:
System.out.println("暂无此功能");//输入1或者2、3以外的操作
break;
}
if("3".equals(cz)){
break;
}
}
System.out.println("退出操作");
break;
}else{
if(count=3){
System.out.println("已输入错误三次,账号被锁");
break;//结束循环
}else{
System.out.println("账号或密码错误,请重新输入");
count++;//错误一次count+1
continue;//进入下次循环
}
}
}
}
}
首先得这个Java程序得常年在后台运行。每天都进行如下操作:
检测到当天日期,如果不是1月1日,则将标志变量置0.
如果是1月1日,检测标志变量,
如果是0:进行一次扣费,然后标志变量置1;
如果是1,不进行任何操作。
package com.cshr.test;
public class bank {
private String card;//账号
private String uname;//储户姓名
private String address;//地址
private double money;//存款余额
public static final double minMoney=0;//最小余额
public bank(){}
public String getCard() {
return card;
}
public void setCard(String card) {
this.card = card;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public static double getMinmoney() {
return minMoney;
}
}
package com.cshr.test;
import java.util.List;
import org.hibernate.Session;
import com.utils.HibernateSessionFactory;
public class bankDao {
//存款
public void addMoney(double money,double Sqlmoney,String card){
Session session=HibernateSessionFactory.getSession();
try{
session.beginTransaction();
String hql="update bank b set b.money+="+money+Sqlmoney+" where b.card='"+card+"'";
session.createQuery(hql).executeUpdate();
session.beginTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.beginTransaction().rollback();
}finally{
HibernateSessionFactory.closeSession();
}
}
//取款
public void delMoney(double money,double Sqlmoney,String card){
Session session=HibernateSessionFactory.getSession();
try{
session.beginTransaction();
String hql="update bank b set b.money+="+(Sqlmoney-money)+" where b.card='"+card+"'";
session.createQuery(hql).executeUpdate();
session.beginTransaction().commit();
}catch(Exception e){
e.printStackTrace();
System.out.println("取款失败");
session.beginTransaction().rollback();
}finally{
HibernateSessionFactory.closeSession();
}
}
//查询所有
public Listbank selectfind(){
Session session=HibernateSessionFactory.getSession();
session.beginTransaction();
String hql="select b from bank b ";
Listbank list=session.createQuery(hql).list();
return list;
}
}
您好,
创建状态
使用new运算符创建一个线程后,该线程仅仅是一个空对象,系统没有分配资源,称该线程处于创建状态(new thread)
可运行状态
使用start()方法启动一个线程后,系统为该线程分配了除CPU外的所需资源,使该线程处于可运行状态(Runnable)
运行中状态
Java运行系统通过调度选中一个Runnable的线程,使其占有CPU并转为运行中状态(Running)。此时,系统真正执行线程的run()方法。
package pack.java.thread.atm;
/**
* 账户类;
* @author Administrator
*
*/
public class Account {
private String name; //用户名;
private int value; //账户余额;
/**
* 存入金额;
* @param monery
*/
public void putMonery(int monery){
this.value = this.value + monery;
}
/**
* 取出金额;
* @param monery
* @return 金额;
*/
public int getMonery(int monery){
//判断是否账户余额是否大于 要取出的钱;
if(this.value monery ){
this.value = this.value - monery;
}else{
monery = this.value; //账户余额不够时,则取出,所有的账户余额的金额.
this.value = 0;
}
//返回取出的钱;
return monery;
}
/**
* 查询余额;
* @return 返回账户余额;
*/
public int search(){
return this.value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
public class Test extends PhoneKouFei{
public Test(String startTime, String stopTime) {
super(startTime, stopTime);
}
public static void main(String[] args) {
String start = "00:12:23";
String stop = "00:15:23";
new Test(start,stop);
}
}
abstract class PhoneKouFei{
private final double fei = 1.5;//1分钟扣1毛5
public String startTime; //通话开始时间
public String stopTime; //通话结束时间
public PhoneKouFei(String startTime, String stopTime){
this.startTime = startTime;
this.stopTime = stopTime;
System.out.println("通话时常:" +getKouFei()+ "分钟");
System.out.println("应扣费:" + (double)getKouFei() * fei);
}
//扣费方法
private final long getKouFei(){
java.text.SimpleDateFormat sim = new java.text.SimpleDateFormat("HH:mm:ss");
try {
java.util.Date start = sim.parse(this.startTime);
java.util.Date stop = sim.parse(this.stopTime);
return (stop.getTime() - start.getTime()) / 60000;
} catch (java.text.ParseException e) {
throw new RuntimeException("时间格式错误..");
}
}
}
//输出结果:
通话时常:3分钟
应扣费:4.5