请帮助我搜索算法[关闭]

因此,我正在使用搜索算法编写程序。 它编译良好,没有给我一个错误。

这是我的代码,

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import java.awt.SystemColor;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.Vector;
import javax.swing.SwingConstants;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Toolkit;
import javax.swing.JComboBox;
import javax.swing.JTextPane;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.print.PrinterException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle.ComponentPlacement;

public class BismillahKasir3 extends javax.swing.JFrame {

    private static final Object Datang = null;
    private JFrame framekasir3;
    private JTable table;
    private JTextField tfKODE;
    private JTextField tfNAMA;
    private JTextField tfSATUAN;
    private JTextField tfBANYAK;
    private JTextField tfTOTAL;
    private JTable tblKERANJANG;
    private JTextField TFBAYAR;
    private JTextField tfKEMBALI;
    private JTextField tfSUM;

    LinkedList <String> namaa = new LinkedList<String>();
    LinkedList <String> noohp = new LinkedList<String>();
    LinkedList <String> kodebuku = new LinkedList<String>();
    LinkedList <String> judulbuku = new LinkedList<String>();
    static linked buku = new linked();
    static node chatta = new node();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    BismillahKasir3 window = new BismillahKasir3();
                    window.framekasir3.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public BismillahKasir3() {
        initialize();
    }


    /**
     * Initialize the contents of the frame.
     */

    private void initialize() {
        framekasir3 = new JFrame();
        framekasir3.setUndecorated(true);
        framekasir3.setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\LENOVO\\eclipse-workspace\\2018Etugasakhir\\STATIONARY4.png"));
        framekasir3.setTitle("Stationaries CORNER");
        framekasir3.setBackground(SystemColor.activeCaption);
        framekasir3.getContentPane().setBackground(Color.BLUE);
        framekasir3.getContentPane().setFont(new Font("Berlin Sans FB", Font.BOLD, 25));
        framekasir3.setBounds(150, 50, 800, 562);
        framekasir3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        framekasir3.getContentPane().setLayout(null);

        JButton btnHitung = new JButton("Hitung");

        JButton btnBuatNota = new JButton("Buat Nota");
        btnBuatNota.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            try {
                tblKERANJANG.print();
            }
                catch(PrinterException e){
                    e.printStackTrace();
                }

            }
        });

        Scanner input = new Scanner(System.in);

        buku.tambahakhir("BUKU", "A001", 13000);
        buku.tambahakhir("5 cm", "A002", 2000);
        buku.tambahakhir("1000 Muslim Mengejar Bintang", "A003", 3000);
        buku.tambahakhir("bismillah", "A004", 5000);

        JTextPane textPane = new JTextPane();
        textPane.setBounds(320, 258, 461, 193);
        framekasir3.getContentPane().add(textPane);
        btnBuatNota.setFont(new Font("Tahoma", Font.PLAIN, 14));
        btnBuatNota.setBounds(438, 520, 104, 29);
        framekasir3.getContentPane().add(btnBuatNota);

        tfKEMBALI = new JTextField();
        tfKEMBALI.setFont(new Font("Tahoma", Font.PLAIN, 14));
        tfKEMBALI.setEditable(false);
        tfKEMBALI.setBounds(376, 209, 116, 29);
        framekasir3.getContentPane().add(tfKEMBALI);
        tfKEMBALI.setColumns(10);


        JButton btnBayar = new JButton("Bayar");
        btnBayar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    int a, b, kembalian;
                    a=Integer.parseInt(tfSUM.getText());
                    b=Integer.parseInt(TFBAYAR.getText());
                    if(b<a) {
                        JOptionPane.showMessageDialog(null, "Inputan Salah!!");
                        TFBAYAR.setText("");
                        tfKEMBALI.setText("");
                    }
                    else {
                    kembalian = b-a;
                    tfKEMBALI.setText(Integer.toString(kembalian));
                    }

                }
                    catch(Exception e1) {
                        JOptionPane.showMessageDialog(null, "Inputan Salah!!");
                        tfBANYAK.setText("");
                        tfTOTAL.setText("");

                    }
            }
        });
        btnBayar.setFont(new Font("Tahoma", Font.PLAIN, 14));
        btnBayar.setBounds(685, 173, 82, 23);
        framekasir3.getContentPane().add(btnBayar);

        TFBAYAR = new JTextField();
        TFBAYAR.setFont(new Font("Tahoma", Font.PLAIN, 14));
        TFBAYAR.setBounds(549, 170, 126, 29);
        framekasir3.getContentPane().add(TFBAYAR);
        TFBAYAR.setColumns(10);

        tblKERANJANG = new JTable();
        tblKERANJANG.setFont(new Font("Tahoma", Font.PLAIN, 10));
        tblKERANJANG.setModel(new DefaultTableModel(
            new Object[][] {
                {null, null, null},
            },
            new String[] {
                "Barang", "Jumlah", "Total"
            }
        ) {
            boolean[] columnEditables = new boolean[] {
                false, false, false
            };
            public boolean isCellEditable(int row, int column) {
                return columnEditables[column];
            }
        });
        tblKERANJANG.getColumnModel().getColumn(0).setPreferredWidth(100);
        tblKERANJANG.getColumnModel().getColumn(1).setPreferredWidth(15);
        tblKERANJANG.getColumnModel().getColumn(2).setPreferredWidth(15);
        tblKERANJANG.setShowVerticalLines(false);
        tblKERANJANG.setShowHorizontalLines(false);
        tblKERANJANG.setShowGrid(false);
        tblKERANJANG.setBorder(null);
        tblKERANJANG.setBounds(99, 365, 203, 151);
        framekasir3.getContentPane().add(tblKERANJANG);


        tfKODE = new JTextField();
        tfKODE.setFont(new Font("Tahoma", Font.PLAIN, 14));
        tfKODE.setBounds(99, 62, 197, 29);
        framekasir3.getContentPane().add(tfKODE);
        tfKODE.setColumns(10);

        linesearch uwu = new linesearch();
        JButton btnCari = new JButton("Cari");
        btnCari.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                chatta = uwu.search(buku, tfKODE.getText());
                if(uwu.indeks ==-1) {
                    JOptionPane.showMessageDialog(null, "Judul buku dengan kode " +chatta.kode+ " tidak ditemukan");
                    tfNAMA.setText("");
                    tfSATUAN.setText("");
                }
                else {
                    tfNAMA.setText("" + chatta.judul);
                    tfSATUAN.setText("" +chatta.harga);
                }
            }
        });
        btnCari.setFont(new Font("Tahoma", Font.PLAIN, 14));
        btnCari.setBounds(99, 96, 89, 23);
        framekasir3.getContentPane().add(btnCari);

        JButton btnReset = new JButton("Reset");
        btnReset.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                tfKODE.setText("");
                tfNAMA.setText("");
                tfSATUAN.setText("");
                tfBANYAK.setText("");
                tfTOTAL.setText("");
            }
        });
        btnReset.setFont(new Font("Tahoma", Font.PLAIN, 14));
        btnReset.setBounds(207, 96, 89, 23);
        framekasir3.getContentPane().add(btnReset);

        tfNAMA = new JTextField();
        tfNAMA.setFont(new Font("Tahoma", Font.PLAIN, 10));
        tfNAMA.setEditable(false);
        tfNAMA.setBounds(99, 129, 197, 29);
        framekasir3.getContentPane().add(tfNAMA);
        tfNAMA.setColumns(10);

        tfSATUAN = new JTextField();
        tfSATUAN.setFont(new Font("Tahoma", Font.PLAIN, 14));
        tfSATUAN.setEditable(false);
        tfSATUAN.setBounds(99, 169, 197, 29);
        framekasir3.getContentPane().add(tfSATUAN);
        tfSATUAN.setColumns(10);

        tfBANYAK = new JTextField();
        tfBANYAK.setFont(new Font("Tahoma", Font.PLAIN, 14));
        tfBANYAK.setBounds(99, 209, 197, 29);
        framekasir3.getContentPane().add(tfBANYAK);
        tfBANYAK.setColumns(10);


        JButton btnAdd = new JButton("Masuk Keranjang");
        btnAdd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                DefaultTableModel dt = (DefaultTableModel) tblKERANJANG.getModel();
                Vector v = new Vector();

                v.add(tfNAMA.getText());
                v.add(tfBANYAK.getText());
                v.add(tfSATUAN.getText());

                dt.addRow(v);   
                }

                catch(Exception e1) {
                    JOptionPane.showMessageDialog(null, "Pilih barang terlebih dahulu!!");
                }

            }
        });
        btnAdd.setFont(new Font("Tahoma", Font.PLAIN, 14));
        btnAdd.setBounds(124, 325, 150, 29);
        framekasir3.getContentPane().add(btnAdd);

        tfTOTAL = new JTextField();
        tfTOTAL.setFont(new Font("Tahoma", Font.PLAIN, 14));
        tfTOTAL.setEditable(false);
        tfTOTAL.setBounds(99, 289, 197, 29);
        framekasir3.getContentPane().add(tfTOTAL);
        tfTOTAL.setColumns(10);

        btnHitung.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    int a, b, hasiltotal;
                    a=Integer.parseInt(tfSATUAN.getText());
                    b=Integer.parseInt(tfBANYAK.getText());
                    hasiltotal = a*b;

                    tfTOTAL.setText(Integer.toString(hasiltotal));
                }
                    catch(Exception e1) {
                        JOptionPane.showMessageDialog(null, "Inputan Salah!!");
                        tfBANYAK.setText("");
                        tfTOTAL.setText("");

                    }
            }

        });
        btnHitung.setFont(new Font("Tahoma", Font.PLAIN, 14));
        btnHitung.setBounds(124, 249, 150, 29);
        framekasir3.getContentPane().add(btnHitung);

        JButton btnBATAL = new JButton("Batal");
        btnBATAL.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        btnBATAL.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                try {
                DefaultTableModel dataModel = (DefaultTableModel) tblKERANJANG.getModel();    
                if (tblKERANJANG.getRowCount() > 0) {
                    for (int i = tblKERANJANG.getRowCount() - 1; i > -1; i--) {
                        dataModel.removeRow(i);
                    }

                    tfKODE.setText("");
                    tfSUM.setText("");
                    tfNAMA.setText("");
                    tfSATUAN.setText("");
                    tfBANYAK.setText("");
                    tfTOTAL.setText("");
                    TFBAYAR.setText("");
                    tfKEMBALI.setText("");
            }
            }
                catch(Exception e1) {
                    JOptionPane.showMessageDialog(null, "Tidak ada daftar pada keranjang!");
                    tfKODE.setText("");
                    tfSUM.setText("");
                    tfNAMA.setText("");
                    tfSATUAN.setText("");
                    tfBANYAK.setText("");
                    tfTOTAL.setText("");
                    TFBAYAR.setText("");
                    tfKEMBALI.setText("");
                }
            }
        });
        btnBATAL.setFont(new Font("Tahoma", Font.PLAIN, 14));
        btnBATAL.setBounds(114, 527, 89, 23);
        framekasir3.getContentPane().add(btnBATAL);

        JButton btnX = new JButton("X");
        btnX.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int s= JOptionPane.showConfirmDialog(null, "Apakah anda ingin menutup program? ");
                if(s==JOptionPane.YES_OPTION)
                {System.exit(0);
                }
            }
        });
        btnX.setFont(new Font("Tahoma", Font.PLAIN, 18));
        btnX.setBounds(731, 16, 50, 35);
        framekasir3.getContentPane().add(btnX);


        JButton btnTotal = new JButton("Total");
          btnTotal.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnHitungActionPerformed(evt);
                }
            });
        btnTotal.setFont(new Font("Tahoma", Font.PLAIN, 14));
        btnTotal.setBounds(213, 527, 89, 23);
        framekasir3.getContentPane().add(btnTotal);

        tfSUM = new JTextField();
        tfSUM.setFont(new Font("Tahoma", Font.PLAIN, 14));
        tfSUM.setEditable(false);
        tfSUM.setBounds(366, 170, 126, 29);
        framekasir3.getContentPane().add(tfSUM);
        tfSUM.setColumns(10);

        JLabel label = new JLabel("");
        label.setIcon(new ImageIcon("C:\\Users\\LENOVO\\eclipse-workspace\\2018Etugasakhir\\STATIONARY4.png"));
        label.setBounds(0, 0, 800, 560);
        framekasir3.getContentPane().add(label);
    }

    private void btnHitungActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHitungActionPerformed
        // TODO add your handling code here:
            int jumlahBaris = tblKERANJANG.getRowCount();
            int totalBiaya = 0;
            int jumlahBarang, hargaBarang;
            TableModel tabelModel;
            tabelModel = tblKERANJANG.getModel();
            for (int i=1; i<jumlahBaris; i++){
                jumlahBarang = Integer.parseInt(tabelModel.getValueAt(i, 1).toString());
                hargaBarang = Integer.parseInt(tabelModel.getValueAt(i, 2).toString());
                totalBiaya = totalBiaya + (jumlahBarang*hargaBarang);
            }
            tfSUM.setText(String.valueOf(totalBiaya));
        }
}

class node {
    String kode;
    String judul;
    int harga;
    node next;
    node prev;
}

class linesearch{
    int indeks;
    public node search(linked b, String kodee){
        indeks = -1;
        node cari = new node();
        cari.kode = kodee;
        node ganti = b.head;
        for (int i = 0; i< b.ukuran(); i++) {
            String smbrng = ganti.kode;
            if(kodee.equals(smbrng)) {
                indeks = i;
                cari.judul = ganti.judul;
                cari.harga = ganti.harga;}
            ganti = ganti.next;
            }
        return cari;
            }
        }

class linked {
    node head;

    public int ukuran() {
        node em = head;
        int size = 0;
        while (em!= null) {
            size = size+1;
            em = em.next;}
        return size;
    }

    public node indeks(int a) {
        node ma = head;
        if (a<ukuran()&& a>= 0) {
            for (int i = 0; i<a; i++) {
                ma = ma.next;
            }
            return ma;}
        else return null;}

    public boolean kosong() {
        boolean b = true;
        if(ukuran()==0) {
            b = true;
        }
        else 
            b = false;
        return b;
    }

    public void tambahawal (String a, String b, int c) {
        node em = new node();
        em.judul = a;
        em.kode = b;
        em.harga = c;
        em.next = head;
        em.prev = null;
        if(head!=null)
            head.prev = em;
            head = em;
            }

    public void tambahakhir (String a, String b, int c) {
        node em = new node();
        em.judul = a;
        em.kode = b;
        em.harga = c;
        node ganti = head;
        if(kosong()) {
            tambahawal(a,b,c);
        }
        else {
            while(ganti.next!=null) {
                ganti = ganti.next;
                ganti.next = em;
                em.prev = ganti;
                em.next = null;
            }
        }
        }

        public void tampil() {
            node em = head;
            System.out.println("\n" +em.kode + "\t" + em.judul + "\t" + em.harga);
            em = em.next;
            while(em!= null) {
                System.out.println("\n" +em.kode+ "\t" + em.judul + "\t" + em.harga);
                em = em.next;
            }
    }

        public void hapusawal() {
            if(kosong()) {
            }
            else {
                head = head.next;
                head.prev = null;
                }
            }

        public void hapusakhir() {
            node em = head;
            if(kosong()) {
            }
            else {
                for (int i = 0;i<ukuran()-1; i++) {
                    em = em.next;
                }
                em.next.prev = null;
                em.next = null;
            }
        }

        public boolean isi(String a) {
            boolean c = false;
            int b = 0;
            node ganti = head;
            for(int i = 0; i<ukuran(); i++) {
                if(ganti.kode ==a) {
                    b = b+1;
                }
                ganti = ganti.next;
            }
            if(b<0) {
                c = true;}
            return c;
            }

            public void hapussemua() {
                head = null;
            }


    }

事实是,当我搜索“ A001”时,结果很好。但是当我搜索“ A002”或“ A003”或“ A004”时,结果不存在(但没有给我一个错误)。

你能帮我吗? 非常感谢。

评论