新冠疫情管理系统java版

简介: 新冠疫情管理系统java版

新冠疫情管理系统java版

用户 user1 user2

密码 user1 user2

管理员 admin1 admin2

密码 admin1 admin2

  • 无C语言版的密码错误的倒计时和密码的隐式输入
  • 无C语言版的插入功能
  • 暂无文件功能
  • 添加:可以多次添加,不会清除原数据
  • 修改:可以直接回车,将不会修改原数据
  • 1.1不足:输入18位身份证号只是用String存储,任何字符都可添加。
  • 改进:用数组
  • 1.2不足:输入11位电话号只是用String存储,任何字符都可添加
  • 改进:用数组

bean包下的Student类

package com.SMS.bean;
import java.util.Objects;
/**
 *Student为实体对象,用来封装客户信息
 *
 */
public class Student {
    private String name;//5位
    private int number;//0-
    double temperature;//35-42
    private String id;//18位
    private String phone;//11位
    private String address;//20
    public Student() {
    }
    public Student(String name, int number, double temperature, String id, String phone, String address) {
        this.name = name;
        this.number = number;
        this.temperature = temperature;
        this.id = id;
        this.phone = phone;
        this.address = address;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }
    public double getTemperature() {
        return temperature;
    }
    public void setTemperature(double temperature) {
        this.temperature = temperature;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return
                "姓名:" + name +
                ", 学号:" + number +
                ", 体温:" + temperature +
                ", 身份证号:" + id +
                ", 电话号:" + phone +
                ", 住址:" + address   ;
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return number == student.number &&
                Double.compare(student.temperature, temperature) == 0 &&
                Objects.equals(name, student.name) &&
                Objects.equals(id, student.id) &&
                Objects.equals(phone, student.phone) &&
                Objects.equals(address, student.address);
    }
    @Override
    public int hashCode() {
        return Objects.hash(name, number, temperature, id, phone, address);
    }
}

service包下的StudentList类

package com.SMS.service;
import com.SMS.bean.Student;
import java.util.ArrayList;
import java.util.Iterator;
public class StudentList {
    private ArrayList<Student> list=new ArrayList<Student>();
    /**
     * 用来初始化集合list的构造器
     * totalStudent 指定集合的长度
     */
    public StudentList() {
        list=new ArrayList<Student>();
    }
    public StudentList(int totalStudent) {
        list=new ArrayList<Student>(totalStudent);
    }
    /**
     * 将指定的学生添加到数组中
     */
    public void  addStudent(Student student) {
        list.add(student);
    }
    /**
     * 修改指定索引位置上的客户信息
     */
    public boolean replaceStudent(int index,Student stud) {
        if(index<0||index>=list.size()) {
            return false;
        }
        list.set(index,stud);
        return true;
    }
    /**
     *插入指定索引位置上的客户
     */
    public boolean Insert(int index,Student s) {
        if(index<0||index>=list.size()) {
            return false;
        }
        System.out.println("无此操作");
        return true;
    }
    /**
     *删除指定索引位置上的客户
     */
    public boolean deleteStudent(int index) {
        if(index<0||index>=list.size()) {
            return false;
        }
       list.remove(index);
        return true;
    }
    public void searchName(String name){
        boolean isFound=false;
        Iterator<Student> iterator=list.iterator();
        while (iterator.hasNext()) {
            Student s = iterator.next();
            if(name.equals(s.getName())){
                isFound=true;
                System.out.println(s);
            }
        }
        if (isFound==false){
            System.out.println("没有找到姓名是"+name+"的人");
        }
    }
    public void searchNum(int num){
        boolean isFound=false;
        Iterator<Student> iterator=list.iterator();
        while (iterator.hasNext()) {
            Student s = iterator.next();
            if(num==s.getNumber()){
                isFound=true;
                System.out.println(s);
            }
        }
        if (isFound==false){
            System.out.println("没有找到学号是"+num+"的人");
        }
    }
    public void searchNameNum(String name, int num){
        boolean isFound=false;
        Iterator<Student> iterator=list.iterator();
        while (iterator.hasNext()) {
            Student s = iterator.next();
            if(num==s.getNumber()&&name.equals(s.getName())){
                isFound=true;
                System.out.println(s);
            }
        }
        if (isFound==false){
            System.out.println("没有找到姓名是"+name+"并且学号是"+num+"的人");
        }
    }
    public void easyCount(double t) {
        boolean isFound=false;
        Iterator<Student> iterator=list.iterator();
        while (iterator.hasNext()) {
            Student s = iterator.next();
            if(t==s.getTemperature()){
                isFound=true;
                System.out.println(s);
            }
        }
        if (isFound==false){
            System.out.println("没有找到温度是"+t+"的人");
        }
    }
    public void Count(double t1, double t2) {
        boolean isFound=false;
        Iterator<Student> iterator=list.iterator();
        while (iterator.hasNext()) {
            Student s = iterator.next();
            if(t1<=s.getTemperature()&&s.getTemperature()<=t2){
                isFound=true;
                System.out.println(s);
            }
        }
        if (isFound==false){
            System.out.println("没有找到温度从"+t1+"到"+t2+"的人");
        }
    }
    /**
     *获取所有的客户信息
     */
    public ArrayList getAllStudents() {
        return list;
    }
    /**
     * 获取指定索引位置的客户信息
     */
    public Student getStudent(int index) {
        if(index<0||index>=list.size()) {
            return null;
        }
        return (Student) list.get(index);
    }
    /**
     *获取存储的客户数量
     */
    public int getTotal() {
        return list.size();
    }
}

UI包下的StudentView类

package com.SMS.ui;
import com.SMS.bean.Student;
import com.SMS.service.StudentList;
import com.SMS.util.SMUtility;
import java.util.*;
public class StudentView {
    /**
     * StudentView为主模块,负责菜单的显示和处理用户的操作
     *
     */
    private StudentList studentList=new StudentList();
    public StudentView() {
        Student student =new Student("张三",1,36.5,"123456789987654321","12345678998","京西");
        studentList.addStudent(student);
    }
    /**
     *显示《学生信息管理软件》界面的方法
     */
    public void userMainMenu() {
        boolean isFlag=true;
        while(isFlag) {
            System.out.println("\n-----------学生信息管理软件-------------\n");
            System.out.println("              1 添 加 ");
            System.out.println("              2 修 改 ");
            System.out.println("              3 插 入 ");
            System.out.println("              4 删 除 ");
            System.out.println("              5 查 询 ");
            System.out.println("              6 功 能 ");
            System.out.println("              7 列 表");
            System.out.println("              8 文 件");
            System.out.println("              9 退 出\n");
            System.out.print("       请选择(1-9):");
            char menu = SMUtility.readMenuSelection(1,9);
            switch (menu) {
                case '1':
                    addNewStudent();
                    break;
                case '2':
                    modifyStudent();
                    break;
                case '3':
                    insertStudent();
                    break;
                case '4':
                case '5':
                case '6':
                    System.out.println("用户无此操作权限");
                    break;
                case '7':
                    listAllStudents();
                    break;
                case '8':
                    System.out.println("用户您无此操作权限");
                    break;
                case '9':
//        System.out.println("退出");
                    System.out.print("确认是否退出(Y/N):");
                    char isExit= SMUtility.readConfirmSelection();
                    if(isExit=='Y') {
                        isFlag=false;
                    }
            }
        }
    }
    public void adminMainMenu() {
        boolean isFlag=true;
        while(isFlag) {
            System.out.println("\n-----------学生信息管理软件-------------\n");
            System.out.println("              1 添 加 ");
            System.out.println("              2 修 改 ");
            System.out.println("              3 插 入 ");
            System.out.println("              4 删 除 ");
            System.out.println("              5 查 询 ");
            System.out.println("              6 功 能 ");
            System.out.println("              7 列 表");
            System.out.println("              8 文 件");
            System.out.println("              9 退 出\n");
            System.out.print("       请选择(1-9):");
            char menu = SMUtility.readMenuSelection(1,9);
            switch (menu) {
                case '1':
                    addNewStudent();
                    break;
                case '2':
                    modifyStudent();
                    break;
                case '3':
                    insertStudent();
                    break;
                case '4':
                    deleteStudent();
                    break;
                case '5':
                    searchStudents();
                    break;
                case '6':
                    fuctionStudents();
                    break;
                case '7':
                    listAllStudents();
                    break;
                case '8':
                    fileStudents();
                    break;
                case '9':
//        System.out.println("退出");
                    System.out.print("确认是否退出(Y/N):");
                    char isExit= SMUtility.readConfirmSelection();
                    if(isExit=='Y') {
                        isFlag=false;
                    }
            }
        }
    }
    /**
     *添加学生的操作
     */
    private void addNewStudent() {
//    System.out.println("添加学生的操作");
        System.out.print("姓名:");
        String name = SMUtility.readOverString(5);
        System.out.print("学号:");
        int number = SMUtility.readInt();
        System.out.print("温度(35-42):");
        double temperature = SMUtility.readDouble(35,42);
        System.out.print("身份证号(18位):");
        String id = SMUtility.readString(18);
        System.out.print("电话号(11位):");
        String phone = SMUtility.readString(11);
        System.out.print("地址:");
        String address = SMUtility.readOverString(20);
        //将上述数据封装到对象中
        Student student =new Student(name,number,temperature,id,phone,address);
        studentList.addStudent(student);
        System.out.println("---------------添加完成---------------");
    }
    /**
     *修改学生的操作
     */
    private void modifyStudent() {
//    System.out.println("修改学生的操作");
        System.out.println("-----------修改学生-------------");
        int n;
        Student stud;
        for(;;) {
            System.out.print("请选择修改学生编号(0退出):");
            n=SMUtility.readInt();
            if(n==0) {
                return ;
            }
            stud = studentList.getStudent(n-1);
            if(stud==null){
                System.out.println("无法找到指定学生");
            }else {//找到了指定学生
                break;
            }
        }
        //修改学生信息
        System.out.print("姓名("+stud.getName()+"):");
        String name=SMUtility.readOverString(5, stud.getName());
        System.out.print("学号("+stud.getNumber()+"):");
        int number=SMUtility.readInt(stud.getNumber());
        System.out.print("温度("+stud.getTemperature()+"):");
        double temperature=SMUtility.readDouble(35,42,stud.getTemperature());
        System.out.print("身份证号("+stud.getId()+"):");
        String id=SMUtility.readString(18,stud.getId());
        System.out.print("电话号("+stud.getPhone()+"):");
        String phone=SMUtility.readString(11,stud.getPhone());
        System.out.print("地址("+stud.getAddress()+"):");
        String address=SMUtility.readOverString(20,stud.getAddress());
        Student newstud =new Student(name,number,temperature,id,phone,address);
        boolean isRepalaced=studentList.replaceStudent(n-1,newstud);
        if(isRepalaced) {
            System.out.println("---------------修改完成---------------");
        }else {
            System.out.println("---------------修改失败---------------");
        }
    }
    /**
     *插入学生的操作
     */
    private void insertStudent(){
        System.out.println("无此操作");
    }
    /**
     *删除学生的操作
     */
    private void deleteStudent() {
//    System.out.println("删除学生的操作");
        System.out.println("-----------删除学生-------------");
        int number;
        for(;;) {
            System.out.print("请选择待删除学生编号(0退出):");
            number=SMUtility.readInt();
            if(number==0) {
                return;
            }
            Student student=studentList.getStudent(number-1);
            if(student==null) {
                System.out.println("无法找到指定学生");
            }else {
                break;
            }
        }
        //找到了指定的学生
        System.out.println("确定是否删除(Y/N):");
        char isDelete=SMUtility.readConfirmSelection();
        if(isDelete=='Y') {
            boolean deleteSuccess=studentList.deleteStudent(number-1);
            if(deleteSuccess) {
                System.out.println("-----------删除完成-------------");
            }else {//没机会
                System.out.println("-----------删除失败-------------");
            }
        }else{//可省
            return;
        }
    }
    private void viewSearchName(){
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入姓名:");
        String name=scanner.nextLine();
        studentList.searchName(name);
    }
    private void viewSearchNum(){
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入学号:");
        int num=scanner.nextInt();
        studentList.searchNum(num);
    }
    private void viewSearchNameNum(){
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入姓名:");
        String name=scanner.nextLine();
        System.out.println("请输入学号:");
        int num=scanner.nextInt();
        studentList.searchNameNum(name,num);
    }
    /**
     *查询学生的操作
     */
    private void searchStudents(){
//        System.out.println("暂无此功能");
        while (true) {
            System.out.println("--------查询-------");
            System.out.println("     1查询 姓名");
            System.out.println("     2查询 学号");
            System.out.println("     3查询 姓名&学号");
            System.out.println("     4退出");
            System.out.print("请选择(1-4):");
            int selection;
            selection = SMUtility.readMenuSelection(1, 4);
            switch (selection) {
                case '1':
                    viewSearchName();
                    break;
                case '2':
                    viewSearchNum();
                    break;
                case '3':
                    viewSearchNameNum();
                    break;
                case '4':
                    System.out.print("请确认是否退出(Y/N):");
                    char choice;
                    choice = SMUtility.readConfirmSelection();
                    if (choice == 'Y') {
                        return;
                    }
            }
        }
    }
    private void viewEasyCount(){
        System.out.println("---简单排序---");
        System.out.println("输入温度(35-42):");
        Scanner scanner=new Scanner(System.in);
        double t=SMUtility.readDouble(35,42);
        studentList.easyCount(t);
    }
    private void viewCount(){
        System.out.println("---综合排序---");
        System.out.println("输入小温度(35-42):");
        double t1=SMUtility.readDouble(35,42);
        System.out.println("输入大温度("+t1+"-42):");
        double t2=SMUtility.readDouble(t1,42);
        studentList.Count(t1,t2);
    }
    private void ascendingSort(ArrayList<Student>list){
        Comparator<Student> com=new Comparator<>() {
            @Override
            public int compare(Student s1, Student s2) {
                return Integer.compare(s1.getNumber(),s2.getNumber());
            }
        };
        list.sort(com);
        for (Student s:list
             ) {
            System.out.println(s);
        }
    }
    private void descendingSort(ArrayList<Student>list){
        Comparator<Student> com=new Comparator<>() {
            @Override
            public int compare(Student s1, Student s2) {
                return -Double.compare(s1.getTemperature(),s2.getTemperature());
            }
        };
        list.sort(com);
        for (Student s:list
        ) {
            System.out.println(s);
        }
    }
    /**
     * 功能
     */
    private void fuctionStudents(){
        while (true) {
            System.out.println("--------功能-------");
            System.out.println("     1简单统计");
            System.out.println("     2综合统计");
            System.out.println("     3学号升序");
            System.out.println("     4温度降序");
            System.out.println("     5退   出");
            System.out.print("请选择(1-5):");
            int selection;
            selection = SMUtility.readMenuSelection(1, 5);
            switch (selection) {
                case '1':
                    viewEasyCount();
                    break;
                case '2':
                    viewCount();
                    break;
                case '3':
                    ascendingSort(studentList.getAllStudents());
                    break;
                case '4':
                    descendingSort(studentList.getAllStudents());
                    break;
                case '5':
                    System.out.print("请确认是否退出(Y/N):");
                    char choice;
                    choice = SMUtility.readConfirmSelection();
                    if (choice == 'Y') {
                        return;
                    }
            }
        }
    }
    /**
     *显示学生列表的操作
     */
    private void listAllStudents() {
//    System.out.println("显示学生列表的操作");
        System.out.println("-----------学生列表-------------");
        int total=studentList.getTotal();
        if(total==0) {
            System.out.println("没有学生记录");
        }else {
            System.out.println("编号\t姓名\t学号\t体温\t\t身份证号\t\t\t\t电话号\t\t地址");
            ArrayList studs=studentList.getAllStudents();
            for(int i=0;i<studs.size();i++) {
                Student stud= (Student) studs.get(i);
                System.out.println((i+1)+"\t"+stud.getName()
                        +"\t"+ stud.getNumber()+"\t"+ stud.getTemperature()+"\t"+stud.getId()+"\t"+stud.getPhone()+"\t"+stud.getAddress());
            }
        }
        System.out.println("---------学生列表完成-------------");
    }
    /**
     * 文件
     */
    private void fileStudents(){
        System.out.println("暂无此功能");
    }
    public void user(){
        Scanner scanner=new Scanner(System.in);
        HashMap<String,String> user=new HashMap<>();
        user.put("user1","user1");
        user.put("user2","user2");
        String name;
        String password=null;
        while (true){
            System.out.println("---------------用户系统---------------");
            System.out.println("               1 登录");
            System.out.println("               2 注册");
            System.out.println("               3 退出");
            System.out.print("请选择(1-3):");
            int choice;
            choice=SMUtility.readMenuSelection(1,3);
            switch (choice){
                case '1':
                    System.out.println("登录");
                    while(true) {
                        System.out.println("请输入账户名");
                        name = scanner.nextLine();
                        int isThree=1 ;
                        boolean sgining=false;
                        if (user.containsKey(name)) {
                            System.out.println("请输入密码"+'('+isThree+')');
                            password = scanner.nextLine();
                            for (; isThree < 4; isThree++) {
                                if (user.get(name).equals(password)) {
                                    System.out.println("欢迎用户" + name);
                                   sgining=true;
                                    break;
                                } else {
                                    if (isThree == 3) {
                                        System.out.println("密码错误超过3次\n");
                                        break;
                                    }
                                    System.out.println("请重新输入密码!"+'('+(isThree+1)+')');
                                    System.out.print("密码:");
                                    password=scanner.nextLine();
                                    if (user.get(name).equals(password)) {
                                        System.out.println("欢迎用户" + name);
                                        sgining=true;
                                        break;
                                    }
                                }
                            }
                        } else {
                            System.out.println("用户名错误");
                            break;
                        }
                        if(sgining==true){
                            StudentView view=new StudentView();
                            view.userMainMenu();
                            break;
                        }
                        if(isThree==3){
                            break;
                        }
                    }
                    break;
                case '2':
                    System.out.println("注册");
                    System.out.println("请输入账户名");
                    name = scanner.nextLine();
                    System.out.println("请输入密码");
                    password = scanner.nextLine();
                    String word;
                    System.out.println("请再次输入密码");
                    word = scanner.nextLine();
                    if(password.equals(word)){
                        user.put(name,password);
                        System.out.println("注册成功!");
                    }else{
                        System.out.println("确认密码失败,请重新注册");
                    }
                    break;
                case '3':
                    System.out.println("请确认是否退出(Y或N):");
                    char isExit;
                    isExit=SMUtility.readConfirmSelection();
                    if(isExit=='Y'){
                        return;
                    }
            }
        }
    }
    public void admin(){
        Scanner scanner=new Scanner(System.in);
        HashMap<String,String> admin=new HashMap<>();
        admin.put("admin1","admin1");
        admin.put("admin2","admin2");
        String name;
        String password;
        while (true){
            System.out.println("---------------管理员系统---------------");
            System.out.println("               1 登录");
            System.out.println("               2 注册");
            System.out.println("               3 退出");
            System.out.print("请选择(1-3):");
            int choice;
            choice=SMUtility.readMenuSelection(1,3);
            switch (choice){
                case '1':
                    System.out.println("登录");
                    while(true) {
                        System.out.println("请输入账户名");
                        name = scanner.nextLine();
                        int isThree=1 ;
                        boolean sgining=false;
                        if (admin.containsKey(name)) {
                            System.out.println("请输入密码"+'('+(isThree)+')');
                            password = scanner.nextLine();
                            for (; isThree < 4; isThree++) {
                                if (admin.get(name).equals(password)) {
                                    System.out.println("欢迎用户" + name);
                                    sgining=true;
                                    break;
                                } else {
                                    if (isThree == 3) {
                                        System.out.println("密码错误超过3次\n");
                                        break;
                                    }
                                    System.out.println("请重新输入密码!"+'('+(isThree+1)+')');
                                    System.out.print("密码:");
                                    password=scanner.nextLine();
                                    if (admin.get(name).equals(password)) {
                                        System.out.println("欢迎管理员" + name);
                                        sgining=true;
                                        break;
                                    }
                                }
                            }
                        } else {
                            System.out.println("用户名错误");
                            break;
                        }
                        if(sgining==true){
                            StudentView view=new StudentView();
                            view.adminMainMenu();
                            break;
                        }
                        if(isThree==3){
                            break;
                        }
                    }
                    break;
                case '2':
                    System.out.println("注册");
                    System.out.println("请输入账户名");
                    name = scanner.nextLine();
                    System.out.println("请输入密码");
                    password = scanner.nextLine();
                    String word;
                    System.out.println("请再次输入密码");
                    word = scanner.nextLine();
                    if(password.equals(word)){
                        admin.put(name,password);
                        System.out.println("注册成功!");
                    }else{
                        System.out.println("确认密码失败,请重新注册");
                    }
                    break;
                case '3':
                    System.out.println("请确认是否退出(Y或N):");
                    char isExit;
                    isExit=SMUtility.readConfirmSelection();
                    if(isExit=='Y'){
                        return;
                    }
            }
        }
    }
    public void help(){
        System.out.println("帮助");
        System.out.println("用户手册:");
        System.out.println("用户只能添加、修改、插入、打印报表");
        System.out.println("管理员手册:");
        System.out.println("管理员可以添加、修改、插入、删除、查询、功能(统计、排序)、打印报表、文件操作");
    }
    public void systemMain() {
        while (true){
            System.out.println("---------------新冠疫情管理系统---------------");
            System.out.println("                1 用户登录");
            System.out.println("                2 管理员登录");
            System.out.println("                3 帮  助");
            System.out.println("                4 退  出");
            System.out.print("请选择(1-4):");
            int selection;
            selection = SMUtility.readMenuSelection(1, 4);
            switch (selection) {
                case '1':
                    user();
                    break;
                case '2':
                    admin();
                    break;
                case '3':
                    help();
                    break;
                case '4':
                    System.out.println("请确认是否退出(Y/N):");
                    char isExit;
                    isExit = SMUtility.readConfirmSelection();
                    if (isExit == 'Y') {
                        return;
                    }
            }
        }
    }
    public static void main(String[] args) {
        StudentView view =new StudentView();
        view.systemMain();
    }
}

