12 Replies - 554 Views - Last Post: 04 March 2012 - 04:39 PM Rate Topic: -----

#1 itsjimmy91  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 77
  • Joined: 19-January 11

Saving a Program?

Posted 02 March 2012 - 08:02 PM

I have a simple program that allows a client to insert a bunch of lottery numbers and then check to see if the winning number is located within their list of numbers. As it stands now, somebody using my program would have to enter all of their numbers (think a group of people buying a lot of tickets and splitting the costs/winnings) into the program each and every time they opened it, which kind of defeats the whole purpose.

My question is, how do I make it so that once the client enters their numbers into the program, they can save the state of the program and pull it up at anytime and check a winning lottery number against the numbers they entered? I realize that I could write the program so that when it opens up it has all of their numbers in it, but I don't really want to do that as I want the client to be able to set it up, add and remove numbers at anytime.

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Saving a Program?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Saving a Program?

Posted 02 March 2012 - 08:09 PM

I wrote that once at the office when we had over 200 numbers, I do not even remember why :)
Enjoy

package loto;
import java.util.*;

/*
 * A class to sort BitSet
 */
public class BitSetComp implements Comparator<BitSet> {

	
	public int compare(BitSet bs1, BitSet bs2) {
		int first1 = 0;
		int first2 = 0;
		
		do {
			first1 = bs1.nextSetBit(first1);
			first2 = bs2.nextSetBit(first2);
			if(first1 == -1)
				break;
			if(first2 == -1)
				break;
			++first1;
			++first2;
		} while(first1 == first2);
		
		
		if(first1 == -1 && first2 == -1)
			return 0;
		if(first1 == -1) 
			return -1;
		if(first2 == -1)
			return +1;
		return first1 - first2;
	}
	
	public static void main(String[] args) {
		Random ran = new Random();
		BitSetComp comp = new BitSetComp();
		BitSet[] bs = new BitSet[10];
		for(int i = 0; i < bs.length; ++i) {
			bs[i] = new BitSet(50);
			for(int j = 0; j < 15; ++j)
				bs[i].set(ran.nextInt(50));
		}
		
		Arrays.sort(bs, comp);
		for(int i = 0; i < bs.length; ++i)
			System.out.println(bs[i]);
	}
}


package loto;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import java.io.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
/*
 * Display the user combinations
 */
