java控制台做一个简单的信息管理系统!(不使用数据库,用list集合操作) 文本文件存储信息,增删查改。
写一个简单的学生信息管理系统需求如下:1、不用做UI界面,直接控制台录入数据和输出数据2、实现学生信息的增、删、改、查3、学生信息录入字段有:id,name,gender...
写一个简单的学生信息管理系统
需求如下:
1、不用做UI界面,直接控制台录入数据和输出数据
2、实现学生信息的增、删、改、查
3、学生信息录入字段有:id,name,gender(性别),enterYear(入学年份),major(专业),interest(兴趣)
4、修改条件:根据学生的ID修改学生信息
5、删除条件:根据学生的ID删除
6、查询条件:根据学生的姓名查询 展开
需求如下:
1、不用做UI界面,直接控制台录入数据和输出数据
2、实现学生信息的增、删、改、查
3、学生信息录入字段有:id,name,gender(性别),enterYear(入学年份),major(专业),interest(兴趣)
4、修改条件:根据学生的ID修改学生信息
5、删除条件:根据学生的ID删除
6、查询条件:根据学生的姓名查询 展开
展开全部
保存在list中了,同时提供写入文件的功能,弱连接db的话,更简单~~望满意~~
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class StudentDeal {
static List<Student> list = null;
public static void main(String[] args) throws IOException {
Date date = new Date(1990, 12, 12, 23, 23);
Student s = new Student(111, "gao", "femal", date, "art", "chess");
StudentDeal sd = new StudentDeal();
sd.add(s);
String name = "gao";
sd.query(name);
sd.update(111, "pig");// 更新id的记录为pig,并写回到文件中;可以修改或者重载modify方法;
sd.delete(111);// 删除id的记录,并写回到文件中;;
}
public StudentDeal() {
list = new ArrayList<Student>();
}
private void query(String name) {
boolean f = false;
for (Student t : list) {
if (t.getName().equals(name)) {
f = true;
}
}
if (f)
System.out.println("yes");
else
System.out.println("no");
}
void add(Student t) throws IOException {
int id = t.getId();
for (Student tmp : list) {
if (tmp.getId() == id) {
// System.out.println("target exsit!");
return;
}
}
list.add(t);
writeToFile(list);
System.out.println("add succeed!");
}
private void delete(int id) throws IOException {
for (int i = 0; i < list.size(); i++) {
Student t = list.get(i);
if (t.getId() == id) {
list.remove(i);
System.out.println("delete succeed!");
} else
System.out.println("delete no such id!");
}
writeToFile(list);
}
private void update(int id, String name) throws IOException {
for (Student t : list) {
if (t.getId() == id) {
t.setName(name);
System.out.println("modify info succeed!");
} else
System.out.println("modify no such id!");
}
writeToFile(list);
}
public List<Student> getList() {
return list;
}
private void writeToFile(List<Student> list2) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter(new File(
"e:/test.txt")));
for (Student t : list) {
bw.write(t.toString());
}
bw.close();
}
}
class Student {
int id;
String intrest;
String gender;
String name;
String major;
Date date;
public Student(int id, String name, String gender, Date date, String major,
String intrest) {
super();
this.id = id;
this.intrest = intrest;
this.name = name;
this.gender = gender;
this.date = date;
this.major = major;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getIntrest() {
return intrest;
}
public void setIntrest(String intrest) {
this.intrest = intrest;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public String toString() {
return this.getId() + " " + this.getName() + " " + this.getGender()
+ " " + this.getDate() + " " + this.getMajor() + " "
+ this.intrest + "\n";
}
}
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class StudentDeal {
static List<Student> list = null;
public static void main(String[] args) throws IOException {
Date date = new Date(1990, 12, 12, 23, 23);
Student s = new Student(111, "gao", "femal", date, "art", "chess");
StudentDeal sd = new StudentDeal();
sd.add(s);
String name = "gao";
sd.query(name);
sd.update(111, "pig");// 更新id的记录为pig,并写回到文件中;可以修改或者重载modify方法;
sd.delete(111);// 删除id的记录,并写回到文件中;;
}
public StudentDeal() {
list = new ArrayList<Student>();
}
private void query(String name) {
boolean f = false;
for (Student t : list) {
if (t.getName().equals(name)) {
f = true;
}
}
if (f)
System.out.println("yes");
else
System.out.println("no");
}
void add(Student t) throws IOException {
int id = t.getId();
for (Student tmp : list) {
if (tmp.getId() == id) {
// System.out.println("target exsit!");
return;
}
}
list.add(t);
writeToFile(list);
System.out.println("add succeed!");
}
private void delete(int id) throws IOException {
for (int i = 0; i < list.size(); i++) {
Student t = list.get(i);
if (t.getId() == id) {
list.remove(i);
System.out.println("delete succeed!");
} else
System.out.println("delete no such id!");
}
writeToFile(list);
}
private void update(int id, String name) throws IOException {
for (Student t : list) {
if (t.getId() == id) {
t.setName(name);
System.out.println("modify info succeed!");
} else
System.out.println("modify no such id!");
}
writeToFile(list);
}
public List<Student> getList() {
return list;
}
private void writeToFile(List<Student> list2) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter(new File(
"e:/test.txt")));
for (Student t : list) {
bw.write(t.toString());
}
bw.close();
}
}
class Student {
int id;
String intrest;
String gender;
String name;
String major;
Date date;
public Student(int id, String name, String gender, Date date, String major,
String intrest) {
super();
this.id = id;
this.intrest = intrest;
this.name = name;
this.gender = gender;
this.date = date;
this.major = major;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getIntrest() {
return intrest;
}
public void setIntrest(String intrest) {
this.intrest = intrest;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public String toString() {
return this.getId() + " " + this.getName() + " " + this.getGender()
+ " " + this.getDate() + " " + this.getMajor() + " "
+ this.intrest + "\n";
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询