该迷你MyBank,存贮是用对象数组来存贮的,所以比较简单,容易理解,适合新手。。

一.创建chengyuan类,在其中声明所需的成员变量:

 public class chengyuan {

     //该类为定义成员变量
public String account;
public String name ;
public String password;
public String idCard;
public int money; }

二.创建fulei类,就是调用

 public class zhulei {
public static void main(String[] args) {
fulei fu=new fulei();
fu.ShowMenu(); }
}

三.创建zhulei类,在其中创建函数来实现具体的功能

 import java.awt.image.SampleModel;
import java.util.Scanner; public class fulei {
Scanner input=new Scanner(System.in);
// public void Init(){
// chen.account[0]="001";
// chen.name[0]="孙悟空";
// chen.password[0]= chengyuan users []=new chengyuan[10];//此处是声明对象数组 public void Init(){    //此类是先对数组中插入一些值
chengyuan u1=new chengyuan();
u1.account = "001";
u1.name = "sunwukong";
u1.password = "wukong";
u1.idCard = "123412341234123412";
u1.money = 10000;
users[0] = u1;
chengyuan u2 = new chengyuan();
u2.account = "002";
u2.name = "zhubajie";
u2.password = "bajie";
u2.idCard = "123412341234123413";
u2.money = 20000;
users[1] = u2;
chengyuan u3 = new chengyuan();
u3.account = "003";
u3.name = "shawujing";
u3.password = "wujing";
u3.idCard = "123412341234123414";
u3.money = 30000;
users[2] = u3;
} public void ShowMenu() {
// TODO Auto-generated method stub
Init(); //初始化该对象数组
while(true){
System.out.println("======================================");
System.out.println("===============MyBank=================");
System.out.println("=功能菜单如下:========================");
System.out.println("==1:开户 2:存款 3:取款 4:转账 5:查询余额==");
System.out.println("====6:修改密码 7:显示所有信息 0:退出=====");
System.out.println("================请选择================"); if(input.hasNextInt()==true){ //判断系统输入是否为int类型
int select=input.nextInt();
switch (select) {
case 1:
CreatUsers();
break;
case 2:
SaveMoney();
break;
case 3:
OutMoney();
break;
case 4:
MoveMoney();
break;
case 5:
FindMoney();
break;
case 6:
UpdatePwd();
break;
case 7:
ShowInfo();
break;
case 0:
System.out.println("系统退出,谢谢使用");
return;
default:
System.out.println("只能输入0-7的数字!!!");
break;
}
}else {
System.out.println("输入内容中含非法字符,请重新尝试!");
input.next(); //释放资源
} System.out.println(); //换行
System.out.println();
}
//-----------------
} /**
* 修改密码
*/
private void UpdatePwd() {
// TODO Auto-generated method stub
System.out.println("请输入账号:");
String zhang=input.next();
System.out.println("请输入密码:");
String pwd=input.next();
chengyuan u=GetUser(zhang);
if (u==null) {
System.out.println("库中无该账户");
return;
}
if (!u.password.equals(pwd)) {
System.out.println("密码错误,请重新尝试");
return;
}
System.out.println("登录成功,可以修改密码!");
System.out.println();
for (int i = 1; i <= 3; i++) { //三次循环
System.out.println("请输入新密码:");
String pwd_1=input.next();
System.out.println("请再次输入密码:");
String pwd_2=input.next();
if(PwdSame(pwd_1, pwd_2)){
u.password=pwd_1;
System.out.println("账号:"+zhang+";密码修改成功,新密码为"+u.password);
return;
}else {
if(i==2)
System.out.println("两次密码输入不正确,请重新输入!!你还要两次输入机会");
if(i==1)
System.out.println("两次密码输入不正确,请重新输入!!你还要一次输入机会");
}
}
} /**
* 查询余额
* 验证是否登录
*/
private void FindMoney() {
// TODO Auto-generated method stub
System.out.println("请输入账号:");
String zhang=input.next();
System.out.println("请输入密码:");
String pwd=input.next();
chengyuan u=GetUser(zhang);
if (u==null) {
System.out.println("库中无该账户");
return;
}
if (!u.password.equals(pwd)) {
System.out.println("密码错误,请重新尝试");
return;
}
System.out.println("具体信息如下");
System.out.println("账号:"+u.account+"\t用户名:"+u.name+"\t密码:"+u.password+"\t身份证号是:"+u.idCard+"\t账户余额:"+u.money);
} /**
* 转账;;;
* 判断两次账号是否存在!!!
*/ private void MoveMoney() {
// TODO Auto-generated method stub
System.out.println("请输入账号:");
String zhang=input.next();
System.out.println("请输入密码:");
String pwd=input.next();
chengyuan u=GetUser(zhang);
if (u==null) {
System.out.println("库中无该账户");
return;
}
if (!u.password.equals(pwd)) {
System.out.println("密码错误,请重新尝试");
return;
}
System.out.println("账户登录成功!");
System.out.println(); //换行
System.out.println("请输入要转入的账号");
String zhang_move=input.next();
chengyuan u_move=GetUser(zhang_move);
if (u_move==null) {
System.out.println("该账户不存在!!请确认后重新尝试!!!");
}
System.out.print("请输入转账的金额:");
int money=input.nextInt();
if (u.money<money) {
System.out.println("该账户余额不足!无法进行转账!!");
return;
}
u.money-=money;
u_move.money+=money;
System.out.println(zhang+"转账成功!!当前余额为"+u.money);
} /**
* 取款
* 接收输入值
* 注意点:字符串的比较
*/
private void OutMoney() {
// TODO Auto-generated method stub
System.out.println("请输入账号:");
String zhang=input.next();
System.out.println("请输入密码:");
String pwd=input.next();
chengyuan u=GetUser(zhang);
if (u==null) {
System.out.println("库中无该账户");
return;
}
if (!u.password.equals(pwd)) {
System.out.println("密码错误,请重新尝试");
return;
}
System.out.print("请输入取款金额:");
int money=input.nextInt();
if (u.money<money) {
System.out.println("账户的余额不足!!!");
return;
}
u.money-=money;
System.out.println(zhang+"账号取款成功,当前余额为:"+u.money); } /**
* 存款
* 验证
* 对值进行修改
*/
private void SaveMoney() {
//chengyuan u=new chengyuan();
// TODO Auto-generated method stub
System.out.println("请输入账号:");
String zhang=input.next();
System.out.println("请输入密码:");
String pwd=input.next();
chengyuan u=GetUser(zhang);
if (u==null) {
System.out.println("库中无该账户");
return;
}
if (!u.password.equals(pwd)) {
System.out.println("密码错误,请重新尝试");
return;
}
System.out.print("请输入存款金额:");
int money=input.nextInt();
u.money+=money;
System.out.println(zhang+"账号存款成功,当前余额为:"+u.money); } //写一个返回用户对象的函数
private chengyuan GetUser(String zhang) {
// TODO Auto-generated method stub
chengyuan u=new chengyuan();
// for (int i = 0; i < users.length; i++) {
// if (users[i].account.equals(zhang)) {
// u=users[i];
//
// }else if(i==users.length-1){
//
// break;
// }
//
// }
u=null;
for (chengyuan sui : users) {
if (sui !=null) {
if (sui.account.equals(zhang)) {
u=sui;
break;
}
}
}
return u;
} /**
* 显示所有信息
* 循环
*/
private void ShowInfo() {
// TODO Auto-generated method stub
for (int i = 0; i < this.users.length; i++) {
if(users[i]!=null){
System.out.println("账号:"+users[i].account+"\t用户名:"+users[i].name+"\t密码:"+users[i].password+"\t身份证号是:"+users[i].idCard+"\t账户余额:"+users[i].money);
}else {
break;
}
}
}
//---------------- /**
* 增加用户
* 接收输入语句
* 先在对象数组中找到空
* 在空中赋值
*/
private void CreatUsers() {
// TODO Auto-generated method stub
chengyuan u=new chengyuan();
System.out.print("请输入姓名:");
String name1=input.next();
System.out.print("请输入密码:");
String pwd_1=input.next();
System.out.print("请再次输入密码:");
String pwd_2=input.next();
System.out.print("请输入身份证号:");
String cardno=input.next();
System.out.print("请输入开户金额:");
int qian=input.nextInt();
String acc="1010"+(int)(Math.random()*10000);
if(PwdSame(pwd_1,pwd_2)){
System.out.println("\n开户成功,开户信息如下:");
System.out.println("账号:"+acc+"\t用户名:"+name1+"\t密码:"+pwd_1+"\t身份证号是:"+cardno+"\t账户余额:"+qian);
for (int i = 0; i < users.length; i++) { //找到空位,赋值
if(users[i]==null){
u.account=acc;
u.name=name1;
u.idCard=cardno;
u.money=qian;
u.password=pwd_1;
users[i]=u;
return;
}
}
}else {
System.out.println("两次密码输入不一致,请重新尝试!");
return;
}
} /**
* 判断两次密码是否一致的类,返回布尔型
*/
public boolean PwdSame(String pwd_1,String pwd_2 ){
return pwd_1.equals(pwd_2);
}
}

仅供参考,无需同意,随意转载。

05-11 13:30