ATM使用数据结构,无需数据库和文件处理

时间:2019-02-13 15:02:40

标签: java swing

我使用joptionpane创建了客户class.store数据的数组列表。我如何在arraylist的特定索引处获取数据以更新客户数据。 这是我的客户类别

  public class Customer_Data {
       public int account_num,starting_balance=0 ;
       public String pincode="",name="",type="",account_num1="";
       public Object status;
    }

用于创建删除和更新客户的管理员类。

 public class ADMIN extends javax.swing.JFrame {

    /**
     * Creates new form ADMIN
     */
    public ADMIN() {
        this.user = new ArrayList<Customer_Data>();
        initComponents();
    }
    List<Customer_Data> user;
    public void create_account() {
        Customer_Data a = new Customer_Data();
        a.account_num = (user.size() - 1)+1; 
        String[] s0 = {"Current", "Savings"};
        String[] s01 = {"Active", "Deactive"};
        String s = "";        a.name=JOptionPane.showInputDialog("Enter Name");
        String s1 = "";
        //a.pincode = JOptionPane.showInputDialog("Enter PinCode", s1);
        do {
            a.pincode = JOptionPane.showInputDialog("Enter 5 digit PinCode", s1);            
        } while (!a.pincode.matches("[0-9]{5}")); String s2="";
        s2 = JOptionPane.showInputDialog("Enter Starting Balance ");
        a.starting_balance = Integer.parseInt(s2);
        //String s3 = "";
        a.status = (String) JOptionPane.showInputDialog(null, "Select Status...", "Status", JOptionPane.QUESTION_MESSAGE, null, s01, s01[0]);
        a.type = (String) JOptionPane.showInputDialog(null, "Select Type...", "Type", JOptionPane.QUESTION_MESSAGE, null, s0, s0[0]);
        user.add(a);
        for (int i = 0; i < user.size(); i++) {
            Customer_Data var = user.get(i);
            JOptionPane.showMessageDialog(null, var.account_num + "\n" + var.name + "\n" + var.pincode + "\n" + var.status + "\n" + var.type, "sad", JOptionPane.PLAIN_MESSAGE);
        }
    }

如何获取搜索功能中特定索引处的数据

    public void Search() {
        String s1 = "", s2 = "";
        s1 = JOptionPane.showInputDialog("Enter Account Number u want to ", s2);
        for (int i = 0; i < user.size(); i++) {
            if (user.contains(s1)) {
                for (int u = 0; u < user.indexOf(i); u++) {
                    Customer_Data var = user.get(u);
                    JOptionPane.showMessageDialog(null, var.account_num + "\n" + var.name + "\n" + var.pincode + "\n" + var.status + "\n" + var.type, "sad", JOptionPane.PLAIN_MESSAGE);
                }
            } else {
                JOptionPane.showMessageDialog(null, "Not Fount");
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

您正在寻找List<E>方法listOject.indexOf(theSearchObject)。 它返回int作为第一个匹配发生索引,如果不包含则返回-1。

答案 1 :(得分:0)

由于您的问题主要是关于搜索功能的,因此我将尝试演示如何解决它。在下面的程序中查看Search()函数。

我仅遍历Customer_Data列表一次。对于列表中的每个Customer_Data,我得到account_num并将其与用户输入的值进行比较。

为避免混淆,我没有更改您使用的任何标识符名称。但是我强烈建议您使用Java命名约定。例如:
使用Admin代替ADMIN
使用CustomerData代替Customer_Data
使用search()代替Search()
使用accountNum代替account_num等。

import javax.swing.JOptionPane;
import java.util.ArrayList;
import java.util.List;

public class ADMIN {

  private List<Customer_Data> user;

  public static void main(String[] args) {
    ADMIN admin = new ADMIN();
    admin.user = new ArrayList<>();

    Customer_Data customer1 = new Customer_Data();
    customer1.account_num = 123;
    customer1.name = "Kevin";
    admin.user.add(customer1);

    Customer_Data customer2 = new Customer_Data();
    customer2.account_num = 456;
    customer2.name = "Sally";
    admin.user.add(customer2);

    Customer_Data customer3 = new Customer_Data();
    customer3.account_num = 789;
    customer3.name = "Peter";
    admin.user.add(customer3);

    admin.Search();
  }

  public void Search() {
    String s1 = "", s2 = "";
    s1 = JOptionPane.showInputDialog("Enter Account Number u want to ", s2);
    boolean found = false;
    for (int i = 0; i < user.size(); i++) {
      Customer_Data var = user.get(i);
      if (var.account_num == Integer.parseInt(s1)) {
        JOptionPane.showMessageDialog(null, var.account_num + "\n" + var.name, "sad", JOptionPane.PLAIN_MESSAGE);
        found = true;
      }
    }
    if (!found) {
      JOptionPane.showMessageDialog(null, "Not Fount");
    }
  }
}

class Customer_Data {
  public int account_num,starting_balance=0 ;
  public String pincode="",name="",type="",account_num1="";
  public Object status;
}