银行管理系统(新手上路)

银行管理系统(新手上路)

1.开户(身份证+电话+密码 账号自动生成 +0元) -输入:身份证+电话+密码 -封装:身份证+电话+密码===Account
2.查询账户 -输入账户密码 -
3.身份证查询 -输入身份证 -查询 -打印账户信息
4.存钱 -输入 :账号+密码 -查找: accounts (条件= 账户+密码) -
找到: 输入 钱数 修改: 账户钱数 = 原来钱数+钱数
没找到:提示
5.取钱 -输入 :账号+密码 -查找: accounts (条件= 账户+密码) -
找到: 输入 钱数 修改: 账户钱数 = 原来钱数-钱数
没找到:提示
6. 转账 - 输入账号+密码 -查询:

  •   账号+密码(找到、没找到)
    
    输入对方账号 - 查询:对方账号 -输入:金额(判断) - 修改金额
    7.冻结 -输入账号 -修改状态
    8.解冻 -输入账户 -修改状态

package com.bank;

public class Account {
public String no; // 账户
public String password; // 密码
public String id; // 身份证
public String tel; // 电话
public int money; // 余额
public boolean state; // 状态

public Account() {
	this.state = true; // 账户可用
}

public Account(boolean state) {
	this.state = state;
}

public Account(String no, String id, String tel, String password) {
	this(no, id, tel, password, true);
}

public Account(String no, String id, String tel, String password, boolean state) {

	this.no = no;
	this.id = id;
	this.tel = tel;
	this.password = password;
	this.state = state;

}

}

package com.bank;

import java.util.Scanner;

import com.bank.util.BankUtil;