public class DisplayCombin extends JPanel implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// number of user seection per draw
	private int nbToSelect;
	// maximum possible value for a number
	private int maxValue;
	private JTable table;
	private Model model;
	private JLabel[] winnerLabel;
	// the 49 is presented 3 rows at a time
	private boolean is49;
	private JButton clear, clearLast, sort, read;
	private BitSetComp bitSetComp;

	// to save every combin
	private ArrayList<BitSet> bitArray;
	private BitSet winnerBitSet;
	private int extra;

	// build a new one with an initial bitArray
	DisplayCombin(int nbToSelect, int maxValue) {
		super(new BorderLayout());
		this.nbToSelect = nbToSelect;
		this.maxValue = maxValue;
		is49 = maxValue == 49;
		bitSetComp = new BitSetComp();
		// build the ArrayList
		File file = new File("loto.dat");
		if(file.exists()) {
			try {
				FileInputStream fis = new FileInputStream("loto.dat");
				ObjectInputStream ois = new ObjectInputStream(fis);
				bitArray = (ArrayList<BitSet>) ois.readObject();
				ois.close();				
			}
			catch(Exception e) {
				System.out.println("Readin file error: " + e);
				bitArray = new ArrayList<BitSet>();
			}
		}
		else {
			bitArray = new ArrayList<BitSet>();
		}
		model = new Model();
		table = new MyTable(model);
		add(new JScrollPane(table), BorderLayout.CENTER);	

		// The button to clear the list
		clear = new JButton("Clear the list");
		clear.addActionListener(this);
		clearLast = new JButton("Clear last");
		clearLast.addActionListener(this);
		sort = new JButton("Sort");
		sort.addActionListener(this);
		read = new JButton("Read loto.txt");
		read.addActionListener(this);
		JPanel p = new JPanel();
		p.add(clearLast);
		p.add(sort);
		p.add(clear);
		p.add(read);
		add(p, BorderLayout.NORTH);
		// no clear button if list ius empty
		if(bitArray.size() == 0) {
			clear.setEnabled(false);
			clearLast.setEnabled(false);
			sort.setEnabled(false);
		}
	}

	// add another user combination
	void addCombin(BitSet bitSet) {
		// clone the BitSet to save it
		BitSet set = (BitSet) bitSet.clone();
		int oldSize = bitArray.size();
		bitArray.add(set);
		model.fireTableRowsInserted(oldSize, oldSize);
		// sure that the clear buttons will become available
		clear.setEnabled(true);
		clearLast.setEnabled(true);
		sort.setEnabled(true);
		try {
			FileOutputStream fos = new FileOutputStream("loto.dat");
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(bitArray);
			oos.close();
		}
		catch(Exception e) {
			System.out.println("Saving file error: " + e);
		}
	}

	// display the winner
	void displayWinner(BitSet bitSet, int extra) {
		winnerBitSet = bitSet;
		this.extra = extra;
		winnerLabel = new JLabel[bitArray.size()];
		for(int i = 0; i < bitArray.size(); ++i) {
			BitSet player = bitArray.get(i);
			BitSet clone = (BitSet) player.clone();
			clone.and(bitSet);
			int nb = clone.cardinality();
			if(nb < 3) {
				winnerLabel[i] = new JLabel("");
				continue;
			}
			String str = " " + nb;
			if(player.get(extra))
				str = str + "+";
			winnerLabel[i] = new JLabel(str);
		}
		model.fireTableDataChanged();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == sort) {
			Collections.sort(bitArray, bitSetComp);
			if(winnerBitSet != null)
				displayWinner(winnerBitSet, extra);
			model.fireTableDataChanged();
			return;
		}
		if(e.getSource() == clearLast) {
			int size = bitArray.size();
			bitArray.remove(--size);
			if(size == 0) {
				clear.setEnabled(false);
				clearLast.setEnabled(false);
				sort.setEnabled(false);
			}
			model.fireTableDataChanged();
			return;
		}
		if(e.getSource() == read) {
			try {
				Scanner scan = new Scanner(new File("loto.txt"));
				while(scan.hasNextLine()) {
					String line = scan.nextLine().trim();
					if(line.length() == 0)
						break;
					String[] token = line.split(" ");
					if(token.length != Loto.NB_TO_SELECT) 
						System.out.println("Error line: " + line + " does not have " + Loto.NB_TO_SELECT + " numbers.");
					BitSet bs = new BitSet();
					for(int i = 0; i < token.length; i++) {
						bs.set(Integer.parseInt(token[i]));
					}
					addCombin(bs);
				}
				scan.close();
			}
			catch(IOException io) {
				System.out.println("Exception reading data file: " + e);
			}
			return;

		}
		// for the clear all we confirm with the user
		int answer = JOptionPane.showConfirmDialog(this, "\n   Are you sure you want to clear the   \n" 
				+ bitArray.size() +
		"    registered user's combinations   \n");
		if(answer == JOptionPane.YES_OPTION) {
			bitArray.clear();
			model.fireTableDataChanged();			
		}	
	}

	/*
	 * We extends JTable just to have getTableCellRenderer
	 */
	class MyTable extends JTable implements TableCellRenderer {

		private static final long serialVersionUID = 1L;

		MyTable(Model model) {
			super(model);
		}

		// this method tell the JTable drawer that it has to call me back to provide
		// the JComponent that will be used to draw the cell
		public TableCellRenderer getCellRenderer(int row, int col) {
			// Call me back for all rows/columns
			return this;
		}

		@Override
		public Component getTableCellRendererComponent(JTable table,
				Object value, boolean isSelected, boolean hasFocus, int row,
				int column) {
			return (Component) value;
		}

	}
	class Model extends AbstractTableModel {

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		JLabel label;
		Color[] color;
		int modulo;

		Model() {
			label = new JLabel();
			label.setHorizontalAlignment(SwingConstants.CENTER);
			label.setOpaque(true);
			// if 49 we present by 3
			if(is49) {
				modulo = 6;
				color = new Color[6];
				color[0] = color[1] = color[2] = Loto.lightBlue;
				color[3] = color[4] = color[5] = Loto.lightYellow;
			}
			else {
				modulo = 2;
				color = new Color[2];
				color[0] = Loto.lightBlue;
				color[1] = Loto.lightYellow;
			}
		}
		@Override
		public int getRowCount() {
			return bitArray.size();
		}

		@Override
		public int getColumnCount() {
			return nbToSelect + 2;
		}

		public String getColumnName(int col) {
			if(col < nbToSelect)
				return "" + (col+1);
			if(col == nbToSelect)
				return "";
			return "Win";
		}
		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			label.setText("");
			// the blank column
			if(columnIndex == nbToSelect) {
				label.setBackground(Color.WHITE);
				return label;
			}
			label.setForeground(Color.BLACK);
			label.setBackground(color[rowIndex % modulo]);
			if(columnIndex > nbToSelect) {
				if(winnerLabel == null)
					return label;
				return winnerLabel[rowIndex];
			}
			// the BitSet of that row
			BitSet bitSet = bitArray.get(rowIndex);
			int nbFound = -1;
			// check each bit starting by 1
			for(int i = 1; i <= maxValue; ++i) {
				// if that number is set
				if(bitSet.get(i)) {
					++nbFound;
					if(nbFound == columnIndex) {
						label.setText("" + i);
						if(winnerLabel != null) {
							if(winnerBitSet.get(i)) 
								label.setForeground(Color.BLUE);
							else if(extra == i)
								label.setForeground(Color.GREEN);
						}
						break;
					}
				}
			}

			return label;
		}

	}
}


