如何写在一个文件中的对象之间执行算术运算? [英] How to perform arithmetic operation between the objects written in a file?

查看:324
本文介绍了如何写在一个文件中的对象之间执行算术运算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的计划中,用户输入值以及获取存储在ArrayList中。 ArryList对象被写入到文件中。我用的文件I / O,对象I /写入O流和file.Now看书我想执行加法和减法的对象中( int或double )。这是从一个账户取钱应该使用其他帐户进行添加,其价值必须减去的,具有特定acount的添加量。最后我想该值必须被更新,以便它可以打印出并显示当前状态。我怎么能这样做呢?

这是主类:

 公共类BankApp_Assignment {    //静态INT numOfAcc = 0;
    公共静态无效的主要(字串[] args){
        的System.out.println(欢迎来我行\\ n \\ n!);        清单<&的BankAccount GT; bankAccounts =新的ArrayList<&的BankAccount GT;();
        清单<&的BankAccount GT; bankAccounts2 =新的ArrayList<&的BankAccount GT;();
        读写器RW =新读写器();
        而(真){
            的System.out.println(选择你的选项:\\ n
                    +1.创建新帐号\\ n
                    +2.存款/退出\\ n
                    +3.查看一个账户\\ n
                    +4.删除帐户\\ n
                    +5.查看所有帐户的\\ n
                    +6.返回菜单\\ n);            的System.out.println(************* \\ n
                    +************);            选项​​1 = sc.nextInt();
            sc.nextLine();
            //开关的情况下启动
            开关(选项1){
                情况1:
                    //创建帐号
                    的BankAccount bankAcc =新的BankAccount();
                    的System.out.println(输入全名:);
                    bankAcc.setName(sc.nextLine());
                    的System.out.println(选择帐号:);
                    bankAcc.setAccNum(sc.nextInt());
                    的System.out.println(选择初始金额:);
                    bankAcc.setInitiateAmount(sc.nextDouble());
                    //添加这些到ArrayList中
                    bankAccounts.add(bankAcc);
                    rw.writeToFile(bankAccounts);                    的System.out.println(------------- \\ n
                            +-------------);
                    打破;
                案例2:
                     bankAccounts2 =(列表<&的BankAccount GT;)rw.readFromFile();
                    //首先显示当前accouns信息
                    的System.out.println(名称\\ tAccount否\\ tInitial金额);
                    对于(的BankAccount的BankAccount:bankAccounts2){                        的System.out.println(的BankAccount);                    }                    的System.out.println(\\ t \\ t ......... \\ n
                            +\\ t \\ t .........);                    的System.out.println(对银行账户内汇款输入1 \\ n
                            +要存/取款在同一个帐户中输入2);
                    选项​​2 = sc.nextInt();
                    sc.nextLine();                    //内的switch-case启动
                    开关(选项2){
                        情况1:
                            / *
                            的BankAccount是setter和getter类
                             bankAccounts2是从文件中读取对象ArrayList中
                            * /
                            bankAccounts2 =(列表<&的BankAccount GT;)rw.readFromFile();
                            的BankAccount fromAcc = NULL;
                            的BankAccount toAcc = NULL;
                            的System.out.println(输入你想全部退出帐号:);                            withdraw_accNum = sc.nextInt();                            的System.out.println(请输入要提取的金额:);                            withdraw_amount = sc.nextDouble();
                            的System.out.println(请输入要存入帐号:);                            deposit_accNum = sc.nextInt(); //存款金额alwyas一样withdraw_amount                            //找到匹配ACC号:withdraw_accNum
                            对于(的BankAccount listItemsFirst:bankAccounts2){
                                //如果退出ACC NUM与给定匹配
                                如果(listItemsFirst.getAccNum()== withdraw_accNum){
                                    //存放
                                    fromAcc = listItemsFirst;
                                    打破;
                                }
                            }
                            //找到匹配ACC号:deposit_accNum
                            对于(的BankAccount listItemsSec:bankAccounts2){
                                //如果退出ACC NUM与给定匹配
                                如果(listItemsSec.getAccNum()== deposit_accNum){
                                    //存放
                                    toAcc = listItemsSec;
                                    打破;
                                }
                            }
                            //如果撤回量小于当前余额更大
                            如果(withdraw_amount> fromAcc.getInitialAmount()){
                                的System.out.println(回抽量低于初始量更大\\ n选择再次菜单。);
                                打破;
                            }
                            //减去和添加的拉出量
                            fromAcc.setInitiateAmount(fromAcc.getInitialAmount() - withdraw_amount);
                            toAcc.setInitiateAmount(toAcc.getInitialAmount()+ withdraw_amount);
                            的System.out.println(!DONE \\ T打印出​​来查看当前的状态。);
                            的System.out.println();                            打破;
                        案例2:在同一个账户//存款/取款
                            bankAccounts2 =(列表<&的BankAccount GT;)rw.readFromFile();
                            的BankAccount fromAcc_SameAcc = NULL;
                            的BankAccount toAcc_SameAcc = NULL;
                            的System.out.println(输入你想存款或撤回帐号:);
                            //读取accNum
                            depOrWithAccountNum = sc.nextInt();
                            的System.out.println(输入金额(撤回输入负值));
                            //读取量
                            depOrWithAmount = sc.nextDouble();
                            //检查在ArrayList中的匹配帐号
                            对于(的BankAccount listItemsThird:bankAccounts2){
                                如果(listItemsThird.getAccNum()== depOrWithAccountNum){
                                    fromAcc_SameAcc = listItemsThird;
                                    打破;
                                }
                            }                            如果(depOrWithAmount - 1所述; fromAcc_SameAcc.getInitialAmount()){
                                的System.out.println(再次撤回量小于电流量越大\\ n选择的菜单。);
                                打破;
                            }                            如果(depOrWithAmount℃,){//量为负
                                fromAcc_SameAcc.setInitiateAmount(fromAcc_SameAcc.getInitialAmount()+ depOrWithAmount);
                            }其他{
                                fromAcc_SameAcc.setInitiateAmount(fromAcc_SameAcc.getInitialAmount()+ depOrWithAmount);
                            }                            打破;
                    }                    //内的switch-case结束
                    的System.out.println(\\ n \\ n);
                    打破;                案例3:
                    //查看一个账户
                    bankAccounts2 =(列表<&的BankAccount GT;)rw.readFromFile();
                    的BankAccount viewOneAccountNum = NULL;                    的System.out.println(输入你想看的帐号:);
                    viewOneAcc = sc.nextInt();
                    的System.out.println(名称\\ tAccount否\\ tInitial金额);
                    对于(的BankAccount viewOneAccountProperty:bankAccounts2){                        如果(viewOneAccountProperty.getAccNum()== viewOneAcc){
                            // viewOneAccountNum = viewOneAccountProperty;
                            viewOneAccountNum = viewOneAccountProperty;                            的System.out.println(viewOneAccountNum);
                        }
                        的System.out.println();                    }
                    打破;
                情况4:
                     bankAccounts2 =(列表<&的BankAccount GT;)rw.readFromFile();
                    //的BankAccount AccToDel = NULL;
                    //删除帐户
                    迭代器<&的BankAccount GT;它= bankAccounts2.iterator();                    的System.out.println(输入你想删除的帐户:);
                    deleteAcc = sc.nextInt();                    而(it.hasNext()){
                        接下来的BankAccount = it.next();
                        如果(next.getAccNum()== deleteAcc){
                            it.remove();
                        }
                    }                    rw.writeToFile(bankAccounts2);
                    打破;                情况5:
                    //浏览所有帐户/打印出来                    bankAccounts2 =(列表<&的BankAccount GT;)rw.readFromFile();
                    的System.out.println(名称\\ tAccount否\\ tInitial金额);
                    对于(的BankAccount的BankAccount:bankAccounts2){                        的System.out.println(的BankAccount);                    }                    的System.out.println(\\ n \\ n);
                    打破;                情况6:
                    //放弃
                    返回;
            }            //开关两端的情况下
        }    }}