public class BankManager {
public Account accounts[] = new Account[10];
public Scanner sc = new Scanner(System.in);

/**
 * 
 * 1.开户(身份证+电话+密码 ======账号自动生成 +0元) -输入:身份证+电话+密码 -封装:身份证+电话+密码=========Account
 * -如何生成账号(不能重复)
 * 
 */

/*
 * 1.开户
 */
public void open() {
	int position = -1;
	for (int x = 0; x < accounts.length; x++) {// 找空位
		if (accounts[x] == null) {
			position = x;
			break;
		}
	}
	if (position == -1) {
		System.out.println("没有空位子了。。。。。。。。。。。");
		return;
	}
	System.out.println("请输入身份证: ");
	String id = sc.next();
	System.out.println("请输入电话 : ");
	String tel = sc.next();
	System.out.println("请输入密码: ");
	String password = sc.next();

	Account a = new Account(BankUtil.getBankNO(), id, tel, BankUtil.encrypt(password));

	accounts[position] = a;
	System.out.println("账号为: " + a.no);

}

/*
 * 2.查询账户 -输入账户密码 -
 * 
 */

public void searchByNo() {

	System.out.println("请输入账号: ");
	String no = sc.next();
	System.out.println("请输入密码:");
	String password = sc.next();
	for (int x = 0; x < accounts.length; x++) {
		if (accounts[x] != null && accounts[x].no.equals(no)
				&& accounts[x].password.equals(BankUtil.encrypt(password))) {
			// System.out.println(accounts[x].no +"\t" +accounts[x].id + "\t"+
			// accounts[x].tel +"\t" + accounts[x].money +
			// "\t" + accounts[x].state);
			this.printAccount(accounts[x]);
		}

	}
}
/*
 * 3.身份证查询 -输入身份证 -查询 -打印账户信息
 */

public void searchById() {
	System.out.println("身份证: ");
	String id = sc.next();
	for (Account a : accounts)// 遍历数组
	{
		if (a != null && a.id.equals(id)) {
			this.printAccount(a);// 类的方法之间可以相互调用
		}
	}
}

// 打印账号信息
public void printAccount(Account a) {
	System.out.println(a.no + "\t" + a.id + "\t" + a.tel + "\t" + a.money + "\t" + a.state);
}

/*
 * 
 * 4.存钱 -输入 :账号+密码 -查找: accounts (条件= 账户+密码) -找到: 输入 钱数 - 修改: 账户钱数 = 原来钱数+钱数
 * 没找到:提示
 */
public void SaveMoney() {

	Account a = queryByNoAndPass();
	if (a != null) {// 找到
		System.out.println("存入钱数: ");
		int money = sc.nextInt();
		a.money += money;

	}
	this.printAccount(a);
}

/*
 * 5.取钱 -输入 :账号+密码 -查找: accounts (条件= 账户+密码) -找到: 输入 钱数 - 修改: 账户钱数 = 原来钱数-钱数
 * 没找到:提示
 */

public void TakeMoney() {

	Account a = queryByNoAndPass();
	if (a != null) {
		System.out.println("取出钱数: ");
		int money = sc.nextInt();
		if (a.money >= money) {
			a.money -= money;
		} else {
			System.out.println("余额不足!!");
		}
	}
	this.printAccount(a);

}

/*
 * 按no 和 password 查询账户 找到返回 没找到返回null
 * 
 */
public Account queryByNoAndPass() {
	System.out.println("账号:");
	String no = sc.next();
	System.out.println("密码: ");
	String password = sc.next();
	for (Account a : accounts) {
		if (a != null && a.no.equals(no) && a.password.equals(BankUtil.encrypt(password))) {
			// 找到
			if (a.state == true) {
				return a;
			} else {
				System.out.println("账户已被冻结!!");
				return null;
			}
		}

	}
	// 没找到
	System.out.println("用户密码输入错误!!");
	return null;
}

public Account queryByNo() {
	System.out.println("账号:");
	String no = sc.next();
	for (Account a : accounts) {
		if (a != null && a.no.equals(no)) {
			// 找到
			if (a.state == true) {
				return a;
			} else {
				System.out.println("账户已被冻结!!");
				return null;
			}
		}

	}
	// 没找到
	System.out.println("用户密码输入错误!!");
	return null;
}
/*
 * 6. 转账 - 输入账号+密码 -查询: 账号+密码(找到、没找到) - 输入对方账号 - 查询:对方账号 -输入:金额(判断) - 修改金额
 * 
 */

public void transfer() {
	Account fromAccount = queryByNoAndPass();
	if (fromAccount == null)
		return;
	Account toAccount = queryByNo();
	if (toAccount == null)
		return;
	System.out.println("转账钱数: ");
	int money = sc.nextInt();
	if (fromAccount.money > money) {
		fromAccount.money -= money;
		toAccount.money = money;
		System.out.println("转账成功!!");
	} else {
		System.out.println("余额不足!!");
	}
}

/*
 * 7.冻结 -输入账号 -修改状态
 */
public void freeze() {
	System.out.println("账号: ");
	String no = sc.next();
	for (Account a : accounts)// 遍历数组
	{
		if (a != null && a.no.equals(no)) {
			this.printAccount(a);// 类的方法之间可以相互调用
		}
		a.state = false;
		this.printAccount(a);
	}

}

/*
 * 8.解冻 -输入账户 -修改状态
 */
public void unfreeze() {
	System.out.println("账号: ");
	String no = sc.next();
	for (Account a : accounts)// 遍历数组
	{
		if (a != null && a.no.equals(no)) {
			this.printAccount(a);// 类的方法之间可以相互调用
		}
		a.state = true;
		this.printAccount(a);
	}

}

/*
 * 9.注销
 */
public void cancel() {
	System.out.println("输入账户:");
	String no = sc.next();
	for (Account a : accounts)// 遍历数组
	{
		if (a != null && a.no.equals(no)) {
			this.printAccount(a);// 类的方法之间可以相互调用
		}
		a.no = null;
		a.id = null;
		a.money = 0;
		a.password = null;
		a.state = false;
		this.printAccount(a);
	}

}

/*
 * 10.遍历大客户 - 输入身份证 - 查询
 */
public void customer() {
	System.out.println("输入身份证:");
	String id = sc.next();
	for (int x = 0; x < accounts.length; x++) {
		int i = 0;
		if (accounts[x] != null && accounts[x].id.equals(id)) {
			if (accounts[x].money > 5000000) {
				System.out.println(accounts[x].no + "是大客户");
			} else {
				for (x = 0; x < accounts.length; x++) {
					i += accounts[x].money;
				}
				System.out.println(accounts[x].no + "是大客户");
			}
		}

		// if(accounts[x]!=null&&accounts[x].id.equals(id)&&accounts[])

	}
}

// 开始

public void start() {
	while (true)

	{
		printMenu();
		int op = sc.nextInt();
		switch (op) {
		case 1:
			open();// 开户
			break;
		case 2:

			searchByNo();// 账户查询
			break;
		case 3:
			searchById();// 身份证查询
			break;
		case 4:
			SaveMoney();// 存钱
			break;
		case 5:
			TakeMoney();// 取钱
			break;
		case 6:
			transfer();// 转账
			break;
		case 7:
			freeze();
			break;
		case 8:
			unfreeze();
			break;
		case 9:
			cancel();
			break;
		case 10:
			customer();
			break;

		default:
			System.out.println("输入信息无效!!");

		}
	}
}

public void printMenu() {

	System.out.println("1.开户:\n" + "2.账户查询:\n" + "3.身份证查询: \n" + "4.存钱 \n" + "5.取钱 \n" + "6.转账 \n"

			+ "7.冻结 \n" + "8.解冻 \n" + "9.注销 \n " + "10.大客户查询 \n");
}

}

