欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java控制臺(tái)實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)(IO版)

 更新時(shí)間:2021年04月28日 11:26:02   作者:沒(méi)事多喝水w  
這篇文章主要為大家詳細(xì)介紹了java控制臺(tái)實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)(IO版),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

使用java語(yǔ)言用本地文件存儲(chǔ)數(shù)據(jù)實(shí)現(xiàn)學(xué)生信息管理系統(tǒng),在控制臺(tái)上編譯執(zhí)行,也就是學(xué)生管理系統(tǒng)IO版

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

集合版可以參考我的另外一篇博文。

代碼如下

StudentManager

提供用戶界面

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é)生管理系統(tǒng) =========");
   System.out.println("=========請(qǐng)選擇您要進(jìn)行的操作 =========");
   System.out.println("=        1 添加學(xué)生信息                                 =");
   System.out.println("=        2 刪除學(xué)生信息                                 =");
   System.out.println("=        3 修改學(xué)生信息                                 =");
   System.out.println("=        4 查詢學(xué)生信息                                 =");
   System.out.println("=        5 退出系統(tǒng)                                         =");
   System.out.println("==================================");
   System.out.println("請(qǐng)輸入您的選擇");
   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("請(qǐng)正確輸入");
    }

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

Student類

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é)號(hào) " + stuNo + ", 姓名 " + name + ", 年齡 " + age ;
 }
 
}

IOUtil類

用于從文件中讀寫信息到集合

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;
 }
 //寫入文件信息
 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

