Java實(shí)現(xiàn)簡(jiǎn)易提款機(jī)
本文實(shí)例為大家分享了vue + element ui實(shí)現(xiàn)錨點(diǎn)定位的具體代碼,供大家參考,具體內(nèi)容如下
需求:
簡(jiǎn)易自動(dòng)提款機(jī)
1.創(chuàng)建用戶類User(包含卡號(hào)、姓名、密碼、余額等屬性),用戶開(kāi)卡時(shí)錄入的姓名和密碼(自動(dòng)分配一個(gè)卡號(hào)、初始金額設(shè)置為0)。
2.使用ArrayList或LinkedList存儲(chǔ)全部注冊(cè)用戶
形式如:ArrayList<User> userList = new ArrayList<User>();
3.實(shí)現(xiàn)功能
(1)存款
(2)取款(如果余額不足要提示)
(3)查詢余額(只能查看當(dāng)前卡號(hào)的余額)
4. 技術(shù)要求,使用異常處理用戶的錯(cuò)誤輸入(即程序保護(hù)容錯(cuò)功能)。
實(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.對(duì)用戶信息(卡號(hào)、姓名、密碼、余額、存款金額、取款金額)進(jìn)行封裝,并對(duì)部分屬性進(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("存入金額錯(cuò)誤!");
?? ??? ?}
?? ?}
?
?? ?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("取款金額錯(cuò)誤!");
?? ??? ?}?? ??? ?
?? ?}
}3.新建一個(gè)類,定義用戶可進(jìn)行的操作方法:
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
?
public class UserOptions {
?
?? ?/**定義一個(gè)ArrayList
? ?
? ? 來(lái)存儲(chǔ)注冊(cè)的銀行卡信息*/
?? ?public static ArrayList
? ??
? ? ? accounts = new ArrayList
? ? ?
? ? ? ();
?? ?
?? ?Scanner scanner = new Scanner(System.in);
?? ?
?? ?/**調(diào)用User 類訪問(wèn)該類下封裝的用戶信息*/
?? ?User user = new User();
?? ?
?? ?//為新用戶注冊(cè)銀行卡信息
?? ?public void register() throws Exception {
?? ??? ?
?? ??? ?System.out.println("請(qǐng)輸入您的姓名:");
?? ??? ?user.setUserName(scanner.next());
?? ??? ?
?? ??? ?String cardNumber = randomCardNumber();
?? ??? ?System.out.println("您的卡號(hào)為:" + cardNumber);
?? ??? ?user.setCardNumber(cardNumber);
?? ??? ?
?? ??? ?
?? ??? ?//用戶輸入密碼時(shí),可能會(huì)出現(xiàn)密碼不足或超過(guò)6位
?? ??? ?try{
?? ??? ??? ?
?? ??? ??? ?System.out.println("請(qǐng)?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ī)生成卡號(hào)
?? ?public String randomCardNumber(){
?? ??? ?
?? ??? ?
?? ??? ?StringBuilder str=new StringBuilder();//定義變長(zhǎng)字符串
?? ??? ?Random random=new Random();?? ?
?? ??? ?
?? ??? ?/**設(shè)立一個(gè)flag,
?? ??? ? * 在隨機(jī)卡號(hào)生成后,用于判斷生成的卡號(hào)是否已經(jīng)存在。
?? ??? ? * 如果該卡號(hào)已經(jīng)存在,則再進(jìn)行一次該方法重新生成卡號(hào),直到生成的卡號(hào)不存在為止
?? ??? ? * 如果該卡號(hào)不存在,則將其返回并賦給register()中的user.setCardNumber();
?? ??? ? */
?? ??? ?boolean flag = true;
?? ??? ?
?? ??? ?//隨機(jī)生成數(shù)字,并添加到字符串
?? ??? ?/**因?yàn)楸救顺钟械你y行卡號(hào)為19位,所以設(shè)置該卡號(hào)為19位*/
?? ??? ?for(int i=0;i<19;i++){
?? ??? ? ? ?str.append(random.nextInt(10));
?? ??? ?}
?? ??? ?
?? ??? ?
?? ??? ?String cardNumber = str.toString();
?? ??? ?/**對(duì)生成的卡號(hào)進(jìn)行存在判斷
?? ??? ? * 如果該卡號(hào)已經(jīng)存在,則flag的值變?yōu)閒alse,并中斷循環(huán)
?? ??? ? * 如果循環(huán)結(jié)束,該卡號(hào)仍不存在,則將其返回
?? ??? ? */
?? ??? ?for(int i = 0 ; i < accounts.size() ; i ++){
?? ??? ??? ?if(cardNumber.equals(accounts.get(i).getCardNumber())){
?? ??? ??? ??? ?flag = false;
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ??? ?/**flag的值若為true,則證明生成的卡號(hào)不存在,可以返回;
?? ??? ? * 若為false,則證明生成的卡號(hào)已經(jīng)存在,需要重新生成,直到生成的卡號(hào)不存在為止*/
?? ??? ?if(flag){
?? ??? ??? ?return cardNumber;
?? ??? ?}else{
?? ??? ??? ?return randomCardNumber();
?? ??? ?}
?? ?}
?? ?
?? ?//存款
?? ?public void depositMoney(){
?? ??? ?
?? ??? ?/**設(shè)立一個(gè)flag,輔助判斷輸入的卡號(hào)是否存在*/
?? ??? ?boolean flag = false;
?? ??? ?
?? ??? ?/**設(shè)立一個(gè)index,當(dāng)卡號(hào)存在時(shí),index 接收此時(shí)的索引值,方便存款*/
?? ??? ?int index = 0;
?? ??? ?
?? ??? ?System.out.println("請(qǐng)輸入您的卡號(hào):");
?? ??? ?String cardNumber = scanner.next();
?? ??? ?
?? ??? ?/**遍歷查詢accouts 中是否存在用戶輸入的卡號(hào)
?? ??? ? * 若存在,flag的值變?yōu)閠rue,同時(shí)結(jié)束循環(huán),*/
?? ??? ?for(int i = 0 ; i < accounts.size() ; i ++){
?? ??? ??? ?if(cardNumber.equals(accounts.get(i).getCardNumber())){
?? ??? ??? ??? ?flag = true;
?? ??? ??? ??? ?index = i;
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ??? ?
?? ??? ?/**若卡號(hào)存在,則需要用戶輸入密碼,密碼正確才可以進(jìn)行操作*/
?? ??? ?if(flag){
?? ??? ??? ?System.out.println("請(qǐng)輸入您的密碼:");
?? ??? ??? ?int code = scanner.nextInt();
?? ??? ??? ?
?? ??? ??? ?if(code == accounts.get(index).getCode()){
?? ??? ??? ??? ?
?? ??? ??? ??? ?
?? ??? ??? ??? ?/**判斷用戶輸入的金額是否大于0, 若小于0,則報(bào)錯(cuò),并退出程序。
?? ??? ??? ??? ? * 若正確,則將金額加到余額中*/
?? ??? ??? ??? ?try{
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?System.out.println("請(qǐng)輸入您想要存儲(chǔ)的金額:");
?? ??? ??? ??? ??? ?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("您輸入的卡號(hào)不存在,操作已結(jié)束!");
?? ??? ?}
?? ?}? ??
?? ?
?? ?//取款
?? ?public void withdraMoney(){
?? ??? ?
?? ??? ?/**設(shè)立一個(gè)flag,輔助判斷輸入的卡號(hào)是否存在*/
?? ??? ?boolean flag = false;
?? ??? ?
?? ??? ?/**設(shè)立一個(gè)index,當(dāng)卡號(hào)存在時(shí),index 接收此時(shí)的索引值,方便取款*/
?? ??? ?int index = 0;
?? ??? ?
?? ??? ?System.out.println("請(qǐng)輸入您的卡號(hào):");
?? ??? ?String cardNumber = scanner.next();
?? ??? ?
?? ??? ?/**遍歷查詢accouts 中是否存在用戶輸入的卡號(hào)
?? ??? ? * 若存在,flag的值變?yōu)閠rue,同時(shí)結(jié)束循環(huán),*/
?? ??? ?for(int i = 0 ; i < accounts.size() ; i ++){
?? ??? ??? ?if(cardNumber.equals(accounts.get(i).getCardNumber())){
?? ??? ??? ??? ?flag = true;
?? ??? ??? ??? ?index = i;
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ??? ?
?? ??? ?/**若卡號(hào)存在,則需要用戶輸入密碼,密碼正確才可以進(jìn)行操作*/
?? ??? ?if(flag){
?? ??? ??? ?System.out.println("請(qǐng)輸入您的密碼:");
?? ??? ??? ?int code = scanner.nextInt();
?? ??? ??? ?
?? ??? ??? ?if(code == accounts.get(index).getCode()){
?? ??? ??? ??? ?
?? ??? ??? ??? ?
?? ??? ??? ??? ?//注意如果用戶要取出的金額大于其余額,要報(bào)錯(cuò)
?? ??? ??? ??? ?/**判斷用戶輸入的金額是否大于0, 若小于0,則報(bào)錯(cuò),并退出程序。
?? ??? ??? ??? ? * 若正確,則將金額加到余額中*/
?? ??? ??? ??? ?try{
?? ??? ??? ??? ??? ?System.out.println("請(qǐng)輸入您想要取出的金額:");
?? ??? ??? ??? ??? ?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("該卡號(hào)不存在!");
?? ?}?? ?
?? ?
?? ??? ?
?? ?//按照卡號(hào)查詢余額
?? ?public void queryBalance(){
?? ??? ?
?? ??? ?/**設(shè)立一個(gè)flag,輔助判斷輸入的卡號(hào)是否存在*/
?? ??? ?boolean flag = false;
?? ??? ?
?? ??? ?/**設(shè)立一個(gè)index,當(dāng)卡號(hào)存在時(shí),index 接收此時(shí)的索引值,方便存款*/
?? ??? ?int index = 0;
?? ??? ?
?? ??? ?System.out.println("請(qǐng)輸入您的卡號(hào):");
?? ??? ?String cardNumber = scanner.next();
?? ??? ?
?? ??? ?/**遍歷查詢accouts 中是否存在用戶輸入的卡號(hào)
?? ??? ? * 若存在,flag的值變?yōu)閠rue,同時(shí)結(jié)束循環(huán),*/
?? ??? ?for(int i = 0 ; i < accounts.size() ; i ++){
?? ??? ??? ?if(cardNumber.equals(accounts.get(i).getCardNumber())){
?? ??? ??? ??? ?flag = true;
?? ??? ??? ??? ?index = i;
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ?}
?? ?
?? ??? ?/**若卡號(hào)存在,則需要用戶輸入密碼,密碼正確才可以進(jìn)行操作*/
?? ??? ?if(flag){
?? ??? ??? ?System.out.println("請(qǐng)輸入您的密碼:");
?? ??? ??? ?int code = scanner.nextInt();
?? ??? ??? ?
?? ??? ??? ?if(code == accounts.get(index).getCode()){
?? ??? ??? ??? ?
?? ??? ??? ??? ?System.out.println("您目前的余額為:" + accounts.get(index).getBalance());
?? ??? ??? ?
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ??? ?if(!flag)
?? ??? ??? ?System.out.println("該卡號(hào)不存在!");
?? ?}
?? ?
}4.再新建一個(gè)類,在該類中寫一個(gè)方法由用戶選擇調(diào)用上個(gè)步驟中寫的各個(gè)方法(即設(shè)置菜單,進(jìn)行功能選擇):
* 1.開(kāi)戶 * 2.存款 * 3.取款 * 4.查詢余額
import java.util.Scanner;
?
public class UserService {
?? ?
?? ?UserOptions uo = new UserOptions();
?? ?Scanner scanner = new Scanner(System.in);
?? ?
?? ?/**設(shè)置菜單,進(jìn)行功能選擇:
?? ? * 1.開(kāi)戶
?? ? * 2.存款
?? ? * 3.取款
?? ? * 4.查詢余額*/
?? ?@SuppressWarnings("finally")
?? ?public void menu(){
?? ??? ?
?? ??? ?bulletin();
?? ??? ?
?? ??? ?while(true){
?? ??? ??? ?
?? ??? ??? ?System.out.println("請(qǐng)選擇您要使用的功能:");
?? ??? ??? ?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("歡迎使用\"不坑你坑誰(shuí)\"牌自動(dòng)取款機(jī)");
?? ??? ?System.out.println("本機(jī)提供以下功能,請(qǐng)自助選擇:");
?? ??? ?System.out.println("1.開(kāi)戶");
?? ??? ?System.out.println("2.存款");
?? ??? ?System.out.println("3.取款");
?? ??? ?System.out.println("4.查詢余額");
?? ??? ?System.out.println("輸入-1結(jié)束操作");
?? ??? ?System.out.println("------------------------------\n");
?? ?}
?? ?
}5.最后再主方法中將上一個(gè)類進(jìn)行實(shí)例化并使用:
public class Demo {
?
?? ?public static void main(String[] args) {
?? ??? ?UserService us = new UserService();
?? ??? ?
?? ??? ?us.menu();
?? ?}
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java實(shí)現(xiàn)簡(jiǎn)單解析XML文件功能示例
這篇文章主要介紹了java實(shí)現(xiàn)簡(jiǎn)單解析XML文件功能,結(jié)合實(shí)例形式分析了java針對(duì)xml文件的讀取、遍歷節(jié)點(diǎn)及輸出等相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
Java實(shí)現(xiàn)synchronized鎖同步機(jī)制
synchronized是java內(nèi)置的同步鎖實(shí)現(xiàn),本文就詳細(xì)的介紹一下Java實(shí)現(xiàn)synchronized鎖同步機(jī)制,具有一定的參考價(jià)值,感興趣的可以了解一下2021-11-11
JAVA中Context的詳細(xì)介紹和實(shí)例分析
這篇文章主要介紹了JAVA中Context的詳細(xì)介紹和實(shí)例分析,Context是維持android各組件能夠正常工作的一個(gè)核心功能類。如果感興趣來(lái)學(xué)習(xí)一下2020-07-07
Java權(quán)重隨機(jī)的實(shí)現(xiàn)方法
這篇文章主要介紹了Java權(quán)重隨機(jī)的實(shí)現(xiàn)方法,實(shí)例分析了權(quán)重隨機(jī)算法的原理與完整實(shí)現(xiàn)方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01
基于@RequestBody和@ResponseBody及Stringify()的作用說(shuō)明
這篇文章主要介紹了基于@RequestBody和@ResponseBody及Stringify()的作用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Java實(shí)現(xiàn)將Object轉(zhuǎn)成List的五種方法
在Java中,將一個(gè)Object轉(zhuǎn)換為L(zhǎng)ist是一個(gè)常見(jiàn)的需求,尤其是在處理集合操作和數(shù)據(jù)轉(zhuǎn)換時(shí),本文將詳細(xì)討論如何實(shí)現(xiàn)這一轉(zhuǎn)換,并提供一些代碼示例,需要的朋友可以參考下2025-03-03
Java報(bào)錯(cuò)java.awt.AWTException: AWT的解決方法
在Java圖形用戶界面(GUI)編程中,java.awt.AWTException是一個(gè)常見(jiàn)的異常,它通常與AWT(Abstract Window Toolkit)組件相關(guān),這個(gè)異常可能在嘗試進(jìn)行與窗口、圖形環(huán)境或系統(tǒng)剪貼板等操作時(shí)拋出,本文將詳細(xì)探討AWTException的成因,并提供多種解決方案2024-12-12