util包下的SMUtility工具类

package com.SMS.util;
import java.util.Scanner;
/**
 *  SMUtility 工具类
 *  将不同的功能封装为方法,就是可以直接调用方法使用它的功能,而无需考虑具体功能实现细节
 */
public class SMUtility {
    private static Scanner scanner=new Scanner(System.in) ;
    /*
    *   用于界面菜单的选择.该方法读取键盘,如果用户键入‘n1’-‘n2’中的任意字符,则方法返回。返回值为用户的输入值
     */
    public static char readMenuSelection(int n1,int n2) {
        char c;
        for(; ;) {
            String str =readKeyBoard(1,false);
            c=str.charAt(0);
            if(c<n1&&c>n2) {
                System.out.println("选择错误,请重新输入:");
            }else break;
        }
        return c;
    }
    /*
     * 用于确认的输入。该方法从键盘读取‘Y'或'N',并将其作为方法的返回值
     */
    public static char readConfirmSelection() {
        char c;
        for(; ;) {
            String str =readKeyBoard(1,false).toUpperCase();
            c=str.charAt(0);
            if(c=='Y'||c=='N') {
                break;
            }else {
                System.out.println("选择错误,请重新输入:");
            }
        }
        return c;
    }
    /**
     * 读正整数
     *
     */
    public static int readInt(){
        int n;
        while(true){
            n=scanner.nextInt();
            if (n>=0){
                break;
            }else {
                System.out.println("输入错误,请重新输入");
            }
        }
        return n;
    }
    public static int readInt(int defaultValue){
        int n;
        while(true){
            String str=scanner.nextLine();
            if(str.length()==0){
                n=defaultValue;
                break;
            }else{
                n=Integer.parseInt(str);
            }
            if (n>=0){
                break;
            }
        }
        return n;
    }
    /*
     * 从键盘读取一个n1-n2的实数,并将其作为方法的返回值
     * 用于输入
     */
    public static double readDouble(double n1,double n2) {
        double n;
        for(;;) {
            double v = scanner.nextDouble();
            if(v>=n1&&v<=n2){
                n=v;
                break;
            }else {
                System.out.println("输入错误,请重新输入");
            }
        }
        return n;
    }
    /*
     * 从键盘读取一个n1-n2的实数,并将其作为方法的返回值
     * 如果用户不输入字符而直接回车,方法将以defaultValue作为返回值。
     * 用于修改
     */
    public static double readDouble(double n1,double n2,double defaultValue) {
        double n;
        for(;;) {
            String str = scanner.nextLine();
            if(str.length()==0){
                n=defaultValue;
                break;
            }
            double v=Double.parseDouble(str);
            if(v>=n1&&v<=n2){
                n=v;
                break;
            }else {
                System.out.println("输入错误,请重新输入");
            }
        }
        return n;
    }
    /*
     * 从键盘读取一个长度是limit的字符串,并将其作为方法的返回值
     * 用于输入
     */
    public static String readString(int limit) {
        return readKeyBoard(limit,false);
    }
    /*
     * 从键盘读取一个长度是limit的字符串,并将其作为方法的返回值
     * 如果用户不输入字符而直接回车,方法将以defaultValue作为返回值。
     * 用于修改
     */
    public static String readString(int limit,String defaultValue) {
        String str=readKeyBoard(limit,true);
        return str.equals("")?defaultValue:str;
    }
    /*
     * 从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值
     * 用于输入
     */
    public static String readOverString(int limit) {
        return readOverKeyBoard(limit,false);
    }
    /*
     * 从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值
     * 如果用户不输入字符而直接回车,方法将以defaultValue作为返回值。
     * 用于修改
     */
    public static String readOverString(int limit,String defaultValue) {
        String str=readOverKeyBoard(limit,true);
        return str.equals("")?defaultValue:str;
    }
    private static String readKeyBoard(int limit,boolean blankReturn) {
        String line="";
        while(scanner.hasNextLine()) {
            line=scanner.nextLine();
            if(line.length()==0) {
                if(blankReturn)return line;
                else continue;
            }
            if(line.length()!=limit) {
                System.out.println("输入长度(不等于"+limit+")错误,请重新输入:");
                continue;
            }
            break;
        }
        return line;
    }
    private static String readOverKeyBoard(int limit,boolean blankReturn) {
        String line="";
        while(scanner.hasNextLine()) {
            line=scanner.nextLine();
            if(line.length()==0) {
                if(blankReturn)return line;
                else continue;
            }
            if(line.length()>=limit) {
                System.out.println("输入长度(不大于"+limit+")错误,请重新输入:");
                continue;
            }
            break;
        }
        return line;
    }
}
相关文章
|
消息中间件 Java Kafka
Java 事件驱动架构设计实战与 Kafka 生态系统组件实操全流程指南
本指南详解Java事件驱动架构与Kafka生态实操,涵盖环境搭建、事件模型定义、生产者与消费者实现、事件测试及高级特性,助你快速构建高可扩展分布式系统。
654 7
基于JavaWeb的销售管理系统设计系统
本系统基于Java、MySQL、Spring Boot与Vue.js技术,构建高效、可扩展的销售管理平台,实现客户、订单、数据可视化等全流程自动化管理,提升企业运营效率与决策能力。
|
数据采集 前端开发 JavaScript
产科专科电子病历系统基于Java开发,实现与HIS、LIS、PACS及区域妇幼信息平台的三级互联互通
产科专科电子病历系统基于Java开发,采用前后端分离架构(Vue+ElementUI前端,MySQL数据库),实现与HIS、LIS、PACS及区域妇幼信息平台的三级互联互通。系统涵盖患者全息视图、快速智能录入、检验检查模块、智能高危评估、异常值提醒及自我监测等功能,支持孕期时间轴和综合评估,自动归集数据并完成高危评分,助力产科数据标准化、结构化,以及临床保健工作的全程智能化管理。
588 1
|
存储 Java 数据库连接
java 初学者必看的系统知识结构图详解
本文详解Java知识结构图,涵盖Java语言基础、JVM原理、集合框架、并发编程、网络通信及主流框架(如Spring Boot、MyBatis),并结合学生信息管理系统实例,帮助初学者构建完整知识体系,提升实战开发能力。
477 0
|
移动开发 监控 小程序
java家政平台源码,家政上门清洁系统源码,数据多端互通,可直接搭建使用
一款基于Java+SpringBoot+Vue+UniApp开发的家政上门系统,支持小程序、APP、H5、公众号多端互通。涵盖用户端、技工端与管理后台,支持多城市、服务分类、在线预约、微信支付、抢单派单、技能认证、钱包提现等功能,源码开源,可直接部署使用。
1039 24
|
设计模式 消息中间件 传感器
Java 设计模式之观察者模式:构建松耦合的事件响应系统
观察者模式是Java中常用的行为型设计模式,用于构建松耦合的事件响应系统。当一个对象状态改变时,所有依赖它的观察者将自动收到通知并更新。该模式通过抽象耦合实现发布-订阅机制,广泛应用于GUI事件处理、消息通知、数据监控等场景,具有良好的可扩展性和维护性。
1047 8
|
安全 前端开发 Java
使用Java编写UDP协议的简易群聊系统
通过这个基础框架,你可以进一步增加更多的功能,例如用户认证、消息格式化、更复杂的客户端界面等,来丰富你的群聊系统。
471 11
|
机器学习/深度学习 人工智能 自然语言处理
Java与生成式AI:构建内容生成与创意辅助系统
生成式AI正在重塑内容创作、软件开发和创意设计的方式。本文深入探讨如何在Java生态中构建支持文本、图像、代码等多种生成任务的创意辅助系统。我们将完整展示集成大型生成模型(如GPT、Stable Diffusion)、处理生成任务队列、优化生成结果以及构建企业级生成式AI应用的全流程,为Java开发者提供构建下一代创意辅助系统的完整技术方案。
626 10
|
人工智能 监控 Java
Java与AI智能体:构建自主决策与工具调用的智能系统
随着AI智能体技术的快速发展,构建能够自主理解任务、制定计划并执行复杂操作的智能系统已成为新的技术前沿。本文深入探讨如何在Java生态中构建具备工具调用、记忆管理和自主决策能力的AI智能体系统。我们将完整展示从智能体架构设计、工具生态系统、记忆机制到多智能体协作的全流程,为Java开发者提供构建下一代自主智能系统的完整技术方案。
1380 4
|
NoSQL Java 关系型数据库
超全 Java 学习路线,帮你系统掌握编程的超详细 Java 学习路线
本文为超全Java学习路线,涵盖基础语法、面向对象编程、数据结构与算法、多线程、JVM原理、主流框架(如Spring Boot)、数据库(MySQL、Redis)及项目实战等内容,助力从零基础到企业级开发高手的进阶之路。
937 1