BufferedReader 相关操作实例

package bufferedreader.cn;
/*
 * 写一个输入数据的标准类
 * 输入数据常见得可能是:整数,小数,日期,字符串,所有最好设计一个专门的输入数据的类,完成输入数据的功能
 */

public class BufferedReaderDemo {

}
package bufferedreader.cn;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.xml.crypto.Data;

public class InputData {
    private BufferedReader  br = null;
    //通过构造方法实例化对象
    public InputData(){
        br = new BufferedReader(new InputStreamReader(System.in));
    }
    //得到字符串信息的方法
    public String getString(String info){
        String temp = null;
        System.out.println(info);
        try {
            temp = this.br.readLine();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return temp;
    }
    //得到一个整数的输入类型
    public int getInt(String info ,String err){
        int temp = 0;
        String str = null;
        boolean flag =  true;
        while (flag) {
            str = this.getString(info);
            //"^\d+$"  //非负整数(正整数   +   0)  
            if (str.matches("^\d+$")) {
                temp = Integer.parseInt(str);
                flag = false;            
            }
            else {
                System.out.println(err);
            }
            
        }
        return temp;
    }    
    //得到一个小数的输入数据
    public float getFloat(String info,String err){
        float temp = 0f;
        String str = null;
        boolean flag = true;
        while (flag) {
            str = this.getString(info);
            //^d+(.d+)?$:非负浮点数
            if (str.matches("^\d+$")){
                temp =Float.parseFloat(str);
                flag = false;        
            }
            else {
                System.out.println(err);
            }
            
            
        }
        return temp;
        
    }
    //日期的输入数据
    public Data getData(String info,String err){
        Data temp = null;
        String str = null;
        boolean flag = true;
        while (flag) {
            str = this.getString(info);
            //^d{4}(-|/|.)d{1,2}1d{1,2}$:简单的日期判断
            if (str.matches("^\d+$")){
                SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    temp = (Data) sd.parse(str);
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                flag = false;        
            }
            else {
                System.out.println(err);
            }
            
        }
        return temp;
        
    }
        
}
package bufferedreader.cn;

public class Menu {
    public Menu(){
        while (true) {
            this.show();
        }
    }
    public void show(){
        System.out.println("===XXX系统===");
        System.out.println("【1】、增加数据");
        System.out.println("【2】、删除数据");
        System.out.println("【4】、查看数据");
        System.out.println("【0】、系统退出");
        InputData ip = new InputData();
        int i = (int)ip.getInt("请选择正确的选项:", "输错了!");
        switch (i) {
        case 1:{
            Operate.add();
            break;
            }
        case 2:{
            Operate.delete();
            break;
            }
        case 3:{
            Operate.update();
            break;
            }
        case 4:{
            Operate.find();
            break;
            }
        case 0:{
            System.exit(1);
            break;
        }
        default:{
            System.out.println("请选择正确的操作!");
        }
            
        }
        
    }

}
package bufferedreader.cn;
/*
 * 操作类
 */
public class Operate {
    public static void add(){
        System.out.println("选择的是增加操作");
    }
    public static void delete(){
        System.out.println("选择的是删除操作");
    }
    public static void update(){
        System.out.println("选择的是更新操作");
    }
    public static void find(){
        System.out.println("选择的是查找操作");
    }
}
package bufferedreader.cn;

public class Test {
    public static void main(String[] args) {
        new Menu();
    }

}
原文地址:https://www.cnblogs.com/yuanyuan2017/p/6950366.html