简单实现学生信息记录系统

*class PersonSave *

package com.nsg.work;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Scanner;


/**
 * 自定义导入
 * 
 * 欢迎界面
 * 增加学生:
 * 查询学生:
 * 退出
 * 
 * @author NSG
 *
 */

public class PersonSave {
	
//	信息保存路径    	读取文件的路径
	String path = "src//Information//Stu.txt";
	
//	主界面
	@SuppressWarnings("resource")
	public void mainInterface() {
		
//		欢迎界面
		System.out.println();
		System.out.println("---------------------欢迎使用本系统--------------------");
		
		Scanner scan1  = new Scanner(System.in);
		System.out.println("  请选择服务项目:");
		System.out.println("  1、添加信息");
		System.out.println("  2、查询信息");
		System.out.println("  3、退出");
		System.out.println(" 请选择所需服务的序号:");
		
		while(scan1.hasNext()) {
			
			switch(scan1.nextInt()) {
			
				 case 1:
					 pAdd();
					 break;
					 
				 case 2:
					 pQuery();
					 break;
					 
				 case 3:
					 pExit();
					 break;
				 
				 default:
					 System.out.println("-----输入有误,请重新输入!-----");
					 this.mainInterface();
			}
		}
		
	}

	
//	添加信息
	@SuppressWarnings("resource")
	private void pAdd() {
		
		System.out.println();
		System.out.println("---------------添加信息-------------------");
		Scanner scan2 = new Scanner(System.in);
		System.out.println("请输入学号:");
		int pId = scan2.nextInt();
		System.out.println("请输入姓名:");
		String pName = scan2.next();
		System.out.println("请输入 性别:(男/女)");
		String pGenger = scan2.next();
		System.out.println("请输入年龄:");
		int pAge = scan2.nextInt();
		
		System.out.println("请核对您输入的信息:");
		System.out.println("学号" + "\t" + "姓名" + "\t" + "性别" + "\t" + "年龄");
		System.out.println(pId + "\t" + pName + "\t" + pGenger + "\t" + pAge);
		System.out.println("若信息无误,请输入Y/y;若信息有误,请输入N/n,若输入其他则视为信息有误:");
		
		
		switch(scan2.next()) {
		
			case "Y":
			case "y":
				break;
				
			case "N":
			case "n":
			default:
				this.pAdd();	
		}
		
	
//		保存信息
		
//		信息保存路径
//		String path = "src//Information//Stu.txt";
		
		String content = pId + "\t" + pName + "\t" + pGenger + "\t" + pAge ;
			
		File file = new File(path);
		
		FileOutputStream fos = null;
		OutputStreamWriter osw = null;
		BufferedWriter bw = null;
		
		try {	
//		TRUE  不覆盖原文件		
			
			fos = new FileOutputStream(file,true);
			osw = new OutputStreamWriter(fos);
			bw = new BufferedWriter(osw);
			
			bw.write(content);
			bw.newLine();
			bw.flush();
			
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			
			try {
				
				if(fos != null) {
					fos.close();
				}
				
				if(osw != null) {
					osw.close();
				}
				
				if(bw != null) {						
					bw.close();
					System.out.println();					
					System.out.println("---------信息添加成功------------");
				}
				
			} catch (IOException e) {
				e.printStackTrace();
			}
	
			
		}
		
		System.out.println();
		System.out.println(" 即将返回主页面,请稍后……");
		
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		this.mainInterface();
	}


//	查询信息
	private void pQuery() {
		
//		读取文件的路径
//		String path = "src//Information//Stu.txt";
		FileReader fRead = null;
		BufferedReader br = null;
		StringBuffer sb = null;
		
		File file = new File(path);
		if(file.length() == 0) {
			System.out.println("暂无记录,请添加相关记录!");
			
			try {
				Thread.sleep(1000);
				System.out.println("正在跳转中,请稍后……");
				Thread.sleep(3000);
				this.fRun();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		}else {
			try {
				fRead = new FileReader(path);
				br = new BufferedReader(fRead);
				sb = new StringBuffer();
				String info;
				
				while((info = br.readLine()) != null) {
					info += "\n";
					sb.append(info);	
				}
	
	//			输出文件内容
	//			System.out.println(sb);
				System.out.println();
				System.out.println("信息如下:");
				System.out.println(sb.toString());
			
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}finally {
				
				try {
					
					if(fRead != null) {				
						fRead.close();
					}
					
					if(br != null) {
						br.close();
					}
					
				} catch (IOException e) {
						e.printStackTrace();
				  }	finally {
					  System.out.println("-----------------------------------");
					  System.out.println("  请选择服务项目:");
					  System.out.println("  1、添加信息");
					  System.out.println("  2、查询信息");
					  System.out.println("  3、退出");
					  System.out.println(" 请选择所需服务的序号:");
				  }
			}
		}
	}

	
//	退出系统
	private void pExit() {
		System.out.println("正在退出中,请稍后……");
		try {
			Thread.sleep(3000);
			System.out.println("-----已退出系统----");
			System.out.println("-----感谢使用本系统,期待您的下次使用-----");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}finally {
			System.exit(0);
		}	
	}
	
//	初次运行
	public void fRun() {
		this.pAdd();
	}
		
}

class Student

package com.nsg.work;

public class Student {
	
	private String name;
	private int id;
	private String genger;
	private int age;
	
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		
		if(id > 0) {
			this.id = id;
		}else {
			this.error();
		}	
	}
	
	public String getGenger() {
		return genger;
	}
	public void setGenger(String genger) {
		if(genger == "男" || genger == "女") {
			this.genger = genger;
		}else {
			this.error();
		}
		
	}
	
	public int getAge() {
			return age;
	}
	public void setAge(int age) {
		if(age >= 0) {
			this.age = age;
		}else {
			this.error();
		}
		
	}
	
//	输入有误时 
	private void error() {
		
		System.out.println("输入有误,请重新输入!");
		
		SystemRun sre = new SystemRun();
		sre.error();
		
	}

}

class SystemRun

package com.nsg.work;

import java.io.File;
import java.io.IOException;

public class SystemRun {

	public void run() {
		firstRun();
	}
	
//	初次运行系统
	private void firstRun() {
		
//		创建文件保存目录
		String dPath = "src//Information";
//		创建文件名
		String path = dPath + "\\Stu.txt";
		
//		判断文件夹是否存在
		File dFile = new File(dPath);
		if(dFile.exists() == false) {
			dFile.mkdirs();
				
	//		判断文件是否存在
			File file = new File(path);
			if(file.exists() == false ) {
				
				try {
	//				创建文件
					file.createNewFile();			
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
	//		判断文件是否为空
			if(file.length() == 0) {				
				System.out.println("由于您是第一次使用本系统,故本系统无相关记录,需要您添加相关记录!");				
				PersonSave ps = new PersonSave();
				ps.fRun();	
			}
		}
			
	}
		
//	程序输入错误	
	 public void error() {		 
		PersonSave ps = new PersonSave();
		ps.fRun();	
	 }	
}

  • class TestMain *
package com.nsg.work;

public class TestMain {
	
	public static void main(String[] args) {
		
		SystemRun sr = new SystemRun();
		sr.run();
		
		PersonSave ps = new PersonSave();
		ps.mainInterface();
		
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值