Java多类合作实例4银行案例以及优化

这里不做过多的解释,需要的人可以自己吧这个代码敲一遍,一般都有注释。适合有一点有基础的人~

  • 银行类()
    */
    import java.util.Scanner;

public class Bank { //假定中国建设银行

Scanner input  = new Scanner(System.in); // 实例方法共享

//User数据的存储,保存已注册用户的信息
User[] users = new User[5];
		
public Bank () {     //第一行逻辑,初始化实例变量!!!!!!

	  new User() ;
	  //存入数据
	  users[0] = new User("123456", "412722199505040012", "靓琦" , "123456", "15660091689", 100000000.0);
	  users[1] = new User("1234567891", "412722199505040012", "吴彦祖" , "123496", "15660091680", 1000.0);
	}

//登陆验证
public void login() {
	//登陆操作
	System.out.print("请输入卡号: ");
	String no = input.next();
	System.out.print("请输入密码:");
	String pwd = input.next();
	
	User u = check(no,pwd);

	if(u != null) {
		this.showMenu(u);
	} else {
		System.out.println("用户名或密码错误");
		}
	
}

//验证
public User check(String no , String pwd) { //单一智能原则
	
	for (int i = 0 ; i < users.length ; i++) {
	//验证卡号和密码 
	 if(users[i] != null) { //非空判断
		if(no.equals(users[i].getCardno()) && pwd.equals(users[i].getPassword())) {
			//查找到该用户,用户存在
			return users[i];//存在
		} 
	  }
	}	
	return null ;
}


//显示菜单
public void showMenu(User u) { 
	
int choice;
//对用户输入的合法性做判断
do {
	System.out.println("--------------------------------------------------------");
	System.out.println("1.开户 , 2.存钱 , 3.取款  ,4.转账  , 5.查询余额  ,6 修改密码  , 0.退出 ");
	System.out.println("--------------------------------------------------------");
	System.out.println("请输入操作编码");
	//定义一个变量赋值为用户输入的数字
	choice = input.nextInt();
	//对用户进行输入的语句进行判断执行相对应的语句
	switch(choice) {
		case 1 :
			System.out.println("执行开户");
			break;
		case 2 :
			System.out.println("执行存款");
			break;
		case 3 :
			this.withdrawal(u);
			break;
		case 4 :
			System.out.println("执行转账");
			break;
		case 5 :
			System.out.println("执行查询余额");
			break;
		case 6 :
			System.out.println("执行修改密码");
			break;
		case 0 :
			System.out.println("执行退出");
			return;
		default :
			System.out.println("输入有误请重新输入!");
			break;
		}
	} while(true); 
}

//取款
public void withdrawal(User mine) {
	System.out.println("请输入取款金额:");
	double money = input.nextDouble();
	
	if (money < mine.getBalance()) {
		//判断是不是可以取款
		mine.setBalance(mine.getBalance() - money);
		System.out.println("取款成功,当前余额为:" + mine.getBalance());
	} else {
		System.out.println("余额不足");
	}
	
}

}

/*

  • 银行案例的测试类
    */

/**

  • @author sakura

*/
public class BankMenu {
public static void main(String[] args) {

	Bank bank = new Bank(); // 创建一家银行网点
	
	bank.login();
}

}

public class User {

private String cardno;//卡号
private String identity;//身份证号
private String username;//用户名
private String password;//密码
private String phone;//电话号
private double balance; //余额

public User() {}  //无参构造方法,不要问写就行了

//在bank构造方法里懒的在去一个一个写,直接定义一个六参方法拿来用就行了
public User (String cardNo , String identity , String username , String password , String phone , double balance) {
	this.cardno = cardNo;
	this.identity = identity;
	this.username = username;
	this.password = password;
	this.phone = phone;
	this.balance = balance;
}
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 getUsername() {
	return username;
}
public void setUsername(String username) {
	this.username = username;
}
public String getPassword() {
	return password;
}
public void setPassword(String password) {
	this.password = password;
}
public String getPhone() {
	return phone;
}
public void setPhone(String phone) {
	this.phone = phone;
}
public double getBalance() {
	return balance;
}
public void setBalance(double balance) {
	this.balance = balance;
}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值