我有编写GUI并将信息写入文本文件的任务,然后当按下评估按钮时,它将读取文本文件中的数据和所需字段的总和,如果总数等于或大于超过8000将这些记录写入它创建的新文件。我的一切工作都很顺利,但在防止重复输入方面遇到了障碍。我希望该程序不允许重复输入。我可以看到有重复,但我似乎无法让打印编写器不能解雇。我已经尝试了一个布尔值,如果存在重复的记录,但它只是阻止条件部分甚至写入空文件。感谢您的帮助。
package reprecord;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class DataEntry extends JFrame
{
private JTextField repIDField;
private JTextField repFirstField;
private JTextField repLastField;
private JTextField suppliesField;
private JTextField booksField;
private JTextField paperField;
private JRadioButton northRadioButton;
private JRadioButton southRadioButton;
private JRadioButton eastRadioButton;
private JRadioButton westRadioButton;
private ButtonGroup districtGroup;
private JRadioButton phoneRadioButton;
private JRadioButton emailRadioButton;
private JRadioButton visitRadioButton;
private ButtonGroup contactGroup;
private JButton enterButton;
private JButton resetButton;
private JButton evaluateButton;
private JButton exitButton;
private JLabel labelRepID;
private JLabel labelRepFirst;
private JLabel labelRepLast;
private JLabel labelSupplies;
private JLabel labelBooks;
private JLabel labelPaper;
private JLabel labelDistrict;
private JLabel labelContact;
private JPanel p1;
private JPanel p2;
private JPanel p3;
private JPanel p4;
private JPanel p5;
private JPanel p6;
private JPanel p7;
private JPanel p8;
private JPanel p9;
private JPanel p10;
private JPanel p11;
private JTextArea eval;
private JFrame evalFrame;
private JTextArea star;
private JFrame starFrame;
String repID = "";
String repFirst = "";
String repLast = "";
String supplies = "";
String books = "";
String paper = "";
String radioText = "";
String radioText1 = "";
private int r1;
private String r2;
private String r3;
private double r4;
private double r5;
private double r6;
private String r7;
private String r8;
private int id;
private int id1;
String repData;
String repData1;
String initData;
String starData;
String starData1;
int J;
public DataEntry()
{
super( "Account Representative" );
setSize(525, 500);
setVisible(true);
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container c = new Container();
add(c);
c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
// create and add jpanels for components
p1 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
c.add(p1);
p2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
c.add(p2);
p3 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
c.add(p3);
p4 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
c.add(p4);
p5 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
c.add(p5);
p6 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
c.add(p6);
p7 = new JPanel();
c.add(p7);
p8 = new JPanel();
c.add(p8);
p9 = new JPanel();
c.add(p9);
p10 = new JPanel();
c.add(p10);
p11 = new JPanel();
c.add(p11);
evalFrame = new JFrame("Sales Rep Data");
eval = new JTextArea();
evalFrame.setVisible(false);
evalFrame.setSize(575, 300);
evalFrame.setLocation(1250, 240);
evalFrame.add(eval);
eval.setBorder(BorderFactory.createLineBorder(Color.black));
eval.setEditable(false);
File f = new File("salesrep.txt");
if(f.exists() && !f.isDirectory())
{
try
{ // start of try eval
FileInputStream fs1 = new FileInputStream("salesrep.txt");
DataInputStream in1 = new DataInputStream(fs1);
BufferedReader br1 = new BufferedReader(new InputStreamReader(in1));
//Read File Line By Line
while ((initData = br1.readLine()) != null)
{
eval.append(initData + "\r\n");
}//compare while end
in1.close();
br1.close();
} // end of try eval
catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
}
}
starFrame = new JFrame("Star Sales Rep Data");
star = new JTextArea();
starFrame.setVisible(false);
starFrame.setSize(575, 300);
starFrame.setLocation(1250, 560);
starFrame.add(star);
star.setBorder(BorderFactory.createLineBorder(Color.black));
star.setEditable(false);
// create text fields
labelRepID = new JLabel("Representative ID");
repIDField = new JTextField( 15 );
p1.add( labelRepID );
p1.add(Box.createRigidArea(new Dimension(10,0)));
p1.add( repIDField );
repIDField.requestFocusInWindow();
p1.add(Box.createRigidArea(new Dimension(100,0)));
p1.add(Box.createVerticalGlue());
p1.add(Box.createHorizontalGlue());
p1.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
labelRepFirst = new JLabel("Representative First Name");
repFirstField = new JTextField( 15 );
p2.add( labelRepFirst );
p2.add(Box.createRigidArea(new Dimension(10,0)));
p2.add( repFirstField );
p2.add(Box.createRigidArea(new Dimension(100,0)));
p2.add(Box.createVerticalGlue());
p2.add(Box.createHorizontalGlue());
labelRepLast = new JLabel("Representative Last Name");
repLastField = new JTextField( 15 );
p3.add( labelRepLast );
p3.add(Box.createRigidArea(new Dimension(10,0)));
p3.add( repLastField );
p3.add(Box.createRigidArea(new Dimension(100,0)));
p3.add(Box.createVerticalGlue());
p3.add(Box.createHorizontalGlue());
labelSupplies = new JLabel("Supplies Total Sales");
suppliesField = new JTextField( 15 );
p4.add( labelSupplies );
p4.add(Box.createRigidArea(new Dimension(10,0)));
p4.add( suppliesField );
p4.add(Box.createRigidArea(new Dimension(100,0)));
p4.add(Box.createVerticalGlue());
p4.add(Box.createHorizontalGlue());
labelBooks = new JLabel("Books Total Sales");
booksField = new JTextField( 15 );
p5.add( labelBooks );
p5.add(Box.createRigidArea(new Dimension(10,0)));
p5.add( booksField );
p5.add(Box.createRigidArea(new Dimension(100,0)));
p5.add(Box.createVerticalGlue());
p5.add(Box.createHorizontalGlue());
labelPaper = new JLabel("Paper Total Sales");
paperField = new JTextField( 15 );
p6.add( labelPaper );
p6.add(Box.createRigidArea(new Dimension(10,0)));
p6.add( paperField );
p6.add(Box.createRigidArea(new Dimension(100,0)));
p6.add(Box.createVerticalGlue());
p6.add(Box.createHorizontalGlue());
// register event for textfields
TextFieldHandler handler = new TextFieldHandler();
repIDField.addActionListener( handler );
repFirstField.addActionListener( handler );
repLastField.addActionListener( handler );
suppliesField.addActionListener( handler );
booksField.addActionListener( handler );
paperField.addActionListener( handler );
// create radio buttons
northRadioButton = new JRadioButton( "North", false );
southRadioButton = new JRadioButton( "South", false );
eastRadioButton = new JRadioButton( "East", false );
westRadioButton = new JRadioButton( "West", false );
p8.setBorder(BorderFactory.createTitledBorder( null, "Sales District"));
p8.add( northRadioButton );
p8.add( southRadioButton );
p8.add( eastRadioButton );
p8.add( westRadioButton );
p8.add(Box.createVerticalGlue());
p8.add(Box.createHorizontalGlue());
districtGroup = new ButtonGroup();
districtGroup.add( northRadioButton );
districtGroup.add( southRadioButton );
districtGroup.add( eastRadioButton );
districtGroup.add( westRadioButton );
phoneRadioButton = new JRadioButton( "Phone", false );
emailRadioButton = new JRadioButton( "Email", false );
visitRadioButton = new JRadioButton( "Visit", false );
p9.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0));
p10.setBorder(BorderFactory.createTitledBorder( null, "Preferred Contact Method"));
p10.add( phoneRadioButton );
p10.add( emailRadioButton );
p10.add( visitRadioButton );
p10.add(Box.createVerticalGlue());
p10.add(Box.createHorizontalGlue());
contactGroup = new ButtonGroup();
contactGroup.add( phoneRadioButton );
contactGroup.add( emailRadioButton );
contactGroup.add( visitRadioButton );
// register events for RadioButtons
RadioButtonHandler handler1 = new RadioButtonHandler();
northRadioButton.addItemListener( handler1 );
southRadioButton.addItemListener( handler1 );
eastRadioButton.addItemListener( handler1 );
westRadioButton.addItemListener( handler1 );
phoneRadioButton.addItemListener( handler1 );
emailRadioButton.addItemListener( handler1 );
visitRadioButton.addItemListener( handler1 );
// create buttons
enterButton = new JButton( " Enter " );
resetButton = new JButton( " Reset " );
evaluateButton = new JButton( "Evaluate");
exitButton = new JButton (" Exit ");
p11.add( enterButton );
p11.add(Box.createRigidArea(new Dimension(20,0)));
p11.add( resetButton );
p11.add(Box.createRigidArea(new Dimension(20,0)));
p11.add( evaluateButton );
p11.add(Box.createRigidArea(new Dimension(20,0)));
p11.add(exitButton);
p11.add(Box.createVerticalGlue());
p11.add(Box.createHorizontalGlue());
p11.setBorder(BorderFactory.createEmptyBorder(10, 0, 5, 0));
// register events for buttons
ButtonHandler handler2 = new ButtonHandler();
enterButton.addActionListener( handler2 );
resetButton.addActionListener( handler2 );
evaluateButton.addActionListener( handler2 );
exitButton.addActionListener( handler2);
}
private class TextFieldHandler implements ActionListener
{
// process textfield events
public void actionPerformed( ActionEvent event )
{
// user pressed Enter in JTextField repIDField
if ( event.getSource() == repIDField )
repID = event.getActionCommand();
getFocusOwner().transferFocus();
// user pressed Enter in JTextField repFirstField
if ( event.getSource() == repFirstField )
repFirst = event.getActionCommand();
getFocusOwner().transferFocus();
// user pressed Enter in JTextField repLastField
if ( event.getSource() == repLastField )
repLast = event.getActionCommand();
getFocusOwner().transferFocus();
// user pressed Enter in JTextField suppliesField
if ( event.getSource() == suppliesField )
supplies = event.getActionCommand();
getFocusOwner().transferFocus();
// user pressed Enter in JTextField booksField
if ( event.getSource() == booksField )
books = event.getActionCommand();
getFocusOwner().transferFocus();
// user pressed Enter in JTextField paperField
if ( event.getSource() == paperField )
paper = event.getActionCommand();
getFocusOwner().transferFocus();
}
}
private class RadioButtonHandler implements ItemListener
{
public void itemStateChanged( ItemEvent e )
{
if (northRadioButton.isSelected())
{
radioText = northRadioButton.getText();
}
if (southRadioButton.isSelected())
{
radioText = southRadioButton.getText();
}
if (eastRadioButton.isSelected())
{
radioText = eastRadioButton.getText();
}
if (westRadioButton.isSelected())
{
radioText = westRadioButton.getText();
}
if (phoneRadioButton.isSelected())
{
radioText1 = phoneRadioButton.getText();
}
if (emailRadioButton.isSelected())
{
radioText1 = emailRadioButton.getText();
}
if (visitRadioButton.isSelected())
{
radioText1 = visitRadioButton.getText();
}
}
}
private class ButtonHandler implements ActionListener
{
public void actionPerformed( ActionEvent event )
{
if (event.getSource() == enterButton)
{
r1 = Integer.parseInt(repID);
r2 = repFirst.substring(0, 1).toUpperCase() + repFirst.substring(1);
r3 = repLast.substring(0, 1).toUpperCase() + repLast.substring(1);
r4 = Double.parseDouble(supplies);
r5 = Double.parseDouble(books);
r6 = Double.parseDouble(paper);
r7 = radioText.toUpperCase();
r8 = radioText1;
repIDField.setText("");
repID = "";
repFirstField.setText("");
repFirst = "";
repLastField.setText("");
repLast = "";
suppliesField.setText("");
supplies = "";
booksField.setText("");
books = "";
paperField.setText("");
paper = "";
contactGroup.clearSelection();
districtGroup.clearSelection();
try
{
FileWriter fw = new FileWriter("salesrep.txt", true);
PrintWriter pw = new PrintWriter(fw);
pw.printf("%d %s %s %s $%.2f %s $%.2f %s $%.2f %s %s\r\n",
r1, r2, r3, "SUPPLIES", r4, "BOOKS", r5, "PAPER", r6, r7, r8);
repData1 = String.format("%d %s %s %s $%.2f %s $%.2f %s $%.2f %s %s\r\n",
r1, r2, r3, "SUPPLIES", r4, "BOOKS", r5, "PAPER", r6, r7, r8);
eval.append(repData1);
evalFrame.setVisible(true);
pw.close();
}
catch(IOException e)
{
System.err.println("Error: " + e.getMessage());
}
repIDField.requestFocusInWindow();
}
if (event.getSource() == resetButton)
{
repIDField.setText("");
repID = "";
repFirstField.setText("");
repFirst = "";
repLastField.setText("");
repLast = "";
suppliesField.setText("");
supplies = "";
booksField.setText("");
books = "";
paperField.setText("");
paper = "";
contactGroup.clearSelection();
districtGroup.clearSelection();
repIDField.requestFocusInWindow();
}
if (event.getSource() == evaluateButton)
{
try
{
FileInputStream fs = new FileInputStream("salesrep.txt");
DataInputStream in = new DataInputStream(fs);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
//Read File Line By Line and split at spaces
while ((repData = br.readLine()) != null)
{
String[] rd = repData.split(" ");
id = Integer.parseInt(rd[0]);
String first = rd[1].substring(0,1).toUpperCase() + rd[1].substring(1);
String last = rd[2].substring(0,1).toUpperCase() + rd[2].substring(1);
String sup = rd[4];
sup = sup.substring(1);
double sup1 = Double.parseDouble(sup);
String bks = rd[6];
bks = bks.substring(1);
double bks1 = Double.parseDouble(bks);
String pap = rd[8];
pap = pap.substring(1);
double pap1 = Double.parseDouble(pap);
String dis = rd[9].toUpperCase();
String con = rd[10];
File f = new File("stars.txt");
if(f.exists() && !f.isDirectory())
{
try
{
FileInputStream fs1 = new FileInputStream("stars.txt");
DataInputStream in1 = new DataInputStream(fs1);
BufferedReader br1 = new BufferedReader(new InputStreamReader(in1));
while ((starData = br1.readLine()) != null)
{
String[] rd1 = starData.split(" ");
id1 = Integer.parseInt(rd1[0]);
if(id == id1)
{
System.out.printf("\n%s %d %s", "Duplicate Record Item " , id, "\n");
int i = id;
i++;
System.out.print(i);
}
}
}
catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
}
}
double total = (sup1 + bks1 + pap1);
if(total >= 8000)
{
try
{
FileWriter fw1 = new FileWriter("stars.txt", true);
PrintWriter pw1 = new PrintWriter(fw1);
pw1.printf("%d %s %s %s $%.2f %s $%.2f %s $%.2f %s %s\r\n",
id, first, last, "SUPPLIES", sup1, "BOOKS", bks1, "PAPER", pap1, dis, con);
starData1 = String.format("%d %s %s %s $%.2f %s $%.2f %s $%.2f %s %s\r\n",
id, first, last, "SUPPLIES", sup1, "BOOKS", bks1, "PAPER", pap1, dis, con);
star.append(starData1);
pw1.close();
}
catch(IOException e)
{
System.err.println("Error: " + e.getMessage());
}
}
evalFrame.setVisible(true);
starFrame.setVisible(true);
}
in.close();
br.close();
}
catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
}
} // end evaluate button
if (event.getSource() == exitButton)
{
System.exit(0);
}
}// end of actionperformed
}// end of buttonhandler
}// end of DataEntry class