东北大学实训小项目1 幸运抽奖(附代码)

收获:

  1. 要传回多个不同种类参数时可以使用object数组。
  2. 用户界面设计时的输入可以构造函数input(类似于python的input),使代码更加优雅。
  3. 学会用接口来写项目,接口命名用I开头, 即interface首字母。接口放在包的外面一层。
  4. 一个项目可以分成三个部分:数据(UserDao),功能(service),界面(view、UI界面)

代码1:User

package com.neu.pojo;

public class User {
    private String name;
    private String pw;
    private String id;

    public User(String name, String pw, String id) {
        this.name = name;
        this.pw = pw;
        this.id = id;
    }

    public User(String name, String pw) {
        this.name = name;
        this.pw = pw;
    }

    public String getName() {
        return name;
    }

    public String getPw() {
        return pw;
    }

    public String getId() {
        return id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPw(String pw) {
        this.pw = pw;
    }

    public void setId(String id) {
        this.id = id;
    }
}

代码2:UserDao

package com.neu.dao.impl;

import com.neu.dao.IUserDao;
import com.neu.pojo.User;

import java.util.Arrays;

public class UserDao implements IUserDao {
    private User[] users = new User[0];

    @Override
    public void add(User user) {
        users = Arrays.copyOf(users,users.length+1);
        users[users.length-1] = user;
    }

    @Override
    public User findByUsername(String username) {
        for(User el:users){
            if(el.getName().equals(username)){
                return el;
            }
        }
        return null;
    }
}

代码3:UserService

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;

public class UserService implements IUserService {
    private UserDao dataCtrl = new UserDao();
    @Override
    public boolean login(User user) {
        User res = dataCtrl.findByUsername(user.getName());
        if(res != null){
            if(res.getPw().equals(user.getPw())){
                return true;
            }else{
                return false;
            }
        }
        return false;
    }

    @Override
    public boolean register(User user) {
        if(dataCtrl.findByUsername(user.getName()) == null){
            dataCtrl.add(user);
            return true;
        }
        return false;
    }

    @Override
    public Object[] lottery(User loginUser) {
        String[] luck = new String[5];
        boolean flag = false;
        Random random = new Random();
        for(int i = 0; i<luck.length;i++){
            luck[i] = random.nextInt(9000)+1000+"";
            if(luck[i].equals(loginUser.getId())){
                flag = true;
            }
        }
        Object[] res = new Object[]{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;

public class SystemUI {
    private UserService serv = new UserService();
    private boolean flagLogin;
    private User userNow;

    public static void main(String[] args) {
        SystemUI ui = new SystemUI();
        while(true){
            ui.showMenu();
            ui.choose();
            ui.end();
        }
    }

    private String input(String tip){
        Scanner input =  new Scanner(System.in);
        System.out.print(tip+": ");
        return input.next();
    }

    public void register(){
        String name = input("请填写个人注册信息:\n用户名");
        String pw = input("密码");
        String id = (int)(Math.random()*9000)+1000+"";
        User user = new User(name, pw, id);
        if(serv.register(user)){
            System.out.println("用户名\t密码\t会员卡号");
            System.out.println(name+"\t"+pw+"\t"+id);
        }else {
            System.out.println("用户名重复, 注册失败!");
        }
    }

    public void login(){
        String name = input("请输入用户名");
        String pw = input("请输入密码");
        User user = new User(name, pw);
        flagLogin = serv.login(user);
        if(flagLogin){
            userNow = user;
            System.out.println("欢迎您:"+name);
        }else{
            System.out.println("登录失败!");
        }
    }

    public void lottery(){
        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("抱歉!您不是本日的幸运会员!");
        }
    }

    public void logout(){
        flagLogin = false;
        userNow = null;
    }

    public void showMenu(){
        System.out.println("*******欢迎进入奖客富翁系统*******");
        System.out.println("       1. 注册");
        System.out.println("       2. 登陆");
        System.out.println("       3. 抽奖");
    }

    public void end(){
        String res = input("继续吗?(y/n)");
        if(res.equals("n")){
            System.out.println("\n退出成功!请重新登录!\n");
            logout();
        }else if(res.equals("y")){
        }else{
            System.out.println("无效输入");
            end();
        }
    }

    public void choose(){
        String choice = input("请输入你的菜单");
        switch (choice){
            case "1":
                register();
                break;
            case "2":
                login();
                break;
            case "3":
                lottery();
                break;
            default:
                System.out.println("输入无效!");
        }
    }
}






评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值