Advertisement
Guest User

Untitled

a guest
Apr 12th, 2017
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.09 KB | None | 0 0
  1. public class DataEditor extends JFrame implements ActionListener {
  2.  
  3. protected JTextField tStudent, tName, tMajor, tMinor, tGPA, tEmail, tState, tYOG, tFileName;
  4.  
  5. protected JLabel lStudent, lName, lMajor, lMinor, lGPA, lEmail, lState, lYOG, lFileName;
  6.  
  7. protected JButton bSave, bNewFile, bNext, bPrev;
  8.  
  9. protected String name, major, minor, email, state, fileName;
  10.  
  11. protected Student[] students = new Student[10];
  12.  
  13. protected int student, yog, placeHolder;
  14.  
  15. protected double gpa;
  16.  
  17.  
  18. /*
  19.  
  20. Student SSN integer
  21. Name
  22. Major
  23. Minor
  24. GPA double
  25. Email
  26. Home State
  27. Expected year of Graduation
  28.  
  29. */
  30.  
  31. public static void main(String args[ ]) throws IOException, ClassNotFoundException {
  32.  
  33. JFrame frame = new DataEditor();
  34. frame.setTitle("Data Editor");
  35. frame.setSize(300, 150);
  36. frame.setLocationRelativeTo(null);
  37. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  38. frame.setVisible(true);
  39.  
  40. }//end of main method
  41.  
  42. public DataEditor() throws ClassNotFoundException, IOException {
  43.  
  44. // Create an output stream for file temp.dat
  45. ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("registrar.dat"));
  46.  
  47.  
  48. //write Student information to the file
  49.  
  50. //Student 1
  51. output.writeObject(new Student(123456789,"Viola Bailey","Political Science","",2.6,"vbailey@smc.edu","MA",2020));
  52. output.writeObject(new Student(192837465,"John Smith","Computer Science","",3.6,"jsmith@smc.edu","MA",2020));
  53. output.writeObject(new Student(928374658,"Jackson James","Philosophy","Science",4.0,"jjames@smc.edu","VT",2020));
  54. output.writeObject(new Student(783648592,"Jane Doe","Biology","Math",3.2,"jdoe@smc.edu","LA",2018));
  55. output.writeObject(new Student(238239742,"Zach James","Political Science","",2.1,"zjames@smc.edu","MA",2021));
  56. output.writeObject(new Student(271638493,"Marvin Smith","Psychology","",3.8,"msmith@smc.edu","MA",2017));
  57. output.writeObject(new Student(123456789,"Allison Brown","Environmental Studies","",2.6,"abrown@smc.edu","MA",2020));
  58. output.writeObject(new Student(123456789,"John Lyons","Media Studies","",4.0,"jlyons@smc.edu","MA",2020));
  59. output.writeObject(new Student(123456789,"Laura Patnode","Journalism","",3.1,"lpatnode@smc.edu","CT",2019));
  60. output.writeObject(new Student(123456789,"Paul Allen","Political Science","",2.9,"pallen@smc.edu","NH",2017));
  61.  
  62.  
  63. //close output stream
  64. output.close();
  65.  
  66.  
  67. ObjectInputStream record = new ObjectInputStream(new FileInputStream("registrar.dat"));
  68.  
  69. Student student1 = (Student)(record.readObject());
  70. Student student2 = (Student)(record.readObject());
  71. Student student3 = (Student)(record.readObject());
  72. Student student4 = (Student)(record.readObject());
  73. Student student5 = (Student)(record.readObject());
  74. Student student6 = (Student)(record.readObject());
  75. Student student7 = (Student)(record.readObject());
  76. Student student8 = (Student)(record.readObject());
  77. Student student9 = (Student)(record.readObject());
  78. Student student10 = (Student)(record.readObject());
  79.  
  80. placeHolder = 0;
  81.  
  82. setLayout(new FlowLayout());
  83.  
  84. JPanel tFields = new JPanel();
  85. tFields.setLayout(new GridLayout(9,2));
  86.  
  87. // student
  88. lStudent = new JLabel("Student SSN");
  89. tStudent = new JTextField(16);
  90. tStudent.setText("");
  91. tFields.add(lStudent);
  92. tFields.add(tStudent);
  93.  
  94. // name
  95. lName = new JLabel("Student Name");
  96. tName = new JTextField(16);
  97. tName.setText("");
  98. tFields.add(lName);
  99. tFields.add(tName);
  100.  
  101. // major
  102. lMajor = new JLabel("Student Major");
  103. tMajor = new JTextField(16);
  104. tMajor.setText("");
  105. tFields.add(lMajor);
  106. tFields.add(tMajor);
  107.  
  108. // Minor
  109. lMinor = new JLabel("Student Minor");
  110. tMinor = new JTextField(16);
  111. tMinor.setText("");
  112. tFields.add(lMinor);
  113. tFields.add(tMinor);
  114.  
  115. // gpa
  116. lGPA = new JLabel("Student GPA");
  117. tGPA = new JTextField(16);
  118. tGPA.setText("");
  119. tFields.add(lGPA);
  120. tFields.add(tGPA);
  121.  
  122. // email
  123. lEmail = new JLabel("Student Email");
  124. tEmail = new JTextField(16);
  125. tEmail.setText("");
  126. tFields.add(lEmail);
  127. tFields.add(tEmail);
  128.  
  129. // state
  130. lState = new JLabel("Student Home State");
  131. tState = new JTextField(16);
  132. tState.setText("");
  133. tFields.add(lState);
  134. tFields.add(tState);
  135.  
  136. // yog
  137. lYOG = new JLabel("Student Year of Graduation");
  138. tYOG = new JTextField(16);
  139. tYOG.setText("");
  140. tFields.add(lYOG);
  141. tFields.add(tYOG);
  142.  
  143. refresh(students[placeHolder]);
  144.  
  145. // Save Changes button
  146. bSave = new JButton("Save Changes");
  147. tFields.add(bSave);
  148. bSave.addActionListener(new ActionListener() {
  149. public void actionPerformed(ActionEvent e) {
  150. switch(placeHolder){
  151. case 0:
  152. update(students[1]);
  153. break;
  154. case 1:
  155. update(students[2]);
  156. break;
  157. case 2:
  158. update(students[3]);
  159. break;
  160. case 3:
  161. update(students[4]);
  162. break;
  163. case 4:
  164. update(students[5]);
  165. break;
  166. case 5:
  167. update(students[6]);
  168. break;
  169. case 6 :
  170. update(students[7]);
  171. break;
  172. case 7:
  173. update(students[8]);
  174. break;
  175. case 8 :
  176. update(students[9]);
  177. break;
  178. case 9:
  179. update(students[10]);
  180. break;
  181. default:
  182. break;
  183. }//end switch
  184. }
  185. });
  186.  
  187. // New File button
  188. bNewFile = new JButton("New File");
  189. tFields.add(bNewFile);
  190. bNewFile.addActionListener(new ActionListener() {
  191. public void actionPerformed(ActionEvent e){
  192. File f = new File(fileName + ".dat");
  193. try {
  194. DataStream.newFile(fileName, student1, student2, student3,
  195. student4, student5, student6, student7, student8, student9, student10);
  196. }
  197. catch(IOException ioe) {
  198. tFileName.setText("Choose a new File Name");
  199. }
  200. catch(ClassNotFoundException cnfe) {
  201. tFileName.setText("Choose a new File Name");
  202. }
  203.  
  204. }
  205.  
  206. });
  207.  
  208. // Next button
  209. bNext = new JButton("Next");
  210. tFields.add(bNext);
  211. bNext.addActionListener(new ActionListener() {
  212. public void actionPerformed(ActionEvent e) {
  213. if(placeHolder < 9){
  214. nextBt();
  215. }
  216. }
  217. });
  218.  
  219. // Prev button
  220. bPrev = new JButton("Prev");
  221. tFields.add(bPrev);
  222. bPrev.addActionListener(new ActionListener() {
  223. public void actionPerformed(ActionEvent e) {
  224. if(placeHolder > 0){
  225. prevBt();
  226. }
  227.  
  228. }
  229. });
  230. }
  231.  
  232. public void actionPerformed(ActionEvent event){
  233.  
  234. }
  235.  
  236. public void update(Student s){
  237. student = Integer.parseInt(tStudent.getText());
  238. name = ""+ tName.getText();
  239. major = ""+ tMajor.getText();
  240. minor = ""+ tMinor.getText();
  241. gpa = Double.parseDouble(tGPA.getText());
  242. email = ""+ tEmail.getText();
  243. state = ""+ tState.getText();
  244. yog = Integer.parseInt(tYOG.getText());
  245.  
  246. s.setSNumber((Integer)student);
  247. s.setName(name);
  248. s.setMajor(major);
  249. s.setMinor(minor);
  250. s.setGPA((Double)gpa);
  251. s.setEmailAddress(email);
  252. s.setHomeState(state);
  253. s.setYearofGrad((Integer)yog);
  254. }
  255.  
  256. public void refresh(Student s) {
  257. tStudent.setText(""+ s.getSNumber());
  258. tName.setText(""+ s.getName());
  259. tMajor.setText(""+ s.getMajor());
  260. tMinor.setText(""+ s.getMinor());
  261. tGPA.setText(""+ s.getGPA());
  262. tEmail.setText(""+ s.getEmailAddress());
  263. tState.setText(""+ s.getHomeState());
  264. tYOG.setText(""+ s.getYearOfGrad());
  265. }
  266.  
  267. public void nextBt(){
  268. if(placeHolder < 9){
  269. placeHolder++;
  270. refresh(students[placeHolder]);
  271. }
  272. }
  273.  
  274. public void prevBt(){
  275. if(placeHolder > 0){
  276. placeHolder--;
  277. refresh(students[placeHolder]);
  278. }
  279. }
  280.  
  281.  
  282. }//end of the class
  283.  
  284. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  285.  
  286. public class DataStream{
  287.  
  288. public static void main(String[] args) throws IOException, ClassNotFoundException{
  289.  
  290.  
  291. // Create an output stream for file temp.dat
  292. ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("registrar.dat"));
  293.  
  294.  
  295. //write Student information to the file
  296.  
  297. //Student 1
  298. output.writeObject(new Student(123456789,"Viola Bailey","Political Science","",2.6,"vbailey@smc.edu","MA",2020));
  299. output.writeObject(new Student(192837465,"John Smith","Computer Science","",3.6,"jsmith@smc.edu","MA",2020));
  300. output.writeObject(new Student(928374658,"Jackson James","Philosophy","Science",4.0,"jjames@smc.edu","VT",2020));
  301. output.writeObject(new Student(783648592,"Jane Doe","Biology","Math",3.2,"jdoe@smc.edu","LA",2018));
  302. output.writeObject(new Student(238239742,"Zach James","Political Science","",2.1,"zjames@smc.edu","MA",2021));
  303. output.writeObject(new Student(271638493,"Marvin Smith","Psychology","",3.8,"msmith@smc.edu","MA",2017));
  304. output.writeObject(new Student(123456789,"Allison Brown","Environmental Studies","",2.6,"abrown@smc.edu","MA",2020));
  305. output.writeObject(new Student(123456789,"John Lyons","Media Studies","",4.0,"jlyons@smc.edu","MA",2020));
  306. output.writeObject(new Student(123456789,"Laura Patnode","Journalism","",3.1,"lpatnode@smc.edu","CT",2019));
  307. output.writeObject(new Student(123456789,"Paul Allen","Political Science","",2.9,"pallen@smc.edu","NH",2017));
  308.  
  309.  
  310. //close output stream
  311. output.close();
  312.  
  313. // Create an input stream for file temp.dat
  314. ObjectInputStream input =
  315. new ObjectInputStream(new FileInputStream("registrar.dat"));
  316.  
  317. Student student1 = (Student)(input.readObject());
  318. Student student2 = (Student)(input.readObject());
  319. Student student3 = (Student)(input.readObject());
  320. Student student4 = (Student)(input.readObject());
  321. Student student5 = (Student)(input.readObject());
  322. Student student6 = (Student)(input.readObject());
  323. Student student7 = (Student)(input.readObject());
  324. Student student8 = (Student)(input.readObject());
  325. Student student9 = (Student)(input.readObject());
  326. Student student10 = (Student)(input.readObject());
  327.  
  328. System.out.print(student1.toString());
  329.  
  330. // Read student test scores from the file
  331. input.close();
  332.  
  333. }
  334. public static void newFile(String file, Student s1, Student s2, Student s3, Student s4,
  335. Student s5, Student s6, Student s7, Student s8, Student s9, Student s10) throws IOException, ClassNotFoundException{
  336.  
  337. ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(""+ file + ".dat"));
  338.  
  339.  
  340. out.writeObject(s1);
  341. out.writeObject(s2);
  342. out.writeObject(s3);
  343. out.writeObject(s4);
  344. out.writeObject(s6);
  345. out.writeObject(s7);
  346. out.writeObject(s8);
  347. out.writeObject(s9);
  348. out.writeObject(s10);
  349. }
  350. }
  351.  
  352.  
  353. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  354.  
  355. public class Student implements java.io.Serializable {
  356.  
  357. int sNumber;
  358. String name;
  359. String major;
  360. String minor;
  361. double gpa;
  362. String emailAddress;
  363. String homeState;
  364. int yearOfGrad;
  365.  
  366. public Student(){
  367. sNumber = 111111092;
  368. name = "John Smith";
  369. major = "Computer Science";
  370. minor = "";
  371. gpa = 3.0;
  372. emailAddress = "jsmith@smc.edu";
  373. homeState = "NH";
  374. yearOfGrad = 2017;
  375. }
  376. public Student(int s, String n, String m1, String m2, double g, String e, String h, int y){
  377. sNumber = s;
  378. name = n;
  379. major = m1;
  380. minor = m2;
  381. gpa = g;
  382. emailAddress = e;
  383. homeState = h;
  384. yearOfGrad = y;
  385. }
  386. public void setSNumber(int s){
  387. sNumber = s;
  388. }
  389. public void setName(String n){
  390. name = n;
  391. }
  392. public void setMajor(String m){
  393. major = m;
  394. }
  395. public void setMinor(String m){
  396. minor = m;
  397. }
  398. public void setGPA(double g){
  399. gpa = g;
  400. }
  401. public void setEmailAddress(String e){
  402. emailAddress = e;
  403. }
  404. public void setHomeState(String h){
  405. homeState = h;
  406. }
  407. public void setYearofGrad(int y){
  408. yearOfGrad = y;
  409. }
  410. public int getSNumber(){
  411. return sNumber;
  412. }
  413. public String getName(){
  414. return name;
  415. }
  416. public String getMajor(){
  417. return major;
  418. }
  419. public String getMinor(){
  420. return minor;
  421. }
  422. public double getGPA(){
  423. return gpa;
  424. }
  425. public String getEmailAddress(){
  426. return emailAddress;
  427. }
  428. public String getHomeState(){
  429. return homeState;
  430. }
  431. public int getYearOfGrad(){
  432. return yearOfGrad;
  433. }
  434. public String toString(){
  435. return sNumber + " "
  436. + name + " "
  437. + major + " "
  438. + minor + " "
  439. + gpa + " "
  440. + emailAddress + " "
  441. + homeState + " "
  442. + yearOfGrad;
  443. }
  444.  
  445.  
  446. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement