首页 > 其他分享 >共享单车

共享单车

时间:2022-11-26 10:01:02浏览次数:41  
标签:scanner System println import 共享 单车 public out

单车操作层:

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++) {
            bikeList.add(new Bike((i + 1), "ofo" + i, Status.NOTBORROW, null, null, 1.5, i * 2 + 235));
        }
        for (int i = 0; i < 5; i++) {
            bikeList.add(new Bike(bikeList.size()+1, "mobile" + i, Status.NOTBORROW, null, null, 1.5, i * 2 + 233));
        }
        for (int i = 0; i < 5; i++) {
            userList.add(new User((i + 1), "nak" + (i + 1), "nakpwd", 33.5 + i, null));
        }
    }

    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)) {
                System.out.println("登录成功!");
                if (next.getId() % 2 == 0) {
                    AdminAction.adminStart(next,scanner);
                } else {
                    UserAction.userStart(next, scanner);
                }
            }
            index++;
        }
        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=" + LendTime +
                ", BackTime=" + BackTime +
                ", 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 {
    void borrow(Scanner scanner, User user, List<Bike> list);

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

    void updatePwd(User user,Scanner 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.setRentTime(bike.getRentTime()+1);
                user.setBike(bike);
            }
        }
    }

    @Override
    public void back(Scanner scanner, User user, List<Bike> list) {
        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);
                System.out.println("还车成功!欢迎下次骑行");
                return;
            }
        }
        throw new NoSuchIdExcetion("输入单车编号错误!");
    }

    @Override
    public void updatePwd(User user, Scanner scanner) {
        System.out.println("请输入修改前的密码:");
        String pass=scanner.next();
        if(user.getPwd().equals(pass)){
            System.out.println("请输入修改后的密码");
            user.setPwd(scanner.next());
            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】");
        }
    }
}

管理员业务层接口:

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 {
    void showBike(List<Bike> bikes);
    List<Bike> add(Scanner scanner,List<Bike> bikes);
    List<Bike> delete(Scanner scanner,List<Bike> bikes);
    void range(List<Bike> bikes);
    void show(List<User> users);
}

管理员业务层接口:

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 {
    void showBike(List<Bike> bikes);
    List<Bike> add(Scanner scanner,List<Bike> bikes);
    List<Bike> delete(Scanner scanner,List<Bike> bikes);
    void range(List<Bike> bikes);
    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.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 name = scanner.next().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().substring(next.getName().length()-1)+bikes.size(), Status.NOTBORROW, null, null, 1.5,0));
                }
                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);
        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();
                if (next.getId().equals(num)) {
                    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.util.Date;

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

    public static int calTime(Date date1, Date date2) {
        return (int) (date1.getTime() - date2.getTime() / 1000 / 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();
    }
}

标签:scanner,System,println,import,共享,单车,public,out
From: https://www.cnblogs.com/Liku-java/p/16926950.html

相关文章