国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

java控制臺實(shí)現學(xué)生信息管理系統(IO版)

發(fā)布時(shí)間:2021-07-17 21:51 來(lái)源:腳本之家 閱讀:0 作者:沒(méi)事多喝水w 欄目: 編程語(yǔ)言 歡迎投稿:712375056

使用java語(yǔ)言用本地文件存儲數據實(shí)現學(xué)生信息管理系統,在控制臺上編譯執行,也就是學(xué)生管理系統IO版

可以實(shí)現基本的學(xué)生信息增加、刪除、修改、查詢(xún)功能(細化了查詢(xún)功能)

集合版可以參考我的。

代碼如下

StudentManager

提供用戶(hù)界面

import java.io.IOException;
import java.util.Scanner;
public class StudentManager {
 public static void main(String[] args) throws IOException, ClassNotFoundException {
  while (true) {
   Scanner sc = new Scanner(System.in);
   System.out.println();
   System.out.println();
   System.out.println("=================================");
   System.out.println("=========歡迎使用學(xué)生管理系統 =========");
   System.out.println("=========請選擇您要進(jìn)行的操作 =========");
   System.out.println("=        1 添加學(xué)生信息                                 =");
   System.out.println("=        2 刪除學(xué)生信息                                 =");
   System.out.println("=        3 修改學(xué)生信息                                 =");
   System.out.println("=        4 查詢(xún)學(xué)生信息                                 =");
   System.out.println("=        5 退出系統                                         =");
   System.out.println("==================================");
   System.out.println("請輸入您的選擇");
   int choose;
   if (sc.hasNextInt()) {
    choose = sc.nextInt();
    if (choose > 0 && choose < 6) {
     StudentDAO std = new StudentDAO();
     switch (choose) {
     case 1:
      std.add();
      break;
     case 2:
      std.del();
      break;
     case 3:
      std.update();
      break;
     case 4:
      std.query();
      break;
     case 5:
      System.out.println("成功退出……");
      System.out.println("歡迎下次使用");
      System.exit(0);
      break;
     }
    } else {
     System.out.println("請正確輸入");
    }

   } else {
    System.out.println("請您輸入正確的選項");
   }
  }
 }
}

Student類(lèi)

public class Student implements Serializable{
 private static final long serialVersionUID = 3420928184417313845L;
 private String stuNo;
 private String name;
 private int age;
 public Student() {
  super();
  // TODO Auto-generated constructor stub
 }
 public Student(String stuNo, String name, int age) {
  super();
  this.stuNo = stuNo;
  this.name = name;
  this.age = age;
 }
 public String getStuNo() {
  return stuNo;
 }
 public void setStuNo(String stuNo) {
  this.stuNo = stuNo;
 }
 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;
 }
 @Override
 public int hashCode() {
  final int prime = 31;
  int result = 1;
  result = prime * result + age;
  result = prime * result + ((name == null) ? 0 : name.hashCode());
  result = prime * result + ((stuNo == null) ? 0 : stuNo.hashCode());
  return result;
 }
 @Override
 public boolean equals(Object obj) {
  if (this == obj)
   return true;
  if (obj == null)
   return false;
  if (getClass() != obj.getClass())
   return false;
  Student other = (Student) obj;
  if (age != other.age)
   return false;
  if (name == null) {
   if (other.name != null)
    return false;
  } else if (!name.equals(other.name))
   return false;
  if (stuNo == null) {
   if (other.stuNo != null)
    return false;
  } else if (!stuNo.equals(other.stuNo))
   return false;
  return true;
 }
 @Override
 public String toString() {
  return "學(xué)生:學(xué)號 " + stuNo + ", 姓名 " + name + ", 年齡 " + age ;
 }
 
}

IOUtil類(lèi)

用于從文件中讀寫(xiě)信息到集合

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
public class IOUtil {
 //讀取文件信息
 public static ArrayList<Student> readStu() {
  ArrayList<Student> stuList = null;
  File file = new File("test.txt");
  if (!file.exists()) {
   try {
    file.createNewFile();
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
  try (

    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));

  ) {
   stuList = (ArrayList<Student>) ois.readObject();

  } catch (Exception e) {
   e.printStackTrace();
  }
  return stuList;
 }
 //寫(xiě)入文件信息
 public static void writeStu(ArrayList<Student> stu) {
  try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("test.txt")));

  ) {
   oos.writeObject(stu);

  } catch (Exception e) {
   e.printStackTrace();
  }
 }

}

