Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用HashMap和链表的银行系统_Java_Io_Linked List_Hashmap - Fatal编程技术网

Java 使用HashMap和链表的银行系统

Java 使用HashMap和链表的银行系统,java,io,linked-list,hashmap,Java,Io,Linked List,Hashmap,如果有人能给我一些关于如何继续的指导,我正在尝试获得它,以便在EnterTransaction jpane中,-一旦用户输入了包括accountID在内的相关信息,日期时间就是创建事务的时间(使用java.util.date),并且,输入账户ID后,应显示账户持有人的姓名及其所有详细信息,并输入交易金额(负数从账户中借记) 问题: -链表和hashmap是收集客户机和事务数据的最佳方式吗 -在createCustomerAccount和enterTransaction方法上达到了一个完整的块,不

如果有人能给我一些关于如何继续的指导,我正在尝试获得它,以便在EnterTransaction jpane中,-一旦用户输入了包括accountID在内的相关信息,日期时间就是创建事务的时间(使用java.util.date),并且,输入账户ID后,应显示账户持有人的姓名及其所有详细信息,并输入交易金额(负数从账户中借记)

问题: -链表和hashmap是收集客户机和事务数据的最佳方式吗

-在createCustomerAccount和enterTransaction方法上达到了一个完整的块,不太确定我做错了什么/我在做什么-任何帮助都会很好

public class MainMenu extends JFrame {
// data fields
private ArrayList<Client> clientCollection = new ArrayList<Client>();
private JPanel contentPane;

HashMap<String,CurrentAccount> CAccMap= new HashMap<String,CurrentAccount>(); //enter a key and get an output
LinkedList<Transaction> TransactionList=new LinkedList<Transaction>(); //for transactions
//myList.add("a");

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

/**
 * Create the frame.
 */
public MainMenu() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(new GridLayout(3, 0, 0, 0));

    JButton btnReadClientData = new JButton("Read Client Data");
    btnReadClientData.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                readClientData();
            }
        });
    contentPane.add(btnReadClientData);

    JButton btnCreateCurrentAccount = new JButton("Create Current Account");
    btnCreateCurrentAccount.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                createCurrentAccount();
            }
        });
    contentPane.add(btnCreateCurrentAccount);

    JButton btnEnterTransaction = new JButton("Transaction");
    btnEnterTransaction.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0){
                //enterTransaction();
            }
        });
    contentPane.add(btnEnterTransaction);
}

/*
public void enterTransaction()
{
String accountID = JOptionPane.showInputDialog(this,"Enter valid accountID", "Add CurrentAccount", 0);
if(CAccMap.CAccMap(accountID)){
JOptionPane.showMessageDialog(this, "Account ID: " + accountID ,"Details",0);
CAccMap.getCurrentAccount(accountID);
}
else{
JOptionPane.showMessageDialog(this, "No account Found " ,"Error",0);
}

}

//public void isCurrentAccount(accountID){
//  cAccMap(contains key(accountid)
//}

public void getCurrentAccount(String accountID){
CurrentAccount CAccMap = CurrentAccount.get(accountID);
Client C = CAccMap.getClient();
String famName = C.getFamilyName();
System.out.println(famName);
}
 */
public void readClientData() {
    JFileChooser chooser = new JFileChooser();
    chooser.setMultiSelectionEnabled(false);
    int opt = chooser.showOpenDialog(this);
    if (opt == JFileChooser.APPROVE_OPTION) {
        // read data from file
        File infile = chooser.getSelectedFile();
        System.out.println("Reading data from " + infile.getAbsolutePath());
        try {
            BufferedReader rdr = new BufferedReader(new FileReader(infile));
            String line;

            int id = 1;
            line = rdr.readLine();
            while (line != null) {
                processNewClient(id, line);
                id++;
                line = rdr.readLine();
            }

            rdr.close();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "File Not Found",
                "Unable to open " + infile.getAbsolutePath(),
                JOptionPane.ERROR_MESSAGE);
        }
    }
}

public void processNewClient(int id, String data) {

    String[] parts = data.split(" ");
    try {
        String familyName = parts[0];
        String firstName = parts[1];
        int houseNumber = Integer.parseInt(parts[2]);
        String postCode = parts[3];

        if(validateNewClient(familyName, firstName, houseNumber, postCode)){
            Client c = new Client(id, familyName, firstName, houseNumber,
                    postCode);
            System.out.println("Added " + c);
            clientCollection.add(id, c);
        }
    } catch (NumberFormatException nfe) {
        JOptionPane.showMessageDialog(this, "Invalid house number",
            "Error", JOptionPane.ERROR_MESSAGE);
    }
}

public boolean validateNewClient(String familyName, String firstName, int houseNumber, String postCode)
{

    if(familyName.equals (""))
    {
        JOptionPane.showMessageDialog(this, "Error, family Name not entered", "Error", JOptionPane.ERROR_MESSAGE);
        return false;
    }

    else if(firstName.equals (""))
    {
        JOptionPane.showMessageDialog(this, "Error, First Name not entered", "Error", JOptionPane.ERROR_MESSAGE);
        return false;
    }
    else if(houseNumber <= 0)
    {
        JOptionPane.showMessageDialog(this, "Error, House number invalid", "Error", JOptionPane.ERROR_MESSAGE);
        return false;
    }
    else if(postCode.equals (""))/*[A-Z]{1,2}[0-9][0-9A-Z]? [0-9]{3}[A-Z]{4-5}*/
    {
        JOptionPane.showMessageDialog(this, "Error, Post code not in correct format", "Error", JOptionPane.ERROR_MESSAGE);
        return false;
    }
    else{
        return true;
    }
}

