62,614
社区成员
发帖
与我相关
我的任务
分享
package andycpp;
import java.util.ArrayList;
public class MyShell {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyShell shell = new MyShell();
shell.run();
}
public void run() {
Data d = new Data();
Key k = new Key();
int i = 1;
while(true) {
switch(i) {
case 1:
i = k.run(num);
break;
case 2:
i = d.run(num);
break;
case 3:
System.out.println("欢迎使用本系统,再见!!");
return;
}
}
}
private ArrayList<Integer> num = new ArrayList<Integer>();
}
package andycpp;
import java.io.*;
import java.util.Collections;
import java.util.List;
public class Key {
public int run(List<Integer> l){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = null;
while(true) {
System.out.print("Key> ");
try {
s = br.readLine();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(s.trim().toLowerCase().equals("display")) {
doDisplay(l);
continue;
}
if(s.trim().toLowerCase().equals("sort")) {
doSort(l);
continue;
}
if(s.trim().toLowerCase().startsWith("find")) {
doFind(l, s);
continue;
}
if(s.trim().toLowerCase().equals("enter"))
return 2;
if(s.trim().toLowerCase().equals("quit"))
return 3;
System.out.println("错误的命令!!!");
}
}
//处理命令display
private void doDisplay(List<Integer> l){
if(l.isEmpty()) {
System.out.println("目前尚未存储任何数据!");
}else {
for(Integer i:l) {
System.out.print(i+" ");
}
System.out.println();
}
}
//处理sort命令
private void doSort(List<Integer> l) {
if(l.isEmpty()) {
System.out.println("目前尚未存储任何数据,无法排序!");
}else {
System.out.println("所有数据已经按照升序排列!!!");
Collections.sort(l);
}
}
//处理find命令
private void doFind(List<Integer> l, String s) {
String[] ss = s.split("\\s");
if(!ss[0].equals("find")) {
System.out.println("错误的命令!!!");
return;
}
int i = 0;
try {
i = Integer.parseInt(ss[1]);
}catch(NumberFormatException e) {
System.out.println("错误的命令!!!");
return;
}
if(l.isEmpty()) {
System.out.println("目前尚未存储任何数据,无法查找!");
}else {
int index = l.indexOf(new Integer(i));
if(index == -1)
System.out.println("未找到数字 "+i);
else
System.out.println("数字 "+i+" 位于第"+index+"位");
}
}
}
package andycpp;
import java.io.*;
import java.util.List;
public class Data {
public int run(List<Integer> l){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = null;
int i;
l.clear();
while(true) {
System.out.print("Data> ");
try {
s = br.readLine();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try{
l.add(Integer.valueOf(s.trim()));
}catch(NumberFormatException e){
return 1;
}
}
}
}