Chinaunix首页 | 论坛 | 博客
  • 博客访问: 314196
  • 博文数量: 69
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 759
  • 用 户 组: 普通用户
  • 注册时间: 2014-09-09 14:15
个人简介

〆 人生就是拼命地奔跑,和华丽的跌倒。 つ

文章分类

全部博文(69)

文章存档

2017年(2)

2016年(16)

2015年(21)

2014年(30)

分类: Java

2017-07-06 20:48:38

1,首先创建Student类 代码如下

点击(此处)折叠或打开

  1. package com.itheima;

  2. public class Student {
  3.     private String id;
  4.     private String name;
  5.     private String age;
  6.     private String address;
  7.     
  8.     public Student() {
  9.     }
  10.     
  11.     public Student(String id, String name, String age, String address) {
  12.         this.id = id;
  13.         this.name = name;
  14.         this.age = age;
  15.         this.address = address;
  16.     }
  17.     
  18.     
  19.     public String getId() {
  20.         return id;
  21.     }
  22.     public void setId(String id) {
  23.         this.id = id;
  24.     }
  25.     
  26.     
  27.     public String getName() {
  28.         return name;
  29.     }
  30.     public void setName(String name) {
  31.         this.name = name;
  32.     }
  33.     
  34.     
  35.     public String getAge() {
  36.         return age;
  37.     }
  38.     public void setAge(String age) {
  39.         this.age = age;
  40.     }
  41.     
  42.     
  43.     public String getAddress() {
  44.         return address;
  45.     }
  46.     public void setAddress(String address) {
  47.         this.address = address;
  48.     }
  49.     
  50.     
  51. }
测试类及方法:

点击(此处)折叠或打开

  1. package com.itheima;

  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.util.Scanner;

  9. public class StudentManager {
  10.     public static void main(String[] args) throws IOException {
  11.         String filename = "student.txt";
  12.         
  13.         while(true){
  14.             System.out.println("--------欢迎使用学生管理系统-----Students---");    
  15.             System.out.println("1查看所有学生");
  16.             System.out.println("2添加学生");
  17.             System.out.println("3删除学生");
  18.             System.out.println("4修改学生");
  19.             System.out.println("5退出");
  20.             Scanner sc = new Scanner(System.in);
  21.             String choiceString = sc.nextLine();
  22.             
  23.             switch(choiceString)
  24.             {
  25.                 case"1":
  26.                     //查看学生
  27.                     findAllStudent(filename);
  28.                     break;
  29.                 case"2":
  30.                     //添加学生
  31.                     addStudent(filename);
  32.                     break;
  33.                 case"3":
  34.                     //删除学生
  35.                     deleteStudent(filename);
  36.                     break;
  37.                 case"4":
  38.                     //修改学生
  39.                     updateStudent(filename);
  40.                     break;
  41.                 case"5":
  42.                 default:
  43.                     System.out.println("谢谢你的使用!");
  44.                     System.out.println("--------------------------------end---");
  45.                     return;
  46.             }
  47.             for (int i = 0; i < 5; i++) {
  48.                 System.out.println();
  49.             }
  50.         }
  51.     }
  52.     
  53.     public static void readData(String filename,ArrayList<Student> array) throws IOException
  54.     {
  55.         BufferedReader br = new BufferedReader(new FileReader(filename));
  56.         
  57.         String line;
  58.         while((line = br.readLine())!=null)
  59.         {
  60.             String[] str = line.split(",");
  61.             Student s = new Student();
  62.             s.setId(str[0]);
  63.             s.setName(str[1]);
  64.             s.setAge(str[2]);
  65.             s.setAddress(str[3]);
  66.             array.add(s);
  67.         }
  68.         br.close();            
  69.     }
  70.     
  71.     public static void writerData(String filename,ArrayList<Student> array) throws IOException
  72.     {
  73.         BufferedWriter bw = new BufferedWriter(new FileWriter(filename));
  74.         
  75.         for (int i = 0; i < array.size(); i++) {
  76.             StringBuilder sb = new StringBuilder();
  77.             Student s = array.get(i);
  78.             sb.append(s.getId()).append(",").append(s.getName()).append(",").append(s.getAge()).append(",").append(s.getAddress());
  79.             bw.write(sb.toString());
  80.             bw.newLine();
  81.         }
  82.         bw.close();            
  83.     }
  84.     
  85.     
  86.     public static void findAllStudent(String filename) throws IOException
  87.     {
  88.         ArrayList<Student> array = new ArrayList<Student>();
  89.         readData(filename,array);
  90.         if(array.size()==0)
  91.         {
  92.             System.out.println("不好意思!目前没有学生信息可供查询,请回去从新选择!");
  93.             return;
  94.         }
  95.         System.out.println("学号\t\t姓名\t年龄\t居住地");
  96.         for (int i = 0; i < array.size(); i++) {
  97.             Student s = array.get(i);
  98.             System.out.println(s.getId()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getAddress());
  99.         }
  100.     }
  101.     
  102.     public static void addStudent(String filename) throws IOException
  103.     {
  104.         ArrayList<Student> array = new ArrayList<Student>();
  105.         readData(filename,array);
  106.         Scanner sc = new Scanner(System.in);
  107.         String id;
  108.         while(true)
  109.         {
  110.             boolean flag = false;
  111.             System.out.println("请输入学生学号:");    
  112.             id = sc.nextLine();
  113.             for (int i = 0; i < array.size(); i++) {
  114.                 Student s = array.get(i);
  115.                 if(s.getId().equals(id))
  116.                 {
  117.                     flag = true;
  118.                     break;
  119.                 }
  120.             }
  121.             if(flag)
  122.             {
  123.                 System.out.println("您输入的学号已被占用,请重新输入:");
  124.             }else
  125.             {
  126.                 break;
  127.             }
  128.         }
  129.         System.out.println("请输入学生姓名:");
  130.         String name = sc.nextLine();
  131.         System.out.println("请输入学生年龄:");
  132.         String age = sc.nextLine();
  133.         System.out.println("请输入学生居住地:");
  134.         String address = sc.nextLine();
  135.         
  136.         Student s = new Student();
  137.         s.setId(id);
  138.         s.setName(name);
  139.         s.setAge(age);
  140.         s.setAddress(address);
  141.         
  142.         array.add(s);
  143.         writerData(filename,array);
  144.         System.out.println("添加成功!");
  145.     }
  146.     
  147.     public static void deleteStudent(String filename) throws IOException
  148.     {
  149.         ArrayList<Student> array = new ArrayList<Student>();
  150.         readData(filename,array);
  151.         Scanner sc = new Scanner(System.in);
  152.         while(true){
  153.             System.out.println("请输入要删除学生的学号:");
  154.             String id = sc.nextLine();
  155.             int index = -1;
  156.             for (int i = 0; i < array.size(); i++) {
  157.                 Student s = array.get(i);
  158.                 if(s.getId().equals(id))
  159.                 {
  160.                     index = i;
  161.                     break;
  162.                 }
  163.             }
  164.             if(index == -1)
  165.             {
  166.                 System.out.println("您输入的学号有误!请重新输入:");
  167.             }else
  168.             {
  169.                 array.remove(index);
  170.                 writerData(filename,array);
  171.                 System.out.println("删除学生成功!");
  172.                 return;
  173.             }
  174.         }
  175.     }
  176.     public static void updateStudent(String filename) throws IOException
  177.     {
  178.         ArrayList<Student> array = new ArrayList<Student>();
  179.         readData(filename,array);
  180.         Scanner sc = new Scanner(System.in);
  181.         while(true){
  182.             System.out.println("请输入你要修改的学生的学号:");
  183.             String id = sc.nextLine();
  184.             int index = -1;
  185.             for (int i = 0; i < array.size(); i++) {
  186.                 Student s = array.get(i);
  187.                 if(s.getId().equals(id))
  188.                 {
  189.                     index = i;
  190.                     break;
  191.                 }
  192.             }
  193.             if(index == -1)            
  194.             {
  195.                 System.out.println("您输入的学生学号不存在,请重新输入");
  196.             }else{
  197.                 System.out.println("请输入学生新姓名:");
  198.                 String name = sc.nextLine();
  199.                 System.out.println("请输入学生新年龄:");
  200.                 String age = sc.nextLine();
  201.                 System.out.println("请输入学生新居住地:");
  202.                 String address = sc.nextLine();
  203.                 Student s = new Student();
  204.                 
  205.                 s.setId(id);
  206.                 s.setName(name);
  207.                 s.setAge(age);
  208.                 s.setAddress(address);
  209.                 
  210.                 array.set(index, s);
  211.                 writerData(filename,array);
  212.                 System.out.println("修改成功");
  213.                 return;
  214.             }
  215.         }
  216.     }
  217. }

阅读(2668) | 评论(0) | 转发(0) |
0

上一篇:java_io流

下一篇:没有了

给主人留下些什么吧!~~