Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.92 KB | None | 0 0
  1. package p03;
  2. //***************************************************************************************************************************
  3. // CLASS: View
  4. //
  5. // AUTHOR
  6. // Your author information
  7. //***************************************************************************************************************************
  8.  
  9.  
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import javax.swing.BoxLayout;
  13. import javax.swing.JButton;
  14. import javax.swing.JFrame;
  15. import javax.swing.JLabel;
  16. import javax.swing.JOptionPane;
  17. import javax.swing.JPanel;
  18. import javax.swing.JTextField;
  19. import java.util.ArrayList;
  20.  
  21.  
  22.  
  23. /**
  24. * The View class implements the GUI.
  25. */
  26. public class View extends JFrame implements ActionListener {
  27.  
  28. public static final int FRAME_WIDTH = 500;
  29. public static final int FRAME_HEIGHT = 250;
  30.  
  31. // Declare instance variables
  32. private JButton mClearButton;
  33. private JTextField[] mExamText;
  34. private JButton mExitButton;
  35. private JTextField[] mHomeworkText;
  36. private Main mMain;
  37. private JButton mSaveButton;
  38. private JButton mSearchButton;
  39. private JTextField mSearchText;
  40. private Student mStudent;
  41.  
  42. /**
  43. * View()
  44. *
  45. * The View constructor creates the GUI interface and makes the frame visible at the end.
  46. */
  47. public View(Main pMain) {
  48.  
  49. // Save a reference to the Main object pMain in mMain.
  50. mMain = pMain;
  51.  
  52. // PSEUDOCODE:
  53. // Create a JPanel named panelSearch which uses the FlowLayout.
  54. // Add a JLabel "Student Name: " to panelSearch
  55. // Create mSearchText and make the field 25 cols wide
  56. // Add mSearchText to the panel
  57. // Create mSearchButton
  58. // Make this View the action listener for the button
  59. // Add the button to the panel
  60. JPanel panelSearch = new JPanel();
  61. JLabel studentName = new JLabel("Student Name:");
  62. panelSearch.add(studentName);
  63. mSearchText = new JTextField(25);
  64. panelSearch.add(mSearchText);
  65. JButton mSearchButton = new JButton("Search");
  66. mSearchButton.addActionListener(this);
  67. panelSearch.add(mSearchButton);
  68.  
  69.  
  70. // PSEUDOCODE:
  71. // Create a JPanel named panelHomework which uses the FlowLayout.
  72. // Add a JLabel "Homework: " to the panel
  73. // Create mHomeworkText which is an array of CourseConstants.NUM_HOMEWORKS JTextFields
  74. // For i = 0 to CourseConstants.NUM_HOMEWORKS - 1 Do
  75. // Create textfield mHomeworkText[i] displaying 5 cols
  76. // Add mHomeworkText[i] to the panel
  77. // End For
  78. JPanel panelHomeWork = new JPanel();
  79. JLabel homeWork = new JLabel("Homework:");
  80. JTextField[] mHomeworkText = new JTextField[CourseConstants.NUM_HOMEWORKS];
  81. for(int i = 0; i <= CourseConstants.NUM_HOMEWORKS - 1; i++){
  82. mHomeworkText[i] = new JTextField(null, 5);
  83. panelHomeWork.add(mHomeworkText[i]);
  84. }
  85.  
  86.  
  87. // Create the exam panel which contains the "Exam: " label and the two exam text fields. The pseudocode is omitted
  88. // because this code is very similar to the code that creates the panelHomework panel.
  89. JPanel panelExam = new JPanel();
  90. JLabel exam = new JLabel("Exam:");
  91. JTextField[] mExamText = new JTextField[CourseConstants.NUM_EXAMS];
  92. for(int i = 0; i <= CourseConstants.NUM_EXAMS - 1; i++){
  93. mExamText[i] = new JTextField(null, 5);
  94. panelExam.add(mExamText[i]);
  95. }
  96.  
  97.  
  98. // PSEUDOCODE:
  99. // Create a JPanel named panelButtons using FlowLayout.
  100. // Create the Clear button mClearButton.
  101. // Make this View the action listener for mClearButton.
  102. // Add the Clear button to the panel.
  103. // Repeat the three above statements for the Save button.
  104. // Repeat the three above statements for the Exit button.
  105. JPanel panelButtons = new JPanel();
  106. JButton mClearButton = new JButton("Clear");
  107. mClearButton.addActionListener(this);
  108. panelButtons.add(mClearButton);
  109. JButton mSaveButton = new JButton("Save");
  110. mSaveButton.addActionListener(this);
  111. panelButtons.add(mSaveButton);
  112. JButton mExitButton = new JButton("Exit");
  113. mExitButton.addActionListener(this);
  114. panelButtons.add(mExitButton);
  115.  
  116. // PSEUDOCODE:
  117. // Create a JPanel named panelMain using a vertical BoxLayout.
  118. // Add panelSearch to panelMain.
  119. // Add panelHomework to panelMain.
  120. // Add panelExam to panelMain.
  121. // Add panelButtons to panelMain.
  122. JPanel panelMain = new JPanel();
  123. panelMain.setLayout(new BoxLayout(panelMain, BoxLayout.Y_AXIS));
  124. //panelMain.add(Box.createVerticalGlue());
  125. panelMain.add(panelSearch);
  126. //panelMain.add(Box.createVerticalGlue());
  127. panelMain.add(panelHomeWork);
  128. //panelMain.add(Box.createVerticalGlue());
  129. panelMain.add(panelExam);
  130. //panelMain.add(Box.createVerticalGlue());
  131. panelMain.add(panelButtons);
  132.  
  133. // Initialize the remainder of the frame, add the main panel to the frame, and make the frame visible.
  134. setTitle("Gradebookulator");
  135. setSize(FRAME_WIDTH, FRAME_HEIGHT);
  136. setResizable(false);
  137. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  138. add(panelMain);
  139. setVisible(true);
  140. }
  141.  
  142. /**
  143. * actionPerformed()
  144. *
  145. * Called when one of the JButtons is clicked. Detects which button was clicked and handles it.
  146. *
  147. * PSEUDOCOODE:
  148. * If the source of the event was the Search button Then
  149. * lastName = retrieve the text from the mSearchText text field.
  150. * If lastName is the empty string Then
  151. * Call messageBox() to display "Please enter the student's last name."
  152. * Else
  153. * student = Call mMain.search(lastName)
  154. * If student is null Then
  155. * Call messageBox() to display "Student not found. Try again."
  156. * Else
  157. * Call displayStudent(student)
  158. * End if
  159. * End If
  160. * Else if the source of the event was the Save button Then
  161. * If mStudent is not null Then Call saveStudent(mStudent)
  162. * Else if the source of the event was the Clear button Then
  163. * Call clear()
  164. * Else if the source of the event was the Exit button Then
  165. * If mStudent is not null Then Call saveStudent(mStudent)
  166. * Call mMain.exit() to terminate the application
  167. * End If
  168. */
  169. @Override
  170. public void actionPerformed(ActionEvent pEvent){
  171. String lastname = mSearchText.getText();
  172.  
  173. if (pEvent.getSource() == mSearchButton){
  174. if (lastname.equals("")){
  175. messageBox("Please enter a student's last name.");
  176. }
  177. else{
  178. mStudent = mMain.search(lastname);
  179. if(mStudent == null){
  180. messageBox("Student was not found. Try again.");
  181. }
  182. else{
  183. displayStudent(mStudent);
  184. }
  185. }
  186. }
  187.  
  188. else if (pEvent.getSource() == mSaveButton){
  189. mStudent = mMain.search(lastname);
  190. if (mStudent!= null){
  191. saveStudent(mStudent);
  192. }
  193. }
  194. else if (pEvent.getSource() == mClearButton){
  195. clear();
  196. }
  197. else if (pEvent.getSource() == mExitButton){
  198. mStudent = mMain.search(lastname);
  199. if (mStudent != null){
  200. saveStudent(mStudent);
  201. mMain.exit();
  202. }
  203. }
  204. }
  205. /*@Override
  206. public void actionPerformed(ActionEvent pEvent) {
  207.  
  208.  
  209. if(pEvent.getSource() == mSearchButton){
  210. String lastName = mSearchText.getText();
  211.  
  212.  
  213. if(lastName.compareTo("") == 0)
  214. messageBox("Please enter the student's last name.");
  215. else{
  216. mStudent = mMain.search(lastName);
  217. if(mStudent == null)
  218. messageBox("Student not found. Try again.");
  219. else {
  220.  
  221. System.out.println(mStudent);
  222. displayStudent(mStudent);
  223. }
  224.  
  225. }
  226. }
  227.  
  228. else if(pEvent.getSource() == mSaveButton) {
  229. if(mStudent != null)
  230. saveStudent(mStudent);
  231. }
  232. else if(pEvent.getSource() == mClearButton) {
  233. clear();
  234. }
  235. else if(pEvent.getSource() == mExitButton) {
  236. if(mStudent != null){
  237. saveStudent(mStudent);
  238. mMain.exit();
  239. }
  240. else
  241. mMain.exit();
  242. }
  243. }
  244. */
  245.  
  246.  
  247.  
  248. /**
  249. * clear()
  250. *
  251. * Called when the Clear button is clicked. Clears all of the text fields by setting the contents to the empty string.
  252. * After clear() returns, no student information is being edited or displayed.
  253. *
  254. * PSEUDOCODE:
  255. * Set the mSearchText text field to ""
  256. * Set each of the homework text fields to ""
  257. * Set each of the exam text fields to ""
  258. * Set the mStudent reference to null
  259. */
  260. private void clear(){
  261. mSearchText.setText("");
  262. for (int i = 0; i <= CourseConstants.NUM_HOMEWORKS-1; i++){
  263. mHomeworkText[i].setText("");
  264. }
  265. for (int i = 0; i <= CourseConstants.NUM_EXAMS-1; i++){
  266. mExamText[i].setText("");
  267. }
  268. mStudent = null;
  269.  
  270. }
  271.  
  272. /**
  273. * displayStudent()
  274. *
  275. * Displays the homework and exam scores for a student in the mHomeworkText and mExamText text fields.
  276. *
  277. * PSEUDOCODE:
  278. * For i = 0 to CourseConstants.NUM_HOMEWORKS - 1 Do
  279. * int hw = pStudent.getHomework(i)
  280. * String hwstr = convert hw to a String (Hint: Integer.toString())
  281. * mHomeworkText[i].setText(hwstr)
  282. * End For
  283. * Write another for loop similar to the one above to place the exams scores into the text fields
  284. */
  285. private void displayStudent(Student pStudent){
  286. for (int i = 0; i <= CourseConstants.NUM_HOMEWORKS-1; i++){
  287. int hw = pStudent.getHomework(i);
  288. String hwstr = Integer.toString(hw);
  289. mHomeworkText[i].setText(hwstr);
  290. }
  291. for (int i = 0; i <= CourseConstants.NUM_EXAMS-1; i++){
  292. int ex = pStudent.getExam(i);
  293. String exstr = Integer.toString(ex);
  294. mExamText[i].setText(exstr);
  295. }
  296. }
  297.  
  298. /**
  299. * messageBox()
  300. *
  301. * Displays a message box containing some text.
  302. */
  303. public void messageBox(String pMessage) {
  304. JOptionPane.showMessageDialog(this, pMessage, "Message", JOptionPane.PLAIN_MESSAGE);
  305. }
  306.  
  307. /**
  308. * saveStudent()
  309. *
  310. * Retrieves the homework and exam scores for pStudent from the text fields and writes the results to the Student record
  311. * in the Roster.
  312. *
  313. * PSEUDOCODE:
  314. * For i = 0 to CourseConstants.NUM_HOMEWORKS - 1 Do
  315. * String hwstr = mHomeworkText[i].getText()
  316. * int hw = convert hwstr to an int (Hint: Integer.parseInt())
  317. * Call pStudent.setHomework(i, hw)
  318. * End For
  319. * Write another for loop similar to the one above to save the exam scores
  320. */
  321. private void saveStudent(Student pStudent)
  322. {
  323. for(int i = 0;i<=CourseConstants.NUM_HOMEWORKS - 1; i++)
  324. {
  325. String hwstr = mHomeworkText[i].getText();
  326. int hw = Integer.parseInt(hwstr);
  327. pStudent.setHomework(i, hw);
  328.  
  329. }
  330.  
  331. for(int i = 0;i<=CourseConstants.NUM_EXAMS - 1; i++)
  332. {
  333. String exmstr = mExamText[i].getText();
  334. int exm = Integer.parseInt(exmstr);
  335. pStudent.setExam(i, exm);
  336.  
  337. }
  338. }
  339.  
  340. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement