欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實(shí)現(xiàn)簡易提款機(jī)

 更新時間:2022年01月24日 15:03:31   作者:吾人為學(xué)  
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡易提款機(jī),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了vue + element ui實(shí)現(xiàn)錨點(diǎn)定位的具體代碼,供大家參考,具體內(nèi)容如下

需求:

簡易自動提款機(jī)

1.創(chuàng)建用戶類User(包含卡號、姓名、密碼、余額等屬性),用戶開卡時錄入的姓名和密碼(自動分配一個卡號、初始金額設(shè)置為0)。

2.使用ArrayList或LinkedList存儲全部注冊用戶

形式如:ArrayList<User> userList = new ArrayList<User>();

3.實(shí)現(xiàn)功能

(1)存款

(2)取款(如果余額不足要提示)

(3)查詢余額(只能查看當(dāng)前卡號的余額)

4. 技術(shù)要求,使用異常處理用戶的錯誤輸入(即程序保護(hù)容錯功能)。

實(shí)例:

1.創(chuàng)建自定義異常:

public class OptionsException extends RuntimeException{
?
?? ?/**
?? ? *?
?? ? */
?? ?private static final long serialVersionUID = 6307237273922255816L;
?
?? ?public OptionsException() {
?? ??? ?super();
?? ??? ?// TODO Auto-generated constructor stub
?? ?}
?
?? ?public OptionsException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
?? ??? ?super(message, cause, enableSuppression, writableStackTrace);
?? ??? ?// TODO Auto-generated constructor stub
?? ?}
?
?? ?public OptionsException(String message, Throwable cause) {
?? ??? ?super(message, cause);
?? ??? ?// TODO Auto-generated constructor stub
?? ?}
?
?? ?public OptionsException(String message) {
?? ??? ?super(message);
?? ??? ?// TODO Auto-generated constructor stub
?? ?}
?
?? ?public OptionsException(Throwable cause) {
?? ??? ?super(cause);
?? ??? ?// TODO Auto-generated constructor stub
?? ?}

?? ?
}

2.對用戶信息(卡號、姓名、密碼、余額、存款金額、取款金額)進(jìn)行封裝,并對部分屬性進(jìn)行異常處理:

public class User {
?
?? ?private String cardNumber;? ??
?? ?private String userName;? ??
?? ?private int code;? ??
?? ?private int balance = 0;? ??
?? ?private int deposit;?? ?//存款金額? ??
?? ?private int withdrawMoney;?? ?//取款金額
?? ?
?? ?public String getCardNumber() {
?? ??? ?return cardNumber;
?? ?}
?
?? ?public void setCardNumber(String cardNumber) {
?? ??? ?this.cardNumber = cardNumber;
?? ?}
?
?? ?public String getUserName() {
?? ??? ?return userName;
?? ?}
?
?? ?public void setUserName(String userName) {
?? ??? ?this.userName = userName;
?? ?}
?
?? ?public int getCode() {
?? ??? ?return code;
?? ?}
?
?? ?public void setCode(int code) throws OptionsException{
?? ??? ?
?? ??? ?Integer temp = (Integer) code;
?? ??? ?
?? ??? ?if(temp.toString().length() == 6){
?? ??? ??? ?this.code = code;
?? ??? ?}else{
?? ??? ??? ?throw new OptionsException("您輸入的位數(shù)有誤!");
?? ??? ?}
?? ??? ?
?? ?}
?
?? ?public int getBalance() {
?? ??? ?return balance;
?? ?}
?
?? ?public void setBalance(int balance) throws OptionsException{
?? ??? ?
?? ??? ?if(balance >= 0){
?? ??? ??? ?this.balance = balance;
?? ??? ?}else{
?? ??? ??? ?throw new OptionsException("余額不足!");
?? ??? ?}
?? ?}
?
?? ?public int getDeposit() {
?? ??? ?return deposit;
?? ?}
?
?? ?public void setDeposit(int deposit) throws OptionsException{
?? ??? ?
?? ??? ?if(deposit >= 0){
?? ??? ??? ?this.deposit = deposit;
?? ??? ?}else{
?? ??? ??? ?throw new OptionsException("存入金額錯誤!");
?? ??? ?}
?? ?}
?
?? ?public int getWithdrawMoney() {
?? ??? ?return withdrawMoney;
?? ?}
?
?? ?public void setWithdrawMoney(int withdrawMoney) throws OptionsException{
?? ??? ?if(withdrawMoney >= 0){
?? ??? ??? ?if(this.getBalance() - withdrawMoney < 0){
?? ??? ??? ??? ?this.withdrawMoney = withdrawMoney;
?? ??? ??? ?}else{
?? ??? ??? ??? ?throw new OptionsException("余額不足!");
?? ??? ??? ?}
?? ??? ?}else{
?? ??? ??? ?throw new OptionsException("取款金額錯誤!");
?? ??? ?}?? ??? ?
?? ?}
}

