Ⅰ 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,并显示所转入用户的相应信息,确认后,把原用户的用户余额取出来减去后,更新到数据库,在输入用户的相应的加数据,这个就可以直接调用之前写的存取方法,最后提示成功后返回原用户的管理界面中