用java编写学生信息管理系统,只需有添加,删除,修改,查询,输出等功能即 ...
发布网友
发布时间:2022-04-30 05:06
我来回答
共3个回答
懂视网
时间:2022-05-01 10:35
很久前写的一个程序了。拿出来存一下,不是为了展示啥,自己用的时候还可以看看。写的很粗糙。
1 import java.io.BufferedReader;
2 import java.io.File;
3 import java.io.FileNotFoundException;
4 import java.io.FileReader;
5 import java.io.IOException;
6 import java.util.Comparator;
7 import java.util.HashMap;
8 import java.util.HashSet;
9 import java.util.Iterator;
10 import java.util.Map;
11 import java.util.Scanner;
12 import java.util.Set;
13 import java.util.TreeSet;
14
15
16 public class studen implements Comparable<studen>{
17 public int id;
18 public String name;
19 public char sex;
20 public String address;
21 public int javascore;
22 public int cscore;
23 public int byscore;
24 public int j2eescore;
25 public int getId() {
26 return id;
27 }
28 public void setId(int id) {
29 this.id = id;
30 }
31 public String getName() {
32 return name;
33 }
34 public void setName(String name) {
35 this.name = name;
36 }
37 public char getSex() {
38 return sex;
39 }
40 public void setSex(char sex) {
41 this.sex = sex;
42 }
43 public String getAddress() {
44 return address;
45 }
46 public void setAddress(String address) {
47 this.address = address;
48 }
49 public int getJavascore() {
50 return javascore;
51 }
52 public void setJavascore(int javascore) {
53 this.javascore = javascore;
54 }
55 public int getCscore() {
56 return cscore;
57 }
58 public void setCscore(int cscore) {
59 this.cscore = cscore;
60 }
61 public int getByscore() {
62 return byscore;
63 }
64 public void setByscore(int byscore) {
65 this.byscore = byscore;
66 }
67 public int getJ2eescore() {
68 return j2eescore;
69 }
70 public void setJ2eescore(int j2eescore) {
71 this.j2eescore = j2eescore;
72 }
73 public void setall(int id, String name,char sex,String address,int javascore,int cscore, int byscore,int j2eescore)
74 {
75 this.id=id;
76 this.address=address;
77 this.name=name;
78 this.byscore=byscore;
79 this.cscore=cscore;
80 this.j2eescore=j2eescore;
81 this.sex=sex;
82 this.javascore=javascore;
83 }
84
85 @Override
86 public String toString() {
87 return "studen [id=" + id + ", name=" + name + ", sex=" + sex
88 + ", address=" + address + ", javascore=" + javascore
89 + ", cscore=" + cscore + ", byscore=" + byscore
90 + ", j2eescore=" + j2eescore + "]";
91 }
92
93 public static void main(String[] args) {
94 // TODO Auto-generated method stub
95 Set<studen> A=new HashSet<studen>();
96 ////
97
98 /////
99 File file = new File("infor.txt");
100 if(file.exists()&&file.isFile())
101 {
102 try {
103 FileReader read=new FileReader("infor.txt");
104 BufferedReader re=new BufferedReader (read);
105 String str;
106 while((str=re.readLine())!=null)
107 {
108 //System.out.println(str);
109
110 String arr[]=str.split(" ");
111 studen st=new studen();
112 st.setId(Integer.parseInt(arr[0]));
113 st.setName(arr[1]);
114 st.setSex(arr[2].charAt(0));
115 st.setAddress(arr[3]);
116 st.setJavascore(Integer.parseInt(arr[4]));
117 st.setJavascore(Integer.parseInt(arr[5]));
118 st.setByscore(Integer.parseInt(arr[6]));
119 st.setJ2eescore(Integer.parseInt(arr[7]));
120 System.out.println(st.toString());
121 A.add(st);
122
123 //System.out.println();
124 }
125
126 for(Iterator<studen> it=A.iterator();it.hasNext();)
127 {
128 String ss=it.next().toString();
129 System.out.println("HashSet1***:"+ss+" ");
130 }
131 } catch (FileNotFoundException e) {
132 // TODO Auto-generated catch block
133 System.out.print("can not read the txt");
134 e.printStackTrace();
135 } catch (IOException e) {
136 // TODO Auto-generated catch block
137 e.printStackTrace();
138 }
139
140 }
141 //插入一个新的student对象给集合SET
142 System.out.print("插入一个学生 2, GOQI, f,beijing, 90, 89, 78,94:");
143 studen stu2=new studen();
144 stu2.setall(2, "GOQI", ‘f‘,"beijing", 90, 89, 78,94);
145 A.add(stu2);
146 TreeSet<studen> ts=new TreeSet<studen>();
147 for(Iterator<studen> it=A.iterator();it.hasNext();)
148 {
149 studen cad=it.next();
150 ts.add(cad);
151 }
152 for(Iterator<studen> it2=ts.iterator();it2.hasNext();)
153 {
154 System.out.println("Treeset(按ID排序结果):"+it2.next().toString());
155 }
156 //serach student
157 System.out.println("输入待查学生姓名:");
158 Scanner cin=new Scanner(System.in);
159 if(cin.hasNext())
160 {
161 String name=cin.next();
162 studen s1=new studen();
163
164 ///?
165 int flage=0;
166 studen Temp;
167 for(Iterator<studen> it=A.iterator();it.hasNext();)
168 {
169 Temp = it.next();
170 if(Temp .getName().equals(name))
171 {
172 flage=1;
173 System.out.println("exist this student.");
174 System.out.println("该生信息为:"+Temp.toString());
175 break;
176 }
177 else
178 {
179 ;
180 }
181 }
182 if(flage==0)
183 {
184 System.out.println("Not exist this student.");
185 }
186 }
187 //删除学生
188
189 System.out.println("输入待删除学生姓名:");
190 Scanner cin2=new Scanner(System.in);
191 if(cin2.hasNext())
192 {
193 String name2=cin2.next();
194
195 int flage=0;
196 studen Temp;
197 for(Iterator<studen> it=A.iterator();it.hasNext();)
198 {
199 Temp = it.next();
200 if(Temp .getName().equals(name2))
201 {
202 flage=1;
203 it.remove();
204 System.out.println("removed this student.");
205
206 break;
207 }
208
209 }
210 if(flage==0)
211 {
212 System.out.println("Not exist this student so we can not remove it.");
213 }
214 }
215 //map
216 Map<Integer,TreeSet<studen>> map=new HashMap<Integer, TreeSet<studen>>();
217 TreeSet<studen> tm0=new TreeSet<studen>();
218 TreeSet<studen> tm1=new TreeSet<studen>();
219 TreeSet<studen> tm2=new TreeSet<studen>();
220 TreeSet<studen> tm3=new TreeSet<studen>();
221 TreeSet<studen> tm4=new TreeSet<studen>();
222 Iterator<studen> treeit=ts.iterator();
223
224 studen sno=new studen();
225 //将学生信息按照%5ID对应treeset 依次放入。
226 while(treeit.hasNext())
227 {
228 sno=(studen) treeit.next();
229 if(sno.getId()%5==0)
230 {
231 tm0.add(sno);
232 }
233 else if(sno.getId()%5==1)
234 {
235 tm1.add(sno);
236 }
237 else if(sno.getId()%5==2)
238 {
239 tm2.add(sno);
240 }
241 else if(sno.getId()%5==3)
242 {
243 tm3.add(sno);
244 }
245 else
246 {
247 tm4.add(sno);
248 }
249 }
250
251 map.put(0,tm0);
252 map.put(1,tm1);
253 map.put(2,tm2);
254 map.put(3,tm3);
255 map.put(4,tm4);
256 ////print map
257 System.out.println("map id:0~4(%5):");
258 for(Object o : map.keySet()){
259
260 System.out.println( "map输出:"+map.get(o).size()+"个:"+map.get(o).toString()); }
261 }//main
262
263
264
265 @Override
266 public int hashCode() {
267 final int prime = 31;
268 int result = 1;
269 result = prime * result + id;
270 return result;
271 }
272 @Override
273 public boolean equals(Object obj) {
274 if (this == obj)
275 return true;
276 if (obj == null)
277 return false;
278 if (getClass() != obj.getClass())
279 return false;
280 studen other = (studen) obj;
281 if (id != other.id)
282 return false;
283 System.out.println("!!!id 重复无法插入,重复ID为:"+id);
284 return true;
285 }
286 @Override
287 public int compareTo(studen o) {
288 // TODO Auto-generated method stub
289 int result=this.getId()>o.getId()?1:
290 (this.getId()==o.getId()?0:-1);
291 return result;
292 }
293
294 }
也是泛型的应用
Java学生信息增删改查(并没用数据库)
标签:
热心网友
时间:2022-05-01 07:43
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
class Student implements java.io.Serializable{
String number,name,specialty,grade,borth,sex;
public Student(){};
public void setNumber(String number){ this.number=number;}
public String getNumber(){ return number;}
public void setName(String name){ this.name=name;}
public String getName(){ return name;}
public void setSex(String sex){ this.sex=sex;}
public String getSex(){ return sex;}
public void setSpecialty(String specialty){ this.specialty=specialty;}
public String getSpecialty(){ return specialty;}
public void setGrade(String grade){ this.grade=grade;}
public String getGrade(){ return grade;}
public void setBorth(String borth){ this.borth=borth;}
public String getBorth(){ return borth;}
}
public class StudentManager extends JFrame{
JLabel lb=new JLabel("录入请先输入记录,查询、删除请先输入学号,修改是对查询" +
"内容改后的保存!");
static JTextField Jnumber,Jname,Jspecialty,Jgrade,Jborth;
static JRadioButton boy,girl;
static ButtonGroup group=null;
static JButton Login,select,delete,update,show;
static JPanel p1,p2,p3,p4,p5,p6,pv,ph;
static Student student=null;
static Hashtable Shashtable=null;
static File file=null;
static FileInputStream inOne=null;
static ObjectInputStream inTwo=null;
static FileOutputStream outOne=null;
static ObjectOutputStream outTwo=null;
public StudentManager(){
super("学生基本信息管理系统");
Jnumber=new JTextField(10);
Jname =new JTextField(10);
Jspecialty=new JTextField(10);
Jgrade=new JTextField(10);
Jborth=new JTextField(10);
group=new ButtonGroup();
boy=new JRadioButton("男",true);
girl=new JRadioButton("女",false);
group.add(boy);
group.add(girl);
Login=new JButton("录入");
select=new JButton("查询");
delete=new JButton("删除");
update=new JButton("修改");
show=new JButton("显示");
Login.addActionListener(new InputAct());
select.addActionListener(new InquestAct());
update.addActionListener(new ModifyAct());
delete.addActionListener(new DeleteAct());
show.addActionListener(new ShowAct());
update.setEnabled(false);
p1=new JPanel();
p1.add(new JLabel("学号:",JLabel.CENTER));
p1.add(Jnumber);
p2=new JPanel();
p2.add(new JLabel("姓名:",JLabel.CENTER));
p2.add(Jname);
p3=new JPanel();
p3.add(new JLabel("性别:",JLabel.CENTER));
p3.add(boy);
p3.add(girl);
p4=new JPanel();
p4.add(new JLabel("专业:",JLabel.CENTER));
p4.add(Jspecialty);
p5=new JPanel();
p5.add(new JLabel("年级:",JLabel.CENTER));
p5.add(Jgrade);
p6=new JPanel();
p6.add(new JLabel("出生:",JLabel.CENTER));
p6.add(Jborth);
pv=new JPanel();
pv.setLayout(new GridLayout(6,1));
pv.add(p1);
pv.add(p2);
pv.add(p3);
pv.add(p4);
pv.add(p5);
pv.add(p6);
ph=new JPanel();
ph.add(Login);
ph.add(select);
ph.add(update);
ph.add(delete);
ph.add(show);
file=new File("学生信息.txt");
Shashtable=new Hashtable();
if(!file.exists()){
try{
FileOutputStream out=new FileOutputStream(file);
ObjectOutputStream objectOut=new ObjectOutputStream(out);
objectOut.writeObject(Shashtable);
objectOut.close();
out.close();
}
catch(IOException e){}
}
Container con=getContentPane();
con.setLayout(new BorderLayout());
con.add(lb, BorderLayout.NORTH);
con.add(pv, BorderLayout.CENTER);
con.add(ph, BorderLayout.SOUTH);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(100,100,600,300);
setVisible(true);
}
public static void main(String[] args) {new StudentManager();}
class InputAct implements ActionListener{
public void actionPerformed(ActionEvent e){
update.setEnabled(false);
String number="";
number=Jnumber.getText();
if(number.length()>0){
try{
inOne=new FileInputStream(file);
inTwo=new ObjectInputStream(inOne);
Shashtable=(Hashtable)inTwo.readObject();
inOne.close();
inTwo.close();
}
catch(Exception ee){System.out.println("创建散列表出现问题!");}
if(Shashtable.containsKey(number)){
String warning="该生信息已存在,请到修改页面修改!";
JOptionPane.showMessageDialog(null,warning,"警告",
JOptionPane.WARNING_MESSAGE);
}//end if1
else{
String m="该生信息将被录入!";
int ok=JOptionPane.showConfirmDialog(null,m,"确认",
JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE);
if(ok==JOptionPane.YES_OPTION){
String name=Jname.getText();
String specialty=Jspecialty.getText();
String grade=Jgrade.getText();
String borth=Jborth.getText();
String sex=null;
if(boy.isSelected()){sex=boy.getText();}
else{sex=girl.getText();}
student=new Student();
student.setNumber(number);
student.setName(name);
student.setSpecialty(specialty);
student.setGrade(grade);
student.setBorth(borth);
student.setSex(sex);
try{
outOne=new FileOutputStream(file);
outTwo=new ObjectOutputStream(outOne);
Shashtable.put(number,student);
outTwo.writeObject(Shashtable);
outTwo.close();
outOne.close();
}
catch(Exception ee){System.out.println("输出散列表出现问题!");}
Jnumber.setText(null);
Jname.setText(null);
Jspecialty.setText(null);
Jgrade.setText(null);
Jborth.setText(null);
}
}//end else1
}//end if0
else{
String warning="必须输入学号!";
JOptionPane.showMessageDialog(null,warning,
"警告",JOptionPane.WARNING_MESSAGE);
}//end else0
}//end actionPerformed
}//end class
class InquestAct implements ActionListener{
public void actionPerformed(ActionEvent e){
String number="";
number=Jnumber.getText();
if(number.length()>0){
try{
inOne=new FileInputStream(file);
inTwo=new ObjectInputStream(inOne);
Shashtable=(Hashtable)inTwo.readObject();
inOne.close();
inTwo.close();
}
catch(Exception ee){System.out.println("散列表有问题!");}
if(Shashtable.containsKey(number)){
update.setEnabled(true);
Student stu=(Student)Shashtable.get(number);
Jname.setText(stu.getName());
Jspecialty.setText(stu.getSpecialty());
Jgrade.setText(stu.getGrade());
Jborth.setText(stu.getBorth());
if(stu.getSex().equals("男")){boy.setSelected(true);}
else{girl.setSelected(true);}
}
else{
update.setEnabled(false);
String warning="该学号不存在!";
JOptionPane.showMessageDialog(null,warning,
"警告",JOptionPane.WARNING_MESSAGE);
}
}
else{
update.setEnabled(false);
String warning="必须输入学号!";
JOptionPane.showMessageDialog(null,warning,
"警告",JOptionPane.WARNING_MESSAGE);
}
}
}
class ModifyAct implements ActionListener{
public void actionPerformed(ActionEvent e){
String number=Jnumber.getText();
String name=Jname.getText();
String specialty=Jspecialty.getText();
String grade=Jgrade.getText();
String borth=Jborth.getText();
String sex=null;
if(boy.isSelected()){sex=boy.getText();}
else{sex=girl.getText();}
Student 学生=new Student();
学生.setNumber(number);
学生.setName(name);
学生.setSpecialty(specialty);
学生.setGrade(grade);
学生.setBorth(borth);
学生.setSex(sex);
try{
outOne=new FileOutputStream(file);
outTwo=new ObjectOutputStream(outOne);
Shashtable.put(number, 学生);
outTwo.writeObject(Shashtable);
outTwo.close();
outOne.close();
Jnumber.setText(null);
Jname.setText(null);
Jspecialty.setText(null);
Jgrade.setText(null);
Jborth.setText(null);
}
catch(Exception ee){
System.out.println("录入修改出现异常!");
update.setEnabled(false);
}
}
}
class DeleteAct implements ActionListener{
public void actionPerformed(ActionEvent e){
update.setEnabled(false);
String number=Jnumber.getText();
if(number.length()>0){
try{
inOne=new FileInputStream(file);
inTwo=new ObjectInputStream(inOne);
Shashtable=(Hashtable)inTwo.readObject();
inOne.close();
inTwo.close();
}
catch(Exception ee){}
if(Shashtable.containsKey(number)){
Student stu=(Student)Shashtable.get(number);
Jname.setText(stu.getName());
Jspecialty.setText(stu.getSpecialty());
Jgrade.setText(stu.getGrade());
Jborth.setText(stu.getBorth());
if(stu.getSex().equals("男")){boy.setSelected(true);}
else{girl.setSelected(true);}
}
String m="确定要删除该学生的记录吗?";
int ok=JOptionPane.showConfirmDialog(null,m,"确认",
JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
if(ok==JOptionPane.YES_OPTION){
Shashtable.remove(number);
try{
outOne=new FileOutputStream(file);
outTwo=new ObjectOutputStream(outOne);
outTwo.writeObject(Shashtable);
outTwo.close();
outOne.close();
Jnumber.setText(null);
Jname.setText(null);
Jspecialty.setText(null);
Jgrade.setText(null);
Jborth.setText(null);
}
catch(Exception ee){System.out.println(ee);}
}
else if(ok==JOptionPane.NO_OPTION){
Jnumber.setText(null);
Jname.setText(null);
Jspecialty.setText(null);
Jgrade.setText(null);
Jborth.setText(null);
}
else{
String warning="该学号不存在!";
JOptionPane.showMessageDialog(null,warning,
"警告",JOptionPane.WARNING_MESSAGE);
}
}
else{
String warning="必须输入学号!";
JOptionPane.showMessageDialog(null,warning,
"警告",JOptionPane.WARNING_MESSAGE);
}
}
}
class ShowAct implements ActionListener{
public void actionPerformed(ActionEvent e){
new StudentShow(file);
}
}
class StudentShow extends JDialog{
Hashtable 学生散列表= null;
JTextArea 显示=null;
FileInputStream inOne=null;
ObjectInputStream inTwo=null;
File file=null;
public StudentShow(File file){
super(new JFrame(),"显示对话框");
this.file=file;
显示=new JTextArea(16,30);
try{
inOne=new FileInputStream(file);
inTwo=new ObjectInputStream(inOne);
学生散列表=(Hashtable)inTwo.readObject();
inOne.close();
inTwo.close();
}
catch(Exception ee){}
if(学生散列表.isEmpty())显示.append("目前还没有学生的信息记录!\n");
else{
显示.setText("学号 姓名 性别 专业 年级 出生\n");
for(Enumeration enm=学生散列表.elements();enm.hasMoreElements();){
Student stu=(Student)enm.nextElement();
String sex="";
if(stu.getSex().equals("男"))sex="男";
else sex="女";
String str=stu.getNumber()+","+stu.getName()+","+sex+","
+stu.getSpecialty()+","+stu.getGrade()+","+stu.getBorth()+"\n";
显示.append(str);
}
}
JScrollPane scroll=new JScrollPane(显示);
Container con=getContentPane();
con.add("Center",scroll);
con.validate();
setVisible(true);
setBounds(200,200,400,300);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){setVisible(false);}
}
);
}
}
}
热心网友
时间:2022-05-01 09:01
可以通过Bai Hi提示我
有时间可能完成你所面临的任务
相关的要求也可能提示我
ES:\\5F97D3CBAC6B9B2690D1D8E74380C342
交易提醒:预付定金是诈骗追问你理解错了,是添加时就是按有序添加进入程序中,你编写过有序链表吗?就是跟有序插入的!