用于執(zhí)行數(shù)據(jù)的增刪改查操作

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é)號(hào)自增
  String stuNo = null;

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

   while (true) {
    System.out.println("請(qǐng)輸入你要添加的學(xué)號(hào)為" + 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("請(qǐng)正確輸入");
     continue a;
    }
   }
   System.out.println("學(xué)號(hào)為" + stuNo + "的學(xué)生信息添加成功");
   System.out.println("添加信息后,學(xué)生的信息為:");
   showStudentInformation();
   System.out.println("是否繼續(xù)執(zhí)行添加操作(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("請(qǐng)輸出你要?jiǎng)h除的學(xué)生的學(xué)號(hào)");
    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ú)此人……請(qǐng)查證后重新輸入");
   }
  } 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("請(qǐng)輸入要修改學(xué)生的學(xué)號(hào):");
    String stuNo = sc.next();
    ListIterator<Student> lit = list.listIterator();
    while (lit.hasNext()) {
     Student stu = lit.next();
     if (stu.getStuNo().equals(stuNo)) {
      System.out.println("請(qǐng)輸入您要修改的學(xué)生的姓名");
      String name = sc.next();
      System.out.println("請(qǐng)輸入您要修改的學(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("請(qǐng)正確輸入年齡");
      }
     }
    }
    System.out.println("查無(wú)此人……請(qǐng)查證后重新輸入");
   }
  } 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é)生信息吧");
  }
 }

 // 查詢學(xué)生信息
 public void query() {
  ArrayList<Student> list = IOUtil.readStu();
  if (list.size() != 0) {
   int choose = 0;
   while (true) {
    System.out.println("請(qǐng)選擇您的查詢條件:1.查詢?nèi)? 2.學(xué)號(hào)   3.姓名  4.年齡");
    System.out.println("請(qǐng)輸入您的選擇");
    try {
     choose = sc.nextInt();
     break;
    } catch (Exception e) {
     System.out.println("請(qǐng)重新輸入選擇");
     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("查詢條件不正確,正在退出查詢……");
    break;
   }

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

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

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

  }
 }

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

    try {
     age = sc.nextInt();
     break;
    } catch (Exception e) {
     System.out.println("請(qǐng)重新輸入年齡");
     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)有此年齡的人,請(qǐng)重新查找");
   } else {
    // 遍歷集合
    for (Student student : list1) {
     System.out.println(student);
    }
    break;
   }

  }
 }

}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解Javaweb狀態(tài)管理的Session和Cookie

    詳解Javaweb狀態(tài)管理的Session和Cookie

    這篇文章主要介紹了Javaweb狀態(tài)管理的Session和Cookie,將瀏覽器與web服務(wù)器之間多次交互當(dāng)做一個(gè)整體來(lái)處理,并且多次交互所涉及的數(shù)據(jù)(狀態(tài))保存下來(lái),需要的朋友可以參考下
    2023-05-05
  • Spring Boot 與DBunit 配合使用方法

    Spring Boot 與DBunit 配合使用方法

    這篇文章主要介紹了Spring Boot 與DBunit 配合使用方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-09-09
  • Java抽象類和普通類區(qū)別、 數(shù)組跟List的區(qū)別解析

    Java抽象類和普通類區(qū)別、 數(shù)組跟List的區(qū)別解析

    這篇文章主要介紹了Java抽象類和普通類區(qū)別、 數(shù)組跟List的區(qū)別,在這里需要注意List是一個(gè)接口,不能直接實(shí)例化,需要使用具體的實(shí)現(xiàn)類來(lái)創(chuàng)建對(duì)象,本文結(jié)合示例代碼介紹的非常詳細(xì),需要的朋友參考下吧
    2023-09-09
  • Java大文件上傳詳解及實(shí)例代碼

    Java大文件上傳詳解及實(shí)例代碼

    這篇文章主要介紹了Java大文件上傳詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • Spring中的集合注入代碼實(shí)例

    Spring中的集合注入代碼實(shí)例

    這篇文章主要介紹了Spring中的集合注入代碼實(shí)例,集合注入是指在Spring框架中,通過(guò)配置文件或注解的方式將集合類型的數(shù)據(jù)注入到Bean中,集合類型包括List、Set、Map和Properties等,需要的朋友可以參考下
    2023-11-11
  • Java?循環(huán)隊(duì)列/環(huán)形隊(duì)列的實(shí)現(xiàn)流程

    Java?循環(huán)隊(duì)列/環(huán)形隊(duì)列的實(shí)現(xiàn)流程

    循環(huán)隊(duì)列又叫環(huán)形隊(duì)列,是一種特殊的隊(duì)列。循環(huán)隊(duì)列解決了隊(duì)列出隊(duì)時(shí)需要將所有數(shù)據(jù)前移一位的問(wèn)題。本文將帶大家詳細(xì)了解循環(huán)隊(duì)列如何實(shí)現(xiàn),需要的朋友可以參考一下
    2022-02-02
  • Java設(shè)計(jì)模式之抽象工廠模式簡(jiǎn)析

    Java設(shè)計(jì)模式之抽象工廠模式簡(jiǎn)析

    這篇文章主要介紹了Java設(shè)計(jì)模式之抽象工廠模式簡(jiǎn)析, 抽象工廠模式是工廠方法模式的升級(jí)版本,他用來(lái)創(chuàng)建一組相關(guān)或者相互依賴的對(duì)象,他與工廠方法模式的區(qū)別就在于,工廠方法模式針對(duì)的是一個(gè)產(chǎn)品等級(jí)結(jié)構(gòu),需要的朋友可以參考下
    2023-12-12
  • Java Lambda List轉(zhuǎn)Map代碼實(shí)例

    Java Lambda List轉(zhuǎn)Map代碼實(shí)例

    這篇文章主要介紹了Java Lambda List轉(zhuǎn)Map代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • Spring Boot 員工管理系統(tǒng)超詳細(xì)教程(源碼分享)

    Spring Boot 員工管理系統(tǒng)超詳細(xì)教程(源碼分享)

    這篇文章主要介紹了Spring Boot 員工管理系統(tǒng)超詳細(xì)教程(源碼分享),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-05-05
  • 如何基于SpringWeb?MultipartFile實(shí)現(xiàn)文件上傳、下載功能

    如何基于SpringWeb?MultipartFile實(shí)現(xiàn)文件上傳、下載功能

    在做項(xiàng)目時(shí),后端經(jīng)常采用上傳文件組件MultipartFile,下面這篇文章主要給大家介紹了關(guān)于如何基于SpringWeb?MultipartFile實(shí)現(xiàn)文件上傳、下載功能的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-07-07

最新評(píng)論