public class ATM {
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;delay<99999999;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); // 执行提款动作
}
}
『贰』 用JAVA编程设计一个银行账户类,其中包括以下内容,并用字符界面模拟存款和取款过程。
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(je>this.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编写一个简单的银行存取款程序
package com.lw.thread;
/*
银行账户类Account(不能透支),
包含账号id(10~16位数字),密码password(6位数字),户主姓名name,余额balence
*/
public class Account {
private String id;
private int password;
private String name;
private double balence;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getPassword() {
return password;
}
public void setPassword(int password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalence() {
return balence;
}
public void setBalence(double balence) {
this.balence = balence;
}
/*
* 默认构造账户信息为:1111111111111111,666666,钱三多,888888.88。
*/
public Account() {
super();
this.id = "1111111111111111";
this.password = 666666;
this.name = "钱三多";
this.balence = 888888.88;
}
/*
* 另一个构造方法带4个参数分别初始化4个属性(带数据有效性验证)。
*/
public Account(String id, int password, String name, double balence) {
this.id = id;
this.password = password;
this.name = name;
this.balence = balence;
}
/*
* 查询余额
*/
public static double selectMoney(Account account) {
return account.getBalence();
}
/*
* 存钱
*/
public static String setMoney(Account account, double balence) {
if (balence < 0) {
return "存钱失败,请正确放入!";
}
double d = balence + account.getBalence();
account.setBalence(d);
return "您存入了" + balence + "元,现账户余额为+" + d;
}
/*
* 取钱
*/
public static String getMoney(Account account, double balence) {
double d = account.getBalence();
if (balence > d) {
return "您的余额不足!";
}
account.setBalence(d - balence);
return "您取出了" + balence + "元,现账户余额为+" + account.getBalence();
}
}
『肆』 Java课程设计,模拟银行存取业务,按照这个流程图去做,其实最主要的是求画圈的部分怎么写和它的方法。
请点击输入图片描述
package com.greatwall.business.controller;
import java.math.BigDecimal;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author xysddjyt
* @since 2020/6/16 15:06
*/
public class BankTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// 余额(单位:分)
Long BALANCE = 10000000L;
// 卡号
String card = "001";
// 密码
String password = "123456";
String inputCard = new String();
String inputPassword = new String();
String quit = new String();
while (true) {
System.out.println(" 欢迎来到网上银行办理存取款业务!");
System.out.println("请输入银行卡号和银行卡密码进行登录!");
while (true) {
System.out.print("请输入银行卡号(按q退出): ");
inputCard = scan.nextLine();
quit = inputCard;
if (inputCard.equals("q")) {
break;
}
if (!inputCard.equals(card)) {
System.out.print("您输入银行卡号不正确,请重新输入 ");
continue;
}
break;
}
if (quit.equals("q")) {
continue;
}
while (true) {
System.out.print("请输入银行卡密码(按q退出): ");
inputPassword = scan.nextLine();
quit = inputPassword;
if (inputPassword.equals("q")) {
break;
}
if (!inputPassword.equals(password)) {
System.out.print("您输入银行卡密码不正确,请重新输入 ");
continue;
}
break;
}
if (quit.equals("q")) {
continue;
}
System.out.print("登录成功,当前登录的账户名:" + inputCard);
String type = "4";
while (!type.equals("0")) {
System.out.print(" 您当前的余额为:" + money(BALANCE) + "元");
System.out.print(" 请选择操作类型。(存款:1;取款:2 ;余额:3;退出:0) ");
type = scan.nextLine();
switch (type) {
case "1": {
System.out.print("请输入您的存款金额(元):");
String chageNumber = scan.nextLine();
if (!isPositiveInteger(chageNumber)) {
System.out.print("请输入正确的存款金额!");
continue;
}
BALANCE = Long.valueOf(chageNumber) * 100 + BALANCE;
continue;
}
case "2": {
System.out.print("请输入您的取款金额(元):");
String chageNumber = scan.nextLine();
if (!isPositiveInteger(chageNumber)) {
System.out.print("请输入正确取款金额!");
continue;
}
BALANCE = BALANCE - Long.valueOf(chageNumber) * 100;
continue;
}
case "3": {
System.out.print("您当前的余额为:" + money(BALANCE) + "元 ");
continue;
}
default: {
continue;
}
}
}
}
}
private static boolean isMatch(String regex, String orginal) {
if (orginal == null || orginal.trim().equals("")) {
return false;
}
Pattern pattern = Pattern.compile(regex);
Matcher isNum = pattern.matcher(orginal);
return isNum.matches();
}
// 判断数据是否为正整数
public static boolean isPositiveInteger(String orginal) {
return isMatch("^\+{0,1}[1-9]\d*", orginal);
}
// 分转元,转换为bigDecimal在toString
public static String money(Long money) {
return BigDecimal.valueOf(money).divide(new BigDecimal(100)).toString();
}
}
『伍』 怎样用Java创建银行账号Account类,实现存款(balance)的存(deposit)、取(withdraw)和查询(getbalan
我也刚开始学,这是我自己写的
import java.util.Scanner;
/**
* 实现取款机功能,存款balance、存钱、取withdraw、查询getbalan
*
*/
public class Account {
public static void main(String[] args) {
double balance = 0;
double deposit = 0;
double withdraw = 0;
for (;;) {
System.out.println("请输入数字:\t1.存钱 \t2.取钱\t3.查询余额\n");
Scanner input = new Scanner(System.in);
String num = input.next();
double s1 = 0, s2= 0 ;
if (num.equals("1")) {
System.out.println("输入存入金额:");
deposit = input.nextDouble();
System.out.println("存钱数目为:" + deposit);
s1 += deposit;
}
if (num.equals("2")) {
System.out.println("输入取钱金额:");
withdraw = input.nextDouble();
System.out.println("取走金额为:" + withdraw);
s2 += withdraw;
}
if (true | num.equals("3")) {
balance += s1 - s2;
System.out.println("账户余额为:" + balance);
}
}
}
}
『陆』 用java编写一个银行新用户现金业务办理程序
publicclassAccount{
protectedStringaccId;
protectedStringname;
protecteddoublemoney;
publicAccount(StringaccId,Stringname){
this(accId,name,0);
}
publicAccount(StringaccId,Stringname,doublemoney){
this.accId=accId;
this.name=name;
this.money=money;
}
publicvoidsaveMoney(doublemoney){
if(money<=0){
System.out.println("存款金额必须大于0");
}
this.money+=money;
System.out.println("存款成功");
}
publicdoublegetMoney(doublemoney){
if(money<=0){
System.out.println("取款金额必须大于0");
return0;
}
if(this.money<=money){
System.out.println("余额不足,无法取款");
return0;
}
this.money-=money;
System.out.println("取款成功");
returnmoney;
}
publicdoublegetBalance(){
returnthis.money;
}
protecteddoublegetOverdraft(){
return0;
}
//实现了equals方法,list比较时才能正确
@Override
publicbooleanequals(Objectobj){
if(obj==null){
returnfalse;
}
if(this==obj){
returntrue;
}
if(objinstanceofAccount){
returnthis.accId.equals(((Account)obj).accId);
}
returnfalse;
}
@Override
publicStringtoString(){
return"账户="+accId+",名字="+name+",余额="+money;
}
}
publicclassBank{
//Account实现了equals方法,list查找时才能正确
privateList<Account>usersAccounts;
publicBank(){
usersAccounts=newArrayList<Account>();
}
publicvoidaddAccount(Accountaccount){
if(usersAccounts.contains(account)){
System.out.println("添加失败,不能添加同样的账户");
return;
}
usersAccounts.add(account);
}
publicbooleandelAccount(Accountaccount){
returnusersAccounts.remove(account);
}
publicbooleandelAccount(StringaccId){
returndelAccount(newAccount(accId,null));
}
publicbooleanexistAccount(Accountaccount){
returnusersAccounts.contains(account);
}
publicbooleanexistAccount(StringaccId){
returnexistAccount(newAccount(accId,null));
}
publicAccountgetAccount(StringaccId){
returnusersAccounts.get(usersAccounts.indexOf(newAccount(accId,null)));
}
publicdoublegetAllMoney(){
//不考虑是否溢出,只是把所有用户余额相加
doubleresult=0;
for(Accountaccount:usersAccounts){
result+=account.getBalance();
}
returnresult;
}
publicdoublegetAllOverdraft(){
//不考虑是否溢出
doubleresult=0;
for(Accountaccount:usersAccounts){
result+=account.getOverdraft();
}
returnresult;
}
publicintgetAccountNum(){
returnusersAccounts.size();
}
publicintgetCreditAccountNum(){
intnum=0;
for(Accountaccount:usersAccounts){
//instanceof性能没有简单的方法快。
if(){
num++;
}
}
returnnum;
}
publicintgetSavingAccountNum(){
intnum=0;
for(Accountaccount:usersAccounts){
if(){
num++;
}
}
returnnum;
}
publicList<Account>getAllAccount(){
returnusersAccounts;
}
}
{
privatedoubleoverdraft;
publicCreditAccount(StringaccId,Stringname){
super(accId,name);
this.overdraft=1000;
}
publicCreditAccount(StringaccId,Stringname,doublemoney){
this(accId,name,money,1000);
}
publicCreditAccount(StringaccId,Stringname,doublemoney,doubleoverdraft){
super(accId,name,money);
this.overdraft=overdraft;
}
@Override
publicdoublegetMoney(doublemoney){
if(money<=0){
System.out.println("取款金额必须大于0");
return0;
}
if(this.money+overdraft<=money){
System.out.println("余额不足,无法取款");
return0;
}
this.money-=money;
System.out.println("取款成功");
returnmoney;
}
@Override
publicdoublegetOverdraft(){
returnoverdraft;
}
@Override
publicStringtoString(){
return"账户="+accId+",名字="+name+",余额="+money+",透支="+overdraft;
}
}
{
publicSavingAccount(StringaccId,Stringname){
super(accId,name);
}
publicSavingAccount(StringaccId,Stringname,doublemoney){
super(accId,name,money);
}
@Override
publicdoublegetMoney(doublemoney){
returnsuper.getMoney(money);
}
@Override
publicdoublegetOverdraft(){
returnsuper.getOverdraft();
}
}
publicclassTest{
privatestaticBankbank=newBank();
publicstaticvoidmain(String[]args){
Test.genAccount();
//开户
Accounta1=newCreditAccount("1","1",200,2000);
Accounta2=newSavingAccount("2","2",300);
Accounta3=newSavingAccount("3","3",400);
Accounta4=newCreditAccount("4","4",500,2000);
Accounta5=newCreditAccount("4","5",600,2000);//帐号4重
bank.addAccount(a1);
bank.addAccount(a2);
bank.addAccount(a3);
bank.addAccount(a4);
bank.addAccount(a5);
showNowAccount();
//销户
bank.delAccount("1");
bank.delAccount("2");
showNowAccount();
//存款
if(bank.existAccount("3")){
Accountacc=bank.getAccount("3");
acc.saveMoney(100);
}
showNowAccount();
//取款
if(bank.existAccount("3")){
Accountacc=bank.getAccount("3");
System.out.println("余额="+acc.getBalance());
acc.getMoney(100);
System.out.println("余额="+acc.getBalance());
acc.getMoney(1000);
System.out.println("余额="+acc.getBalance());
}
if(bank.existAccount("4")){
Accountacc=bank.getAccount("4");
System.out.println("余额="+acc.getBalance());
acc.getMoney(100);
System.out.println("余额="+acc.getBalance());
acc.getMoney(1000);
System.out.println("余额="+acc.getBalance());
acc.getMoney(10000);
System.out.println("余额="+acc.getBalance());
}
System.out.println(bank.getAccountNum());
System.out.println(bank.getAllMoney());
System.out.println(bank.getAllOverdraft());
System.out.println(bank.getCreditAccountNum());
System.out.println(bank.getSavingAccountNum());
}
publicstaticvoidgenAccount(){
Strings="100000000000000";
Accounta=null;
for(inti=1;i<11;i++){
if((i&2)==0){
a=newCreditAccount(s+String.valueOf(i),"账户"+String.valueOf(i));
}else{
a=newSavingAccount(s+String.valueOf(i),"账户"+String.valueOf(i));
}
bank.addAccount(a);
}
}
(){
for(Accountaccount:bank.getAllAccount()){
System.out.println(account);
}
}
}