添加&使用扫描仪从列表打印

时间:2014-12-14 09:53:52

标签: java

我正在创建一个项目,允许用户创建,删除或显示银行账户以及存款,取款或打印交易。

当用户提供帐户ID时,我不知道如何使用扫描仪存取,取款和打印交易。

主要

  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 (input == 0) {
      System.out.println("");
      System.out.println("Please choose one of the following: ");
      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");
      selection = scan.nextInt();

      switch (selection) {

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

        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: ");
          bank.removeAccounts(bankProcess.openNewAccount(bank.getAccounts(), bankAccount));
          break;

        case 4:
          System.out.println("[4] Deposit has been selected");

          break;

        case 5:
          System.out.println("[5] Withdraw has been selected");

          break;

        case 6:
          System.out.println("[6] Print Transaction has been selected");

          break;

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

      }
    }
  }

班级银行

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 void removeAccounts(TreeMap < Integer, BankAccount > accounts) {
    this.bankAccounts = accounts;
  }
}

班级BankProcess

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

班级BankAccount

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

类交易

public class Transaction {
  private int transactionId;
  private String transactionType;
  private double transactionAmount;
  private double moneyBeforeTransaction;
  private double moneyAfterTransaction;

  public Transaction() {}

  public Transaction(int transactionId, String transactionType, double transactionAmount, double moneyBeforeTransaction) {
    this.transactionId = transactionId;
    this.transactionType = transactionType;
    this.transactionAmount = transactionAmount;
    this.moneyBeforeTransaction = moneyBeforeTransaction;
  }

  public int getTransactionId() {
    return transactionId;
  }

  public void setTransactionId(int transactionId) {
    this.transactionId = transactionId;
  }

  public String getTransactionType() {
    return transactionType;
  }

  public void setTransactionType(String transactionType) {
    this.transactionType = transactionType;
  }

  public double getTransactionAmount() {
    return transactionAmount;
  }

  public void setTransactionAmount(double transactionAmount) {
    this.transactionAmount = transactionAmount;
  }

  public double getMoneyBeforeTransaction() {
    return moneyBeforeTransaction;
  }

  public void setMoneyBeforeTransaction(double moneyBeforeTransaction) {
    this.moneyBeforeTransaction = moneyBeforeTransaction;
  }

  public double getMoneyAfterTransaction() {
    return moneyAfterTransaction;
  }

  public void setMoneyAfterTransaction(double moneyAfterTransaction) {
    this.moneyAfterTransaction = moneyAfterTransaction;
  }

  //Override the toString() method of String ? 
  public String toString() {
    return "Transaction ID : " + this.transactionId +
      " Transaction Type : " + this.transactionType +
      " Transaction Amount : " + this.transactionAmount +
      " Money Before Transaction : " + this.moneyBeforeTransaction +
      " Money After Transaction : " + this.moneyAfterTransaction;
  }

}

类TransactionProcess

public class TransactionProcess {

  //Always Provide another class for process.
  //Pass the bankAccount of the user
  public void deposit(BankAccount bankAccount, double depositAmount) {
    //Get the CurrentBalance
    double currentBalance = bankAccount.getCurrentBalance();

    //First Argument : set the Id of transaction
    //Second Argument : set the Type of Transaction
    //Third Argument : set the TransactionAmount 
    //Fourth Argument : set the Balance Before the transaction (for record purposes)
    Transaction transaction = new Transaction(bankAccount.getTransactions().size(), "Deposit", depositAmount, currentBalance);

    if (depositAmount <= 0) {
      System.out.println("Amount to be deposited should be positive");
    } else {
      //Set the updated or transacted balance of bankAccount.
      bankAccount.setCurrentBalance(currentBalance + depositAmount);
      //then set the MoneyAfterTransaction
      transaction.setMoneyAfterTransaction(bankAccount.getCurrentBalance());
      //after the transaction add it to the list of Transaction of bankAccount
      bankAccount.getTransactions().add(transaction);
    }

  }

  // Explanation same as above
  public void withdraw(BankAccount bankAccount, double withdrawAmount) {
    double currentBalance = bankAccount.getCurrentBalance();
    Transaction transaction = new Transaction(bankAccount.getTransactions().size(), "Withdraw", withdrawAmount, currentBalance);

    if (withdrawAmount <= 0) {
      System.out.println("Amount to be withdrawn should be positive");
    } else {
      if (currentBalance < withdrawAmount) {
        System.out.println("Insufficient balance");
      } else {
        bankAccount.setCurrentBalance(currentBalance - withdrawAmount);
        transaction.setMoneyAfterTransaction(bankAccount.getCurrentBalance());
        bankAccount.getTransactions().add(transaction);
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

存款撤回等操作的工作方式几乎相同,所以现在让我们专注于存款

您已经拥有适当的开关案例:

case 4: {
     // deposit
     break;
}

(顺便说一句:大括号{}在这里不是强制性的,它们只是帮助隔离代码,因此更容易阅读)

反正;要在银行帐户上存款,您首先需要帐号,然后您需要从银行获取帐户

 case 4: {
     int accountNumber =  scan.nextInt();  // user types account number
     TreeMap <Integer, BankAccount> bankAccounts = bank.getAccounts();  // bank returns all accounts (this is quite an insecure operation, it would be better to add a method to the bank class that returns only one account when passing the accountNumber
     BankAccount bankAccount = bankAccounts.get(accountNumber);
     break;
}

使用方法bank.getAccounts不是一个好的设计,因为该方法将从银行返回所有帐户。最好在班级Bank中使用一个返回单个帐户的方法。

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

因为这将封装逻辑以将一个帐户检索到Bank类。在那里你还可以添加一些错误处理逻辑,例如当帐号无效时。

因此,此时您拥有用户的帐户,并且您知道用户想要存入一些钱。你只需要问他想要存入多少钱。再次使用扫描仪让用户输入金额。

现在您已获得BankAccount以及Transaction或更高TransactionProcess

所需的金额
 TransactionProcess transactionProcess = new TransactionProcess();  // use the standard constructor to create an empty transactionProcess
 transactionProcess.deposit(bankAccount, amount);

如果我理解您的程序正确,方法deposit将已使用用户的BankAccount并在为帐户创建交易时添加资金。我对么?那你实际上已经完成了。

switch-chase 4的代码放在一起,如果有效,请使用类似的逻辑来实现withdraw

我希望这有帮助!

//编辑// 以回答您的其他问题,这就是我的想法:

在你的BankAccount课程中添加如下方法:

public void addTransaction(Transaction transaction){
   if(transactions.size() >= 6){  // test if the list has 6 or more transactions saved 
      transactions.remove(0);     // if so, then remove the first (it's the oldest)
   }
   transactions.add(transaction); // the new transaction is always added, no matter how many other transactions there are already in the list
}

这只是一个例子,但我认为你应该能够像这样控制每个银行账户的交易金额。此外,您现在可以简化通话:查看deposit课程中TransactionProcess方法的最后一行。你打电话给

bankAccount.getTransactions().add(transaction);  // takes the bank account, retrieves the list of transactions from the bank account and adds a transaction to the list

使用新方法addTransaction,您现在可以像这样编写它:

bankAccount.addTransaction(transaction);    // adds a transaction to the bank account

更清洁,你不觉得吗?现在,您可以将所有逻辑用于管理和添加新方法中的事务:)

相关问题