StudentDAO

用于執行數據的增刪改查操作

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Scanner;

public class StudentDAO {

 static Scanner sc = new Scanner(System.in);

 // 增加學(xué)生信息
 public void add() throws IOException, ClassNotFoundException {
  ArrayList<Student> list = IOUtil.readStu();
  // 學(xué)號自增
  String stuNo = null;

  a: while (true) {
   // 如果不存在學(xué)生則學(xué)號為1000
   if (list.size() == 0) {
    stuNo = "1000";
   } else {
    // 創(chuàng  )建新的學(xué)號,讓新的學(xué)號在列表中的最后一個(gè)學(xué)生學(xué)號的基礎上加1
    int sNo = Integer.parseInt(list.get(list.size() - 1).getStuNo()) + 1;
    // 將學(xué)號類(lèi)型轉為String類(lèi)型
    stuNo = String.valueOf(sNo);
   }
   System.out.println("請輸入你要添加的學(xué)號為" + stuNo + "的學(xué)生的姓名");
   String name = sc.next();

   while (true) {
    System.out.println("請輸入你要添加的學(xué)號為" + stuNo + "的學(xué)生的年齡");
    Scanner sc1 = new Scanner(System.in);
    if (sc1.hasNextInt()) {
     int age = sc1.nextInt();
     list.add(new Student(stuNo, name, age));
     IOUtil.writeStu(list);
     break;
    } else {
     System.out.println("請正確輸入");
     continue a;
    }
   }
   System.out.println("學(xué)號為" + stuNo + "的學(xué)生信息添加成功");
   System.out.println("添加信息后,學(xué)生的信息為:");
   showStudentInformation();
   System.out.println("是否繼續執行添加操作(y/n)");
   String result = sc.next();
   if (result.equalsIgnoreCase("n") || result.equalsIgnoreCase("y")) {
    if (result.equalsIgnoreCase("n")) {
     break;
    }
   } else {

   }

  }
 }

 // 刪除學(xué)生信息
 public void del() throws IOException, ClassNotFoundException {
  ArrayList<Student> list = IOUtil.readStu();

  if (list.size() != 0) {
   a: while (true) {
    showStudentInformation();
    System.out.println("請輸出你要刪除的學(xué)生的學(xué)號");
    String str = sc.next();
    Iterator<Student> it = list.iterator();
    while (it.hasNext()) {
     Student stu = it.next();

     if (stu.getStuNo().equals(str)) {
      it.remove();
      System.out.println("刪除成功!");
      System.out.println("刪除操作后,學(xué)生的信息為:");
      IOUtil.writeStu(list);
      showStudentInformation();
      break a;// 跳出到指定循環(huán)外
     }

    }
    System.out.println("查無(wú)此人……請查證后重新輸入");
   }
  } else {
   System.out.println("還沒(méi)有添加學(xué)生信息,快去添加學(xué)生信息吧");
  }
 }

 // 修改學(xué)生信息
 public void update() {
  ArrayList<Student> list = IOUtil.readStu();
  if (list.size() != 0) {
   a: while (true) {
    showStudentInformation();
    System.out.println("請輸入要修改學(xué)生的學(xué)號:");
    String stuNo = sc.next();
    ListIterator<Student> lit = list.listIterator();
    while (lit.hasNext()) {
     Student stu = lit.next();
     if (stu.getStuNo().equals(stuNo)) {
      System.out.println("請輸入您要修改的學(xué)生的姓名");
      String name = sc.next();
      System.out.println("請輸入您要修改的學(xué)生的年齡");
      if (sc.hasNextInt()) {
       int age = sc.nextInt();
       stu.setName(name);
       stu.setAge(age);
       System.out.println("修改操作后,學(xué)生的信息為:");
       IOUtil.writeStu(list);
       showStudentInformation();
       break a;
      } else {
       System.out.println("請正確輸入年齡");
      }
     }
    }
    System.out.println("查無(wú)此人……請查證后重新輸入");
   }
  } else {
   System.out.println("還沒(méi)有添加學(xué)生信息,快去添加學(xué)生信息吧");
  }

 }