读写器:

 公共类读写器{
 公共无效将writeToFile(列表<&的BankAccount GT;账户){
   尝试{
            FOS的FileOutputStream =新的FileOutputStream(C:\\\\ \\\\用户\\\\文件\\\\的NetBeansProjects BankFile_assignment.txt);
            ObjectOutputStream的OOS =新的ObjectOutputStream(FOS);
            oos.writeObject(账户); //取的ArrayList
            oos.flush();
            oos.close();
            fos.close();        }赶上(例外五){
            e.printStackTrace();
        }
    }    公开名单<&的BankAccount GT; readFromFile(){
        清单<&的BankAccount GT; READDATA = NULL;
        尝试{            FIS的FileInputStream =新的FileInputStream(C:\\\\用户\\文档\\\\ \\\\的NetBeansProjects BankFile_assignment.txt);
            ObjectInputStream的OIS =新的ObjectInputStream(FIS);
            //做一个ArrayList让那些反对回来
            //数组列表            READDATA =(列表<&的BankAccount GT;)ois.readObject();         ois.close();
         fis.close();
        }赶上(例外五){
        e.printStackTrace();
        }
        //返回READDATA;
        返回READDATA;
    }
}

的BankAccount:

 包bankapp_assignment;的Bean;公共类的BankAccount实现Serializable {    私人字符串名称;
    私人诠释accNum;
    私人双initiateAmount;    //构造
    公众的BankAccount(){        this.name = NULL;
        this.accNum = 0;
        this.initiateAmount = 0;    }    公共无效setname可以(字符串名称){
        this.name =名称;    }    公共无效setAccNum(INT accNum){        this.accNum = accNum;
    }    公共字符串的getName(字符串名称){
        返回名称;    }
    公众诠释getAccNum(){
        返回accNum;
    }
    公共无效setInitiateAmount(双initiateAmount){
        this.initiateAmount = initiateAmount;
    }    公共双getInitialAmount(){
        返回initiateAmount;
    }    @覆盖
    公共字符串的toString(){
        返回名称+\\ t \\ t+ accNum +\\ t \\ t+ initiateAmount;
    }}


