Ⅰ java 編寫(類的方法)模擬ATM機進行帳戶余額查詢
//以下是一個賬戶類,帶有存取款和查詢功能
public class Account {
private int money = 0; //余額
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
//存款
public void deposit(int num){
setMoney(getMoney() + num);
System.out.println("存款" + num + "元\r\n當前余額:" + getMoney());
}
//取款
public void getMoney_fromAccount(int num){
if(num > getMoney()){
System.out.println("抱歉,您的余額不足");
}else{
setMoney(getMoney() - num );
System.out.println("取款" + num + "元\r\n當前余額:" + getMoney());
}
}
//查詢余額
public void query(){
System.out.println("您的當前余額未:" + getMoney());
}
}
這個不簡單,
從分析設計,到編碼實現,
都是需要精力和時間花費的體現,
建議還是自己親自走一遍吧,
能學點東西。
Ⅲ 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 模擬ATM櫃員機模擬程序
/*
要求:使用字元用戶界面。當輸入給定的卡號和密碼(初始卡號和密碼為123456)時,系統能登錄ATM櫃員機系統,用戶可以按照以下規則進行:
1、查詢余額:初始余額為10000元
2、ATM取款:每次取款金額為100的倍數,總額不超過5000元,支取金額不允許透支。
3、ATM存款:不能出現負存款。
4、修改密碼:新密碼長度不小於6位,不允許出現6位完全相同的情況,只有舊密碼正確,新密碼符合要求,且兩次輸入相同的情況下才可以成功修改密碼。
(卡號密碼余額放到文件中)
*/
public class ATM {
private Account acc;
private File dataFile;
private FileWriter fw;
private BufferedWriter bw;
private String filePath = "./data.txt";
public ATM() {
this.acc = new Account();
try {
this.dataFile = new File(this.filePath);
if (!this.dataFile.exists()) {
this.dataFile.createNewFile();
}
this.fw = new FileWriter(this.filePath);
this.bw = new BufferedWriter(this.fw);
} catch (IOException io) {
System.err.println("Cannot open file");
io.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new ATM().interact();
}
public void interact() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Account #: ");
String temp = br.readLine();
System.out.println("Password: ");
String temp2 = br.readLine();
if (!this.acc.isValid(Long.parseLong(temp.trim()), temp2.trim()) {
System.err.println("Wrong password");
return;
}
System.out.println("1. Account Inquery.");
System.out.println("2. Withdraw");
System.out.println("3. Deposit.");
System.out.println("4. Change Password.");
System.out.println("5. Export to File.");
System.out.println("0. Exit.");
int c = 100;
while (c != 0) {
String str = br.readLine();
try {
int c = Integer.parseInt(str.trim());
} catch (NumberFormatException nfe) {
System.err.println("Invalid choice");
continue;
}
switch (c) {
case 0:
System.out.println("Thank you");
break;
case 1:
System.out.println("Balance: " + this.acc.balanceInquery());
break;
case 2:
System.out.println("How much? ");
String temp = br.readLine();
try {
long ammount = Long.parseLong(temp.trim());
this.acc.withdraw(ammount);
break;
} catch (NumberFormatException nfe) {
System.err.println("Invalid amount");
continue;
}
case 3:
System.out.println("How much? ");
String temp = br.readLine();
try {
long ammount = Long.parseLong(temp.trim());
this.acc.deposit(ammount);
break;
} catch (NumberFormatException nfe) {
System.err.println("Invalid amount");
continue;
}
case 4:
System.out.println("Old password: ");
String temp = br.readLine();
System.out.println("New password: ");
String temp2 = br.readLine();
this.acc.changePassword(temp, temp2);
break;
case 5:
this.bw.write(this.acc.toString());
break;
default:
break;
}
}
}
}
class Account {
private long accNo = 123456;
private String pass = "123456";
private long balance = 10000;
public Account() {
}
public boolean isValid(long accNo, String pass) {
return (this.accNo == accNo) && (pass.equals(this.pass));
}
public void changePassword(String oldPass, String password) {
if (!oldPass.equals(this.pass)) {
System.err.println("Wrong password.");
return;
}
if (password.length < 6) {
System.err.println("Password too short");
return;
}
if (password.equals(this.pass)) {
System.err.println("Password cannot be the same.");
return;
}
this.pass = password;
}
public long balanceInquery() {
return this.balance;
}
public void withdraw(long amount) {
if (amount > 5000 || amount < 0) {
System.err.println("Withdraw limit: $0-$5000");
return;
}
if ((amount % 100) != 0) {
System.err.println("The amount has to be a proct of 100");
return;
}
long newBalance = this.balance - amount;
if (newBalance < 0) {
System.err.println("Not enough money in the account");
return;
}
this.balance = newBalance;
}
public void deposit(long amount) {
if (amount < 0) {
System.err.println("Cannot deposit negative amount");
return;
}
this.balance += amount;
}
public String toString() {
return ("Account #: " + this.accNo + "\n" + "Password: " + this.pass + "\n" + "Balance: " + this.balance);
}
}
Ⅳ java需要寫一個JAVA程序,模擬銀行賬戶的查詢、轉賬功能。
「\ T」選項卡,當輸出數據顯示,幾乎以表格形式顯示,目的是為了使數據看起來整齊點。
「\ n」換行,其他行顯示下一個(...),目的是換行字元或字元串,使用System.out.println();效果是一樣的。
Ⅵ JAVA編寫銀行賬戶程序摸擬銀行賬戶的存\取款操作
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語言編寫程序,模擬銀行賬戶功能。要有: 屬性:賬號、儲戶姓名、地址、存款余額、最小余額。
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 List<bank> selectfind(){
Session session=HibernateSessionFactory.getSession();
session.beginTransaction();
String hql="select b from bank b ";
List<bank> list=session.createQuery(hql).list();
return list;
}
}
Ⅷ java 模擬實現銀行自動存取一體機~~求程序指導~~
1.創建資料庫test,在test中創建一張表,用戶id,用戶名,登錄密碼,用戶狀態(0為普通用戶,大於0則為vip),用戶余額(BigDecimal簡單點就用int類型)
2.寫一個用戶的javaBean
3.封裝用戶的存,取,查詢功能(存取,取出用戶余額做加減,再把值更新回去)
4.編譯用戶的登錄界面,獲取用戶輸入的id和密碼,把這個值會傳入所寫的查詢方法的參量中去,如果id和密碼都符合,那麼再判斷一下用戶狀態,進入到相應的管理界面
5.編譯管理界面,先編譯普通用戶的管理界面
6.編譯vip用戶的管理界面可以繼承普通用戶的管理界面,在面板上再加兩個按鈕就可以了。
7.至於透支和轉賬的功能,轉賬,可以要求vip用戶輸入所要轉賬用戶的id,並顯示所轉入用戶的相應信息,確認後,把原用戶的用戶余額取出來減去後,更新到資料庫,在輸入用戶的相應的加數據,這個就可以直接調用之前寫的存取方法,最後提示成功後返回原用戶的管理界面中