• java学生信息管理Arraylist的运用


      1 import java.io.File;
      2 import java.io.FileNotFoundException;
      3 import java.io.PrintStream;
      4 import java.util.ArrayList;
      5 import java.util.Iterator;
      6 import java.util.Scanner;
      7  
      8 public class Windzly {
      9     
     10     static ArrayList<Student> list = new ArrayList<Student>();
     11     
     12     public static void main(String[] arg) throws FileNotFoundException {
     13         Scanner sc = new Scanner(System.in);
     14         
     15         readfile();//读取文本文件
     16         
     17         
     18         int n;
     19         while(true)
     20         {
     21             System.out.println("1、添加学生    2、删除指定学号的学生    3、修改指定学号的成绩   "
     22                     + " 4、查找指定学号的信息    5、打印所有的学生信息    6、退出");
     23             n = sc.nextInt();
     24             if(n == 1)
     25             {
     26                 addstudent();//添加学生;
     27             }
     28             else if(n == 2)
     29             {
     30                 erasestudent();//删除指定学号的学生
     31                     
     32             }
     33             else if(n == 3)
     34             {
     35                 editstudent();//修改指定学号的成绩
     36             }
     37             else if(n == 4)
     38             {
     39                 findstudent();//查找学生信息
     40             }
     41             else if(n == 5)
     42             {
     43                 printstudent();//打印所有学生的信息
     44             }
     45             else if(n == 6)
     46             {
     47                 savestudent();
     48                 System.exit(0);
     49                 break;
     50             }
     51         }
     52     }
     53 
     54     public static void savestudent() throws FileNotFoundException {
     55         PrintStream put = System.out;
     56         System.setOut(new PrintStream(new File("y.txt")));
     57         Iterator it = list.iterator();
     58         while(it.hasNext())
     59         {
     60             Object object = it.next();
     61             System.out.println(object);
     62         }
     63         System.setOut(put);
     64     }
     65 
     66     public static void readfile() throws FileNotFoundException {
     67         Scanner sc = new Scanner(new File("s.txt"));
     68         while(sc.hasNext())
     69         {
     70             String s = sc.nextLine();
     71             String []ss = s.split(" ");
     72             Student student = new Student(ss[0],ss[1],ss[2]);
     73             list.add(student);
     74         }
     75     }
     76 
     77     public static void printstudent() {
     78         for(int i = 0; i < list.size(); i ++)
     79         {
     80             System.out.println(list.get(i).toString());
     81         }
     82     }
     83 
     84     public static void findstudent() {
     85         String s;
     86         Scanner sc = new Scanner(System.in);
     87         System.out.println("请输入学号");
     88         s = sc.nextLine();
     89         for(int i = 0; i < list.size(); i ++)
     90         {
     91             if(list.get(i).getNo().equals(s))
     92             {
     93                 System.out.println(list.get(i).toString());
     94             }
     95         }
     96     }
     97 
     98     public static void editstudent() {
     99         String s,sco;
    100         Scanner sc = new Scanner(System.in);;
    101         System.out.println("请输入学号");
    102         s = sc.nextLine();
    103         for(int i = 0; i < list.size(); i ++)
    104         {
    105             if(list.get(i).getNo().equals(s))
    106             {
    107                 System.out.println("请输入修改后的成绩");
    108                 sco = sc.nextLine();
    109                 list.get(i).setScore(sco);
    110             }
    111         }
    112     }
    113 
    114     public static void erasestudent() {
    115         Scanner sc = new Scanner(System.in);
    116         String s;
    117         System.out.println("请输入指定的学号");
    118         s = sc.nextLine();
    119         for(int i = 0; i < list.size(); i ++)
    120         {
    121             if(list.get(i).getNo().equals(s))
    122             {
    123                 list.remove(i);
    124             }
    125         }
    126     }
    127 
    128     public static void addstudent() {
    129         String s; 
    130         Scanner sc = new Scanner(System.in);
    131         System.out.println("输出新同学的学号,姓名,成绩");
    132         s = sc.nextLine();
    133         String [] ss = s.split(" ");
    134         Student student = new Student(ss[0],ss[1],ss[2]);
    135         list.add(student);
    136     }
    137 }
    138 
    139 class Student
    140 {
    141     String no;
    142     String name;
    143     String score;
    144     public Student() {
    145         super();
    146     }
    147     public Student(String no, String name, String score) {
    148         super();
    149         this.no = no;
    150         this.name = name;
    151         this.score = score;
    152     }
    153     @Override
    154     public String toString() {
    155         return "Student [学号:" + no + ", 姓名:" + name + ", 成绩:" + score
    156                 + "]";
    157     }
    158     @Override
    159     public int hashCode() {
    160         final int prime = 31;
    161         int result = 1;
    162         result = prime * result + ((name == null) ? 0 : name.hashCode());
    163         result = prime * result + ((no == null) ? 0 : no.hashCode());
    164         result = prime * result + ((score == null) ? 0 : score.hashCode());
    165         return result;
    166     }
    167     public String getNo() {
    168         return no;
    169     }
    170     public void setNo(String no) {
    171         this.no = no;
    172     }
    173     public String getName() {
    174         return name;
    175     }
    176     public void setName(String name) {
    177         this.name = name;
    178     }
    179     public String getScore() {
    180         return score;
    181     }
    182     public void setScore(String score) {
    183         this.score = score;
    184     }
    185     
    186 }
    View Code
  • 相关阅读:
    TAdvStringGrid控件使用技巧[转]
    char, array of char, PChar
    C#操作xml文件入门
    用C#实现生成PDF文档(附源码)
    使用DDE技术,为您的应用程序增辉
    我要减肥了
    查询一个表中相同的记录
    获得Windows特殊目录
    用VB创建快捷方式(无需第三方DLL)
    为office编写插件
  • 原文地址:https://www.cnblogs.com/WINDZLY/p/9943951.html
Copyright © 2020-2023  润新知