package Reckoner;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
public class Caculate {
String[]buttons={"1","2","3","+","←","4","5","6","-","C","7","8","9","*","01",".","0","=","/","02"};
public Caculate(){
}
JPanel buttonPanel1=new JPanel();
JPanel panelResult=new JPanel();
JOptionPane jop = new JOptionPane("出错了");
JLabel expression=new JLabel();
JLabel result=new JLabel();
JPanel panel=new JPanel(new GridLayout(2,1));
String eps = "";
String res = "";
int judge=0;
JMenuBar menuBar=new JMenuBar();
JMenu menu=new JMenu("菜单");
JMenuItem menuItem1=new JMenuItem("历史记录");
JMenuItem menuItem2=new JMenuItem("清空文件");
JMenuItem menuItem3=new JMenuItem("退出");
File file=new File("记录.txt");
JDialog historyDialog=new JDialog();
JTextArea historyTextArea=new JTextArea();
FileOutputStream fileOutputStream;
public void Show()
{
JFrame window=new JFrame("AIIT计算器");
window.setSize(500,400);
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buttonPanel1.setLayout(new GridLayout(4,5));
window.setLayout(new GridLayout(2,1));
try {
file.createNewFile();
} catch (IOException e) {
throw new RuntimeException(e);
}
initButton();
initAll();
initmenuBar();
window.setJMenuBar(menuBar);
window.add(panel);
window.add(buttonPanel1);
window.setVisible(true);
}
public void initmenuBar()
{
menuItem1.addActionListener(new MenuListener());
menu.add(menuItem1);
menuItem2.addActionListener(new MenuListener());
menu.add(menuItem2);
menuItem3.addActionListener(new MenuListener());
menu.add(menuItem3);
menuBar.add(menu);
}
public void initButton()
{
for(String index:buttons)
{
JButton button=new JButton(index);
button.addActionListener(new ButtonListener());
buttonPanel1.add(button);
}
}
public void initAll()
{
panel.add(expression);
panel.add(result);
}
public void saveResultByChar()
{
if(!file.exists()) {
try {
file.createNewFile();
System.out.println("文件不存在,重新创建文件");
System.out.println("正在读取文件...");
}catch (IOException e){
System.out.println("文件创建失败!");
e.printStackTrace();
}
}
else {
try {
FileWriter fileWriter=new FileWriter(file,true);
String saveRecord;
saveRecord = "操作时间:"+new Date().toString().split(" ")[5]+":"+new Date().toString().split(" ")[3]+" 表达式:"+eps+" 结果:"+res;
saveRecord+="\n";
fileWriter.write(saveRecord);
fileWriter.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public StringBuilder readFileByChar()
{
StringBuilder builder=new StringBuilder();
if(!file.exists()) {
try {
file.createNewFile();
System.out.println("文件不存在,重新创建文件");
System.out.println("正在读取文件...");
} catch (IOException e) {
System.out.println("文件创建失败!");
e.printStackTrace();
throw new RuntimeException(e);
}
}
try {
FileReader fileReader=new FileReader(file);
int len=0;
while((len=fileReader.read())!=-1)
{
builder.append((char)len);
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
return builder;
}
public void compute()
{
double num1=0,num2 = 0;
num1=Double.parseDouble(eps.split(" ")[0]);
num2=Double.parseDouble(eps.split(" ")[2]);
if(eps.split(" ")[1].equals("+"))
{
res=String.valueOf(num1+num2);
}
else if(eps.split(" ")[1].equals("-"))
{
res=String.valueOf(num1-num2);
}
else if(eps.split(" ")[1].equals("*"))
{
res=String.valueOf(num1*num2);
}
else if(eps.split(" ")[1].equals("/"))
{
res=String.valueOf(num1/num2);
}
}
public class MenuListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String x=e.getActionCommand();
switch (x)
{
case "历史记录":
System.out.println(readFileByChar());
String xx = new String(readFileByChar());
historyTextArea.setText(xx);
historyDialog.add(historyTextArea);
historyDialog.setSize(500, 400);
historyDialog.setLocationRelativeTo(null);
historyDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
historyTextArea.setEditable(false);
historyDialog.setVisible(true);
break;
case "清空文件":
try {
fileOutputStream=new FileOutputStream("记录.txt");
eps="";
byte[] b =eps.getBytes() ;
fileOutputStream.write(b);
} catch (FileNotFoundException ex) {
throw new RuntimeException(ex);
}
catch (IOException ex) {
throw new RuntimeException(ex);
}
break;
}
}
}
public class ButtonListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e) {
String x=e.getActionCommand();
String i=x;
switch(x)
{
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
case "0":
if(!res.equals(""))
{
eps="";
res="";
}
if(x.equals(i+""))
{
eps+=i;
expression.setText(eps);
break;
}
case "+":
case "-":
case "*":
case "/":
char lastChar = eps.charAt(eps.length() - 1);
if(lastChar==' ')
{
jop.showMessageDialog(null, "出错了", "错误", JOptionPane.ERROR_MESSAGE);
eps=" ";
expression.setText(eps);
}
else{
i = " " + x + " ";
eps += i;
expression.setText(eps);
}
break;
case "←":
if (eps.length() > 0) {
eps = eps.substring(0, eps.length() - 1);
expression.setText(eps);
}
break;
case "C":
expression.setText("");
break;
case "=":
compute();
eps=eps+"= ";
saveResultByChar();
eps="";
result.setText(res);
break;
}
}
}
public static void main(String[] args)
{
new Caculate().Show();
}
}