3.新建一個類,定義用戶可進(jìn)行的操作方法:

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
?
public class UserOptions {
?
?? ?/**定義一個ArrayList
? ?
? ? 來存儲注冊的銀行卡信息*/
?? ?public static ArrayList
? ??
? ? ? accounts = new ArrayList
? ? ?
? ? ? ();
?? ?
?? ?Scanner scanner = new Scanner(System.in);
?? ?
?? ?/**調(diào)用User 類訪問該類下封裝的用戶信息*/
?? ?User user = new User();
?? ?
?? ?//為新用戶注冊銀行卡信息
?? ?public void register() throws Exception {
?? ??? ?
?? ??? ?System.out.println("請輸入您的姓名:");
?? ??? ?user.setUserName(scanner.next());
?? ??? ?
?? ??? ?String cardNumber = randomCardNumber();
?? ??? ?System.out.println("您的卡號為:" + cardNumber);
?? ??? ?user.setCardNumber(cardNumber);
?? ??? ?
?? ??? ?
?? ??? ?//用戶輸入密碼時,可能會出現(xiàn)密碼不足或超過6位
?? ??? ?try{
?? ??? ??? ?
?? ??? ??? ?System.out.println("請?jiān)O(shè)置您的密碼(6位數(shù)字):");
?? ??? ??? ?
?? ??? ??? ?int code = scanner.nextInt();
?? ??? ??? ??? ?
?? ??? ??? ?user.setCode(code);
?? ??? ??? ?
?? ??? ?}catch(OptionsException o){
?? ??? ??? ?o.printStackTrace();
?? ??? ?}finally{
?? ??? ??? ?System.out.println("您卡內(nèi)目前余額為:" + user.getBalance());
?? ??? ??? ?
?? ??? ??? ?accounts.add(user);
?? ??? ?}
?? ??? ?
?? ?}
?? ?
?? ?
?? ?//隨機(jī)生成卡號
?? ?public String randomCardNumber(){
?? ??? ?
?? ??? ?
?? ??? ?StringBuilder str=new StringBuilder();//定義變長字符串
?? ??? ?Random random=new Random();?? ?
?? ??? ?
?? ??? ?/**設(shè)立一個flag,
?? ??? ? * 在隨機(jī)卡號生成后,用于判斷生成的卡號是否已經(jīng)存在。
?? ??? ? * 如果該卡號已經(jīng)存在,則再進(jìn)行一次該方法重新生成卡號,直到生成的卡號不存在為止
?? ??? ? * 如果該卡號不存在,則將其返回并賦給register()中的user.setCardNumber();
?? ??? ? */
?? ??? ?boolean flag = true;
?? ??? ?
?? ??? ?//隨機(jī)生成數(shù)字,并添加到字符串
?? ??? ?/**因?yàn)楸救顺钟械你y行卡號為19位,所以設(shè)置該卡號為19位*/
?? ??? ?for(int i=0;i<19;i++){
?? ??? ? ? ?str.append(random.nextInt(10));
?? ??? ?}
?? ??? ?
?? ??? ?
?? ??? ?String cardNumber = str.toString();
?? ??? ?/**對生成的卡號進(jìn)行存在判斷
?? ??? ? * 如果該卡號已經(jīng)存在,則flag的值變?yōu)閒alse,并中斷循環(huán)
?? ??? ? * 如果循環(huán)結(jié)束,該卡號仍不存在,則將其返回
?? ??? ? */
?? ??? ?for(int i = 0 ; i < accounts.size() ; i ++){
?? ??? ??? ?if(cardNumber.equals(accounts.get(i).getCardNumber())){
?? ??? ??? ??? ?flag = false;
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ??? ?/**flag的值若為true,則證明生成的卡號不存在,可以返回;
?? ??? ? * 若為false,則證明生成的卡號已經(jīng)存在,需要重新生成,直到生成的卡號不存在為止*/
?? ??? ?if(flag){
?? ??? ??? ?return cardNumber;
?? ??? ?}else{
?? ??? ??? ?return randomCardNumber();
?? ??? ?}
?? ?}
?? ?
?? ?//存款
?? ?public void depositMoney(){
?? ??? ?
?? ??? ?/**設(shè)立一個flag,輔助判斷輸入的卡號是否存在*/
?? ??? ?boolean flag = false;
?? ??? ?
?? ??? ?/**設(shè)立一個index,當(dāng)卡號存在時,index 接收此時的索引值,方便存款*/
?? ??? ?int index = 0;
?? ??? ?
?? ??? ?System.out.println("請輸入您的卡號:");
?? ??? ?String cardNumber = scanner.next();
?? ??? ?
?? ??? ?/**遍歷查詢accouts 中是否存在用戶輸入的卡號
?? ??? ? * 若存在,flag的值變?yōu)閠rue,同時結(jié)束循環(huán),*/
?? ??? ?for(int i = 0 ; i < accounts.size() ; i ++){
?? ??? ??? ?if(cardNumber.equals(accounts.get(i).getCardNumber())){
?? ??? ??? ??? ?flag = true;
?? ??? ??? ??? ?index = i;
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ??? ?
?? ??? ?/**若卡號存在,則需要用戶輸入密碼,密碼正確才可以進(jìn)行操作*/
?? ??? ?if(flag){
?? ??? ??? ?System.out.println("請輸入您的密碼:");
?? ??? ??? ?int code = scanner.nextInt();
?? ??? ??? ?
?? ??? ??? ?if(code == accounts.get(index).getCode()){
?? ??? ??? ??? ?
?? ??? ??? ??? ?
?? ??? ??? ??? ?/**判斷用戶輸入的金額是否大于0, 若小于0,則報錯,并退出程序。
?? ??? ??? ??? ? * 若正確,則將金額加到余額中*/
?? ??? ??? ??? ?try{
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?System.out.println("請輸入您想要存儲的金額:");
?? ??? ??? ??? ??? ?accounts.get(index).setDeposit(scanner.nextInt());
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?int newBalance = accounts.get(index).getBalance() + accounts.get(index).getDeposit();
?? ??? ??? ??? ??? ?accounts.get(index).setBalance(newBalance);
?? ??? ??? ??? ?}catch(OptionsException o){
?? ??? ??? ??? ??? ?o.printStackTrace();
?? ??? ??? ??? ?}finally{
?? ??? ??? ??? ??? ?System.out.println("您目前的余額為:" + accounts.get(index).getBalance());
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}else{
?? ??? ??? ?System.out.println("您輸入的卡號不存在,操作已結(jié)束!");
?? ??? ?}
?? ?}? ??
?? ?
?? ?//取款
?? ?public void withdraMoney(){
?? ??? ?
?? ??? ?/**設(shè)立一個flag,輔助判斷輸入的卡號是否存在*/
?? ??? ?boolean flag = false;
?? ??? ?
?? ??? ?/**設(shè)立一個index,當(dāng)卡號存在時,index 接收此時的索引值,方便取款*/
?? ??? ?int index = 0;
?? ??? ?
?? ??? ?System.out.println("請輸入您的卡號:");
?? ??? ?String cardNumber = scanner.next();
?? ??? ?
?? ??? ?/**遍歷查詢accouts 中是否存在用戶輸入的卡號
?? ??? ? * 若存在,flag的值變?yōu)閠rue,同時結(jié)束循環(huán),*/
?? ??? ?for(int i = 0 ; i < accounts.size() ; i ++){
?? ??? ??? ?if(cardNumber.equals(accounts.get(i).getCardNumber())){
?? ??? ??? ??? ?flag = true;
?? ??? ??? ??? ?index = i;
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ??? ?
?? ??? ?/**若卡號存在,則需要用戶輸入密碼,密碼正確才可以進(jìn)行操作*/
?? ??? ?if(flag){
?? ??? ??? ?System.out.println("請輸入您的密碼:");
?? ??? ??? ?int code = scanner.nextInt();
?? ??? ??? ?
?? ??? ??? ?if(code == accounts.get(index).getCode()){
?? ??? ??? ??? ?
?? ??? ??? ??? ?
?? ??? ??? ??? ?//注意如果用戶要取出的金額大于其余額,要報錯
?? ??? ??? ??? ?/**判斷用戶輸入的金額是否大于0, 若小于0,則報錯,并退出程序。
?? ??? ??? ??? ? * 若正確,則將金額加到余額中*/
?? ??? ??? ??? ?try{
?? ??? ??? ??? ??? ?System.out.println("請輸入您想要取出的金額:");
?? ??? ??? ??? ??? ?accounts.get(index).setWithdrawMoney(scanner.nextInt());
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?int newBalance = accounts.get(index).getBalance() - accounts.get(index).getWithdrawMoney();
?? ??? ??? ??? ??? ?accounts.get(index).setBalance(newBalance);
?? ??? ??? ??? ?}catch(OptionsException o){
?? ??? ??? ??? ??? ?o.printStackTrace();
?? ??? ??? ??? ?}finally{
?? ??? ??? ??? ??? ?System.out.println("您取出了" +accounts.get(index).getWithdrawMoney());
?? ??? ??? ??? ??? ?System.out.println("取出后的余額為:" + accounts.get(index).getBalance());
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ??? ?if(!flag)
?? ??? ??? ?System.out.println("該卡號不存在!");
?? ?}?? ?
?? ?
?? ??? ?
?? ?//按照卡號查詢余額
?? ?public void queryBalance(){
?? ??? ?
?? ??? ?/**設(shè)立一個flag,輔助判斷輸入的卡號是否存在*/
?? ??? ?boolean flag = false;
?? ??? ?
?? ??? ?/**設(shè)立一個index,當(dāng)卡號存在時,index 接收此時的索引值,方便存款*/
?? ??? ?int index = 0;
?? ??? ?
?? ??? ?System.out.println("請輸入您的卡號:");
?? ??? ?String cardNumber = scanner.next();
?? ??? ?
?? ??? ?/**遍歷查詢accouts 中是否存在用戶輸入的卡號
?? ??? ? * 若存在,flag的值變?yōu)閠rue,同時結(jié)束循環(huán),*/
?? ??? ?for(int i = 0 ; i < accounts.size() ; i ++){
?? ??? ??? ?if(cardNumber.equals(accounts.get(i).getCardNumber())){
?? ??? ??? ??? ?flag = true;
?? ??? ??? ??? ?index = i;
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ?}
?? ?
?? ??? ?/**若卡號存在,則需要用戶輸入密碼,密碼正確才可以進(jìn)行操作*/
?? ??? ?if(flag){
?? ??? ??? ?System.out.println("請輸入您的密碼:");
?? ??? ??? ?int code = scanner.nextInt();
?? ??? ??? ?
?? ??? ??? ?if(code == accounts.get(index).getCode()){
?? ??? ??? ??? ?
?? ??? ??? ??? ?System.out.println("您目前的余額為:" + accounts.get(index).getBalance());
?? ??? ??? ?
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ??? ?if(!flag)
?? ??? ??? ?System.out.println("該卡號不存在!");
?? ?}
?? ?
}

4.再新建一個類,在該類中寫一個方法由用戶選擇調(diào)用上個步驟中寫的各個方法(即設(shè)置菜單,進(jìn)行功能選擇):
   * 1.開戶  * 2.存款  * 3.取款   * 4.查詢余額

import java.util.Scanner;
?
public class UserService {
?? ?
?? ?UserOptions uo = new UserOptions();
?? ?Scanner scanner = new Scanner(System.in);
?? ?
?? ?/**設(shè)置菜單,進(jìn)行功能選擇:
?? ? * 1.開戶
?? ? * 2.存款
?? ? * 3.取款
?? ? * 4.查詢余額*/
?? ?@SuppressWarnings("finally")
?? ?public void menu(){
?? ??? ?
?? ??? ?bulletin();
?? ??? ?
?? ??? ?while(true){
?? ??? ??? ?
?? ??? ??? ?System.out.println("請選擇您要使用的功能:");
?? ??? ??? ?int choice = scanner.nextInt();
?? ??? ??? ?
?? ??? ??? ?switch(choice){
?? ??? ??? ??? ?case 1:
?? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ?uo.register();
?? ??? ??? ??? ?} catch (Exception e) {
?? ??? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ?}finally{
?? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?case 2:
?? ??? ??? ??? ??? ?uo.depositMoney();
?? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ?case 3:
?? ??? ??? ??? ??? ?uo.withdraMoney();
?? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ?case 4:
?? ??? ??? ??? ??? ?uo.queryBalance();
?? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ?case -1:
?? ??? ??? ??? ??? ?System.exit(0);
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ??? ?System.out.println("------------------------------\n");
?? ??? ?}
?? ?}
?? ?
?? ?public void bulletin(){
?? ??? ?System.out.println("歡迎使用\"不坑你坑誰\"牌自動取款機(jī)");
?? ??? ?System.out.println("本機(jī)提供以下功能,請自助選擇:");
?? ??? ?System.out.println("1.開戶");
?? ??? ?System.out.println("2.存款");
?? ??? ?System.out.println("3.取款");
?? ??? ?System.out.println("4.查詢余額");
?? ??? ?System.out.println("輸入-1結(jié)束操作");
?? ??? ?System.out.println("------------------------------\n");
?? ?}
?? ?
}

5.最后再主方法中將上一個類進(jìn)行實(shí)例化并使用:

public class Demo {
?
?? ?public static void main(String[] args) {
?? ??? ?UserService us = new UserService();
?? ??? ?
?? ??? ?us.menu();
?? ?}
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 解決spring中redistemplate不能用通配符keys查出相應(yīng)Key的問題

    解決spring中redistemplate不能用通配符keys查出相應(yīng)Key的問題

    這篇文章主要介紹了解決spring中redistemplate不能用通配符keys查出相應(yīng)Key的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • MyBatis之關(guān)于動態(tài)SQL解讀

    MyBatis之關(guān)于動態(tài)SQL解讀

    這篇文章主要介紹了MyBatis之關(guān)于動態(tài)SQL解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Java線程池ThreadPoolExecutor的使用及其原理詳細(xì)解讀

    Java線程池ThreadPoolExecutor的使用及其原理詳細(xì)解讀

    這篇文章主要介紹了Java線程池ThreadPoolExecutor的使用及其原理詳細(xì)解讀,線程池是一種多線程處理形式,處理過程中將任務(wù)添加到隊(duì)列,然后在創(chuàng)建線程后自動啟動這些任務(wù),線程池線程都是后臺線程,需要的朋友可以參考下
    2023-12-12
  • SpringBoot中如何啟動Tomcat流程

    SpringBoot中如何啟動Tomcat流程

    這篇文章主要介紹了SpringBoot中如何啟動Tomcat流程,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • Java利用IO流實(shí)現(xiàn)簡易的記事本功能

    Java利用IO流實(shí)現(xiàn)簡易的記事本功能

    本文將利用Java中IO流編寫一個模擬日記本的程序,通過在控制臺輸入指令,實(shí)現(xiàn)在本地新建文件,打開日記本和修改日記本等功能,感興趣的可以了解一下
    2022-05-05
  • Java實(shí)現(xiàn)合并word文檔的示例代碼

    Java實(shí)現(xiàn)合并word文檔的示例代碼

    在做項(xiàng)目中,經(jīng)常會遇到一種情況,需要將一個小word文檔的內(nèi)容插入到一個大word(主文檔)中。本文就為大家準(zhǔn)備了Java實(shí)現(xiàn)合并word文檔的方法,需要的可以參考一下
    2022-08-08
  • 解決springboot引入swagger2不生效問題

    解決springboot引入swagger2不生效問題

    這篇文章主要為大家介紹了解決springboot引入swagger2不生效問題的方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • Spring Cloud Config 使用本地配置文件方式

    Spring Cloud Config 使用本地配置文件方式

    這篇文章主要介紹了Spring Cloud Config 使用本地配置文件方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 詳解SpringBoot中添加@ResponseBody注解會發(fā)生什么

    詳解SpringBoot中添加@ResponseBody注解會發(fā)生什么

    這篇文章主要介紹了詳解SpringBoot中添加@ResponseBody注解會發(fā)生什么,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • 詳解Springboot 優(yōu)雅停止服務(wù)的幾種方法

    詳解Springboot 優(yōu)雅停止服務(wù)的幾種方法

    這篇文章主要介紹了詳解Springboot 優(yōu)雅停止服務(wù)的幾種方法 ,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08

最新評論