package loto;

import java.util.*;
import java.awt.*;
import javax.swing.*;

public class Loto extends JFrame {

	private static final long serialVersionUID = 1L;
	static final int MAX_VALUE = 49, NB_TO_SELECT = 7;
	public static final Color lightBlue = new Color(220, 220, 255), lightYellow = new Color(255, 255, 200);

	private DisplayCombin displayCombin;
	
	Loto() {
		super("Loto");

		displayCombin = new DisplayCombin(NB_TO_SELECT, MAX_VALUE);
		add(displayCombin);
		add(new SelectPanel(this, NB_TO_SELECT, MAX_VALUE), BorderLayout.SOUTH);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setBounds(20, 20, 417, 600);
		setVisible(true);
	}
	
	// a new Combination to add
	void addCombin(BitSet bitSet) {
		displayCombin.addCombin(bitSet);
	}
	// to show the winner
	void displayWinner(BitSet bitSet, int extra) {
		displayCombin.displayWinner(bitSet, extra);
	}
	
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new Loto();
			}
		});
	}
}


package loto;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.BitSet;
/*
 * To Enter the loto numbers held by the user
 */
public class SelectPanel extends JPanel implements ActionListener {

	// the label displaying the numbers
	private LotoLabel[] lotoLabel;
	// number of numbers to select for a combination
	private int nbToSelect;
	// number of numbers selected
	private int nbSelected;
	// the two Buttons
	private JButton add, reset, winner;
	// my father for the call back
	private Loto father;
	// the BitSet of the selected number
	private BitSet bitSet;
	// the label for Extra
	private JLabel extra;
	// if we enter extra
	private boolean enterExtra, done;

