Java银行开户,取钱,存钱,查询余额,退出。。。。。
生活随笔
收集整理的這篇文章主要介紹了
Java银行开户,取钱,存钱,查询余额,退出。。。。。
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一:上碼
package com.wyj.two;import java.util.Scanner;/*** 封裝的練習(xí)*/ public class Demo8 {public static void main(String[] args) {Scanner in = new Scanner(System.in);Account account = new Account();System.out.println("歡迎來到杰哥銀行");// menu();boolean flag = true;while (flag) {menu();System.out.println("請(qǐng)輸入你的服務(wù)選項(xiàng):");int option = in.nextInt();switch (option) {case 1: {account.open_account();break;}case 2: {account.save_money();break;}case 3: {account.draw_money();break;}case 4: {account.check();break;}case 5: {System.out.println("感謝您的使用!!");flag = false;break;}}}}//菜單欄public static void menu() {System.out.println("1.設(shè)置用戶的基本信息(姓名,新建賬戶,新建密碼)");System.out.println("2.存錢");System.out.println("3.取錢");System.out.println("4.查詢余額");System.out.println("5.退出");}}class Account {private String name;private double balance;//余額private String acc;//賬戶private String password;//密碼Scanner in = new Scanner(System.in);public void setName(String name) {if (name.length() >= 2 && name.length() <= 4)this.name = name;else {System.out.println("請(qǐng)輸入正確格式的姓名,默認(rèn)姓名:無名");this.name = "無名";}}public void setBalance(double balance) {if (balance > 20)this.balance = balance;else {System.out.println("余額不足,余額默認(rèn)為:20");this.balance = 20;}}public void setPassword(String password) {if (password.length() == 6)this.password = password;else {System.out.println("密碼格式有誤,密碼為6為字符,默認(rèn)為123456");this.password = "123456";}}public void setAcc(String acc) {this.acc = acc;}public String getName() {return name;}public double getBalance() {return balance;}public String getPassword() {return password;}public String getAcc() {return acc;}//開戶public void open_account(){System.out.println("請(qǐng)輸入姓名:");String name = in.next();//不用in.nextLine();他會(huì)把一些無效字符給讀進(jìn)去this.name = name;System.out.println("請(qǐng)輸入新建賬號(hào):");// in.nextLine();//將換行符讀掉String acc = in.next();this.acc =acc;System.out.println("請(qǐng)輸入新建密碼:");// in.nextLine();//將換行符讀掉String pas = in.next();this.password = pas;System.out.println("恭喜您開戶成功!");}//存錢public void save_money() {System.out.print("請(qǐng)輸入您的賬號(hào):");String acc1 = in.next();System.out.print("請(qǐng)輸入您的密碼:");String pas1 = in.next();System.out.print("請(qǐng)輸入存錢的金額:");double money = in.nextDouble();boolean flag = true;while (flag) {if (acc1.equals(this.acc) && pas1.equals(this.password)) {if (money < 0)money = 0;this.balance += money;System.out.println("存錢成功!");flag = false;} else if (!acc1.equals(this.acc) && pas1.equals(this.password)) {System.out.println("您輸入的賬號(hào)有誤!請(qǐng)重新輸入您的賬號(hào)");acc1 = in.nextLine();} else if (acc1.equals(this.acc) && !pas1.equals(this.password)) {System.out.println("您輸入的密碼有誤,請(qǐng)重新輸入您的密碼");pas1 = in.nextLine();}}}//取錢public void draw_money() {System.out.println("請(qǐng)輸入您的賬號(hào):");in.nextLine();//將換行符讀掉String acc1 = in.next();System.out.println("請(qǐng)輸入您的密碼:");in.nextLine();//將換行符讀掉String pas1 = in.next();System.out.println("請(qǐng)輸入取錢的金額:");double money = in.nextDouble();boolean flag = true;while (flag) {if (acc1.equals(this.acc) && pas1.equals(this.password)) {if (money < 0)money = 0;if(this.balance < money){System.out.println("對(duì)不起您的余額不足!");}else{this.balance -= money;System.out.println("取錢成功!");}flag = false;} else if (!acc1.equals(this.acc) && pas1.equals(this.password)) {System.out.println("您輸入的賬號(hào)有誤!請(qǐng)重新輸入您的賬號(hào)");acc1 = in.nextLine();} else if (acc1.equals(this.acc) && !pas1.equals(this.password)) {System.out.println("您輸入的密碼有誤,請(qǐng)重新輸入您的密碼");pas1 = in.nextLine();}}}//查詢余額public void check() {System.out.println("請(qǐng)輸入您的賬號(hào):");String acc1 = in.next();System.out.println("請(qǐng)輸入您的密碼:");String pas1 = in.next();boolean flag = true;while (flag) {if (acc1.equals(this.acc) && pas1.equals(this.password)) {System.out.println("您的余額為:" + this.balance);flag = false;} else if (!acc1.equals(this.acc) && pas1.equals(this.password)) {System.out.println("您輸入的賬號(hào)有誤!請(qǐng)重新輸入您的賬號(hào)");acc1 = in.nextLine();} else if (acc1.equals(this.acc) && !pas1.equals(this.password)) {System.out.println("您輸入的密碼有誤,請(qǐng)重新輸入您的密碼");pas1 = in.nextLine();}}}@Overridepublic String toString() {return "Account{" +"name='" + name + '\'' +", balance=" + balance +", password='" + password + '\'' +'}';} }
其他極限條件自行測(cè)試,有問題直接留言,我速到!!!!!!!!!!!!!!!
總結(jié)
以上是生活随笔為你收集整理的Java银行开户,取钱,存钱,查询余额,退出。。。。。的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 哪款翻译字幕的软件比较好用在线翻译字幕的
- 下一篇: java美元兑换,(Java实现) 美元