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);
}
}
}