通过扫描仪从Treemap中删除元素

时间:2014-12-14 19:39:09

标签: java

我正在创建一个银行系统项目。

我希望用户能够使用扫描仪中的accountid删除银行帐户。 我遇到麻烦,因为我试图移动,例如首先是accountid(123),second accountid(999),它将始终删除最后创建的帐户。

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mainsample;
import java.util.*;
/**
 *
 * @author Khalid
 */
public class MainSample {

  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    BankProcess bankProcess = new BankProcess();
    TransactionProcess transactionProcess = new TransactionProcess();


    Bank bank = new Bank();
    BankAccount bankAccount = new BankAccount();

    int input = 0;
    int selection = 0;

    while (true) {
      System.out.println("");
      System.out.println("######## MENU ########");
      System.out.println("[1] Create an account");
      System.out.println("[2] Print all existing accounts");
      System.out.println("[3] Delete an account");
      System.out.println("[4] Deposit");
      System.out.println("[5] Withdraw");
      System.out.println("[6] Print transactions");
      System.out.println("[0] Exit");
      System.out.println("######################");
      System.out.println("Please choose one of the following: ");
      selection = scan.nextInt();

      switch (selection) {

        case 0:
          System.out.println("Exit Successful");
          System.exit(0);
          break;

        case 1:
          System.out.println("'[1] Create an account' has been selected.");
          System.out.print("Account Id: ");
          int accountId = scan.nextInt();
          scan.nextLine();

          System.out.print("Holder Name: ");
          String holderName = scan.nextLine();

          System.out.print("Holder Address: ");
          String holderAddress = scan.nextLine();

          System.out.print("Opening Balance: ");
          double openingBalance = scan.nextDouble();

          System.out.print("Open Date: ");
          String openDate = scan.next();

          bankAccount = new BankAccount(accountId, holderName, openingBalance, holderAddress, openDate);
          bank.setAccounts(bankProcess.openNewAccount(bank.getAccounts(), bankAccount));

          System.out.println("Successfully Added.");
          break;

        case 2:
          System.out.println("'[2] Display all existing accounts' has been selected");
          System.out.println("-----------------------------------------------------");
          bank.getAccounts().forEach((i, b) - > System.out.println(b));
          System.out.println("-----------------------------------------------------");
          break;

        case 3:
          System.out.println("[3] Delete an account has been selected");
          System.out.println("Enter the account ID: ");
          int accountNo = scan.nextInt();
          bank.removeAccounts(bankProcess.removeAccount(bank.getAccounts(), bankAccount));
          break;

        case 4:
          System.out.println("[4] Deposit has been selected");
          System.out.println("Enter account ID: ");
          int accountNumber = scan.nextInt();

          System.out.println("Enter deposit amount: ");
          double depositAmount = scan.nextDouble();

          transactionProcess.deposit(bankAccount, depositAmount);

          System.out.println(depositAmount + " has been deposited.");
          break;

        case 5:
          System.out.println("[5] Withdraw has been selected");
          System.out.println("Enter account ID: ");
          int accountNu = scan.nextInt();

          System.out.println("Enter withdraw amount: ");
          double withdrawAmount = scan.nextDouble();

          transactionProcess.withdraw(bankAccount, withdrawAmount);

          System.out.println(withdrawAmount + " has been withdrawed.");
          break;


        case 6:
          System.out.println("[6] Print Transaction has been selected");
          System.out.println("Enter account ID: ");
          int accountN = scan.nextInt();



          break;






        default:
          System.out.println("Your choice was not valid!");

      }

    }
  }
}

-

package mainsample;
import java.util.*;
/**
 *
 * @author Khalid
 */
public class Bank {

  private TreeMap < Integer, BankAccount > bankAccounts = new TreeMap < Integer, BankAccount > ();



  public TreeMap < Integer, BankAccount > getAccounts() {
    return bankAccounts;
  }
  public void setAccounts(TreeMap < Integer, BankAccount > accounts) {
    this.bankAccounts = accounts;
  }

  public BankAccount getAccount(Integer accountNumber) {
    return bankAccounts.get(accountNumber);
  }

  public void removeAccounts(TreeMap < Integer, BankAccount > accounts) {
    this.bankAccounts = accounts;
  }
}

-

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mainsample;
import java.util.*;
/**
 *
 * @author Khalid
 */
public class BankAccount {
  private int accountId;
  private String holderName;
  private String holderAddress;
  private String openDate;
  private double currentBalance;

  private List < Transaction > transactions = new ArrayList < Transaction > ();

  //Provide Blank Constructor
  public BankAccount() {}

  //Constructor with an arguments.
  public BankAccount(int accountNum, String holderNam, double currentBalance, String holderAdd, String openDate) {
    this.accountId = accountNum;
    this.holderName = holderNam;
    this.holderAddress = holderAdd;
    this.openDate = openDate;
    this.currentBalance = currentBalance;
  }

  // Always Provide Setter and Getters
  public int getAccountId() {
    return accountId;
  }
  public void setAccountId(int accountId) {
    this.accountId = accountId;
  }
  public String getHolderName() {
    return holderName;
  }
  public void setHolderName(String holderName) {
    this.holderName = holderName;
  }
  public String getHolderAddress() {
    return holderAddress;
  }
  public void setHolderAddress(String holderAddress) {
    this.holderAddress = holderAddress;
  }
  public String getOpenDate() {
    return openDate;
  }
  public void setOpenDate(String openDate) {
    this.openDate = openDate;
  }
  public double getCurrentBalance() {
    return currentBalance;
  }
  public void setCurrentBalance(double currentBalance) {
    this.currentBalance = currentBalance;
  }

  public List < Transaction > getTransactions() {
    return transactions;
  }

  public void setTransactions(List < Transaction > transactions) {
    this.transactions = transactions;
  }

  public String toString() {
    return "\nAccount number: " + accountId + "\nHolder's name: " + holderName + "\nHolder's address: " + holderAddress + "\nOpen Date: " + openDate + "\nCurrent balance: " + currentBalance;
  }
}

-

import java.util.*;
/**
 *
 * @author Khalid
 */
public class BankProcess {
    // return the  Updated list of BankAccounts
    public TreeMap<Integer,BankAccount> openNewAccount(TreeMap<Integer,BankAccount> bankAccounts,BankAccount bankAccount) {
        //Get the List of existing bank Accounts then add the new BankAccount to it.
        bankAccounts.put(bankAccount.getAccountId(), bankAccount);
        return bankAccounts;
    }

    public TreeMap<Integer,BankAccount> removeAccount(TreeMap<Integer,BankAccount> bankAccounts,BankAccount bankAccount) {
      bankAccounts.remove(bankAccount.getAccountId(), bankAccount);
        return bankAccounts;  
    }
}

1 个答案:

答案 0 :(得分:0)

您问题的简单解决方案是,您根本不会计算要删除的帐户。

从你的案例3开始:

int accountNo = scan.nextInt();
bank.removeAccounts(bankProcess.removeAccount(bank.getAccounts(), bankAccount));
break;

您可以从命令行获取帐号no accountNo,然后您就不会执行任何操作。您只需使用之前设置为bankAccount的值调用removeAccount。由于您之前确实添加了一些帐户,因此您只需删除最后添加的帐户即可。

对于那些想知道为什么编译的人:

Map接口定义了自Java 8以来的remove(Object,Object)方法,请参见此处:java.util.Map#remove(Object, Object)

它基本上确保只有在获得给定值时才从地图中删除密钥。