package com.neu.service.impl;import com.neu.dao.impl.UserDao;import com.neu.pojo.User;import com.neu.service.IUserService;import java.util.Random;publicclassUserServiceimplementsIUserService{private UserDao dataCtrl =newUserDao();@Overridepublicbooleanlogin(User user){
User res = dataCtrl.findByUsername(user.getName());if(res != null){if(res.getPw().equals(user.getPw())){returntrue;}else{returnfalse;}}returnfalse;}@Overridepublicbooleanregister(User user){if(dataCtrl.findByUsername(user.getName())== null){
dataCtrl.add(user);returntrue;}returnfalse;}@Overridepublic Object[]lottery(User loginUser){
String[] luck =newString[5];boolean flag =false;
Random random =newRandom();for(int i =0; i<luck.length;i++){
luck[i]= random.nextInt(9000)+1000+"";if(luck[i].equals(loginUser.getId())){
flag =true;}}
Object[] res =newObject[]{luck,flag};return res;}}
代码4:SystemUI
package com.neu.view;import com.neu.pojo.User;import com.neu.service.impl.UserService;import java.util.Scanner;publicclassSystemUI{private UserService serv =newUserService();privateboolean flagLogin;private User userNow;publicstaticvoidmain(String[] args){
SystemUI ui =newSystemUI();while(true){
ui.showMenu();
ui.choose();
ui.end();}}private String input(String tip){
Scanner input =newScanner(System.in);
System.out.print(tip+": ");return input.next();}publicvoidregister(){
String name =input("请填写个人注册信息:\n用户名");
String pw =input("密码");
String id =(int)(Math.random()*9000)+1000+"";
User user =newUser(name, pw, id);if(serv.register(user)){
System.out.println("用户名\t密码\t会员卡号");
System.out.println(name+"\t"+pw+"\t"+id);}else{
System.out.println("用户名重复, 注册失败!");}}publicvoidlogin(){
String name =input("请输入用户名");
String pw =input("请输入密码");
User user =newUser(name, pw);
flagLogin = serv.login(user);if(flagLogin){
userNow = user;
System.out.println("欢迎您:"+name);}else{
System.out.println("登录失败!");}}publicvoidlottery(){if(userNow == null){
System.out.println("请先登录!!!");return;}
String id =input("请输入你的卡号");
userNow.setId(id);
Object[] res = serv.lottery(userNow);
String[] luck =(String[]) res[0];
System.out.println("本日的幸运数字为:"+luck[0]+" "+luck[1]+" "+luck[2]+" "+luck[3]+" "+luck[4]);if((boolean)res[1]){
System.out.println("恭喜!您是本日的幸运会员!");}else{
System.out.println("抱歉!您不是本日的幸运会员!");}}publicvoidlogout(){
flagLogin =false;
userNow = null;}publicvoidshowMenu(){
System.out.println("*******欢迎进入奖客富翁系统*******");
System.out.println(" 1. 注册");
System.out.println(" 2. 登陆");
System.out.println(" 3. 抽奖");}publicvoidend(){
String res =input("继续吗?(y/n)");if(res.equals("n")){
System.out.println("\n退出成功!请重新登录!\n");logout();}elseif(res.equals("y")){}else{
System.out.println("无效输入");end();}}publicvoidchoose(){
String choice =input("请输入你的菜单");switch(choice){case"1":register();break;case"2":login();break;case"3":lottery();break;default:
System.out.println("输入无效!");}}}