类的封装

实现代码:

package li;

import java.util.Scanner;

public class main {
public static void main(String[] args) {
bank bank1 = new bank();
int i = 3;
Scanner input = new Scanner(System.in);
while (true) {
System.out.println("1:添加用户");
System.out.println("2:查询用户");
System.out.println("3:退出程序");
i = input.nextInt();
switch (i) {
case 1:
String name;
long password;
long number;
System.out.println("请输入你的姓名");
name = input.next();
System.out.println("请输入你的密码");
password = input.nextLong();
System.out.println("请输入你的身份证号");
number = input.nextLong();
count e = new count(name, password, number);
if (e != null) {
if (bank1.fall(e)) {
System.out.println("用户注册成功");
bank.add();
} else {
System.out.println("用户注册失败");
}
}
break;
case 2:
String name1;
long password1;
System.out.println("请输入你的姓名");
name1 = input.next();
System.out.println("请输入你的密码");
password1 = input.nextLong();
bank.check(name1, password1);
break;
case 3:
System.exit(0);
}
}
}
}

package li;

public class count {
String name;
long number;
long password;
double money;
public count (String name,long password,long number){
this.name=name;
this.number=number;
this.password=password;
}
@Override
public String toString() {
return "name:"+name+" number"+number+" money"+money;
}
}

package li;

public class bank {
protected static count[] storerroom=new count[10];
public static int i=0;
public boolean fall(count e){
if(e!=null) {
storerroom[i] = e;return true;
}
else {
return false;
}
}
public static void check(String name,long password){
int j=0;
for (j=0;j<i;j++){
if(storerroom[j].name.equals(name)&&storerroom[j].password==password) {
System.out.println(storerroom[j].toString());
break;
}
}
if(j==i){
System.out.println("用户不存在");
}
}
public static void add(){
i++;
}
}

实验结果:

实验心得:

1.本次实验中,由于输出时数据类型的不同,我都用了int型得输出导致出现了错误。

2.通过本次实验,我加强了对类的封装的理解。

2.此次实验对我来说比较有难度,是通过同学的帮助才完成实验的,以后还是要多加练习。

 

posted @ 2019-04-14 20:56  搁浅12345  阅读(151)  评论(0编辑  收藏  举报