	SelectPanel(Loto loto, int nbToSelect, int maxValue) {
		super(new BorderLayout());
		father = loto;
		this.nbToSelect = nbToSelect;
		bitSet = new BitSet(maxValue + 1);
		// number of rows 
		int nbRow = maxValue / 10 + 1;
		// the JPanel displaying the possible combination
		JPanel p = new JPanel(new GridLayout(nbRow, 10, 1, 1));
		p.setBackground(Color.BLUE);
		p.setBorder(BorderFactory.createLineBorder(Color.BLUE));
		lotoLabel = new LotoLabel[maxValue+1];
		for(int i = 0; i <= maxValue; ++i) {
			lotoLabel[i] = new LotoLabel(i);
			p.add(lotoLabel[i]);
		}
		add(p, BorderLayout.CENTER);

		// the 3 buttons
		add = new JButton("Add");
		add.addActionListener(this);
		add.setEnabled(false);
		reset = new JButton("Reset");
		reset.addActionListener(this);
		winner = new JButton("Winner");
		winner.addActionListener(this);
		winner.setEnabled(false);

		// add the 2 buttons
		JPanel south = new JPanel();
		south.add(add);
		south.add(new JLabel("  "));
		south.add(reset);
		south.add(new JLabel("  "));
		south.add(winner);
		add(south, BorderLayout.SOUTH);
		
		// the label
		extra = new JLabel("Enter user or Winning combination");
		extra.setHorizontalAlignment(SwingConstants.CENTER);
		JPanel pExtra = new JPanel();
		pExtra.add(extra);
		add(pExtra, BorderLayout.NORTH);
		
		
		
	}

	/*
	 * for the Buttons Add and Reset
	 */
	public void actionPerformed(ActionEvent e) {
		Object o = e.getSource();
		// the Add button was clicked
		if(o == add) {
			// list of the number selected
			int[] number = new int[nbToSelect];
			// index in the array of numbers
			int nb = 0;
			for(int i = 1; i < lotoLabel.length; ++i) {
				if(bitSet.get(i)) {
					lotoLabel[i].setBackground(Color.WHITE);
					number[nb++] = i;
					if(nb == nbToSelect)
						break;
				}
			}
			father.addCombin(bitSet);
			nbSelected = 0;
			add.setEnabled(false);
			winner.setEnabled(false);
			bitSet.clear();
			return;
		}
		
		// the winner button was selected
		if(o == winner) {
			enterExtra = true;
			extra.setText("Click on the Extra value");
			winner.setEnabled(false);
			reset.setEnabled(false);
			add.setEnabled(false);
			
			return;
		}

		// the reset button was cliked
		for(int i = 1; i < lotoLabel.length; ++i) {
			if(bitSet.get(i)) {
				lotoLabel[i].setBackground(Color.WHITE);
				--nbSelected;
				if(--nbSelected == 0)
					break;
			}
		}
		nbSelected = 0;
		add.setEnabled(false);
		winner.setEnabled(false);
		bitSet.clear();
	}

	/*
	 * The class to display the loto numbers
	 */
	private class LotoLabel extends JLabel implements MouseListener {

		private static final long serialVersionUID = 1L;
		private int value;
		LotoLabel(int value) {
			super("" + value);
			// the value 0 is not displayed and does not have special formatting
			if(value == 0) {
				setText("");
				return;
			}
			this.value = value;
			setOpaque(true);
			setFocusable(true);
			setBackground(Color.WHITE);
			this.setHorizontalAlignment(SwingConstants.CENTER);
			addMouseListener(this);
		}
		@Override
		public void mouseClicked(MouseEvent e) {
			// we are done exit
			if(done)
				return;
			// if we are waiting for exra
			if(enterExtra) {
				if(bitSet.get(value)) {
					extra.setText("Extra can't be an already selected number");
					return;
				}
				setBackground(Color.GREEN);
				father.displayWinner(bitSet, value);
				done = true;
			}
			// if already selected
			if(bitSet.get(value)) {
				setBackground(Color.WHITE);		// reset to white
				--nbSelected;					// number selected
				add.setEnabled(false);			// surely not ready to be add
				winner.setEnabled(false);
				bitSet.clear(value);			// clear the value
			}
			else {
				if(nbToSelect == nbSelected)	// no more room
					return;
				// OK show as selected
				setBackground(Color.YELLOW);
				++nbSelected;
				add.setEnabled(nbSelected == nbToSelect);
				winner.setEnabled(nbSelected == nbToSelect);
				bitSet.set(value);				// remember as set
			}

		}

		@Override
		public void mousePressed(MouseEvent e) {}
		@Override
		public void mouseReleased(MouseEvent e) {}
		@Override
		public void mouseEntered(MouseEvent e) {}
		@Override
		public void mouseExited(MouseEvent e) {}

	}
}


Was This Post Helpful? 1
  • +
  • -

#3 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Saving a Program?

Posted 02 March 2012 - 08:21 PM

pbl's source code! Quick let's study it before he sobers up and takes it down!
Was This Post Helpful? 1
  • +
  • -

#4 itsjimmy91  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 77
  • Joined: 19-January 11

Re: Saving a Program?

Posted 02 March 2012 - 08:21 PM

Thanks man. Looks a lot more involved than mine haha it's pretty simple before. Which part of yours deals with the saving?
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Saving a Program?

Posted 02 March 2012 - 08:26 PM

View PostSheph, on 02 March 2012 - 10:21 PM, said:

pbl's source code! Quick let's study it before he sobers up and takes it down!

I can assume my code, even if written drunk :) I'll bet you what you want that is will work

This post has been edited by pbl: 02 March 2012 - 08:27 PM

Was This Post Helpful? 0
  • +
  • -

#6 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,533
  • Joined: 05-May 05

Re: Saving a Program?

Posted 02 March 2012 - 08:42 PM

Quote

My question is, how do I make it so that once the client enters their numbers into the program, they can save the state of the program and pull it up at anytime and check a winning lottery number against the numbers they entered?


You need to use a data structure. Arrays, lists, etc. You'll probably need to use a 2d array.

This post has been edited by blackcompe: 02 March 2012 - 08:43 PM

Was This Post Helpful? 0
  • +
  • -

#7 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Saving a Program?

Posted 02 March 2012 - 09:39 PM

Actually, I think he means persistence and File IO. What you should realize is that all the variables and data in your program is stored in the RAM and that is volatile memory. Meaning once the program is shut off, the memory is gone. You need to write your data to the disk (Your hard drive or other storage device) in order to for any data to stay persistent from one run of your program to another. A very easy way to do this would be to aggregate all of the data you want to write to a file into a single class, and use an ObjectOutputStream and ObjectInputStream. These will write objects to a file, which can be read the next time you open a program and they will be in the same state they were when you wrote them.

http://docs.oracle.c...tputStream.html
http://docs.oracle.c...nputStream.html
Was This Post Helpful? 0
  • +
  • -

#8 itsjimmy91  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 77
  • Joined: 19-January 11

Re: Saving a Program?

Posted 02 March 2012 - 10:59 PM

View PostSheph, on 02 March 2012 - 09:39 PM, said:

Actually, I think he means persistence and File IO. What you should realize is that all the variables and data in your program is stored in the RAM and that is volatile memory. Meaning once the program is shut off, the memory is gone. You need to write your data to the disk (Your hard drive or other storage device) in order to for any data to stay persistent from one run of your program to another. A very easy way to do this would be to aggregate all of the data you want to write to a file into a single class, and use an ObjectOutputStream and ObjectInputStream. These will write objects to a file, which can be read the next time you open a program and they will be in the same state they were when you wrote them.

http://docs.oracle.c...tputStream.html
http://docs.oracle.c...nputStream.html


Yes, this is what I'm referring to. I've been reading up on the links you gave me and about serialization in general. I'm having trouble understanding it though. Does anybody have any good links to a tutorial for this kind of thing?
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2727
  • View blog
  • Posts: 11,469
  • Joined: 20-September 08

Re: Saving a Program?

Posted 03 March 2012 - 03:40 AM

If you post what you've done already then we can see what the best route is to add persistence
Was This Post Helpful? 0
  • +
  • -

#10 itsjimmy91  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 77
  • Joined: 19-January 11

Re: Saving a Program?

Posted 03 March 2012 - 10:04 AM

Sure, here it is.

import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 * Lottery Checker is a program used for checking lottery numbers.
 * Lottery Checker has a list of lottery numbers and allows the client 
 * to check whether the winning lottery numbers exist in their list of numbers.
 * 
 * @author  jhalter
 * @version MAR 2012
 *
 */
public class LotteryChecker {
	
	// ArrayList to hold the Lottery #'s
	private ArrayList<String> numbers;
	
	// Constructor
	public LotteryChecker()
	{
		numbers = new ArrayList<String>();
	}
	
	/**
	 * Add a new lottery number to the list of lottery nums
	 * 
	 * @param String
	 */
	public void addNumber(String number)
	{
		if(!numbers.contains(number))
		{
			numbers.add(number);
			JOptionPane.showMessageDialog(null, ""+ number + " added successfully.");
		}
		else		// give an error if the number already exists
		{
			JOptionPane.showMessageDialog(null, "The number " + number + " already exists.", 
					"Duplicate Number", JOptionPane.ERROR_MESSAGE); 
		}
	}
	
	/**
	 * Remove a lottery number from the list of lottery nums
	 * 
	 * @param String
	 */
	public void removeNumber(String number)
	{
		// Check to see if the given number exists
		if(numbers.contains(number))
		{
			numbers.remove(number);
			JOptionPane.showMessageDialog(null, "" + number + " was successfully removed.");
		}
		else	// the number is not in the list
		{
			JOptionPane.showMessageDialog(null, "The number " + number + " does not exist.", "Invalid Number",
					JOptionPane.ERROR_MESSAGE);
		}
	}
	
	/**
	 * Clear the list of lottery numbers
	 */
	public void clearNumbers()
	{
		numbers.clear();
		JOptionPane.showMessageDialog(null, "All of the numbers have been cleared.");
	}
	
	/**
	 * Determine whether or not the winning lottery number
	 * exists in the list of lotter nums
	 * 
	 * @param String
	 * @return boolean
	 */
	public boolean isWinner(String number)
	{
		return numbers.contains(number);
	}

}



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * LotteryGUI provides a GUI for the LotteryChecker program.
 * A client has the option to add a number, remove a number
 * and check to see if they have a winning number. A client 
 * also has the ability to clear the entire list of numbers.
 * 
 * @author  jhalter
 * @version MAR 2012
 *
 */
public class LotteryGUI {
	
	private static final int WINDOW_WIDTH = 500;
	private static final int WINDOW_HEIGHT = 500;
	private static final double VERSION = 1.0;
	
	private JFrame frame;
	private JPanel northPanel, westPanel, eastPanel;
	private JButton addNum, removeNum, clearNums, checkNum;
	
	public LotteryGUI()
	{
		frame = new JFrame("Lottery Checker - Version " + VERSION);
		frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLayout(new BorderLayout());
		
		Container contentPane = frame.getContentPane();
		
		northPanel = new JPanel();
		westPanel = new JPanel();
		eastPanel = new JPanel();
//		JPanel southPanel = new JPanel();
//		JPanel centerPanel = new JPanel();
		
		westPanel.setLayout(new BoxLayout(westPanel, BoxLayout.Y_AXIS));
		eastPanel.setLayout(new BoxLayout(eastPanel, BoxLayout.Y_AXIS));
		
		JLabel northLabel = new JLabel("Welcome to Lottery Checker");
		northPanel.add(northLabel);
		
		addNum = new JButton("Add Number");
		removeNum = new JButton("Remove Number");
		clearNums = new JButton("Clear All Numbers");
		checkNum = new JButton("Check Winner");
		
		setActionListeners(); 		// call the method that will set up the buttons' actionListeners
		
		westPanel.add(addNum);
		westPanel.add(removeNum);
		westPanel.add(clearNums);
		eastPanel.add(checkNum);
		
		contentPane.add(northPanel, BorderLayout.NORTH);
		contentPane.add(westPanel, BorderLayout.WEST);
		contentPane.add(eastPanel, BorderLayout.EAST);
		
		frame.pack();
		frame.setVisible(true);
	}
	
	private void setActionListeners()
	{	
		// Add Number's actionListener
		addNum.addActionListener(new ActionListener() { 
			public void actionPerformed(ActionEvent e)
			{
				new AddNumberWindow();
			}
		});
		
		// Remove Number's actionListener
		removeNum.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				new RemoveNumberWindow();
			}
		});
		
		// Clear Number's actionListener
		clearNums.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				Main.l.clearNumbers();
			}
		});
		
		// Check Number's actionListener
		checkNum.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				new CheckNumberWindow();
			}
		});
	}
	
}



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * AddNumberWindow creates a window for the client to
 * add a new lottery number to the program. This window
 * is opened when the client clicks the "Add Number" button
 * from the home screen of Lottery Checker.
 * 
 * @author  jhalter
 * @version MAR 2012
 *
 */
public class AddNumberWindow {

	private static final int WINDOW_WIDTH = 250;
	private static final int WINDOW_HEIGHT = 250;

	private JFrame frame;
	private JButton confirmButton, cancelButton;
	private JTextField numberField;
	
	public AddNumberWindow()
	{
		frame = new JFrame("Add Number");
		frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
		frame.setLayout(new BorderLayout());
		
		Container contentPane = frame.getContentPane();
		
		JPanel centerPanel = new JPanel();
		JPanel southPanel = new JPanel();

		southPanel.setLayout(new FlowLayout());
		centerPanel.setLayout(new FlowLayout());
		
		confirmButton = new JButton("Confirm");
		cancelButton = new JButton("Cancel");
		
		southPanel.add(confirmButton);
		southPanel.add(cancelButton);
		
		JLabel textBoxLabel = new JLabel("Lottery Number: ");
		numberField = new JTextField(20);
		
		centerPanel.add(textBoxLabel);
		centerPanel.add(numberField);
		
		setActionListeners();
		
		contentPane.add(centerPanel, BorderLayout.CENTER);
		contentPane.add(southPanel, BorderLayout.SOUTH);
		
		frame.pack();
		frame.setVisible(true);
	}
	
	// Handle all of the actionListeners
	private void setActionListeners()
	{
		// Cancel Button's actionListener
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				frame.setVisible(false);
			}
		});
		
		// Confirm Button's actionListener
		confirmButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				String number = numberField.getText();
				Main.l.addNumber(number);
				frame.setVisible(false);
			}
		});
	}
}



import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

/**
 * RemoveNumberWindow creates a window for the client 
 * to remove a specific number from the list of Lottery
 * numbers. This window is activated when the client clicks
 * "Remove Number" from the home screen of Lottery Checker.
 * 
 * @author  jhalter
 * @version MAR 2012
 *
 */
public class RemoveNumberWindow {
	
	private static final int WINDOW_WIDTH = 250;
	private static final int WINDOW_HEIGHT = 250;
	
	private JFrame frame;
	private JButton confirmButton, cancelButton;
	private JTextField numberField;
	
	public RemoveNumberWindow()
	{
		frame = new JFrame("Remove Number");
		frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
		frame.setLayout(new BorderLayout());
		
		Container contentPane = frame.getContentPane();
		
		JPanel centerPanel = new JPanel();
		JPanel southPanel = new JPanel();
		
		centerPanel.setLayout(new FlowLayout());
		southPanel.setLayout(new FlowLayout());
		
		confirmButton = new JButton("Confirm");
		cancelButton = new JButton("Cancel");
		
		southPanel.add(confirmButton);
		southPanel.add(cancelButton);
		
		JLabel textBoxLabel = new JLabel("Lottery Number: ");
		numberField = new JTextField(20);
		
		centerPanel.add(textBoxLabel);
		centerPanel.add(numberField);
		
		setActionListeners();
		
		contentPane.add(centerPanel, BorderLayout.CENTER);
		contentPane.add(southPanel, BorderLayout.SOUTH);
		
		frame.pack();
		frame.setVisible(true);
	}
	
	private void setActionListeners()
	{
		// Confirm Button's actionListener
		confirmButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				String number = numberField.getText();
				Main.l.removeNumber(number);
				frame.setVisible(false);
			}
		});
		
		// Cancel Button's actionListener
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				frame.setVisible(false);
			}
		});
	}

}



import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

/**
 * CheckNumberWindow creates a window that will allow
 * the client to check a number against the numbers that 
 * are in their current list of lottery numbers. The 
 * window is displayed when the client clicks the 
 * "Check Number" button from the home screen of Lottery Checker.
 * 
 * @author  jhalter
 * @version MAR 2012
 *
 */
