今からはじめるプログラミング77(よくわからない)

意味のあるプログラミンごをしてしまったので、混乱しているw

 

という感じで作っていた意味不明なもの。。。

CharactorData

-------------------------------------------------------------------------

package sample40;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.util.*;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

/**
 *
 ********/
public class CharactorData extends JFrame implements ActionListener {

    private List<Map<String, String>> properties = new ArrayList<Map<String, String>>();
    private List<String> keyList = new ArrayList<String>();
    private JTable table = null;
    private JCheckBox bxSlct = new JCheckBox("のみ");
    private JTextField txtWord = new JTextField("いっち");

    public CharactorData() {
        init();
    }

    private void init() {
        Container container = this.getContentPane();
        container.setLayout(new BorderLayout());

        // Data read
        File data = new File("./data.txt");
        int cntDt = 0;
        if (data.exists()) {
            try {
                FileReader fr = new FileReader(data);
                BufferedReader br = new BufferedReader(fr);
                while (br.ready()) {
                    String line = br.readLine();
                    System.out.println(line);
                    int indexEq = line.indexOf("=");
                    String key = line.substring(0, indexEq);
                    keyList.add(key);
                    String value = line.substring(indexEq + 1);
                    Map<String, String> mapDt = new HashMap<String, String>();
                    mapDt.put(key, value);
                    properties.add(mapDt);
                    cntDt++;
                }
                br.close();
                fr.close();

                // Configur Table

                table = new JTable(cntDt, 2);
                // set Data
                for (int i = 0; i < cntDt; i++) {
                    Map<String, String> dt = properties.get(i);
                    String key = keyList.get(i);
                    table.setValueAt(dt.get(key), i, 1);
                    table.setValueAt(key, i, 0);

                }
                // Visible
                JScrollPane scr = new JScrollPane(table);
                container.add(scr, BorderLayout.CENTER);

            } catch (IOException e) {
                // TODO 自動生成された catch ブロック
                e.printStackTrace();
            }

        }
        JButton btnTotal = new JButton("たす");
        container.add(btnTotal, BorderLayout.EAST);
        btnTotal.setActionCommand("total");
        btnTotal.addActionListener(this);

        container.add(bxSlct, BorderLayout.WEST);
        bxSlct.setActionCommand("?"); //TODO huyou

        container.add(txtWord, BorderLayout.NORTH);
        txtWord.setActionCommand("match");//TODO huyou


        this.setTitle("よくわからないけれどなんかいろいろある画面");
        this.setSize(new Dimension(800, 600));
        this.setVisible(true);

    }

    private int total(boolean flg) {
        System.out.println("flg=" + flg);

        // 合計
        int rowNum = table.getRowCount();
        int total = 0;
        // いっち
        boolean flg2 = false;
        String moji = txtWord.getText();
        if(!"".equals(moji)){
            flg2 = true;
        }


        if (flg) {
            //on
            for (int i = 0; i < rowNum; i++) {
                try {

                    String datO = (String) table.getValueAt(i, 0);
                    String datS = (String) table.getValueAt(i, 1);

                    if (table.isCellSelected(i, 1)) {

                        if(flg2){
                            if(moji.equals(datO)){
                                total += Integer.parseInt(datS);
                            }
                        }else{
                            total += Integer.parseInt(datS);
                        }
                    }

                } catch (NumberFormatException e1) {
                    System.out.println("数値ではありません[" + table.getValueAt(i, 1)
                            + "]");
                }

            }
        } else {
            //off
            for (int i = 0; i < rowNum; i++) {
                try {

                    String datO = (String) table.getValueAt(i, 0);
                    String datS = (String) table.getValueAt(i, 1);

                    if(flg2){
                        if(moji.equals(datO)){
                            total += Integer.parseInt(datS);
                        }
                    }else{
                        total += Integer.parseInt(datS);

                    }
                } catch (NumberFormatException e1) {
                    System.out.println("数値ではありません[" + table.getValueAt(i, 1)
                            + "]");
                }

            }

        }

        System.out.println("total=" + total);
        return total;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        if ("total".equals(command)) {
            //

            int total = this.total(bxSlct.isSelected());
            JOptionPane.showConfirmDialog(this, "ごうけい="+total);
        }
    }

    public static void main(String[] args) {
        new CharactorData();
    }

}

-----------------------------------------------------------------------------

なおパスにdata.txtというファイルが必要です。

ファイルにある

「~=~」という形式のテキストを「=」で分割して、テーブルに表示します。

数値を「たし」ます。

フラグがあります。

テキスト入力があります。

期間をかけて作ったので、スパゲッティコード化しています。

 

https://amzn.to/3HRYevF