Home arrow static arrow Java Programming [Archive] - Please help me to write the codes!!!
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Please help me to write the codes!!!
This topic has 2 replies on 1 page.

Posts:481
Registered: 7/7/04
Please help me to write the codes!!!  
Jul 9, 2004 8:56 PM



 
This is a POS system and it complies with no problems and runs fine. The arrays food[] and price[] read the data from a text file:

itemdetails.txt
============
Apple 3.5
Banana 2
Cherry 1
Strawberry 3
Blueberry 1.5
Red Bean 1
Green Bean 1
Black Bean 2
Onion 4
Sugar 6
Salt 5
Rice 10.5

You can run it and have a look at it if you have time.

After you run this application, you will see a "Set button Properties", click it and it will prompt you the password. The password is "admin". Then you will see a Dialog which can be used to edit the button properties. Here comes my problems. After the user makes changes to the button properties. It does not actually save the changes to the itemdetails.txt. This means, the next time the user run this application, he cannot see the changes that he made to the button properties last time.

So please help me write the codes if you are interested in it. Thank you very much! Thank you!

import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*; public class Pos extends JFrame implements ActionListener {	private JTextField jtfWeight = new JTextField(5);	private JTextField jtfIUP = new JTextField(10);	private JTextField jtfIC = new JTextField(10);	private JTextField jtfTT = new JTextField(10);	public JButton[] jbtFood = new JButton[12];	 public String[] food =new String[12]; public double[] price = new double[12];  //Start of the method used to read the food and price arrays from the text filespublic void readtext(){try{ BufferedReader br = new BufferedReader(new FileReader("itemdetails.txt")); String line; for(int i =0; i<12 ; i++) {  line = br.readLine();  System.out.println(line);  int indexOfLastSpace = line.lastIndexOf(' ');  food[i] = line.substring(0,indexOfLastSpace );  price[i] = Double.parseDouble( line.substring(indexOfLastSpace).trim() ) ; } br.close();}catch(Exception e){}} //End  	private JLabel jlbIUP = new JLabel("Item Unit Price: ");	private JLabel jlbIC = new JLabel("Item Cost: ");	private JLabel jlbTT = new JLabel("Transaction Total: ");	private JButton jbtTD = new JButton("Transaction Done");	private JButton jbtSBP = new JButton("Set Button Properties");	private JMenuItem jmiD = new JMenuItem("Done");	private JMenuItem jmiS = new JMenuItem("Set Button Properties");	public String selected = new String();	private String wei = new String();	private double ic=0;	public double tt=0;		public static void main(String[] args) {		                  Pos frame = new Pos();		frame.setSize(450, 400);		frame.setTitle("POS Machine");		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		frame.setVisible(true);	} 	public Pos() {                readtext(); //call the readtext method		JMenuBar jmb = new JMenuBar();		setJMenuBar(jmb);		JMenu tra = new JMenu("Transaction");		JMenu adm = new JMenu("Administration");		tra.add(jmiD);		adm.add(jmiS);		jmb.add(tra);		jmb.add(adm);				JPanel p = new JPanel();		p.setLayout(new GridLayout(3, 3, 6, 6));		p.add(jlbIUP);		p.add(jtfIUP);		p.add(new JLabel(" "));		p.add(jlbIC);		p.add(jtfIC);		p.add(new JLabel(" "));		p.add(jlbTT);		p.add(jtfTT);		p.add(new JLabel(" "));		jlbIUP.setHorizontalAlignment(JLabel.RIGHT);		jlbIC.setHorizontalAlignment(JLabel.RIGHT);		jlbTT.setHorizontalAlignment(JLabel.RIGHT);		jtfIUP.setEditable(false);		jtfIC.setEditable(false);		jtfTT.setEditable(false); 		JPanel p2 = new JPanel();		p2.setLayout(new GridLayout(3, 1));		p2.add(jbtTD);		p2.add(new JLabel(" "));		p2.add(new JLabel("  1. Enter Weight     2. Select item"));		jbtTD.setBackground(Color.gray); 		JPanel p3 = new JPanel();		p3.setLayout(new GridLayout(2, 1));		p3.add(p);		p3.add(p2);				JPanel p4 = new JPanel();		p4.setLayout(new GridLayout(3, 4));		for(int i=0; i<12; i++) {		jbtFood[i] = new JButton(food[i]);		p4.add(jbtFood[i]);		} 		JPanel p5 = new JPanel();		p5.setLayout(new FlowLayout());		p5.add(new JLabel("Weight (kg): "));		p5.add(jtfWeight); 		JPanel p6 = new JPanel();		p6.setLayout(new GridLayout(2, 1, 8, 8));		p6.add(p5);		p6.add(jbtSBP); 		JPanel p7 = new JPanel();		p7.setLayout(new BorderLayout());		p7.add(p4, BorderLayout.CENTER);		p7.add(p6, BorderLayout.SOUTH);				getContentPane().setLayout(new GridLayout(2,1));		getContentPane().add(p3);		getContentPane().add(p7); 		for(int i=0; i<12; i++)		jbtFood[i].addActionListener(this);		jmiD.addActionListener(this);		jmiS.addActionListener(this);		jbtTD.addActionListener(this);		jbtSBP.addActionListener(this);	} 	public void actionPerformed(ActionEvent e) {		if(e.getSource() == jbtTD || e.getSource() == jmiD) {		TranDia subFrame = new TranDia(this, selected, tt);		subFrame.setVisible(true);		jtfIUP.setText("");		jtfIC.setText("");		jtfTT.setText("");		jtfWeight.setText("");		selected = "";		tt = 0;		}				else if(e.getSource() == jbtSBP || e.getSource() == jmiS) {		String pwd = JOptionPane.showInputDialog(null, "Enter Password:", "Input",			JOptionPane.QUESTION_MESSAGE);		if("admin".equals(pwd)) {			AdminDia subFrame2 = new AdminDia(this, this);			subFrame2.setVisible(true);			jtfIUP.setText("");			jtfIC.setText("");			jtfTT.setText("");			jtfWeight.setText("");			selected = "";			tt = 0;	 	}	 	else {	 		JOptionPane.showMessageDialog(null, "Invalid Password!  Try angin.", 	 			"Error", JOptionPane.INFORMATION_MESSAGE);	 	}	 	} 		else {		for(int i=0; i<12; i++) {		if(e.getSource() == jbtFood[i]) {			wei = jtfWeight.getText().trim();			ic = Double.parseDouble(wei) * price[i];			jtfIUP.setText(String.valueOf(price[i]));			jtfIC.setText(String.valueOf(ic));			tt += ic;			jtfTT.setText(String.valueOf(tt));			selected+=food[i]+"\t"+price[i]+"\t"+wei+"\t"+ic+"\n";		}		}		}	}}   class TranDia extends JDialog implements ActionListener {	private String title = "Item"+"\t"+"Price/kg"+"\t"+"Weight(kg)"+"\t"+"Cost"+"\n"+		"------------------------------------------------------------------------------"+"\n";	private String ttotal = "\t\t\t"+"-----------"+"\n"+"\t"+"          Transaction Total:"+"\t";	private JButton jbtOk = new JButton("Ok");		public TranDia(JFrame f, String rep, double total) {		super(f, "Transaction Dialog",true);		setSize(400,400);		JTextArea jta = new JTextArea(title+rep+ttotal+total);		jta.setEditable(false);		getContentPane().setLayout(new BorderLayout());		getContentPane().add(jta, BorderLayout.CENTER);		getContentPane().add(jbtOk, BorderLayout.SOUTH);		jbtOk.addActionListener(this);	}		public void actionPerformed(ActionEvent e) {		if(e.getSource() == jbtOk)		this.setVisible(false);			}}   class AdminDia extends JDialog implements ActionListener {	private JLabel jlbTitle = new JLabel("Change Button Settings");	private JLabel jlbBN = new JLabel("Button No: ");	private JLabel jlbID = new JLabel("Item Description: ");	private JLabel jlbIP = new JLabel("Item Price: ");	private JTextField jtfBN = new JTextField(10);	private JTextField jtfID = new JTextField(10);	private JTextField jtfIP = new JTextField(10);	private JButton jbtSet = new JButton("Set");	private JButton jbtQuit = new JButton("Quit"); 	int bn;	String id = new String();	private Pos own;		public AdminDia(JFrame owner, Pos pos) {		super(owner, "Administrator Dialog", true);		setSize(250, 200);		own = pos;				JPanel pa = new JPanel();		pa.setLayout(new GridLayout(6,2));		pa.add(new JLabel(""));		pa.add(new JLabel(""));		pa.add(jlbBN);		pa.add(jtfBN);		pa.add(new JLabel(""));		pa.add(new JLabel(""));		pa.add(jlbID);		pa.add(jtfID);		pa.add(new JLabel(""));		pa.add(new JLabel(""));		pa.add(jlbIP);		pa.add(jtfIP);		jlbBN.setHorizontalAlignment(JLabel.RIGHT);		jlbID.setHorizontalAlignment(JLabel.RIGHT);		jlbIP.setHorizontalAlignment(JLabel.RIGHT);				JPanel pb = new JPanel();		pb.setLayout(new FlowLayout());		pb.add(jbtSet);		pb.add(jbtQuit);				jlbTitle.setHorizontalAlignment(JLabel.CENTER);		getContentPane().setLayout(new BorderLayout());		getContentPane().add(jlbTitle, BorderLayout.NORTH);		getContentPane().add(pa, BorderLayout.CENTER);		getContentPane().add(pb, BorderLayout.SOUTH);					jbtQuit.addActionListener(this);		jbtSet.addActionListener(this);	}		public void actionPerformed(ActionEvent e) {		if(e.getSource() == jbtSet) {			bn = Integer.parseInt(jtfBN.getText().trim());			own.jbtFood[bn-1].setText(jtfID.getText());			own.food[bn-1] = jtfID.getText();			own.price[bn-1] = Integer.parseInt(jtfIP.getText().trim());                                         //I guess the codes should be placed here			}				else if(e.getSource() == jbtQuit){			this.setVisible(false);		}	}}
 

Posts:2,433
Registered: 03-08-11
Re: Please help me to write the codes!!!  
Jul 9, 2004 9:58 PM (reply 1 of 2)



 
You must add a method to update the itemdetails.txt file, then use it when the values are updated:
public class Pos...    public void writeText() {        try {            BufferedWriter bw = new BufferedWriter(new FileWriter("itemdetails.txt"));            for (int i = 0; i < food.length; i++)            {                bw.write(food[i] + " " + price[i]);                bw.newLine();            }            bw.close();        }        catch(Exception e) {}    }    ...}...class AdminDia...	public void actionPerformed(ActionEvent e) {		if(e.getSource() == jbtSet) {			bn = Integer.parseInt(jtfBN.getText().trim());			own.jbtFood[bn-1].setText(jtfID.getText());			own.food[bn-1] = jtfID.getText();			own.price[bn-1] = Integer.parseInt(jtfIP.getText().trim());                                         //I guess the codes should be placed here	        own.writeText(); // <--- ***********		}		...}
 

Posts:481
Registered: 7/7/04
Re: Please help me to write the codes!!!  
Jul 9, 2004 10:08 PM (reply 2 of 2)



 
THANK YOU VERY MUCH!!!!!!!!!!!!!
YOU ARE GREAT!!!!
 
This topic has 2 replies on 1 page.