一:上码
package com.wyj.two;import java.util.Scanner;/*** 封装的练习*/
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("请输入你的服务选项:");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.设置用户的基本信息(姓名,新建账户,新建密码)");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("请输入正确格式的姓名,默认姓名:无名");this.name = "无名";}}public void setBalance(double balance) {if (balance > 20)this.balance = balance;else {System.out.println("余额不足,余额默认为:20");this.balance = 20;}}public void setPassword(String password) {if (password.length() == 6)this.password = password;else {System.out.println("密码格式有误,密码为6为字符,默认为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("请输入姓名:");String name = in.next();//不用in.nextLine();他会把一些无效字符给读进去this.name = name;System.out.println("请输入新建账号:");// in.nextLine();//将换行符读掉String acc = in.next();this.acc =acc;System.out.println("请输入新建密码:");// in.nextLine();//将换行符读掉String pas = in.next();this.password = pas;System.out.println("恭喜您开户成功!");}//存钱public void save_money() {System.out.print("请输入您的账号:");String acc1 = in.next();System.out.print("请输入您的密码:");String pas1 = in.next();System.out.print("请输入存钱的金额:");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("您输入的账号有误!请重新输入您的账号");acc1 = in.nextLine();} else if (acc1.equals(this.acc) && !pas1.equals(this.password)) {System.out.println("您输入的密码有误,请重新输入您的密码");pas1 = in.nextLine();}}}//取钱public void draw_money() {System.out.println("请输入您的账号:");in.nextLine();//将换行符读掉String acc1 = in.next();System.out.println("请输入您的密码:");in.nextLine();//将换行符读掉String pas1 = in.next();System.out.println("请输入取钱的金额:");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("对不起您的余额不足!");}else{this.balance -= money;System.out.println("取钱成功!");}flag = false;} else if (!acc1.equals(this.acc) && pas1.equals(this.password)) {System.out.println("您输入的账号有误!请重新输入您的账号");acc1 = in.nextLine();} else if (acc1.equals(this.acc) && !pas1.equals(this.password)) {System.out.println("您输入的密码有误,请重新输入您的密码");pas1 = in.nextLine();}}}//查询余额public void check() {System.out.println("请输入您的账号:");String acc1 = in.next();System.out.println("请输入您的密码:");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("您输入的账号有误!请重新输入您的账号");acc1 = in.nextLine();} else if (acc1.equals(this.acc) && !pas1.equals(this.password)) {System.out.println("您输入的密码有误,请重新输入您的密码");pas1 = in.nextLine();}}}@Overridepublic String toString() {return "Account{" +"name='" + name + '\'' +", balance=" + balance +", password='" + password + '\'' +'}';}
}
其他极限条件自行测试,有问题直接留言,我速到!!!!!!!!!!!!!!!