Jva实现银行系统的简写

来源:互联网 发布:mac怎么关闭键盘灯 编辑:程序博客网 时间:2024/06/05 11:51

练习1:(面向对象基础语法)

写一个账户类(Account),

属性: id:账户号码 长整数

password:账户密码

name:真实姓名

personId:身份证号码 字符串类型

email:客户的电子邮箱

balance:账户余额

 

方法: deposit:存款方法,参数是double型的金额

withdraw:取款方法,参数是double型的金额

 

练习2:(封装)

Account类作成完全封装,注意:要辨别每个属性的set/get方法是否需要公开

 

为这两种用户编写相关的类

同时要求编写Bank,属性:

1.当前所有的账户对象的集合,存放在数组中

2.当前账户数量

方法:

1.用户开户,需要的参数:id,密码,密码确认,姓名,身份证号码,邮箱,账户类型(int),返回新创建的Account对象

2.用户登录,参数:id,密码 返回Account对象,提示 用s1.equals(s2)判断s1s2两个字符串内容是否相等

3.用户存款,参数:id,存款数额,返回修改过的Account对象

4.用户取款,参数:id,取款数额,返回修改过的Account对象

5.设置透支额度 参数:id,新的额度  ,返回修改过的Account对象.这个方法需要验证账户是否是信用账户

用户会通过调用Bank对象以上的方法来操作自己的账户,请分析各个方法需要的参数

另外,请为Bank类添加几个统计方法

1.统计银行所有账户余额总数

2.统计所有信用账户透支额度总数

写个主方法测试你写的类

 


package Encapsulation;



import java.util.Scanner;


