Commit a99da721 authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch16] Initial code from book

parent b82a8634
Loading
Loading
Loading
Loading
+15 −0
Changes for src/main/java/ch16/QuizCard.java: 15 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch16;

public class QuizCard {
  public QuizCard(String text, String text1) {

  }

  public int getQuestion() {
    return 0;
  }

  public int getAnswer() {
    return 0;
  }
}
+111 −11
Changes for src/main/java/ch16/QuizCardBuilder.java: 111 added lines, 11 removed lines.
Original line number Diff line number Diff line
package ch16;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.*;
import java.util.ArrayList;

public class QuizCardBuilder {

  private JTextArea question;
  private JTextArea answer;
  private ArrayList<QuizCard> cardList;
  private JFrame frame;


  public static void main (String[] args) {
    QuizCardBuilder builder = new QuizCardBuilder();
    builder.go();
  }


  public void go() {
        // build and display gui
    // build gui

    frame = new JFrame("Quiz Card Builder");
    JPanel mainPanel = new JPanel();
    Font bigFont = new Font("sanserif", Font.BOLD, 24);
    question = new JTextArea(6,20);
    question.setLineWrap(true);
    question.setWrapStyleWord(true);
    question.setFont(bigFont);

    JScrollPane qScroller = new JScrollPane(question);
    qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

    answer = new JTextArea(6,20);
    answer.setLineWrap(true);
    answer.setWrapStyleWord(true);
    answer.setFont(bigFont);

    JScrollPane aScroller = new JScrollPane(answer);
    aScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    aScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);


    JButton nextButton = new JButton("Next Card");

    cardList = new ArrayList<QuizCard>();

    JLabel qLabel = new JLabel("Question:");
    JLabel aLabel = new JLabel("Answer:");

    mainPanel.add(qLabel);
    mainPanel.add(qScroller);
    mainPanel.add(aLabel);
    mainPanel.add(aScroller);
    mainPanel.add(nextButton);
    nextButton.addActionListener(new NextCardListener());
    JMenuBar menuBar = new JMenuBar();
    JMenu fileMenu = new JMenu("File");
    JMenuItem newMenuItem = new JMenuItem("New");

    JMenuItem saveMenuItem = new JMenuItem("Save");
    newMenuItem.addActionListener(new NewMenuListener());

    saveMenuItem.addActionListener(new SaveMenuListener());
    fileMenu.add(newMenuItem);
    fileMenu.add(saveMenuItem);
    menuBar.add(fileMenu);
    frame.setJMenuBar(menuBar);
    frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
    frame.setSize(500,600);
    frame.setVisible(true);
  }

   private class NextCardListener implements ActionListener {
  public class NextCardListener implements ActionListener {
    public void actionPerformed(ActionEvent ev) {
          // add the current card to the list and clear the text areas 

      QuizCard card = new QuizCard(question.getText(), answer.getText());
      cardList.add(card);
      clearCard();
    }
  }
     private class SaveMenuListener implements ActionListener {

  public class SaveMenuListener implements ActionListener {
    public void actionPerformed(ActionEvent ev) {
           // bring up a file dialog box
           // let the user name and save the set
      QuizCard card = new QuizCard(question.getText(), answer.getText());
      cardList.add(card);

      JFileChooser fileSave = new JFileChooser();
      fileSave.showSaveDialog(frame);
      saveFile(fileSave.getSelectedFile());
    }
  }
   private class NewMenuListener implements ActionListener {

  public class NewMenuListener implements ActionListener {
    public void actionPerformed(ActionEvent ev) {
           // clear out the card list, and clear out the text areas        
      cardList.clear();
      clearCard();
    }
  }

  private void clearCard() {
    question.setText("");
    answer.setText("");
    question.requestFocus();
  }

  private void saveFile(File file) {
        // iterate through the list of cards, and write each one out to a text file
       // in a parseable way (in other words, with clear separations between parts)
    try {
      BufferedWriter writer = new BufferedWriter(new FileWriter(file));

      for(QuizCard card:cardList) {
        writer.write(card.getQuestion() + "/");
        writer.write(card.getAnswer() + "\n");
      }
      writer.close();

    } catch(IOException ex) {
      System.out.println("couldn’t write the cardList out");
      ex.printStackTrace();
    }
  }
}
       
           
          
          
       
 No newline at end of file
+39 −0
Changes for src/main/java/ch16/QuizCardPlayer.java: 39 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch16;

import java.awt.event.*;
import java.io.File;

public class QuizCardPlayer {
     
    public void go() {
        // build and display gui
    }

    class NextCardListener implements ActionListener {
       public void actionPerformed(ActionEvent ev) {
          // if this is a question, show the answer, otherwise show next question
          // set a flag for whether we’re viewing a question or answer
        }
     }

     class OpenMenuListener implements ActionListener {
        public void actionPerformed(ActionEvent ev) {
           // bring up a file dialog box
           // let the user navigate to and choose a card set to open
         }
     }
    
    private void loadFile(File file) {
         // must build an ArrayList of cards, by reading them from a text file
        // called from the OpenMenuListener event handler, reads the file one line at a time
        // and tells the makeCard() method to make a new card out of the line
       // (one line in the file holds both the question and answer, separated by a "/")
    } 


      private void makeCard(String lineToParse) {
         // called by the loadFile method, takes a line from the text file
         // and parses into two pieces—question and answer—and creates a new QuizCard
         // and adds it to the ArrayList called CardList
      } 
}
 No newline at end of file
+29 −0
Changes for src/main/java/ch16/ReadAFile.java: 29 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch16;

import java.io.*;

class ReadAFile {
    public static void main (String[] args) {

       try {
          File myFile = new File("MyText.txt");
          FileReader fileReader = new FileReader(myFile);

          BufferedReader reader = new BufferedReader(fileReader);





          String line = null;

          while ((line = reader.readLine()) != null) {
             System.out.println(line);
          }
          reader.close();  

      } catch(Exception ex) {
           ex.printStackTrace();
      }
   }
}
 No newline at end of file