Java习题:共享单车

单车操作层:

package action;

import bean.Bike;
import bean.User;
import com.sun.org.apache.bcel.internal.generic.BREAKPOINT;
import constans.Roles;
import constans.Status;
import dao.AdminService;
import dao.UserService;
import dao.impl.AdminServiceImpl;
import dao.impl.UserServiceImpl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

/**
 * @Author:Liku
 * @Date:2022/11/25-21:29
 * @Name:ShareBicycle
 * @Since:jdk1.8
 * @Description:
 */
public class BikeAction {
    private BikeAction() {
    }

    static List<Bike> bikeList = new ArrayList<>(10);
    static List<User> userList = new ArrayList<>(10);
    static Scanner scanner = new Scanner(System.in);
    static UserService userService = new UserServiceImpl();
    static AdminService adminService = new AdminServiceImpl();

    static {
        for (int i = 0; i < 5; i++) {
            userList.add(new User((i + 1), "nak" + (i + 1), "nakpwd", 33.5 + i, null));
        }
        for (int i = 0; i < 5; i++) {
            bikeList.add(new Bike((i + 1), "ofo" + i, Status.NOTBORROW, null, null, 1.5, i * 2 + 235, null));
        }
        for (int i = 0; i < 5; i++) {
            bikeList.add(new Bike(bikeList.size() + 1, "mobile" + i, Status.HADBORROW, ParseUtil.date("2022-11-12"), null, 1.5, i * 2 + 233, userList.get(1)));
        }

    }

    public static void startMenu() {
        boolean b = true;
        while (b) {
            System.out.println("*****************欢迎使用迷你共享单车管理系统*******************");
            System.out.println("\t\t1、登录");
            System.out.println("\t\t2、注册");
            System.out.println("\t\t0、退出系统");
            System.out.println("请选择:");
            int choose = scanner.nextInt();
            switch (choose) {
                case 1:
                    login();
                    break;
                case 0:
                    System.out.println("[谢谢使用,系统已退出!]");
                    b = false;
                    break;
            }
        }
    }

    private static void login() {
        System.out.println("请输入您的用户名");
        String name = scanner.next();
        System.out.println("请输入您的密码");
        String pwd = scanner.next();
        int index = -1;
//        userList.forEach(user -> {
//            if (user.getName().equals(name) && user.getPwd().equals(pwd)) {
//                System.out.println("登录成功!");
//                if (user.getId() % 2 == 0) {
//                    adminStart(user);
//                } else {
//                    userStart(user);
//                }
//            }
//          lambda不可以改变变量,只能用常量
//        });
        Iterator<User> iterator = userList.iterator();
        while (iterator.hasNext()) {
            User next = iterator.next();
            if (next.getName().equals(name) && next.getPwd().equals(pwd)) {
                  index=1;
                System.out.println("登录成功!");
                if (next.getId() % 2 == 0) {
                    AdminAction.adminStart(next,scanner);
                } else {
                    UserAction.userStart(next, scanner);
                }
            }
            
        }
        if (index == -1) {
            System.out.println("登陆失败!请您先注册");
            register();
        }
    }

    private static void register() {
        System.out.println("请输入注册用户名:");
        String next = scanner.next();
        System.out.println("请输入注册密码:");
        String pwd = scanner.next();
        userList.add(new User(1, next, pwd, 0, null));
        System.out.println("注册成功!");
    }

}

用户操作层:

package action;

import bean.Bike;
import bean.User;
import constans.Roles;
import exception.AmountOfMoneyIllegal;
import exception.NoSuchIdExcetion;

import java.util.Scanner;

/**
 * @Author:Liku
 * @Date:2022/11/25-23:01
 * @Name:ShareBicycle
 * @Since:jdk1.8
 * @Description:
 */
public class UserAction {
    private UserAction() {
    }

    public static void userStart(User user, Scanner scanner) {
        boolean b = true;
        while (b) {
            System.out.println("迷你共享单车管理系统>>>用户菜单列表");
            System.out.println("欢迎您:" + user.getName() + "\t\t角色:" + Roles.USER + "\t\t余额:" + user.getBalance());
            System.out.println("\t\t1、借出单车");
            System.out.println("\t\t2、归还单车");
            System.out.println("\t\t3、修改登录密码");
            System.out.println("\t\t4、余额充值");
            System.out.println("\t\t0、退出登录");
            System.out.println("请选择:");
            try {
                int choice = scanner.nextInt();
                switch (choice) {
                    case 1:
                        BikeAction.userService.borrow(scanner, user, BikeAction.bikeList);
                        break;
                    case 2:
                        BikeAction.userService.back(scanner, user, BikeAction.bikeList);
                        break;
                    case 3:
                        BikeAction.userService.updatePwd(user, scanner);
                        break;
                    case 4:
                        BikeAction.userService.deposit(user, scanner);
                        break;
                    case 0:
                        System.out.println("已退出当前用户,返回主界面");
                        b = false;
                        break;
                }
            } catch (NoSuchIdExcetion | AmountOfMoneyIllegal e) {
                System.out.println(e.getMessage());
            }
        }

    }
}

管理员操作层:

package action;

import bean.Bike;
import bean.User;
import constans.Roles;
import exception.NoSuchIdExcetion;

import java.util.Scanner;

/**
 * @Author:Liku
 * @Date:2022/11/25-23:02
 * @Name:ShareBicycle
 * @Since:jdk1.8
 * @Description:
 */
public class AdminAction {
    private AdminAction() {
    }
    public static void adminStart(User user, Scanner scanner) {
        boolean b = true;
        while (b) {
            System.out.println("迷你共享单车管理系统>>>管理员菜单列表");
            System.out.println("欢迎您:" + user.getName() + "\t\t角色:" + Roles.ADMIN);
            System.out.println("\t\t1、查看单车");
            System.out.println("\t\t2、投放单车");
            System.out.println("\t\t3、删除单车");
            System.out.println("\t\t4、单车排行榜");
            System.out.println("\t\t5、查看全部注册用户");
            System.out.println("\t\t0、退出登录");
            System.out.println("请选择:");
            try {
                int choice = scanner.nextInt();
                switch (choice) {
                    case 1:
                        BikeAction.adminService.showBike(BikeAction.bikeList);
                        break;
                    case 2:
                        BikeAction.bikeList = BikeAction.adminService.add(scanner, BikeAction.bikeList);
                        break;
                    case 3:
                        BikeAction.bikeList = BikeAction.adminService.delete(scanner, BikeAction.bikeList);
                        break;
                    case 4:
                        BikeAction.adminService.range(BikeAction.bikeList);
                        break;
                    case 5:
                        BikeAction.adminService.show(BikeAction.userList);
                        break;
                    case 0:
                        System.out.println("已退出当前用户,返回主界面");
                        b = false;
                        break;
                }
            } catch (NoSuchIdExcetion e) {
                System.out.println(e.getMessage());
            }
        }

    }

}

单车实体类层:

package bean;

import constans.Status;
import lombok.*;

import javax.annotation.Generated;
import java.util.Date;

/**
 * @Author:Liku
 * @Date:2022/11/25-21:22
 * @Name:ShareBicycle
 * @Since:jdk1.8
 * @Description:
 */
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class Bike {
    private Integer id;
    private String name;
    private Integer status;
    private Date LendTime;
    private Date BackTime;
    private double rentMoney;
    private Integer rentTime;

    @Override
    public String toString() {
        return "Bike{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", status=" + (status.equals(Status.NOTBORROW) ? "未借出" : "已借出") +
                ", LendTime=" + (status.equals(Status.NOTBORROW) ? null : ParseUtil.date(LendTime)) +
                ", rentMoney=" + rentMoney +
                ", rentTime=" + rentTime +
                '}';
    }
}

用户实体类:

package bean;

import lombok.*;

/**
 * @Author:Liku
 * @Date:2022/11/25-21:22
 * @Name:ShareBicycle
 * @Since:jdk1.8
 * @Description:
 */
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private Integer id;
    private String name;
    private String pwd;
    private double balance;
    private Bike bike;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", pwd='" + pwd + '\'' +
                ", bike=" + bike +
                '}';
    }
}

常量包:

package constans;

/**
 * @Author:Liku
 * @Date:2022/11/25-22:59
 * @Name:ShareBicycle
 * @Since:jdk1.8
 * @Description:
 */
public class Roles {

    public static final String ADMIN = "管理员";
    public static final String USER = "普通用户";


}
package constans;

/**
 * @Author:Liku
 * @Date:2022/11/26-0:12
 * @Name:ShareBicycle
 * @Since:jdk1.8
 * @Description:
 */
public class Status {
    public static final Integer HADBORROW=0;
    public static final Integer NOTBORROW=1;
}

用户业务层接口:

package dao;

import bean.Bike;
import bean.User;

import java.util.List;
import java.util.Scanner;

/**
 * @Author:Liku
 * @Date:2022/11/25-23:05
 * @Name:ShareBicycle
 * @Since:jdk1.8
 * @Description:
 */
public interface UserService {
    /**
     * 租借单车
     * @param scanner
     * @param user
     * @param list
     */
    void borrow(Scanner scanner, User user, List<Bike> list);

    /**
     * 归还单车
     * @param scanner
     * @param user
     * @param list
     */

    void back(Scanner scanner,User user,List<Bike> list);

    /**
     * 修改密码
     * @param user
     * @param scanner
     */
    void updatePwd(User user,Scanner scanner);

    /**
     * 余额充值
     * @param user
     * @param scanner
     */
    void deposit(User user,Scanner scanner);
}

用户业务接口实现层:

package dao.impl;

import bean.Bike;
import bean.User;
import constans.Status;
import dao.UserService;
import exception.AmountOfMoneyIllegal;
import exception.NoSuchIdExcetion;
import util.CalMoney;

import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
 * @Author:Liku
 * @Date:2022/11/25-23:06
 * @Name:ShareBicycle
 * @Since:jdk1.8
 * @Description:
 */
public class UserServiceImpl implements UserService {
    @Override
    public void borrow(Scanner scanner, User user, List<Bike> list) {
        System.out.println("请输入你要借的单车编号");
        Integer id = scanner.nextInt();
        for (Bike bike : list) {
            if (bike.getId().equals(id)) {
                System.out.println("借车成功");
                bike.setLendTime(new Date());
                bike.setStatus(Status.HADBORROW);
                bike.setUser(user);
                bike.setRentTime(bike.getRentTime() + 1);
                user.setBike(bike);
                return;
            }
        }
        throw new NoSuchIdExcetion("单车无权限借出");
    }

    @Override
    public void back(Scanner scanner, User user, List<Bike> list) {
        System.out.println("该公司下已归还单车:");
        for (Bike bike : list) {
            if (bike.getStatus().equals(Status.NOTBORROW)) {
                System.out.println(bike);
            }
        }
        System.out.println("请输入你要还的单车编号");
        Integer id = scanner.nextInt();
        for (Bike bike : list) {
            if (bike.getId().equals(id) && user.getBike().equals(bike)) {
                bike.setBackTime(new Date());
                int time = CalMoney.calTime(bike.getBackTime(), bike.getLendTime());
                double extra = user.getBalance() - time * bike.getRentMoney();
                if (extra < 0) {
                    System.out.println("余额不足请先充值!");
                    deposit(user, scanner);
                }
                user.setBike(null);
                bike.setLendTime(null);
                bike.setBackTime(null);
                bike.setStatus(Status.NOTBORROW);
                user.setBalance(extra);
                System.out.println("还车成功!欢迎下次骑行");
                return;
            }
        }
        throw new NoSuchIdExcetion("输入单车编号错误!");
    }

    @Override
    public void updatePwd(User user, Scanner scanner) {
        while (true) {
            System.out.println("请输入修改前的密码:");
            String pass = scanner.next();
            if (user.getPwd().equals(pass)) {
                System.out.println("请输入修改后的密码");
                user.setPwd(scanner.next());
                System.out.println("修改密码成功!");
                return;
            }
            System.out.println("输入密码错误");
        }
    }

    @Override
    public void deposit(User user, Scanner scanner) {
        System.out.println("您当前余额为:");
        System.out.println(user.getBalance());
        System.out.println("请输入您想充值的金额");
        double mon = scanner.nextDouble();
        if (mon > 1000 || mon < 10) {
            throw new AmountOfMoneyIllegal("充值金额不合法,【最大金额1000,最小金额10】");
        }
        user.setBalance(user.getBalance()+mon);
        System.out.println("充值成功!您当前余额为:"+user.getBalance());
    }
}

管理员业务层接口:

package dao;

import bean.Bike;
import bean.User;

import java.util.BitSet;
import java.util.List;
import java.util.Scanner;

/**
 * @Author:Liku
 * @Date:2022/11/25-23:05
 * @Name:ShareBicycle
 * @Since:jdk1.8
 * @Description:
 */
public interface AdminService {
    /**
     * 查看所有单车
     * @param bikes
     */
    void showBike(List<Bike> bikes);

    /**
     * 投放单车
     * @param scanner
     * @param bikes
     * @return
     */
    List<Bike> add(Scanner scanner, List<Bike> bikes);

    /**
     * 删除单车
     * @param scanner
     * @param bikes
     * @return
     */
    List<Bike> delete(Scanner scanner, List<Bike> bikes);

    /**
     * 单车排行榜
     * @param bikes
     */
    void range(List<Bike> bikes);

    /**
     * 展示用户信息
     * @param users
     */
    void show(List<User> users);
}

管理员业务层接口实现类:

package dao.impl;

import bean.Bike;
import bean.User;
import constans.Status;
import dao.AdminService;
import exception.NoSuchIdExcetion;

import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;

/**
 * @Author:Liku
 * @Date:2022/11/25-23:06
 * @Name:ShareBicycle
 * @Since:jdk1.8
 * @Description:
 */
public class AdminServiceImpl implements AdminService {
    @Override
    public void showBike(List<Bike> bikes) {
        System.out.println("现有单车如下:");
        bikes.forEach(System.out::println);
    }

