//domain包下的类和接口
package domain;
public class Architect extends Designer{
private int stock;//公司奖励的股票的数量
public Architect(int id, String name, int age, double salary, double bonus, int stock,Equipment equipment) {
super(id, name, age, salary,bonus,equipment);
this.stock = stock;
}
public Architect() {
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
public String getOccupation(){
return"架构师";
}
}
package domain;
public class Designer extends Programmer implements occupation{//设计继承于程序员类
private double bonus;//表示奖金
public Designer(int id, String name, int age, double salary, double bonus,Equipment equipment) {
super(id, name, age, salary, equipment);
equipment.getDescription();
this.bonus = bonus;
}
public Designer() {
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
public String getOccupation(){
return"设计师";
}
}
package domain;
public class Employee {
private int id;
private String name;
private int age;
private double salary;
public Employee(){
}
public Employee(int id,String name,int age,double salary){
this.id=id;
this.name=name;
this.age=age;
this.salary=salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
package domain;
//此接口用来输出实现类的信息
public interface Equipment {//让接口被其子类实现
public String getDescription();//抽象方法 用来输出子类中的信息
}
package domain;
public class Notebook implements Equipment {//实现接口输出其型号和价格
private String model;//机器的型号
private double price;//机器的价格
public Notebook(String model, double price){//构造器在这里!!
this.model=model;
this.price=price;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getDescription(){//重写抽象方法输出NOTE的信息
return model+"("+price+")";
}
}
package domain;
//此接口没有要求 作者为实现作业加入的
public interface occupation {
public String getOccupation();//输出职业信息
}
package domain;
public class PC implements Equipment{//实现接口返沪属性
private String model;//机器的型号
private String display;//显示器的名称
public PC(String model, String display){//构造器初始化类中的属性
this.display=display;
this.model=model;
}
public PC() {
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getDisplay() {
return display;
}
public void setDisplay(String display) {
this.display = display;
}
public String getDescription(){//重写接口中的抽象方法让其返回这个实现类的信息
return model+"("+display+")";
}
}
package domain;
public class printer implements Equipment{//实现了Equipment接口
private String name;
private String type;
public printer(String name,String type){
this.name=name;
this.type=type;
}
public printer() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDescription(){//重写接口中的抽象方法返回类的信息
return name+"("+type+")";
}
}
package domain;
import service.Status;
public class Programmer extends Employee implements occupation{
private int members;//记录加入开发团队之后的id
private Status Status= service.Status.FREE;//空闲状态
private Equipment equipment;//表示该成员领用的设备
public Programmer() {
}
public Programmer(int id, String name, int age, double salary, Equipment equipment){
super(id,name,age,salary);
this.equipment=equipment;
}
public int getMembers() {
return members;
}
public void setMembers(int members) {
this.members = members;
}
public service.Status getStatus() {
return Status;
}
public void setStatus(service.Status status) {
Status = status;
}
public Equipment getEquipment() {
return equipment;
}
public void setEquipment(Equipment equipment) {
this.equipment = equipment;
}
public String getOccupation(){
return"程序员";
}
}
package service;
//此下类都是 Service的
public class Data {
public static final int EMPLOYEE = 10;
public static final int PROGRAMMER = 11;
public static final int DESIGNER = 12;
public static final int ARCHITECT = 13;
public static final int PC = 21;
public static final int NOTEBOOK = 22;
public static final int PRINTER = 23;
//Employee : 10, id, name, age, salary
//Programmer: 11, id, name, age, salary
//Designer : 12, id, name, age, salary, bonus
//Architect : 13, id, name, age, salary, bonus, stock
public static final String[][] EMPLOYEES = {
{"10", "1", "马云", "22", "3000"},
{"13", "2", "马化腾", "32", "18000", "15000", "2000"},
{"11", "3", "李彦宏", "23", "7000"},
{"11", "4", "刘强东", "24", "7300"},
{"12", "5", "雷军", "28", "10000", "5000"},
{"11", "6", "任志强", "22", "6800"},
{"12", "7", "柳传志", "29", "10800","5200"},
{"13", "8", "杨元庆", "30", "19800", "15000", "2500"},
{"12", "9", "史玉柱", "26", "9800", "5500"},
{"11", "10", "丁磊", "21", "6600"},
{"11", "11", "张朝阳", "25", "7100"},
{"12", "12", "杨致远", "27", "9600", "4800"}
};
//如下的EQUIPMENTS数组与上面的EMPLOYEES数组元素一一对应
//PC :21, model, display
//NoteBook:22, model, price
//Printer :23, name, type
public static final String[][] EQUIPMENTS = {
{},
{"22", "联想T4", "6000"},
{"21", "戴尔", "NEC17寸"},
{"21", "戴尔", "三星 17寸"},
{"23", "佳能 2900", "激光"},
{"21", "华硕", "三星 17寸"},
{"21", "华硕", "三星 17寸"},
{"23", "爱普生20K", "针式"},
{"22", "惠普m6", "5800"},
{"21", "戴尔", "NEC 17寸"},
{"21", "华硕","三星 17寸"},
{"22", "惠普m6", "5800"}
};
}
package service;
import domain.*;
import org.junit.Test;
public class NameListService {
private Employee[] employee;
int total = 0;
public NameListService() {
employee = new Employee[12];
Employee emp;
Programmer pga;
Designer ds;
Architect at;
PC pc;
printer PRT;
Notebook nb;
for (int i = 0; i < Data.EMPLOYEES.length; i++) {
for (int j = 0; j < Data.EMPLOYEES[i].length; j++) {
try {
if (Integer.parseInt(Data.EMPLOYEES[i][0]) == Data.EMPLOYEE) {//普通雇工
//每次进入if语句 新new对象存入父类的数组中(测试1)
employee[total++] = new Employee(Integer.parseInt(Data.EMPLOYEES[i][1]), Data.EMPLOYEES[i][2], Integer.parseInt(Data.EMPLOYEES[i][3]), Double.parseDouble(Data.EMPLOYEES[i][4]));
break;//判断完跳出内循环开始下一次判断
}
//判断条件满足哪种电子器械
else if (Integer.parseInt(Data.EMPLOYEES[i][0]) == Data.PROGRAMMER) {//程序员
if (Integer.parseInt(Data.EQUIPMENTS[i][0]) == Data.NOTEBOOK) {//笔记本//一个元素是String一个是double(String转换为double)用包装类Double.parseDouble()
employee[total++] = new Programmer(Integer.parseInt(Data.EMPLOYEES[i][1]), Data.EMPLOYEES[i][2], Integer.parseInt(Data.EMPLOYEES[i][3]), Double.parseDouble(Data.EMPLOYEES[i][4]), new Notebook(Data.EQUIPMENTS[i][1], Double.parseDouble(Data.EQUIPMENTS[i][2])));
break;
} else if (Integer.parseInt(Data.EQUIPMENTS[i][0]) == Data.PC) {//电脑
//total++ 数组存放位置
employee[total++] = new Programmer(Integer.parseInt(Data.EMPLOYEES[i][1]), Data.EMPLOYEES[i][2], Integer.parseInt(Data.EMPLOYEES[i][3]), Double.parseDouble(Data.EMPLOYEES[i][4]), new PC(Data.EQUIPMENTS[i][1], Data.EQUIPMENTS[i][2]));
break;
} else if (Integer.parseInt(Data.EQUIPMENTS[i][0]) == Data.PRINTER) {//打印机 两个元素都是String
employee[total++] = new Programmer(Integer.parseInt(Data.EMPLOYEES[i][1]), Data.EMPLOYEES[i][2], Integer.parseInt(Data.EMPLOYEES[i][3]), Double.parseDouble(Data.EMPLOYEES[i][4]), new printer(Data.EQUIPMENTS[i][1], Data.EQUIPMENTS[i][2]));
break;
}
} else if (Integer.parseInt(Data.EMPLOYEES[i][0]) == Data.DESIGNER) {//设计师
if (Integer.parseInt(Data.EQUIPMENTS[i][0]) == Data.NOTEBOOK) {//笔记本//一个元素是String一个是double(String转换为double)用包装类Double.parseDouble()
employee[total++] = new Designer(Integer.parseInt(Data.EMPLOYEES[i][1]), Data.EMPLOYEES[i][2], Integer.parseInt(Data.EMPLOYEES[i][3]), Double.parseDouble(Data.EMPLOYEES[i][4]), Double.parseDouble(Data.EMPLOYEES[i][5]), new Notebook(Data.EQUIPMENTS[i][1], Double.parseDouble(Data.EQUIPMENTS[i][2])));
break;
} else if (Integer.parseInt(Data.EQUIPMENTS[i][0]) == Data.PC) {//电脑 两个元素都是String
//total++ 数组存放位置
employee[total++] = new Designer(Integer.parseInt(Data.EMPLOYEES[i][1]), Data.EMPLOYEES[i][2], Integer.parseInt(Data.EMPLOYEES[i][3]), Double.parseDouble(Data.EMPLOYEES[i][4]), Double.parseDouble(Data.EMPLOYEES[i][5]), new PC(Data.EQUIPMENTS[i][1], Data.EQUIPMENTS[i][2]));
break;
} else if (Integer.parseInt(Data.EQUIPMENTS[i][0]) == Data.PRINTER) {//打印机 两个元素都是String
employee[total++] = new Designer(Integer.parseInt(Data.EMPLOYEES[i][1]), Data.EMPLOYEES[i][2], Integer.parseInt(Data.EMPLOYEES[i][3]), Double.parseDouble(Data.EMPLOYEES[i][4]), Double.parseDouble(Data.EMPLOYEES[i][5]), new printer(Data.EQUIPMENTS[i][1], Data.EQUIPMENTS[i][2]));
break;
}
} else if (Integer.parseInt(Data.EMPLOYEES[i][0]) == Data.ARCHITECT) {//架构师 多一个元素
if (Integer.parseInt(Data.EQUIPMENTS[i][0]) == Data.NOTEBOOK) {//笔记本//一个元素是String一个是double(String转换为double)用包装类Double.parseDouble()
employee[total++] = new Architect(Integer.parseInt(Data.EMPLOYEES[i][1]), Data.EMPLOYEES[i][2], Integer.parseInt(Data.EMPLOYEES[i][3]), Double.parseDouble(Data.EMPLOYEES[i][4]), Double.parseDouble(Data.EMPLOYEES[i][5]), Integer.parseInt(Data.EMPLOYEES[i][6]), new Notebook(Data.EQUIPMENTS[i][1], Double.parseDouble(Data.EQUIPMENTS[i][2])));
break;
} else if (Integer.parseInt(Data.EQUIPMENTS[i][0]) == Data.PC) {//电脑 两个元素都是String
//total++ 数组存放位置
employee[total++] = new Architect(Integer.parseInt(Data.EMPLOYEES[i][1]), Data.EMPLOYEES[i][2], Integer.parseInt(Data.EMPLOYEES[i][3]), Double.parseDouble(Data.EMPLOYEES[i][4]), Double.parseDouble(Data.EMPLOYEES[i][5]), Integer.parseInt(Data.EMPLOYEES[i][6]), new PC(Data.EQUIPMENTS[i][1], Data.EQUIPMENTS[i][2]));
break;
} else if (Integer.parseInt(Data.EQUIPMENTS[i][0]) == Data.PRINTER) {//打印机 两个元素都是String
employee[total++] = new Architect(Integer.parseInt(Data.EMPLOYEES[i][1]), Data.EMPLOYEES[i][2], Integer.parseInt(Data.EMPLOYEES[i][3]), Double.parseDouble(Data.EMPLOYEES[i][4]), Double.parseDouble(Data.EMPLOYEES[i][5]), Integer.parseInt(Data.EMPLOYEES[i][6]), new printer(Data.EQUIPMENTS[i][1], Data.EQUIPMENTS[i][2]));
break;
}
}
} catch (NumberFormatException e) {
} catch (ArrayIndexOutOfBoundsException e) {
}
}
}
}
public Employee[] getAllEmployees() {//获取所有雇员的信息
return employee;
}
public Employee getEmployee(int id) throws TeamException {//获取指定雇员信息
for (int i = 0; i < employee.length; i++) {
if (employee[i].getId() == id) {//用户输入的id与员工id相等则返回此员工对象
return employee[i];
}
}
throw new TeamException();//未找到员工对象 抛出自定义的异常类
}
// public static void main (String[]args) {
// NameListService n = new NameListService();
// for (int i = 0; i < n.getAllEmployees().length; i++) {
// Employee[] l = n.getAllEmployees();
// if(l[i].getClass()==Employee.class){
// System.out.println(l[i].getId()+" "+l[i].getName()+" "+l[i].getAge()+" "+l[i].getSalary());
// }
// else if(l[i].getClass()== Programmer.class) {
// //如果数组中存放的对象的类型一致
// //强制类型转换
// System.out.println(((Programmer) l[i]).getId()+" "+((Programmer) l[i]).getName()+" "+((Programmer) l[i]).getAge()+" "+((Programmer) l[i]).getSalary()+" "+((Programmer) l[i]).getEquipment().getDescription());
// }
// else if(l[i].getClass()==Designer.class){
// System.out.println(((Designer)l[i]).getId()+" "+((Designer)l[i]).getName()+" "+((Designer)l[i]).getAge()+" "+((Designer)l[i]).getSalary()+" "+((Designer)l[i]).getBonus()+" "+((Designer)l[i]).getEquipment().getDescription());
// }
// else if(l[i].getClass()==Architect.class){
// System.out.println(((Architect)l[i]).getId()+" "+((Architect)l[i]).getName()+" "+((Architect)l[i]).getAge()+" "+((Architect)l[i]).getSalary()+" "+((Architect)l[i]).getBonus()+" "+((Architect)l[i]).getStock()+" "+((Architect)l[i]).getEquipment().getDescription());
// }
// }
// }
}
//屏蔽的代码作者用于测试
package service;
public class Status {
private final String NAME;
private Status(String name) {
this.NAME = name;
}
public static final Status FREE = new Status("FREE");
public static final Status VOCATION = new Status("VOCATION");
public static final Status BUSY = new Status("BUSY");
public String getNAME() {
return NAME;
}
@Override
public String toString() {
return NAME;
}
}
package service;
//此类为异常类 但我并没使用.....
import domain.Employee;
public class TeamException extends RuntimeException {
static final long serialVersionUID = -28489555665294L;
public TeamException(){
}
public TeamException(String msg){
super(msg);
}
}
package service;
//在视图调用的操作方法?可以这么说吧
import domain.Employee;
import domain.Programmer;
public class TeamService {
private static int counter = 1;
private final int MAX_MEMBER = 5;
private Programmer[] team = new Programmer[MAX_MEMBER];
private int total = 0;
public Programmer[] getTeam() {//获取数组实际添加的长度
return team;
}
public void addMember(Employee e) throws TeamException, ClassCastException {//添加用户
if (total < MAX_MEMBER) {
team[total] = (Programmer) e;
total++;
((Programmer) e).setMembers(counter++);//指定的编号
}
}
public void removerMember(int memberLd) throws TeamException,ArrayIndexOutOfBoundsException {
memberLd=memberLd-1;
for (int i = memberLd; i < total; i++) {
team[i]=team[i+1];
}
team[--total]=null;
}
public int getTotal() {
return total;
}
}
package view;//改良版
import domain.*;
import service.NameListService;
import service.TeamException;
import service.TeamService;
import java.util.*;
public class TeamView {
private NameListService listSvc = new NameListService();
private TeamService teamSvc = new TeamService();
Scanner scan=new Scanner(System.in);
public void enterMainMenu() {
try {
while (true) {
listAllEmployees();
int input = scan.nextInt();//根据用户输入的数调用不同的方法
if (input == 1) {
getTeam();
TSUtility.readReturn();
} else if (input == 2) {
addMember();
TSUtility.readReturn();
} else if (input == 3) {
deleteMember();
TSUtility.readReturn();
} else if (input == 4) {
System.out.println("确定要退出吗?y/n");
char in = TSUtility.readConfirmSelection();
if (in=='Y') {
System.exit(0);
TSUtility.readReturn();
break;
}
else if(in=='N'){
continue;
}
}
else{
System.out.println("输入错误!");
TSUtility.readReturn();
}
}
}catch(NullPointerException e){
TSUtility.readReturn();
enterMainMenu();
}catch (InputMismatchException e){
System.out.println("输入错误!");
TSUtility.readReturn();
enterMainMenu();
}catch (ClassCastException e){
System.out.println("你让资本家当苦力?,不要命了???");
TSUtility.readReturn();
enterMainMenu();
}catch(ArrayIndexOutOfBoundsException e){
}
}
private void listAllEmployees() {
System.out.println("-------------------------------------开发团队调度软件--------------------------------------");
System.out.println("ID\t姓名\t\t年龄\t 工资\t职位\t\t状态\t\t奖金\t\t股票\t\t领用设备");
for (int i = 0; i < listSvc.getAllEmployees().length; i++) {
Employee[] l = listSvc.getAllEmployees();
if (l[i].getClass() == Employee.class) {
System.out.println(l[i].getId() + " \t" + l[i].getName() + " \t" + l[i].getAge() + "\t" + l[i].getSalary());
} else if (l[i].getClass() == Programmer.class) {
//如果数组中存放的对象的类型一致
//强制类型转换
System.out.println(((Programmer) l[i]).getId() + "\t " + ((Programmer) l[i]).getName() + "\t" + ((Programmer) l[i]).getAge() + "\t" + ((Programmer) l[i]).getSalary() + "\t" + (((Programmer) l[i]).getOccupation()) + "\t" + ((Programmer) l[i]).getStatus() + "\t \t" + ((Programmer) l[i]).getEquipment().getDescription());
} else if (l[i].getClass() == Designer.class) {
System.out.println(((Designer) l[i]).getId() + "\t " + ((Designer) l[i]).getName() + "\t" + ((Designer) l[i]).getAge() + "\t" + ((Designer) l[i]).getSalary() + "\t" + ((Designer) l[i]).getOccupation() + "\t" + ((Designer) l[i]).getStatus() + "\t" + ((Designer) l[i]).getBonus() + "\t " + ((Designer) l[i]).getEquipment().getDescription());
} else if (l[i].getClass() == Architect.class) {
System.out.println(((Architect) l[i]).getId() + "\t" + ((Architect) l[i]).getName() + "\t" + ((Architect) l[i]).getAge() + "\t" + ((Architect) l[i]).getSalary() + "\t" + (((Architect) l[i]).getOccupation()) + "\t" + (((Architect) l[i]).getStatus()) + "\t" + ((Architect) l[i]).getBonus() + "\t" + ((Architect) l[i]).getStock() + "\t" + ((Architect) l[i]).getEquipment().getDescription());
}
}
System.out.println("----------------------------------------------------------------------------------------------");
System.out.println("1-团队列表 2-添加团队成员 3-删除团队成员 4-退出 请选择(1-4): ");
}
//开发团队人员组成要求
//最多一名架构师
//最多两名设计师
//最多三名程序员
private void getTeam() throws NullPointerException{
System.out.println("--------------------团队成员列表---------------------");
System.out.println("TDI/ID\t姓名\t 年龄\t 工资 \t职位 \t奖金 \t股票");
for(int i=0;i<teamSvc.getTeam().length;i++) {
if(teamSvc.getTeam()[0]==null){
System.out.println("目前团队里还没有员工");
}
if (teamSvc.getTeam()[i].getClass().equals(Employee.class)) {
System.out.println(teamSvc.getTeam()[i].getId() + "\t" + teamSvc.getTeam()[i].getMembers() + "\t" + teamSvc.getTeam()[i].getName() + "\t" + teamSvc.getTeam()[i].getAge() + "\t" + teamSvc.getTeam()[i].getSalary() + "\t" + teamSvc.getTeam()[i].getOccupation() + "\t"+teamSvc.getTeam()[i].getStatus());
}
else if(teamSvc.getTeam()[i].getClass().equals(Programmer.class)){
System.out.println(((Programmer)teamSvc.getTeam()[i]).getId() + "\t" + ((Programmer)teamSvc.getTeam()[i]).getMembers() + "\t" + ((Programmer)teamSvc.getTeam()[i]).getName() + "\t" + ((Programmer)teamSvc.getTeam()[i]).getAge() + "\t" + ((Programmer)teamSvc.getTeam()[i]).getSalary() + "\t" + ((Programmer)teamSvc.getTeam()[i]).getOccupation() + "\t");
}
else if(teamSvc.getTeam()[i].getClass().equals(Designer.class)){
System.out.println(((Designer)teamSvc.getTeam()[i]).getId() + "\t" + ((Designer)teamSvc.getTeam()[i]).getMembers() + "\t" + ((Designer)teamSvc.getTeam()[i]).getName() + "\t " + ((Designer)teamSvc.getTeam()[i]).getAge() + "\t" + ((Designer)teamSvc.getTeam()[i]).getSalary() + "\t" + ((Designer)teamSvc.getTeam()[i]).getOccupation() + "\t" + ((Designer)teamSvc.getTeam()[i]).getBonus() + "\t");
}
else if(teamSvc.getTeam()[i].getClass().equals(Architect.class)){
System.out.println(((Architect)teamSvc.getTeam()[i]).getId() + "\t" + ((Architect)teamSvc.getTeam()[i]).getMembers() + "\t" + ((Architect)teamSvc.getTeam()[i]).getName() + "\t " + ((Architect)teamSvc.getTeam()[i]).getAge() + "\t" + ((Architect)teamSvc.getTeam()[i]).getSalary() + "\t" + ((Architect)teamSvc.getTeam()[i]).getOccupation() + "\t" + ((Architect)teamSvc.getTeam()[i]).getBonus() + "\t" + ((Architect)teamSvc.getTeam()[i]).getStock());
}
}
System.out.println("-----------------------------------------------------");
}
private void addMember()throws InputMismatchException,ClassCastException {
System.out.println("---------------------添加成员---------------------");
System.out.println("请输入要员工添加的TID:");
int input = TSUtility.readInt();
boolean b = true;
while (b) {
if(TeamService.getCounter()>5){
System.out.println("员工已满");
break;
}
if (input > 12) {
System.out.println("输入错误,请重新输入!");
break;
}
for(int i=0;i<teamSvc.getTeam().length;i++) {
if (teamSvc.getTeam()[i]==listSvc.getEmployee(input)){
System.out.println("此人员已经在开发团队中");
break;
}
else{
teamSvc.addMember(listSvc.getEmployee(input));//调用添加队员的方法(调用获取指定雇工的方法)
System.out.println("添加完成");
break;
}
}
break;
}
}
//删除team数组里的指定元素
private void deleteMember() throws ArrayIndexOutOfBoundsException {
boolean b = true;
while (b) {
System.out.println("---------------------删除成员---------------------");
System.out.println("请输入要删除员工的TID: ");
int TID = TSUtility.readInt();
if (TID > 5) {//数组长度只有5
System.out.println("输入错误");
continue;
}
teamSvc.removerMember(TID);
System.out.println("删除成功");
break;
}
}
public static void main(String[] args) {
TeamView t=new TeamView();
t.enterMainMenu();
}
}
package view;
//宋老师写的工具类,作者本人也可写但是我太懒了
import java.util.*;
/**
*
* @Description 项目中提供了TSUtility.java类,可用来方便地实现键盘访问。
* @author shkstart Email:shkstart@126.com
* @version
* @date 2019年2月12日上午12:02:58
*
*/
public class TSUtility {
private static Scanner scanner = new Scanner(System.in);
/**
*
* @Description 该方法读取键盘,如果用户键入’1’-’4’中的任意字符,则方法返回。返回值为用户键入字符。
* @author shkstart
* @date 2019年2月12日上午12:03:30
* @return
*/
public static char readMenuSelection() {
char c;
for (; ; ) {
String str = readKeyBoard(1, false);
c = str.charAt(0);
if (c != '1' && c != '2' &&
c != '3' && c != '4') {
System.out.print("选择错误,请重新输入:");
} else break;
}
return c;
}
/**
*
* @Description 该方法提示并等待,直到用户按回车键后返回。
* @author shkstart
* @date 2019年2月12日上午12:03:50
*/
public static void readReturn() {
System.out.print("按回车键继续...");
readKeyBoard(100, true);
}
/**
*
* @Description 该方法从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。
* @author shkstart
* @date 2019年2月12日上午12:04:04
* @return
*/
public static int readInt() {
int n;
for (; ; ) {
String str = readKeyBoard(2, false);
try {
n = Integer.parseInt(str);
break;
} catch (NumberFormatException e) {
System.out.print("数字输入错误,请重新输入:");
}
}
return n;
}
/**
*
* @Description 从键盘读取‘Y’或’N’,并将其作为方法的返回值。
* @author shkstart
* @date 2019年2月12日上午12:04:45
* @return
*/
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.print("选择错误,请重新输入:");
}
}
return c;
}
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() < 1 || line.length() > limit) {
System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:");
continue;
}
break;
}
return line;
}
}