要求:
从键盘输入以下数据:“Tom:89.1|Jerry:90.1|qiangqiang:59.9”,数据格式为“姓名:成绩|姓名:成绩|姓名:成绩”,对输入的内容按成绩由高到低排序。
1、设置一个文件的处理类,该类制作只是提供有数据的增加和读取。
1 package cn.mldn.demo.util; 2 import java.io.File; 3 import java.io.FileOutputStream; 4 import java.io.PrintStream; 5 import java.util.Scanner; 6 public class FileUtil { 7 public static String load(File file) { 8 Scanner scan = null; 9 try { 10 scan = new Scanner(file) ; // 文件加载 11 if (scan.hasNext()) { 12 String str = scan.next(); // 获取数据 13 return str ; 14 } 15 return null ; 16 } catch (Exception e) { 17 return null ; 18 } finally { 19 if (scan != null) { 20 scan.close(); 21 } 22 } 23 } 24 public static boolean append(File file,String content) { 25 PrintStream out = null ; 26 try { 27 out = new PrintStream(new FileOutputStream(file,true)) ; 28 out.print(content); // 内容追加 29 return true ; 30 } catch (Exception e) { 31 return false ; 32 } finally { 33 if (out != null) { 34 out.close(); 35 } 36 } 37 } 38 }
2、扩充IStudentService操作方法
1 package com.sohu.demo.service; 2 import com.sohu.demo.vo.Student; 3 public interface IStudentService { 4 public Student[] getData(); 5 }
3、修改StudentServiceImpl中的功能
1 package com.sohu.demo.service.impl; 2 import java.util.Arrays; 3 import com.sohu.demo.service.IStudentService; 4 import com.sohu.demo.vo.Student; 5 6 public class StudentServiceImpl implements IStudentService { 7 private String content; 8 private Student[] students;//这是什么意思? 9 public StudentServiceImpl(String content) { 10 this.content = content; 11 this.handle(); 12 } 13 public void handle() { 14 String result[] = this.content.split("\\|"); 15 this.students = new Student[result.length]; 16 for(int x=0;x<result.length;x++) { 17 String temp[] = result[x].split(":"); 18 this.students[x] = new Student(temp[0], Double.parseDouble(temp[1])); 19 } 20 } 21 @Override 22 public Student[] getData() { 23 Arrays.sort(this.students); 24 return this.students; 25 } 26 }
4、此时工厂类不再需要输入数据
1 package com.sohu.demo.factory; 2 3 import com.sohu.demo.service.IStudentService; 4 import com.sohu.demo.service.impl.StudentServiceImpl; 5 import cn.com.sina.demo.util.InputUtil; 6 7 public class Factory { 8 private Factory() {} 9 public static IStudentService getInstance() { 10 return new StudentServiceImpl(InputUtil.getString("请输入数据信息:")); 11 } 12 }
5、定义一个菜单处理
1 package cn.mldn.demo.menu; 2 import java.util.Arrays; 3 import cn.mldn.demo.factory.Factory; 4 import cn.mldn.demo.service.IStudentService; 5 import cn.mldn.demo.util.InputUtil; 6 public class Menu { 7 public Menu() { 8 this.choose(); 9 } 10 public void choose() { 11 this.show() ; 12 String choose = InputUtil.getString("请进行选择:") ; 13 switch(choose) { 14 case "1" : { // 接收输入数据 15 String str = InputUtil.getString("请输入要追加的数据:") ; 16 IStudentService studentService = Factory.getInstance() ; 17 studentService.append(str); // 追加数据 18 choose() ; // 重复出现 19 } 20 case "2" : { // 显示数据 21 IStudentService studentService = Factory.getInstance() ; 22 System.out.println(Arrays.toString(studentService.getData())); 23 choose() ; // 重复出现 24 } 25 case "0" : { 26 System.out.println("下次再见,拜拜!"); 27 System.exit(1); 28 } 29 default : { 30 System.out.println("您输入了非法的选项,无法进行处理,请确认后再次执行程序!"); 31 choose() ; 32 } 33 } 34 } 35 public void show() { 36 System.out.println("【1】追加字符串数据\n"); 37 System.out.println("【2】显示所有的学生数据\n"); 38 System.out.println("【0】结束程序执行。"); 39 System.out.println("\n\n\n"); 40 } 41 }
6、编写测试类
1 package cn.mldn.demo; 2 import cn.mldn.demo.menu.Menu; 3 public class IOCaseDemo { 4 public static void main(String[] args) { 5 new Menu(); 6 } 7 }
运行结果:
[姓名:Jerry、成绩:90.1, 姓名:Tom、成绩:89.1, 姓名:qiangqiang、成绩:59.9]
那么问题来了,第三步StudentServiceImpl类中 private Student[] students;这是个什么意思?