 // 顯示學(xué)生信息
 public void showStudentInformation() {
  ArrayList<Student> list = IOUtil.readStu();
  if (list.size() != 0) {
   System.out.println("=============學(xué)生信息==============");
   for (Student stu : list) {
    System.out.println(stu);
   }
   System.out.println("=================================");
  } else {
   System.out.println("還沒(méi)有添加學(xué)生信息,快去添加學(xué)生信息吧");
  }
 }

 // 查詢(xún)學(xué)生信息
 public void query() {
  ArrayList<Student> list = IOUtil.readStu();
  if (list.size() != 0) {
   int choose = 0;
   while (true) {
    System.out.println("請選擇您的查詢(xún)條件:1.查詢(xún)全部  2.學(xué)號   3.姓名  4.年齡");
    System.out.println("請輸入您的選擇");
    try {
     choose = sc.nextInt();
     break;
    } catch (Exception e) {
     System.out.println("請重新輸入選擇");
     sc = new Scanner(System.in);
    }
   }
   switch (choose) {
   case 1:
    System.out.println("=============學(xué)生信息==============");
    for (Student stu : list) {
     System.out.println(stu);
    }
    System.out.println("=================================");
    break;
   case 2: {
    queryStuNo(list);
    break;
   }
   case 3:
    queryStuName(list);
    break;
   case 4:
    queryStuAge(list);
    break;
   default:
    System.out.println("查詢(xún)條件不正確,正在退出查詢(xún)……");
    break;
   }

  } else {
   System.out.println("還沒(méi)有添加學(xué)生信息,快去添加學(xué)生信息吧");
  }
 }

 // 按學(xué)號查詢(xún)
 public static void queryStuNo(ArrayList<Student> list) {
  a: while (true) {
   System.out.println("請輸入您要查詢(xún)的學(xué)號");
   String stuNo = sc.next();
   for (Student student : list) {
    if ((student.getStuNo()).equals(stuNo)) {
     System.out.println(student);
     break a;
    }
   }
   System.out.println("查無(wú)此人……請查證后重新輸入");
  }
 }

 // 按姓名查詢(xún)
 public static void queryStuName(ArrayList<Student> list) {
  while (true) {
   ArrayList<Student> list1 = new ArrayList<>();
   System.out.println("請輸入您要查詢(xún)的姓名");
   String stuName = sc.next();
   for (Student student : list) {
    if ((student.getName()).equals(stuName)) {
     list1.add(student);
    }
   }
   if (list1.size() == 0) {
    System.out.println("查無(wú)此人……請查證后重新輸入");
   } else {
    // 遍歷集合
    for (Student student : list1) {
     System.out.println(student);
    }
    break;
   }

  }
 }

 // 按年齡查詢(xún)
 public static void queryStuAge(ArrayList<Student> list) {
  while (true) {
   ArrayList<Student> list1 = new ArrayList<>();
   int age;
   System.out.println("請輸入您要查詢(xún)的年齡");
   while (true) {

    try {
     age = sc.nextInt();
     break;
    } catch (Exception e) {
     System.out.println("請重新輸入年齡");
     sc = new Scanner(System.in);
    }
   }
   for (Student student : list) {

    if (student.getAge() == age) {
     list1.add(student);
    }
   }
   if (list1.size() == 0) {
    System.out.println("沒(méi)有此年齡的人,請重新查找");
   } else {
    // 遍歷集合
    for (Student student : list1) {
     System.out.println(student);
    }
    break;
   }

  }
 }

}

以上就是本文的全部?jì)热?,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。

ja
亚洲性猛交xxxx| 扒开双腿疯狂进出爽爽爽| 黑人巨大精品欧美一区二区| 国产在线视频主播区| 欧美乱码伦视频免费| 久久精品国产精油按摩|