package com.bank;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.bank.util.BankUtil;

public class TestBankManager {
BankManager manager = new BankManager();
@After
public void printAccounts()
{
for(Account a:manager.accounts)
{
if(a!=null)
{

			manager.printAccount(a);
			
		}
	}
}
@Before
public void initAccounts() {
	manager.accounts[3] = new Account("test00005" ,"123" , "456 " , BankUtil.encrypt("777"),true);
	manager.accounts[5] = new Account("test00003" ,"123" , "456 " , BankUtil.encrypt("777"),true);
 	manager.accounts[7] = new Account("test00008" ,"123" , "456 " , BankUtil.encrypt("777"),true);
    manager.accounts[9] = new Account("test00009" ,"123" , "456 " , BankUtil.encrypt("777"),true);
}



@Test
public void testOpen()
{
	manager.open();
}
@Test
public void testSearchByNo()
{
	manager.searchByNo();
}

}

package com.bank.main;
//主函数
import com.bank.BankManager;

public class bank {

public static void main(String[] args) {
	BankManager manager = new BankManager();
	manager.start();

}

}

package com.bank.util;
//生成账户 加密 解密
public class BankUtil {//工具类中的方法都是static
public static int count = 1;
public static String getBankNO()
{//生成账户
return String.format(“ny%05d”, count++);//前面补0
}

// public static void main(String[] args) {
// for (int x =0;x<100;x++)
// {
// System.out.println(BankUtil.getBankNO());
// }
// }

// 2.
public static String encrypt(String password)
{//加密 解密
	char cs[] = password.toCharArray();//字符串变成数组
	for(int x=0;x

}

package com.bank.util;

import org.junit.Test;

public class TestBankUtil {
@Test
public void testGetBankNo()
{
for(int x = 0;x<200;x++)
{
System.out.println(BankUtil.getBankNO());
}
}
@Test
public void testEncrypt()
{
System.out.println(BankUtil.encrypt(“abc123”));
System.out.println(BankUtil.encrypt(BankUtil.encrypt(“abc123”)));
}

}

引用文本

你可能感兴趣的:(练习,java,银行管理系统)