public class CheckNumberWindow {

	private final static int WINDOW_WIDTH = 250;
	private final static int WINDOW_HEIGHT = 250;
	
	private JFrame frame;
	private JButton checkButton, closeButton;
	private JTextField numberField;
	
	public CheckNumberWindow()
	{
		frame = new JFrame("Check Numbers");
		frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
		frame.setLayout(new BorderLayout());
		
		Container contentPane = frame.getContentPane();
		
		JPanel centerPanel = new JPanel();
		JPanel southPanel = new JPanel();
		centerPanel.setLayout(new FlowLayout());
		southPanel.setLayout(new FlowLayout());
		
		checkButton = new JButton("Check");
		closeButton = new JButton("Close");
		
		southPanel.add(checkButton);
		southPanel.add(closeButton);
		
		JLabel textFieldLabel = new JLabel("Lottery Number: ");
		numberField = new JTextField(20);
		
		centerPanel.add(textFieldLabel);
		centerPanel.add(numberField);
		
		setActionListeners();
		
		contentPane.add(centerPanel, BorderLayout.CENTER);
		contentPane.add(southPanel, BorderLayout.SOUTH);
		
		frame.pack();
		frame.setVisible(true);
	}
	
	// Set the actionListeners
	private void setActionListeners()
	{
		// set the Check actionListener
		checkButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				String number = numberField.getText();
				boolean winner = Main.l.isWinner(number); // check if the # exists
				numberField.setText(null);
				if(winner)
				{
					JOptionPane.showMessageDialog(null, "You have this number!");
				}
				else
				{
					JOptionPane.showMessageDialog(null, "You do not have this number");
				}
			}
		});
		
		// set the Close actionListener
		closeButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				frame.setVisible(false);
			}
		});
	}
}



/**
 * Run the LotteryChecker program.
 * 
 * @author  jhalter
 * @version MAR 2012
 *
 */
public class Main {
	
	static LotteryChecker l;
	
	public static void main(String[] args) {
		l = new LotteryChecker();
		new LotteryGUI();
	}

}


Was This Post Helpful? 0
  • +
  • -

#11 itsjimmy91  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 77
  • Joined: 19-January 11

Re: Saving a Program?

Posted 04 March 2012 - 04:30 PM

Can anybody give me an idea of how I should get started?
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Saving a Program?

Posted 04 March 2012 - 04:35 PM

Not 100% sure what you want to do but one thing for sure:
/**
 * Run the LotteryChecker program.
 * 
 * @author  jhalter
 * @version MAR 2012
 *
 */
public class Main {
	
	static LotteryChecker l;
	
	public static void main(String[] args) {
		l = new LotteryChecker();
		new LotteryGUI();
	}

}


LotteryChecker l is an orphan variable. It is defined in your main() but not use anywhere.
Are you sure you shouldn't be passing it as parameter to your LotteryGUI constructor ?
Was This Post Helpful? 0
  • +
  • -

#13 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,533
  • Joined: 05-May 05

Re: Saving a Program?

Posted 04 March 2012 - 04:39 PM

Make LotteryChecker serializable, and create a load and persist method, where you write the object to and from a file with an object stream. Here's a example of how to code the streams.

class LotteryNumbers {
	List<Integer> numbers;
	
	//your existing methods
	
	boolean load(String file) {
		//wrap a file input stream in an object input stream
		//call ObjectInputStream.readObject
		//set LotteryNumbers.numbers to the result from readObject
		//close streams
	}
	
	boolean persist(String file) {
		//wrap a file output stream in an object output stream
		//call ObjectOutputStream.writeObject and write the List of numbers only
		//close streams
	}
}



The very first code in your application should create a new LotteryNumbers instance and load the numbers from the file.

main() {
	LotteryNumbers nums = new LotterNumbers();
	nums.load("user.dat");
	//display numbers to ensure validity
}


This post has been edited by blackcompe: 04 March 2012 - 04:42 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1