在UserService中用户信息修改方法modify,一开始一切都还好,直到遍历集合,出现报红和提示
Non-static 'getUid()' cannot be referenced from a static context
翻译过来是:
Non-static方法'getUid()' cannot be referenced from a static context 不能从静态上下文引用非静态方法‘getUid()’
静态方法的使用:
在一个类中调用另一个static类中方法或变量时要使用类名.方法名或者类名.属性名;如果调用的是非静态的方法或属性则要用类的对象去调用,即对象方法或对象属性。
破案了:原来是增强for遍历的时候将user对象的u大写了,改过来就好了,可恶啊,一个人找了一上午加三个人找了半小时
异常退出:
EOFException表示在输入过程中意外地到达文件结束或流结束。
这个异常主要由数据输入流用来表示流的结尾。 请注意,许多其他输入操作在流的末尾返回特殊值,而不是抛出异常
标志位的设置和终止条件以及存放位置
在注册时要对输入的uid和已有师生的学号/工号进行比对,只有匹配上了才能继续输入姓名进行比对,只有账号(uid)和姓名都匹配上了才能进行登录注册操作,
图中是以
Scanner sc = new Scanner(System.in);
System.out.println("请输入您的账号(uid):");
String uid = sc.next();
Set<String> uids = pro.stringPropertyNames();
boolean flag = true;
if (pro != null) {
for (String key : uids) { //获取键
if (key.equals(uid)) {
flag= false; //遍历后匹配成功
}
}if (flag) { //遍历后匹配不成功,输出提示
System.out.println("您输入的账号有误,请仔细核对!");
}
}
空指针异常:
出现了堆栈溢出错误
“Exception in thread "main" java.lang.StackOverflowError”
出现原因:
在创建对象时出现循环依赖,在IndexView中创建对象:
JobView jv = new JobView; IndexView iv = new IndexView;
在JobView中创建对象:
private JobService js = new JobService;
在JobService中创建对象:
IndexView iv= new IndexView;
形成了一个完整的闭环,不停地循环,超出了堆栈空间,造成了溢出。
在实例化对象的时候,调用构造方法,然后构造方法里又要实例化一个对象,这个对象还是会调用构造方法。。。然后一直死循环下去,直到栈溢出。
解决方法:
创建了一个对象池模式(Object Pool Pattern),是创建型设计模式的一种,将对象预先创建并初始化后放入对象池中,对象提供者就能利用已有的对象来处理请求,减少频繁创建对象所占用的内存空间和初始化时间。
使用时直接从对象池中调用即可,这样就不会出现堆栈溢出的错误啦!
加入一个标志位,控制其结束
public void loginMenu() throws IOException, ClassNotFoundException, ParseException, InterruptedException {
Map<String, Integer> count = new HashMap<String, Integer>();
Scanner sc = new Scanner(System.in);
boolean flag = true;
while (flag){
for (; ; ) {
System.out.println("====================");
System.out.println("==\t 登录界面\t ==");
System.out.println("====================");
System.out.println("请输入uid:");
String uid = sc.next();
if (count.containsKey(uid) && count.get(uid) >= 5) {
System.out.println("您的账户已经被停用,请联系管理员");
continue;
}
System.out.println("请输入密码:");
String password = sc.next();
if (ls.login(uid, password)) {
System.out.println("登录成功!欢迎您!" + uid);
flag = false ; //登录成功,退出死循环
//IndexView.softwareMainMenu();
} else {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(UserService.INF));
ArrayList<User> list = (ArrayList<User>) ois.readObject();
for (User user : list) {
if (user.getUid().equals(uid)) {
if (count.containsKey(uid)) {
count.put(uid, count.get(uid) + 1);
System.out.println("登陆次数还剩" + (5 - count.get(uid)) + "次");
} else {
count.put(uid, 1);
System.out.println("登陆次数还剩" + (5 - count.get(uid)) + "次");
}
if (count.get(uid) == 5) {
user.setAlive(false);
System.out.println("登录次数还剩0次,该账户已停用!");
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(UserService.INF));
oos.writeObject(list);
oos.flush();
oos.close();
oos.close();
}
}
}
if(flag){ //若遍历后匹配不成功,则输出提示
System.out.println("账户未被注册");
}
}
}
}
}