public class Account {
private long accoundId;//账户号码
private String passsWord;//密码
private String name;//真实姓名
private String personId;//身份证号码
private String email;//邮箱
private double balance;//账户余额
private char isCredit;//记录是否是信用账户
private double overDraft;//是信用卡用户,透支额度
private double overDraftMoney;//记录已经透支多少?
//无参构造法函数方便使用
public Account(){}
public Account(long accoundId,String passsWord,String name,String personId,String email,double balance){
//构造方法,用于初始化
this.accoundId = accoundId;
this.passsWord = passsWord;
this.name = name;
this.personId = personId;
this.email = email;
this.balance = balance;
}
//显示账户信息
public void show(){
System.out.println(accoundId+"\t"+name+"\t"+personId+"\t"+email+"\t"+balance);
}
//存钱方法
public void deposit(){
do {
System.out.println("请输入存款额度:");
Scanner input=new Scanner(System.in);
double depositAmount=input.nextDouble();
if(depositAmount <= 0){
System.out.println("存款失败");
break;
}else{
this.balance = this.balance + depositAmount;
System.out.println("存款成功");
System.out.println("账户余额:"+this.getBalance());
System.out.println("是否继续?y/n");
char isGo = input.next().charAt(0);
if(isGo == 'y'){
continue;
}else{
System.out.println("已安全退出");
break;
}
}
} while (true);
}
//取款方法
public double withdraw(){
do {
Scanner input = new Scanner(System.in);
System.out.println("请输入取款金额");
double withdrawAmount = input.nextDouble();
//判断取款金额是正确的
//金额必须是大于0正数,并且小于账户余额
if(withdrawAmount > 0 && withdrawAmount <= this.balance){
this.balance = this.balance - withdrawAmount;
System.out.println("取款成功,账户余额:"+this.getBalance());
System.out.println("是否继续?y/n");
char isGo = input.next().charAt(0);
if(isGo == 'y'){
continue;
}else{
System.out.println("已安全退出");
return this.balance;
}
}else{
System.out.println("取款失败");
return this.balance;
}
} while (true);
}
public long getAccoundId() {
return accoundId;
}
public void setAccoundId(long accoundId) {
this.accoundId = accoundId;
}
//用户密码是谁都看不见的,不可以通过方法显示
public String getPasssWord() {
return passsWord;
}
//返回boolean以判断客户是否输入正确的长度
public boolean setPasssWord(String passsWord) {
if(passsWord.length() != 6){
System.out.println("密码长度不够,请输入6位数的密码:");
return false;
}else{
this.passsWord = passsWord;
return true;
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPersonId() {
return personId;
}
public void setPersonId(String personId) {
this.personId = personId;
}
public String getEmail() {
return email;
}
//email可能是错误的,在设置的时候就判断
public boolean setEmail(String email) {
int a = email.indexOf('@');
int d = email.indexOf('.');
if(a>0 && d>(a+1) && (email.length()-d) >= 2){
this.email = email;
return true;
}
else{
return false;
}
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}

}



//主类(无main函数)

package Encapsulation;


import java.io.PrintStream;
import java.util.Scanner;
import ClassTest.SuperMarketManage;


public class ABCBank {
Account account[]=new Account[100];
Scanner input=new Scanner(System.in);
Account account2;
PrintStream outPut;
int accountNum;//记录已开户账户的数量
double moneyAll;//记录所有账户的存款总额
double outMoney;//记录信用卡透支总额
long newCardNum;
int loginTimes = 1;

//中国农业银行主界面
public void mainView(){
int choice=0;
initData();

outPrint("欢迎进入ABCBank管理系统\n1.登录\n2.开户\n3.退出");
outPrint("请选择操作序列号:");
if(checkInputType()){
choice = input.nextInt();
if(choice == 1){
//登录成功后,把登录账户记录到account2里
account2 = login();
outPrint("请选择操作序列号:\n1.存款\n2.取款");
if(checkInputType()){
int choice1 = input.nextInt();
if(choice1 == 1){
//调用存款方法
account2.deposit();
}else if(choice1 == 2){
//调用取款方法
account2.withdraw();
}else{
outPrint("操作失误");
}
}
}else if (choice == 2) {
openAccount();
}else {
outPrint("已安全退出");
System.exit(0);
}
}
}
//初始化
public void initData(){
newCardNum=1111;
outPut=System.out;
account[0]=new Account(1111, "123456", "张海", "100125199503201548", "zhannghai@126.com", 200.00);
account[1]=new Account(1112, "123456", "黄道", "100125199503201548", "huangdao@126.com", 1000000.00);
account[2]=new Account(1113, "123456", "柳城", "100125199503201548", "liucheng@126.com", 20000.00);
accountNum=3;
}

//开户
public Account openAccount(){
do {
String pwd1;
account[accountNum]=new Account();
outPrint("请输入您的真实姓名:");
account[accountNum].setName(input.next());
outPrint("请输入您的身份证号码:");
account[accountNum].setPersonId(input.next());
//邮箱可能输入错误,判断,正确则赋值并返回true,错误,不赋值,返回false。
do {
outPrint("请输入您的邮箱:");
if(account[accountNum].setEmail(input.next())){
break;
}else{
outPrint("邮箱输入错误");
continue;
}
} while (true);
//密码可能长度不对,在封装里面做了判断,长度正确则赋值并返回true,长度错误,不赋值,返回false。
do {
outPrint("请输入您的密码");
pwd1=input.next();
if(account[accountNum].setPasssWord(pwd1)){
break;
}else{
continue;
}
} while (true);
outPrint("请再次输入您的密码");
String pwd2=input.next();
if(pwd2.equals(pwd1)){
long accoundId=newCardNum+accountNum;
outPrint("注册成功\n您的账号是:"+(accoundId));
account[accountNum].setAccoundId(accoundId);
return account[accountNum];
}else{
outPrint("两次输入密码不一致,请重新输入:");
continue;
}
} while (true);
}
//登录(成功后可以调用存款,取款方法,账户余额高于100000,则可以申请信用卡账户,信用卡可以透支10000)
public Account login(){
do {
System.out.println("请依次输入您的银行卡号和密码:(失败3次该卡会被锁定)");
long accountID = input.nextLong();
String pwd = input.next();
//调用核对登录用户名和密码方法
boolean isLoginSuccess = check_login(accountID, pwd);
if(isLoginSuccess){
System.out.println("登录成功");
for(int i = 0;i < accountNum;i++){
if(account[i].getAccoundId() == accountID ){
return account[i];
}
}
break;
}
else{
System.out.println("登录失败");
loginTimes++;
continue;
}
} while (loginTimes < 4);
return null;
}
//login核对用户登录账号密码
public boolean check_login(long accountID,String pwd){
for(int i = 0;i < accountNum;i++){
if(account[i].getAccoundId() == accountID){
if(account[i].getPasssWord().equals(pwd)){
return true;
}
}else{
continue;
}
}
return false;
}
//统计用户数据
public void countAllaccountIfo(){
double allMoney = 0.00;
for(int i=0;i<accountNum;i++){
allMoney+=account[i].getBalance();

}
outPrint("已注册账户总数量:"+accountNum);
outPrint("注册账户存储总金额:"+allMoney);
outPrint("信用卡用户透支总额:");
}
//输出方法(泛型)
public <T> void outPrint(T t){
outPut.println(t);
}
//判断输入是否为数字方法2
public boolean checkInputType(){
//可以用if(!input.hasNextInt){}来代替下面的语句
if (!input.hasNextInt()){
//hasNextInt是判断接下来输入的是否是int型
//nextline是让输入进入下一行(用来防止死循环——无限次判定,并且得到同样的结果)
input.nextLine();
return false;
}
else 
return true;
}
}

0 0