java小案例(适合初学者)-银行系统的存款收款转账等功能代码

User类

package com.cwy.demo1;
/**
 * User类
 * @author dell
 *
 */
public class User {
	//卡号
	private String cardNo;
	//身份证号
	private String identity;
	//用户名
	private String username;
	//密码
	private String password;
	//电话
	private String phone;
	//金额
	private double balance;
	
	//无参数的构造方法
	public User() {
		// TODO Auto-generated constructor stub
	}
	//生成带参数的构造方法   
	public User(String cardNo, String identity, String username,
			String password, String phone, double balance) {
		super();
		this.cardNo = cardNo;
		this.identity = identity;
		this.username = username;
		this.password = password;
		this.phone = phone;
		this.balance = balance;
	}
	
	
	//生成get或者set方法
	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;
	}
}

Bank类

package com.cwy.demo1;

import java.util.Scanner;

/**
 * Bank类
 * @author dell
 *
 */
public class Bank {
	//保存用户的数组
	private User[] users= new User[5];

	//保存个数
	private int size;
	//初始化方法
	public void initial(){
		//创建用户1
		User user1 = new User();
		user1.setCardNo("257300199708143311");
		user1.setBalance(151315);
		user1.setIdentity("5485156161");
		user1.setPassword("123456");
		user1.setUsername("小明");
		user1.setPhone("12345678901");
		//创建用户
		User user2 = new User();
		user2.setCardNo("257300199708143524");
		user2.setBalance(100000);
		user2.setIdentity("5485156162");
		user2.setPassword("123456");
		user2.setUsername("小光");
		user2.setPhone("12345678901");	
		
		//嵌入数组
		users[0]=user1;
		users[1]=user2;
		size=2;
		//System.out.println("用户初始完成");
	    	
	}
	public Bank() {
		initial();		
	}
	//用户登入
	
	public void login(){
		Scanner input = new Scanner(System.in);
		System.out.println("请输入卡号");
		String cardNo = input.next();
		System.out.println("请输入密码");
		String password = input.next();
		
		//遍历数组
		User u =null; //保存找到的用户
		for (int i = 0; i < size; i++) {
			if(users[i].getCardNo().equals(cardNo)&&users[i].getPassword().equals(password)){
				u = users[i];
				break;
			}
		}
		if(u!=null){
			System.out.println("登入成功");
			//显示菜单
			showMenu(u);
		}else{
			System.out.println("卡号或者密码错误");
		}
			
			
		}
	//显示菜单
	public void showMenu(User u){
		Scanner input = new Scanner(System.in);
		System.out.println("==========欢迎进入银行系统====当前用户"+u.getCardNo()+"==========");
		//选择完成后继续弹出菜单
		do {
			System.out.println("1 存款 2 收款 3 转账4 查询余额 5 修改密码 0 退出");
			System.out.println("请输入您的选择");
			int choice = input.nextInt();
			switch (choice) {
			case 1:
				this.save(u);
				break;
			case 2:
				this.withDraw(u);
				break;
			case 3:
				this.trans(u);
				break;
			case 4:
				this.checkBalance(u);
				break;
			case 5:
				this.updatePassword(u);
				break;
			case 0:
				return;
			default:
				break;
			}
		} while (true);
		
	}
	//4 存钱
	public void save(User u){
		Scanner input = new Scanner(System.in);
		System.out.println("请输入存款金额");
		double m = input.nextDouble();
		if(m>0){
			u.setBalance(u.getBalance()+m);
			System.out.println("存钱成功,余额为"+u.getBalance());
	
		}else{
			System.out.println("存钱失败,请重新输入");
			
		}
	}
	//5 取钱
	public void withDraw(User u){
		Scanner input = new Scanner(System.in);
		System.out.println("请输入取款金额");
		double m = input.nextDouble();
		if(m>0){
			if(u.getBalance()>=m){
			u.setBalance(u.getBalance()-m);
			System.out.println("取钱成功,余额为"+u.getBalance());
	
			}else{
			System.out.println("余额不足");
			}
		}
		else{
			System.out.println("取钱失败,请重新输入");	
		}
		
	}
	//6转账
	public void trans(User u){
		Scanner input = new Scanner(System.in);
		System.out.println("请输入对方卡号");
		String cardNo = input.next();
		System.out.println("请输入金额");
		double m = input.nextDouble();
		
		//判断对方卡号是否存在
		
		User toUser =  null;
		for(int i =0;i<size;i++){
			if(users[i].getCardNo().equals(cardNo)){     //这里是equals()  不是 ==
				toUser = users[i];
				break;
			}
		}
		if(toUser!=null){
			if(m>0){
				if(u.getBalance()>=m){
					toUser.setBalance(toUser.getBalance()+m);//接受转账人的余额   +m 扣钱
					u.setBalance(u.getBalance()-m);//转账人的余额  -m    加钱
					System.out.println("转账成功,已转账"+m+"余额为"+u.getBalance());
						
				}else{
					System.out.println("余额不足");
				}
				
			}
			else{
				System.out.println("请输入大于0的数值");
			}
		}
		else{
			System.out.println("转账人没找到");
		}
		
		
		
		
	}
	//查询余额
	public void checkBalance(User u){
		System.out.println("您的余额为"+u.getBalance());
	}
	// 修改密码
	public void updatePassword(User u){
		Scanner input = new Scanner(System.in);
		System.out.println("请输入您的密码");
		String p1 = input.next();
		System.out.println("请再输入一遍密码");
		String p2 = input.next();
		if(p1.equals(p2)){
			System.out.println("密码修改正确");
		}else{
			System.out.println("您输入的两次密码不一致");
		}
		u.setPassword(p2);
		
	}
}

测试类

package com.cwy.demo1;

public class TestBank {
	public static void main(String[] args) {
		Bank bank = new Bank();
		//bank.initial();
		bank.login();
	}
}


你可能感兴趣的:(java案例,java)