    @Override
    public List<Bike> add(Scanner scanner, List<Bike> bikes) {
        System.out.println("请输入需要投放的单车公司名称:");
        String next1 = scanner.next();
        Objects.requireNonNull(next1);
        String name = next1.substring(0, 2);
        Iterator<Bike> iterator = bikes.iterator();
        while (iterator.hasNext()) {
            Bike next = iterator.next();
            if (next.getName().substring(0, 2).equals(name)) {
                System.out.println("请输入需要投放的单车数量:");
                int num = scanner.nextInt();
                for (int i = 0; i < num; i++) {
                    bikes.add(new Bike(bikes.size(), next.getName() + bikes.size(), Status.NOTBORROW, null, null, 1.5, 0, null));
                }
                System.out.println("投放成功!");
                return bikes;
            }
        }
        System.out.println("投放失败!输入单车公司不合作!");
        return bikes;
    }

    @Override
    public List<Bike> delete(Scanner scanner, List<Bike> bikes) {
        System.out.println("请输入需要删除的单车公司名称:");
        String name = scanner.next().substring(0,2);
        for (Bike bike : bikes) {
            if (bike.getName().substring(0, 2).equals(name)) {
                Iterator<Bike> iterator = bikes.iterator();
                while (iterator.hasNext()) {
                    Bike next = iterator.next();
                    System.out.println("请输入需要删除的单车编号:");
                    int num = scanner.nextInt();
                    if (next.getName().substring(0, 2).equals(name) && next.getId().equals(num) && next.getStatus().equals(Status.NOTBORROW)) {
                        bikes.remove(next);
                        System.out.println("删除成功!");
                        return bikes;

                    }
                }
            }
        }
        throw new NoSuchIdExcetion("无权删除单车");
    }

    @Override
    public void range(List<Bike> bikes) {
        for (int i = 1; i < bikes.size(); i++) {
            for (int j = 0; j < bikes.size() - i; j++) {
                if (bikes.get(j).getRentTime() < bikes.get(j + 1).getRentTime()) {
                    Bike temp = bikes.get(j);
                    bikes.set(j, bikes.get(j + 1));
                    bikes.set(j + 1, temp);
                }
            }
        }
        bikes.forEach(System.out::println);
    }


    @Override
    public void show(List<User> users) {
        users.forEach(System.out::println);
    }
}

异常:

package exception;

/**
 * @Author:Liku
 * @Date:2022/11/25-23:50
 * @Name:ShareBicycle
 * @Since:jdk1.8
 * @Description:
 */
public class AmountOfMoneyIllegal extends RuntimeException {
    public AmountOfMoneyIllegal() {
        super();
    }

    public AmountOfMoneyIllegal(String message) {
        super(message);
    }
}
package exception;

/**
 * @Author:Liku
 * @Date:2022/11/26-0:00
 * @Name:ShareBicycle
 * @Since:jdk1.8
 * @Description:
 */
public class NoSuchIdExcetion extends RuntimeException {
    public NoSuchIdExcetion() {
        super();
    }

    public NoSuchIdExcetion(String message) {
        super(message);
    }
}

工具包:

package util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * @Author:Liku
 * @Date:2022/11/26-11:50
 * @Name:ShareBicycle
 * @Since:jdk1.8
 * @Description:
 */
public class ParseUtil {
    private static final String PATTERN = "yyyy-MM-dd";

    private ParseUtil() {
    }

    /**
     * 日期格式转换
     * @param date
     * @return string
     */
    public static String date(Date date) {
        Objects.requireNonNull(date);
        DateFormat dateFormat = new SimpleDateFormat(PATTERN);
        return dateFormat.format(date);
    }

    /**
     * 日期格式转换
     * @param string
     * @return Date
     */
    public static Date date(String string) {
        Objects.requireNonNull(string);
        DateFormat dateFormat = new SimpleDateFormat(PATTERN);
        try {
            return dateFormat.parse(string);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}

package util;

import java.util.Date;

/**
 * @Author:Liku
 * @Date:2022/11/25-23:40
 * @Name:ShareBicycle
 * @Since:jdk1.8
 * @Description:
 */
public class CalMoney {
    private CalMoney() {
    }
/**
     * 计算时间差
     * @param date1
     * @param date2
     * @return
     */
    public static int calTime(Date date1, Date date2) {
        long l = (date1.getTime() - date2.getTime()) / 1000 / 60;
        if (l > 30) {
            return (int) ((l / 60) + 1);
        }
        return (int) (l / 60);
    }
}

test:

import action.BikeAction;

/**
 * @Author:Liku
 * @Date:2022/11/25-21:22
 * @Name:ShareBicycle
 * @Since:jdk1.8
 * @Description:
 */
public class test {
    public static void main(String[] args) {
        BikeAction.startMenu();
    }
}
posted @ 2022-11-26 09:51  Liku007  阅读(146)  评论(0编辑  收藏  举报