Java實(shí)現(xiàn)簡(jiǎn)單銀行ATM功能
本文實(shí)例為大家分享了Java實(shí)現(xiàn)簡(jiǎn)單銀行ATM功能的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)功能
1、用戶(hù)需要通過(guò)輸入銀行卡號(hào)和密碼才能進(jìn)入ATM系統(tǒng)
2、用戶(hù)可以在ATM中實(shí)現(xiàn)取款、存款、轉(zhuǎn)賬、余額查詢(xún)、退出系統(tǒng)等功能
簡(jiǎn)單分析
1、創(chuàng)建User類(lèi)(cardNo,identity,phone,username,password,balance(余額))
2、創(chuàng)建Bank類(lèi),主要實(shí)現(xiàn)初始化用戶(hù)、用戶(hù)登錄、顯示菜單、取款、存款、轉(zhuǎn)賬、余額查詢(xún)、退出系統(tǒng)等功能。
代碼實(shí)現(xiàn)
User.java
public class User {
private String username;
private int password;
private String cardNo;
private String identity;
private String phone;
private double blance;
public User() {
}
public User(String username, int password, String cardNo, String identity, String phone, double blance) {
this.username = username;
this.password = password;
this.cardNo = cardNo;
this.identity = identity;
this.phone = phone;
this.blance = blance;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getPassword() {
return password;
}
public void setPassword(int password) {
this.password = password;
}
public String getCardNo() {
return cardNo;
}
public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}
public String getIdentity() {
return identity;
}
public void setIdentity(String identity) {
this.identity = identity;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public double getBlance() {
return blance;
}
public void setBlance(double blance) {
this.blance = blance;
}
}
Bank.java
public class Bank {
//創(chuàng)建用戶(hù)數(shù)組
private User[] users=new User[100];
//用戶(hù)個(gè)數(shù)
private int size=0;
//當(dāng)前登錄的用戶(hù)
private User loginuser;
public Bank() {
initial();//初始化用戶(hù)
}
/**
* 初始化用戶(hù)
*/
public void initial(){
User user1=new User("鐘愛(ài)",12346,"6226789234023434","130324192309123074","13133565435",2312313);
User user2=new User("冷冬",12354,"6226789234023567","1305472309123074","13446745675",2333);
User user3=new User("小龍",9893,"6226789234023564","13032414575467457","13145745435",255553);
User user4=new User("趙麗穎",6342,"6226789234029324","130324192647456774","13145675435",288883);
User user5=new User("徐三哥",8445,"6226789234025487","1303241923456744","1457785435",28989);
users[0]=user1;
users[1]=user2;
users[2]=user3;
users[3]=user4;
users[4]=user5;
size=5;
}
/**
* 用戶(hù)登錄
*/
public User login(String cardNo,int password){
for (int i = 0; i <size ; i++) {
if((users[i].getCardNo().equals(cardNo))&&(users[i].getPassword()==password)){
loginuser=users[i];
return users[i];
}
}
return null;
}
/**
* 顯示菜單
*/
public void showMenus(){
Scanner input =new Scanner(System.in);
do { System.out.println("**********************************歡迎進(jìn)入ATM系統(tǒng)*****************************************");
System.out.println("*********************1 存款 2 取錢(qián) 3 轉(zhuǎn)賬 4 查詢(xún)余額 5 修改密碼 0 退出***********************");
int choice=input.nextInt();
switch (choice){
case 1:
//取款
save();
break;
case 2:
//存錢(qián)
withdraw();
break;
case 3:
//轉(zhuǎn)賬
trans();
break;
case 4:
//查詢(xún)余額
query();
break;
case 5:
//修改密碼
revise();
break;
case 0:
//退出ATM系統(tǒng)
System.out.println("歡迎下次使用本系統(tǒng)");
return;
}
} while (true);
}
/**
* 存款操作
*/
public void save(){
System.out.println("請(qǐng)輸入您的存款金額:");
Scanner input=new Scanner(System.in);
int money=input.nextInt();
if(loginuser!=null){
if(money>0){
loginuser.setBlance(loginuser.getBlance()+money);
System.out.println("存款成功");
System.out.println("您的當(dāng)前余額為"+loginuser.getBlance());
}else{
System.out.println("輸入錯(cuò)誤!");
}
}else{
System.out.println("您還未進(jìn)行賬戶(hù)登錄");
}
}
/**
* 取款操作
*/
public void withdraw(){
Scanner input=new Scanner(System.in);
System.out.println("請(qǐng)輸入你要取出的金額為:");
int money=input.nextInt();
if(loginuser!=null){
if(money>0&&money<=loginuser.getBlance()){
loginuser.setBlance(loginuser.getBlance()-money);
System.out.println("取款成功");
System.out.println("您的當(dāng)前余額為:"+loginuser.getBlance());
}else{
System.out.println("輸入錯(cuò)誤");
}
}else{
System.out.println("您還未進(jìn)行賬戶(hù)登錄");
}
}
/**
* 轉(zhuǎn)賬操作
*/
public void trans(){
Scanner input=new Scanner(System.in);
System.out.println("請(qǐng)輸入要轉(zhuǎn)入賬戶(hù)的卡號(hào):");
String cardNo=input.next();
User nuser=null;//對(duì)方賬戶(hù)
boolean IsExit=false;//判斷對(duì)方賬戶(hù)是否存在
for (int i = 0; i <size; i++) {
if(users[i].getCardNo().equals(cardNo)){
IsExit=true;//檢索到對(duì)方賬戶(hù)
nuser=users[i];
}
}
if(loginuser==nuser){
System.out.println("不可以自己給自己賬戶(hù)進(jìn)行轉(zhuǎn)賬");
return;
}
if(loginuser!=null){
System.out.println("請(qǐng)輸入要轉(zhuǎn)賬的金額:");
int money=input.nextInt();
if(money>0&&money<=loginuser.getBlance()){
loginuser.setBlance(loginuser.getBlance()-money);
nuser.setBlance(nuser.getBlance()+money);
System.out.println("轉(zhuǎn)賬成功");
System.out.println("您當(dāng)前余額為"+loginuser.getBlance());
System.out.println(nuser.getBlance());
}else{
System.out.println("輸入錯(cuò)誤");
}
}else{
System.out.println("請(qǐng)登錄賬戶(hù)");
}
}
/**
* 查詢(xún)余額
*/
public void query(){
System.out.println("您的余額為:"+loginuser.getBlance());
}
/**
* 修改密碼
*/
public void revise(){
Scanner input=new Scanner(System.in);
System.out.println("請(qǐng)輸入您的新密碼");
int newpassword=input.nextInt();
for (int i = 0; i < size; i++) {
if(users[i]==loginuser){
users[i].setPassword(newpassword); //進(jìn)行修改
}
}
System.out.println("密碼修改成功");
}
}
BankSystem.java
public class BankSystem {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
Bank bank=new Bank();
System.out.println("請(qǐng)輸入您的卡號(hào):");
String cardNo=input.next();
System.out.println("請(qǐng)輸入您的密碼:");
int password=input.nextInt();
User user = bank.login(cardNo, password);//用戶(hù)登錄
if(user!=null){
System.out.println(user.getUsername()+"登陸成功");
bank.showMenus();
}else{
System.out.println("登陸失敗");
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- java實(shí)現(xiàn)銀行ATM管理系統(tǒng)
- Java實(shí)現(xiàn)基礎(chǔ)銀行ATM系統(tǒng)
- Java簡(jiǎn)單實(shí)現(xiàn)銀行ATM系統(tǒng)
- java實(shí)現(xiàn)簡(jiǎn)單銀行ATM系統(tǒng)
- Java實(shí)現(xiàn)銀行ATM系統(tǒng)
- Java 模擬銀行自助終端系統(tǒng)
- java代碼實(shí)現(xiàn)銀行管理系統(tǒng)
- java實(shí)現(xiàn)簡(jiǎn)單銀行管理系統(tǒng)
- java實(shí)現(xiàn)銀行管理系統(tǒng)
- java模擬實(shí)現(xiàn)銀行ATM機(jī)操作
相關(guān)文章
詳解如何為SpringBoot Web應(yīng)用的日志方便追蹤
在Web應(yīng)用程序領(lǐng)域,有效的請(qǐng)求監(jiān)控和可追溯性對(duì)于維護(hù)系統(tǒng)完整性和診斷問(wèn)題至關(guān)重要,SpringBoot是一種用于構(gòu)建Java應(yīng)用程序的流行框架,在本文中,我們探討了在SpringBoot中向日志添加唯一ID的重要性,需要的朋友可以參考下2023-11-11
解析Java中所有錯(cuò)誤和異常的父類(lèi)java.lang.Throwable
這篇文章主要介紹了Java中所有錯(cuò)誤和異常的父類(lèi)java.lang.Throwable,文章中簡(jiǎn)單地分析了其源碼,說(shuō)明在代碼注釋中,需要的朋友可以參考下2016-03-03
Java中類(lèi)的初始化和實(shí)例化區(qū)別詳解
這篇文章主要介紹了Java中類(lèi)的初始化和實(shí)例化區(qū)別詳解,類(lèi)的初始化<BR>是完成程序執(zhí)行前的準(zhǔn)備工作,類(lèi)的實(shí)例化(實(shí)例化對(duì)象)是指創(chuàng)建一個(gè)對(duì)象的過(guò)程,需要的朋友可以參考下2023-08-08
Java數(shù)據(jù)結(jié)構(gòu)之循環(huán)隊(duì)列簡(jiǎn)單定義與用法示例
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)之循環(huán)隊(duì)列簡(jiǎn)單定義與用法,簡(jiǎn)要描述了循環(huán)隊(duì)列的概念、原理,并結(jié)合實(shí)例形式分析了java循環(huán)隊(duì)列的定義與使用方法,需要的朋友可以參考下2017-10-10
Java面向?qū)ο蠡A(chǔ)知識(shí)之抽象類(lèi)和接口
這篇文章主要介紹了Java面向?qū)ο蟮某橄箢?lèi)和接口,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有很好的幫助,需要的朋友可以參考下2021-11-11
Intellij IDEA中一次性折疊所有Java代碼的快捷鍵設(shè)置
這篇文章主要介紹了Intellij IDEA中一次性折疊所有Java代碼的快捷鍵設(shè)置,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
BeanUtils.copyProperties在拷貝屬性時(shí)忽略空值的操作
這篇文章主要介紹了BeanUtils.copyProperties在拷貝屬性時(shí)忽略空值的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
java使用socket實(shí)現(xiàn)一個(gè)多線程web服務(wù)器的方法
今天小編就為大家分享一篇java使用socket實(shí)現(xiàn)一個(gè)多線程web服務(wù)器的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10

