银行系统使用HashMap和链接列表

时间:2013-12-19 10:32:04

标签: java io linked-list hashmap

如果有人能给我一些关于如何继续的指导,那么试图让它在EnterTransaction jpane中, - 一旦用户输入了相关信息,包括accountID,日期时间就是创建事务(使用java.util.Date),并且一旦输入帐户ID,就会显示帐户持有人的姓名,并显示所有详细信息,并输入交易金额(其中为负数)号码从帐户中扣除)

问题: - 链接列表和散列映射是收集客户端和事务数据的最佳方式吗?

- 在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);
    }

}

}

}

0 个答案:

没有答案