public void createCurrentAccount() {
    Object[] clients = clientCollection.toArray();
    Client cl;

    Object clientObj = JOptionPane.showInputDialog(this, "Select client", "Add Current Account", JOptionPane.INFORMATION_MESSAGE, null, clients, clients[0]);
    cl = (Client) clientObj;
    System.out.println("Selected " + cl);
    String sortCode = "01-02-03";
    String accountID = JOptionPane.showInputDialog(this,
            "Enter valid accountID", "Add CurrentAccount", 0);

    if (Account.validateID(accountID))  {
        CurrentAccount ca = new CurrentAccount(cl,accountID, sortCode, 0);
        CAccMap.put(accountID,ca);//Using the .put method any newly created accounts are added to the hashmap and linked with their client
        System.out.println("Account Created");

    } else {
        JOptionPane.showMessageDialog(this, "Invalid account ID " + accountID,
                "Error", JOptionPane.ERROR_MESSAGE);
    }

}
public类主菜单扩展JFrame{
//数据字段
private ArrayList clientCollection=new ArrayList();
私有JPanel内容窗格;
HashMap CAccMap=new HashMap();//输入一个键并获得一个输出
LinkedList TransactionList=新建LinkedList();//用于事务
//myList.添加(“a”);
//启动应用程序。
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
公开募捐{
试一试{
主菜单框=新的主菜单();
frame.setVisible(true);
}捕获(例外e){
e、 printStackTrace();
}
}
});
}
/**
*创建框架。
*/
公共主菜单(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
立根(100100450300);
contentPane=newjpanel();
setboorder(新的EmptyBorder(5,5,5,5));
setContentPane(contentPane);
setLayout(新的GridLayout(3,0,0,0));
JButton btnReadClientData=新JButton(“读取客户端数据”);
btnReadClientData.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件arg0){
readClientData();
}
});
contentPane.add(btnReadClientData);
JButton btnCreateCurrentAccount=新JButton(“创建当前帐户”);
btnCreateCurrentAccount.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件arg0){
createCurrentAccount();
}
});
contentPane.add(btnCreateCurrentAccount);
JButton btnEnterTransaction=新JButton(“事务”);
btnEnterTransaction.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件arg0){
//enterTransaction();
}
});
contentPane.add(btEntertransaction);
}
/*
公开交易()
{
字符串accountID=JOptionPane.showInputDialog(这是“输入有效的accountID”,“添加当前帐户”,0);
if(CAccMap.CAccMap(accountID)){
showMessageDialog(此“帐户ID:+accountID”,“详细信息”,0);
CAccMap.getCurrentAccount(accountID);
}
否则{
showMessageDialog(此“未找到帐户”、“错误”,0);
}
}
//public void为当前帐户(accountID){
//cAccMap(包含密钥(accountid)
//}
公共无效getCurrentAccount(字符串accountID){
CurrentAccount CAccMap=CurrentAccount.get(accountID);
Client C=CAccMap.getClient();
字符串famName=C.getFamilyName();
System.out.println(famName);
}
*/
public void readClientData(){
JFileChooser chooser=新的JFileChooser();
chooser.setMultiSelectionEnabled(false);
int opt=chooser.showOpenDialog(此);
if(opt==JFileChooser.APPROVE_选项){
//从文件中读取数据
File infile=chooser.getSelectedFile();
System.out.println(“从“+infle.getAbsolutePath()读取数据”);
试一试{
BufferedReader rdr=新的BufferedReader(新文件读取器(infle));
弦线;
int-id=1;
line=rdr.readLine();
while(行!=null){
processNewClient(id,行);
id++;
line=rdr.readLine();
}
rdr.close();
}捕获(例外e){
showMessageDialog(此“未找到文件”,
“无法打开”+infle.getAbsolutePath(),
JOptionPane.ERROR\u消息);
}
}
}
public void processNewClient(int-id,字符串数据){
String[]parts=data.split(“”);
试一试{
字符串familyName=parts[0];
字符串firstName=parts[1];
int houseNumber=Integer.parseInt(部分[2]);
字符串邮政编码=零件[3];
if(validateNewClient(家庭名、名、门牌号、邮政编码)){
客户c=新客户(id、familyName、firstName、houseNumber、,
邮政编码);;
系统输出打印项次(“添加”+c);
clientCollection.add(id,c);
}
}捕获(NumberFormatException nfe){
JOptionPane.showMessageDialog(此“无效门牌号”,
“错误”,作业窗格。错误消息);
}
}
公共布尔validateNewClient(字符串familyName、字符串firstName、int houseNumber、字符串邮编)
{
if(familyName.equals(“”)
{
showMessageDialog(此“错误,未输入姓氏”,“错误”,JOptionPane.Error\u消息);
返回false;
}
else if(firstName.equals(“”)
{
showMessageDialog(此“错误,未输入名字”,“错误”,JOptionPane.Error\u消息);
返回false;
}

else if(houseNumber)问题在哪里?现在问题已经出来了,很抱歉我无法将accountID和客户端ca添加到集合中的是createCurrentAccount方法不应该存在的是for循环,但我不知道应该在那里放置什么,而不是标题(accountID=e.getKey())应该是if(accountID.equals(e.getKey())现在您只是在重新分配accountIdit,它应该是if语句,在开始时验证它是否有效,然后将其添加到列表else pri中