今日练习代码如下:
1.包com.yan.app
package com.yan.app;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import com.yan.dao.IUserDao;
import com.yan.entity.User;
import com.yan.util.DaoFactory;
public class UserApplication {
private static User user = null;
public static void main(String[] args) throws Exception {
IUserDao userDao = DaoFactory.getUserDao();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) {
menu1();
String ss = br.readLine();
switch (ss) {
case "1":
User user = inputUser(br);
userDao.save(user);
break;
case "2":
User user1 = loginUser(br);
User bb = userDao.login(user1);
if (bb != null) {
UserApplication.user = bb;
System.out.println("登录成功!");
} else {
System.out.println("登录失败!请重新登录");
}
break;
case "3":
System.out.println("======目前已注册的用户列表======");
Object[] arr = userDao.getAll().getArray();
for (Object temp : arr) {
if (temp != null)
System.out.println(temp);
}
System.out.println("==============================");
break;
case "5":
UserApplication.user=null;
System.out.println("退出登录");
break;
case "4":
User user2 = mofidyUser(br);
userDao.update(user2);
UserApplication.user=null;
System.out.println("修改完毕!请重新登录");
break;
case "6":
System.out.println("输入要删除的用户编号:");
Long id=null;
while(true) {
String ss1=br.readLine();
try {
id=Long.parseLong(ss1);
break;
} catch (Exception e) {
System.out.println("输入的数据格式错误!请重新输入");
id=null;
}
}
if(id.equals(UserApplication.user.getId()))
UserApplication.user=null;
userDao.delete(id);
break;
// default:
// System.exit(0);
// break;
}
if ("0".equals(ss)) {
System.out.println("bye~~bye");
break;
}
}
br.close();
}
private static User mofidyUser(BufferedReader br)throws Exception {
User res=UserApplication.user;
System.out.println("请输入新的用户名称和口令:");
System.out.println("原始名称为:"+res.getUsername());
System.out.println("请输入新用户名称:(不修改则直接回车)");
String username=br.readLine();
if(username!=null && username.trim().length()>=6 && username.trim().length()&l