Программа:
Файл Main.java
package orfogaut;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.logging.*;
public class Main {
public String Text = "Привет Юра давай пойдем погуляем на улице, там хорошая погода !";
public char[] TextCh;
private double Т = 0.08;
List<Users> UserInData = new ArrayList<Users>();
public void toChar() {
TextCh = new char[Text.length()];
for (int i = 0; i < TextCh.length; i++) {
TextCh[i] = Text.charAt(i);
}
}
public boolean nameUser(String Name, long Time, double Coef) {
boolean A = false;
for (Users UID : UserInData) {
if (UID.getName() == null ? Name == null : UID.getName().equals(Name)) {
if ((Math.abs(Time - UID.getTime())) <= 5) {
if ((Math.abs(Coef - UID.getCoef())) <= Т) {
A = true;
break;
}
}
}
}
return A;
}
public void readFromFile() throws IOException {
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader("data.txt"));
} catch (FileNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
String line = null;
while ((line = in.readLine()) != null) {
Scanner s = new Scanner(line);
String Name = s.next();
long Time = s.nextLong();
int Err = s.nextInt();
double Coef = Double.parseDouble(s.next());
UserInData.add(new Users(Name, Time, Err, Coef));
}
}
public void writeToFile() throws IOException {
String UserData = null;
FileWriter os = new FileWriter(new File("data.txt"));
for (Users us : UserInData) {
UserData = us.toString();
os.write(UserData);
os.write("\n");
}
os.close();
}
}
Файл MainWindow.java
package orfogaut;
import java.awt.*;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
public class MainWindow extends javax.swing.JFrame {
Main prog = new Main();
String Name;
int i = 0, Err = 0, t = 0, k = 0;
long Start = 0, Time = 0;
double Coef = 0;
public MainWindow() {
initComponents();
try {
prog.readFromFile();
} catch (IOException ex) {
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
}
prog.toChar();
jTextArea1.setEditable(false);
jTextArea2.setEditable(false);
}
private void jTextArea1KeyReleased(java.awt.event.KeyEvent evt) {
if (i < prog.TextCh.length) {
if (prog.TextCh[i] == evt.getKeyChar()) {
i++;
} else if ((16 != evt.getKeyCode()) && (evt.getKeyCode() != 18)
&& (evt.getKeyCode() != 17) && (evt.getKeyCode() != 20)
&& (evt.getKeyCode() != 8)) {
Err++;
i++;
t++;
jLabel2.setForeground(Color.red);
jLabel2.setText("Была допущена ошибка!");
jLabel3.setText("Ошибок: " + Err);
}
if (evt.getKeyCode() == 8) {
if (i > 0) {
i--;
}
if (k > 0) {
t = 0;
k--;
}
}
if (t > 0) {
k++;
}
if (k == 0) {
jLabel2.setForeground(Color.green);
jLabel2.setText("Продолжайте вводить текст!");
}
}
if (i == prog.TextCh.length) {
jTextArea1.setEditable(false);
}
jLabel4.setText("Символов: " + i);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextArea1.setEditable(true);
jLabel3.setText("Ошибок: ");
jLabel4.setText("Символов:");
jLabel1.setText("");
jLabel2.setText("");
jLabel6.setText("");
Name = JOptionPane.showInputDialog("Введите ваше имя");
i = 0;
t = 0;
k = 0;
Err = 0;
Start = System.currentTimeMillis();
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
Time = (System.currentTimeMillis() - Start) / 1000;
JOptionPane.showMessageDialog(null, "Тест Закончен");
jTextArea1.setText("");
Coef = (double) Time / prog.TextCh.length;
jLabel2.setText("");
jLabel6.setText("Время: " + Time + " Ошибок: " + Err + " Кэфф: " + Coef);
if (prog.nameUser(Name, Time, Coef)) {
jLabel1.setForeground(Color.green);
jLabel1.setText("Вы прошли тест!");
} else {
jLabel1.setForeground(Color.red);
jLabel1.setText("Вы не прошли тест!");
}
try {
prog.writeToFile();
} catch (IOException ex) {
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
try {
prog.writeToFile();
} catch (IOException ex) {
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
}
System.exit(1);
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
prog.UserInData.add(new Users(Name, Time, Err, Coef));
try {
prog.writeToFile();
} catch (IOException ex) {
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainWindow().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextArea jTextArea2;
// End of variables declaration
}
Файл Users.java
package orfogaut;
public class Users {
private String Name;
private long Time;
private int Err;
private double Coef;
public Users(String Name, long Time, int Err, double Coef) {
this.Name = Name;
this.Time = Time;
this.Err = Err;
this.Coef = Coef;
}
public double getCoef() {
return Coef;
}
public int getErr() {
return Err;
}
public String getName() {
return Name;
}
public long getTime() {
return Time;
}
public void setCoef(double Coef) {
this.Coef = Coef;
}
public void setErr(int Err) {
this.Err = Err;
}
public void setName(String Name) {
this.Name = Name;
}
public void setTime(long Time) {
this.Time = Time;
}
@Override
public String toString() {
return Name + " " + Time + " " + Err + " " + Coef;
}
}