解决方案

  1. 您应该重构整个主要方法。如果我是你,我会用
    主要只午餐项目,在其他方法间歇处理。这样,您就能够遍布程序使用类字段,然后除以code到许多方法。

  2. 您应该划分主要beetween等方法,至少方法
    每个动作为例:

     案例1:
        创建帐号();
        打破;
    (......)
    公共无效的createAccount(){
    从你的情况SWICH 1 code
    }

    等。您仍然可以使用SWICH控制,而应该replece逻辑code到另一种方法。


  3. 当我运行code,输入/输出没有工作。您是否能够
        保存/载入文件?我只好恰克:

     文件fil​​e =新的文件(//文件路径);
    FOS的FileOutputStream =新的FileOutputStream(文件);
    OOS的ObjectOutput =新的ObjectOutputStream(FOS);

    与输入相同。然后它的工作。


  4. 现在,我们将尝试从文件BankAccounts处理操作。我改变一点点的code从案例2 /案例2:存款/收回,让我们看看吧:

      bankAccounts2 = rw.readFromFile(); //加载从文件列表
    编辑的BankAccount =新的BankAccount(); //创建新对象
    的System.out.println(输入你想存款或撤回帐号:);
    双输入= sc.nextInt(); //你并不需要创建新的变量,每次你把输入表单的用户
    对于(的BankAccount帐户:bankAccounts2){//在每个帐户列表
           如果(account.getAccNum()==输入)编辑=账户; //如果acccNum比赛,给编辑的参考选择账户
    }
    的System.out.println(输入金额(撤回输入负值));
    输入= sc.nextDouble();双重结果= edited.getInitialAmount()+输入; //运算的结果
    如果(结果和小于0){//检查是否有足够的钱帐户
        的System.out.println(再次撤回量小于电流量越大\\ n选择的菜单。);
        打破;
    }其他{
        edited.setInitiateAmount(结果); //如果有,设定新的价值
    }
    rw.writeToFile(bankAccounts2); // 保存存档


您可以实现类似的风格另一个操作。但尽管如此,你应该考虑将主要为其他方式,加入类字段等。

编辑:

有关在注释中的问题。

由于您使用的定金收回,对initialAmount的操作不会总是相同的正数和负数:initialAmount +输入(如果为负,是从量中减去),这样你就可以像一个案件对待它在code。在您的code您使用此行两次:

  fromAcc_SameAcc.setInitiateAmount(fromAcc_SameAcc.getInitialAmount()+ depOrWithAmount);

所以你已经可以这两种情况下合并成一个,以避免不必要的重复。所以当动作会有所不同唯一情况,就是当没有足够的钱在一个帐户,您可以通过检查:

 如果(depOrWithAmount  -  1 LT; fromAcc_SameAcc.getInitialAmount())

但我想我将无法正常工作,因为在撤出操作 depOrWithAmount 是负数,因此它总是比 fromAcc_SameAcc.getInitialAmount小()。您可以使用:

 如果(depOrWithAmount< 1  -  fromAcc_SameAcc.getInitialAmount())

(但我认为这是不是太可读)或:

 如果(Math.abs(depOrWithAmount)GT; fromAcc_SameAcc.getInitialAmount()){}

与initialAmout比较输入的绝对值。但是我用:

 双重结果= edited.getInitialAmount()+输入;
如果(结果℃,){...}

,因为它给了我结果变量,我可以重复使用,如果有足够的钱,以使新的价值 initialAmount 。如果结果是否定的,则意味着撤回量比initialAmount更大,所以疗法没有足够monay撤回

我希望你能找到一些有用的东西在我的职务。

In my program, users enter values and those get stored in arrayList. ArryList objects are written into a file. I have used file-i/o, object-i/o stream for writing and readin in file.Now I want to perform addition and subtraction among the objects (int or double). That is withdrawing money from one account should be added with another account, and their value must be subtracted and added with the particular acount's amount. And finally I want that value must be updated so that it can print out and show the current status. How could I do that?

This is the main class:

public class BankApp_Assignment {

    //static int numOfAcc = 0;
    public static void main(String[] args) {
        System.out.println("WELCOME TO OUR BANK!\n\n");

        List<BankAccount> bankAccounts = new ArrayList<BankAccount>();
        List<BankAccount> bankAccounts2 = new ArrayList<BankAccount>();
        ReaderWriter rw=new ReaderWriter();


        while (true) {
            System.out.println("Choose your option:\n"
                    + "1. Create new account\n"
                    + "2. Deposit/withdraw\n"
                    + "3. View One account\n"
                    + "4. Deleting an account\n"
                    + "5. View all the accounts\n"
                    + "6. Return to menu\n");

            System.out.println("*************\n"
                    + "************");

            option1 = sc.nextInt();
            sc.nextLine();
            //switch-case starts
            switch (option1) {
                case 1:
                    //create account
                    BankAccount bankAcc = new BankAccount();
                    System.out.println("Enter Full Name:");
                    bankAcc.setName(sc.nextLine());
                    System.out.println("Choose an Account Number:");
                    bankAcc.setAccNum(sc.nextInt());
                    System.out.println("Choose the initial amount:");
                    bankAcc.setInitiateAmount(sc.nextDouble());
                    //adding those into the arrayList
                    bankAccounts.add(bankAcc);
                    rw.writeToFile(bankAccounts);

                    System.out.println("-------------\n"
                            + "-------------");
                    break;
                case 2:
                     bankAccounts2=(List<BankAccount>)rw.readFromFile();
                    //First displaying the current accouns info
                    System.out.println("Name \tAccount No \tInitial Amount");
                    for (BankAccount bankAccount : bankAccounts2) {

                        System.out.println(bankAccount);

                    }

                    System.out.println("\t\t.........\n"
                            + "\t\t.........");

                    System.out.println("To transfer money within the bank accounts enter 1\n"
                            + "To deposit/withdraw money in the same account enter 2");
                    option2 = sc.nextInt();
                    sc.nextLine();

                    //inner switch-case starts
                    switch (option2) {
                        case 1:
                            /*
                            BankAccount is the class for setter and getter
                             bankAccounts2 is the arrayList for reading objects from file
                            */
                            bankAccounts2 = (List<BankAccount>) rw.readFromFile();
                            BankAccount fromAcc = null;
                            BankAccount toAcc = null;
                            System.out.println("Enter the account number you want to withdraw from:");

                            withdraw_accNum = sc.nextInt();

                            System.out.println("Enter the amount you want to withdraw:");

                            withdraw_amount = sc.nextDouble();
                            System.out.println("Enter the account number you want to deposit to:");

                            deposit_accNum = sc.nextInt();//the deposit amount is alwyas the same as withdraw_amount

                            //find the matching acc number:withdraw_accNum
                            for (BankAccount listItemsFirst : bankAccounts2) {
                                //if the withdraw acc num matches with the given one
                                if (listItemsFirst.getAccNum() == withdraw_accNum) {
                                    //store it
                                    fromAcc = listItemsFirst;
                                    break;
                                }
                            }
                            //find the matching acc number:  deposit_accNum
                            for (BankAccount listItemsSec : bankAccounts2) {
                                //if the withdraw acc num matches with the given one
                                if (listItemsSec.getAccNum() == deposit_accNum) {
                                    //store it
                                    toAcc = listItemsSec;
                                    break;
                                }
                            }
                            //if the withdraw amount is bigger than the current balance
                            if (withdraw_amount > fromAcc.getInitialAmount()) {
                                System.out.println("Withdrawing Amount was bigger than the Initial amount.\nChoose the menu again. .");
                                break;
                            }
                            //subtracting and adding the withdrawn amount
                            fromAcc.setInitiateAmount(fromAcc.getInitialAmount() - withdraw_amount);
                            toAcc.setInitiateAmount(toAcc.getInitialAmount() + withdraw_amount);
                            System.out.println("DONE!\t print them out to see the current status.");
                            System.out.println("");

                            break;
                        case 2://deposit/withdraw money in the same accounts
                            bankAccounts2=(List<BankAccount>)rw.readFromFile();
                            BankAccount fromAcc_SameAcc = null;
                            BankAccount toAcc_SameAcc = null;
                            System.out.println("Enter the account number you want to deposit or withdraw from:");
                            //read the accNum
                            depOrWithAccountNum = sc.nextInt();
                            System.out.println("Enter the amount (To withdraw enter a negative value)");
                            //read the amount
                            depOrWithAmount = sc.nextDouble();
                            //checking the matching account number in arrayList
                            for (BankAccount listItemsThird : bankAccounts2) {
                                if (listItemsThird.getAccNum() == depOrWithAccountNum) {
                                    fromAcc_SameAcc = listItemsThird;
                                    break;
                                }
                            }

                            if (depOrWithAmount - 1 < fromAcc_SameAcc.getInitialAmount()) {
                                System.out.println("Withdraw amount is bigger than the current amount.\nChoose the menu again. .");
                                break;
                            }

                            if (depOrWithAmount < 0) {//the amount is negative
                                fromAcc_SameAcc.setInitiateAmount(fromAcc_SameAcc.getInitialAmount() + depOrWithAmount);
                            } else {
                                fromAcc_SameAcc.setInitiateAmount(fromAcc_SameAcc.getInitialAmount() + depOrWithAmount);
                            }

                            break;
                    }

                    //inner switch-case ends
                    System.out.println("\n\n");
                    break;

                case 3:
                    //View One account
                    bankAccounts2=(List<BankAccount>)rw.readFromFile();
                    BankAccount viewOneAccountNum = null;

                    System.out.println("Enter the account number you want to see:");
                    viewOneAcc = sc.nextInt();
                    System.out.println("Name\tAccount No\tInitial Amount");
                    for (BankAccount viewOneAccountProperty : bankAccounts2) {

                        if (viewOneAccountProperty.getAccNum() == viewOneAcc) {
                            //viewOneAccountNum=viewOneAccountProperty;
                            viewOneAccountNum = viewOneAccountProperty;

                            System.out.println(viewOneAccountNum);
                        }
                        System.out.println("");

                    }
                    break;
                case 4:
                     bankAccounts2=(List<BankAccount>)rw.readFromFile();
                    //BankAccount AccToDel = null;
                    //Deleting an account
                    Iterator<BankAccount> it = bankAccounts2.iterator();

                    System.out.println("Enter the account you want to delete:");
                    deleteAcc = sc.nextInt();

                    while (it.hasNext()) {
                        BankAccount next = it.next();
                        if (next.getAccNum() == deleteAcc) {
                            it.remove();
                        }
                    }

                    rw.writeToFile(bankAccounts2);


                    break;

                case 5:
                    //View all the accounts/printing them out

                    bankAccounts2=(List<BankAccount>)rw.readFromFile();
                    System.out.println("Name\tAccount No\tInitial Amount");
                    for (BankAccount bankAccount : bankAccounts2) {

                        System.out.println(bankAccount);

                    }

                    System.out.println("\n\n");
                    break;

                case 6:
                    //Quit
                    return;
            }

            //switch-case ends
        }

    }

}

ReaderWriter:

public class ReaderWriter{
 public void writeToFile(List<BankAccount> accounts){
   try {
            FileOutputStream fos=new FileOutputStream("C:\\Users\\Documents\\NetBeansProjects\\BankFile_assignment.txt");
            ObjectOutputStream oos=new ObjectOutputStream(fos);
            oos.writeObject(accounts);//take the arrayList 
            oos.flush();
            oos.close();
            fos.close();

        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public  List<BankAccount> readFromFile(){
        List<BankAccount>readData=null;
        try {



            FileInputStream fis=new FileInputStream("C:\\Users\Documents\\NetBeansProjects\\BankFile_assignment.txt");
            ObjectInputStream ois=new ObjectInputStream(fis);
            //make an arrayList to get those object back
            //arrayList

            readData=(List<BankAccount>)ois.readObject();

         ois.close();
         fis.close();
        } catch (Exception e) {
        e.printStackTrace();
        }
        //return readData;
        return readData;


    }
}

BankAccount:

    package bankapp_assignment;

import java.io.Serializable;

public class BankAccount implements Serializable{

    private String name;
    private int accNum;
    private double initiateAmount;

    //constructor
    public BankAccount() {

        this.name = null;
        this.accNum = 0;
        this.initiateAmount = 0;

    }

    public void setName(String name) {
        this.name = name;

    }

    public void setAccNum(int accNum) {

        this.accNum = accNum;
    }

    public String getName(String name){
        return name;

    }


    public int getAccNum() {
        return accNum;
    }


    public void setInitiateAmount(double initiateAmount) {
        this.initiateAmount = initiateAmount;
    }

    public double getInitialAmount(){
        return initiateAmount;
    }

    @Override
    public String toString() {
        return name + "\t\t" + accNum + "\t\t" + initiateAmount;
    }

}

解决方案

  1. you should refactor whole main method. If I were you, I would use main only to lunch program, rest process in other methods. This way you would be able to use class fields all over program, and divide code into many methods.
  2. You should divide main beetween other methods, to at least method per action, exemple:

    case 1:
        createAccount();
        break;
    (...) 
    public void createAccount(){
    code from your swich case 1
    }
    

    etc. You can still use swich control, but should replece logic code to another method.

  3. When i run your code, input/output didn't work. Were you able to save/load file? I had to chage:

    File file = new File("//file path");
    FileOutputStream fos = new FileOutputStream(file);
    ObjectOutput oos = new ObjectOutputStream(fos);
    

    Same with input. Then it worked.

  4. Now, we will try to deal with operation on BankAccounts from files. I change a little bit your code from case2/case2: deposit/withdraw, lets look at it:

    bankAccounts2 = rw.readFromFile();               // loading list from file
    BankAccount edited = new BankAccount();          // creating new object
    System.out.println("Enter the account number you want to deposit or withdraw from:");
    double input = sc.nextInt();                     // you don't need to create new variable every time you take input form user
    for(BankAccount account : bankAccounts2){        // every account in list
           if(account.getAccNum() == input) edited = account;  // if acccNum match, give edited reference to chosen account
    }
    System.out.println("Enter the amount (To withdraw enter a negative value)");
    input = sc.nextDouble();
    
    double result = edited.getInitialAmount() + input;  // result of operation
    if(result < 0){   // check if there is enough money on account
        System.out.println("Withdraw amount is bigger than the current amount.\nChoose the menu again. .");
        break;
    }else{
        edited.setInitiateAmount(result);               // if there is, set new value
    }
    rw.writeToFile(bankAccounts2);                      // save file
    

You can implement another operation in similar style. But still, you should consider dividing main into other methods, adding class fields, etc.

EDIT:

For question in comments.

Because you use positive numbers for deposit, and negative numbers for withdraw, the operation on initialAmount will be always the same: initialAmount + input (if negative, is will subtract from amount), so you can treat it like an one case in your code. In your code you use this line twice:

fromAcc_SameAcc.setInitiateAmount(fromAcc_SameAcc.getInitialAmount() + depOrWithAmount); 

so you already could merge both cases into one, to avoid unnecessary repetitions. So only case when action will differ, is when there is not enough money on a account, you check it by:

if (depOrWithAmount - 1 < fromAcc_SameAcc.getInitialAmount())

but i think i will not work, because in withdraw operation depOrWithAmount is in negative numbers, so it will always be smaller than fromAcc_SameAcc.getInitialAmount(). You can use:

if (depOrWithAmount < 1 - fromAcc_SameAcc.getInitialAmount())

(but i think it is not too readable) or:

if (Math.abs(depOrWithAmount) > fromAcc_SameAcc.getInitialAmount()){}

to compare absolute value of input with initialAmout. However i used:

double result = edited.getInitialAmount() + input;
if(result < 0){...}

because it gives me result variable, which I can reuse if there is enough money, to gives new value to initialAmount. And if result is negative, it means that withdraw amount was bigger than initialAmount, so ther is no enough monay to withdraw.

I hope you found something useful in my post.

这篇关于如何写在一个文件中的对象之间执行算术运算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