2

問題は、たとえば 1、2、3 の 3 つのアカウントを作成する場合です。次に、2 を削除して新しいアカウントを追加すると、結果は 1、3、2 になり、すべて問題ありません。別のアカウントを追加すると、結果は 1、3、2、3 になります。さらに別のアカウントを追加すると、1、3、2、3、4 になります。値は、配列が読み書きするテキスト ファイルに保存されます。

問題は、同じ口座番号を 2 回取得することです。また、削除されたアカウントからのギャップを埋める必要があるため、常に1ずつインクリメントするだけでは問題ありません。

問題を理解できず、助けていただければ幸いです。

担当するコード:

private int choice;
public String name;
public int accountNr = 1;
public int cash;
public int funds;
private boolean run = true;
private int index = 0;
private int index1 = 0;
Scanner scan = new Scanner(System.in);
Random rand = new Random();
ArrayList<Account> acc = new ArrayList<Account>();
ArrayList<TransferHistory> transferHistory = new ArrayList<TransferHistory>();
public int c;
public int amount;
private int c1;
private int size;
ReaderWriter io = new ReaderWriter();
private int account0;
private Account ac;
private int account1;
private int account3;
private int account2;
private int viewAnswer;
private int deleteAnswer;
private String s = "";

public void startMessage() {
    System.out.println("***** Welcome to our bank system *****");
}

public void mainMenu() {
    while (run == true) {
        acc = io.readFromFile();
        Scanner scan = new Scanner(System.in);
        System.out.println("**** Main menu ****");
        System.out.println("1. Create a new account");
        System.out.println("2. Deposit/Withdraw from account");
        System.out.println("3. Transfer money");
        System.out.println("4. View the account properties");
        System.out.println("5. View one account properties");
        System.out.println("6. Delete account");
        System.out.println("7. Show transfer history");
        System.out.println("8. Show transfer history for one account");
        System.out.println("9. Quit");

        try {
            choice = scan.nextInt();
        } catch (InputMismatchException e) {
            System.out.println("Please enter valid choice");
        }

        if (choice == 1) {
            addAccount();
        }
        if (choice == 2) {
            transfer();
        }
        if (choice == 3) {
            transferWithin();
        }
        if (choice == 4) {
            view();
        }
        if (choice == 5) {
            viewOne();
        }
        if (choice == 6) {
            delete();
        }
        if (choice == 7) {
            showTransfers();
        }
        if (choice == 8) {
            showOneTransfer();
        }
        if (choice == 9) {
            quit();
        }
    }
}

public void addAccount() {
    System.out.print("Enter your name: ");
    name = scan.next();
    for (int i = 0; i < acc.size(); i++) {
            if(acc.get(i).getAcc() == accountNr) {
                accountNr++;
            }              

    }
    System.out.println("Your account nr is: " + accountNr);
    System.out.print("Enter your starting funds: ");
    try {
        cash = scan.nextInt();
        if(cash < 0){
        System.out.println("Incorrect input!");
        System.out.println(s);
        scan.reset();
        return;                
        }
        index = acc.size();
        acc.add(index, new Account(name, accountNr, cash)); //Add new account object to specified element in acc arraylist
        index = acc.size();
    io.writeToFile(acc);            
    } catch (InputMismatchException e) {
        System.out.println("The scanner couldn´t read your input, use digits next time.");
        System.out.println("The funds for this account has been set to zero, use transfers to adjust");
        scan.reset();
    }

}

public void transfer() {

    System.out.print("Enter account number to withdraw/deposit to: ");
    try {
        account1 = Integer.parseInt(scan.nextLine());
        if(account1 > acc.size() || account1 <= 0){
        System.out.println("Incorrect input!");
        System.out.println(s);
        scan.reset();
        return;                
        }
    } catch (Exception e) {
        System.out.println("Enter a number!");
        scan.reset();
        return;
    }
    System.out.print("Enter a positive number to deposit and negative to withdraw: ");
    try {
        funds = Integer.parseInt(scan.nextLine());
    } catch (Exception e) {
        System.out.println("Enter a number!");
        scan.reset();
        return;
    }

    for (int i = 0; i < acc.size() + 1; i++) {
        if (account1 == i) {
            if (funds > 0) {
                acc.get(account1 - 1).setCash(funds + acc.get(account1 - 1).getCash());
                System.out.println("The amount is changed to " + acc.get(account1 - 1).getCash());
                index1 = transferHistory.size();
                transferHistory.add(index1, new TransferHistory(account1, funds, account1));
                io.writeTransferedToFile(transferHistory);
                index1 = transferHistory.size();
            }
            if (funds < 0 && funds + acc.get(account1 - 1).getCash() > 0) {

                acc.get(account1 - 1).setCash(funds + acc.get(account1 - 1).getCash());
                System.out.println("The amount is changed to " + acc.get(account1 - 1).getCash());
                index1 = transferHistory.size();
                transferHistory.add(index1, new TransferHistory(account1, funds, account1));
                io.writeTransferedToFile(transferHistory);
                index1 = transferHistory.size();
            } else if (acc.get(account1 - 1).getCash() + funds < 0) {
                System.out.println("This transaction is not allowed since the balance will be negative");
            }
        }
    }


    io.writeToFile(acc);
}

public void view() {
    acc = io.readFromFile();
    for (int i = 0; i < acc.size(); i++) {
        System.out.println(s);
        System.out.println("Account name: " + acc.get(i).tempName);
        System.out.println("Account number: " + acc.get(i).tempAccNr);
        System.out.println("Funds: " + acc.get(i).tempCash);
    }
    if (acc.isEmpty()) {
        System.out.println("No accounts to show");
    }

}

public void quit() {
    System.exit(0);
}

private void transferWithin() {
    System.out.print("Enter account you want to transfer from: ");
    try {
        account3 = Integer.parseInt(scan.nextLine());
        if(account3 > acc.size() || account3 <= 0){
        System.out.println("Incorrect input!");
        System.out.println(s);
        scan.reset();
        return;                
        }
    } catch (Exception e) {
        System.out.print("Enter a account number: ");
        scan.reset();
        return;
    }
    System.out.print("Enter amount you want to transfer: ");
    try {
        amount = Integer.parseInt(scan.nextLine());
    } catch (Exception e) {
        System.out.print("Enter a number: ");
        scan.reset();
        return;
    }
    System.out.print("Enter account you want to transfer to: ");
    try {
        account2 = Integer.parseInt(scan.nextLine());
        if(account2 > acc.size() || account2 <= 0){
        System.out.println("Incorrect input!");
        System.out.println(s);
        scan.reset();
        return;                
        }
    } catch (Exception e) {
        System.out.print("Enter a account number:");
        scan.reset();
        return;
    }
    for (int i = 0; i < acc.size() + 1; i++) {
        if (i == account3) {
            c = acc.get(i - 1).getCash();
            if (c - amount >= 0) {
                acc.get(i - 1).setCash(c - amount);
                System.out.println("Funds in account: " + acc.get(i - 1).getAcc() + " " + acc.get(i - 1).getCash());
                index1 = transferHistory.size();
                transferHistory.add(index1, new TransferHistory(account3, amount, account2));
                io.writeTransferedToFile(transferHistory);
                index1 = transferHistory.size();
            } else if (c - amount < 0) {
                System.out.println("Not enough funds in account");
                mainMenu();
            }
        }
    }
    for (int j = 0; j < acc.size() + 1; j++) {
        if (j == account2) {
            c1 = acc.get(j - 1).getCash();
            acc.get(j - 1).setCash(c1 + amount);
            System.out.println("Funds in account " + acc.get(j - 1).getAcc() + " " + acc.get(j - 1).getCash());
        }
    }
    io.writeToFile(acc);
}

private void viewOne() {
    System.out.println("Enter account number you want to look at");
    try {
        viewAnswer = Integer.parseInt(scan.nextLine());
        if(viewAnswer > acc.size() || viewAnswer <= 0){
        System.out.println("Incorrect input!");
        System.out.println(s);
        scan.reset();
        return;                
        }
    } catch (Exception e) {
        System.out.println("Enter a account number!");
        scan.reset();
        return;
    }

    for (int i = 0; i < acc.size() + 1; i++) {
        if (i == viewAnswer) {
            System.out.println("Account name: " + acc.get(i - 1).getName());
            System.out.println("Account nr: " + acc.get(i - 1).getAcc());
            System.out.println("Funds: " + acc.get(i - 1).getCash());
        }
    }
}

private void delete() {
    System.out.print("Enter account you want to delete: ");
    try {
        deleteAnswer = Integer.parseInt(scan.nextLine());
        if(deleteAnswer > acc.size() || deleteAnswer < 0){
        System.out.println("Incorrect input!");
        System.out.println(s);
        scan.reset();
        return;                
        }
    } catch (Exception e) {
        System.out.println("Enter a account number!");
        scan.reset();
        return;
    }
    for (int i = 0; i < acc.size(); i++) {
        if (acc.get(i).getAcc() == deleteAnswer) {
            acc.remove(i);
            io.writeToFile(acc);
        }

    }
}

private void showTransfers() {
    transferHistory = io.readTransferedFromFile();
    System.out.println(s);
    System.out.println("***Transactions***");
    System.out.println(s);
    for (int i = 0; i < transferHistory.size(); i++) {
        int t = transferHistory.get(i).tempFromAccount;
        int t1 = transferHistory.get(i).temptransfered;
        int t2 = transferHistory.get(i).tempToAccount;
        System.out.println("Transfer from: " + t);
        System.out.println("Transfered amount: " + t1);
        System.out.println("Transfered to: " + t2);
        System.out.println(s);
    }
}

private void showOneTransfer() {
    transferHistory = io.readTransferedFromFile();
    System.out.println(s);
    System.out.println("***Transactions***");
    System.out.println(s);
    System.out.print("Enter account nr: ");
    int z = scan.nextInt();
    System.out.println("Transactions made my account nr "+z+":");
    for (int i = 0; i < transferHistory.size(); i++) {
        if(transferHistory.get(i).tempFromAccount == z){
        int t = transferHistory.get(i).tempFromAccount;
        int t1 = transferHistory.get(i).temptransfered;
        int t2 = transferHistory.get(i).tempToAccount;
        System.out.println("Transfer from: " + t);
        System.out.println("Transfered amount: " + t1);
        System.out.println("Transfered to: " + t2);
        System.out.println(s);
        }
    }
}

}

ここでは、ファイルから配列を作成します。

public ArrayList<Account> readFromFile() {
    FileReader reader = null;
    ArrayList<Account> result = new ArrayList<Account>();
    try {
        reader = new FileReader(new File(text));
        BufferedReader br = new BufferedReader(reader);
        String row = br.readLine();
        while (row != null) {
            String[] splits = row.split(":");
            if (splits.length == 3) {
                int saveNR = Integer.valueOf(splits[1]);
                int saveAmount = Integer.valueOf(splits[2]);
                String saveName = splits[0];
                result.add(new Account(saveName,saveNR,saveAmount));
            } else {
                System.out.println("Error in file format");
            }
            row = br.readLine();
        }

    } catch (Exception e) {
        System.out.println("Error while reading from file");
    } finally {
        try {
            reader.close();
        } catch (IOException ex) {
            System.out.println("Ignore");
        }
        return result;
    }
}

private void delete() {
    System.out.print("Enter account you want to delete: ");
    try {
        deleteAnswer = Integer.parseInt(scan.nextLine());
        if(deleteAnswer > acc.size() || deleteAnswer < 0){
        System.out.println("Incorrect input!");
        System.out.println(s);
        scan.reset();
        return;                
        }
    } catch (Exception e) {
        System.out.println("Enter a account number!");
        scan.reset();
        return;
    }
    for (int i = 0; i < acc.size(); i++) {
        if (acc.get(i).getAcc() == deleteAnswer) {
            acc.remove(i);
            io.writeToFile(acc);
        }

    }
}
4

2 に答える 2

3

カウンターが必要な場合は、クラスの静的変数にして、新しい値が必要なときにインクリメントします。何かのようなもの:

private static int counter = 0;
private static int nextCounter() {
    return ++counter;
}

または、同期上の理由から、

private static AtomicInteger counter = new AtomicInteger();
private static int nextCounter() {
    return counter.incrementAndGet();
}

ただし、口座番号のすべてのギャップを埋める必要があると考えるのはやめることをお勧めします。通常、データベース作業では、アカウント番号が再利用されることはありません。正式にはデータベースを使用していませんが、同じように作業する必要があります。再利用しても何の得にもなりません。コードが新しいユーザー 17 と古いユーザー 17 を混同する可能性は常にあります。米国社会保障局が社会保障番号を再利用するとどうなるか想像してみてください。

ちなみに、これがあなたのエラーの理由です。コード内:

for (int i = 0; i < acc.size(); i++) {
    if(acc.get(i).getAcc() == accountNr) {
        accountNr++;
    }              
}

accountNr が 1 から始まり、3 つのアカウントがあり、それらのアカウント番号が 2、1、3 であるとします。ループの各実行後、accountNr は次のように変化します。

1 ⇢ 1⇢ 2 ⇢ 2.

2は既存のアカウント番号ですが、最後に確認された後、コードによってアカウント番号が 2 に設定されます。

最初の未使用の整数を取得する

で最初の未使用の整数を取得する方法が必要ですacc。ここに行きます:

private int firstUnusedId(List<Account> accounts) {
    List<Integer> ids = new ArrayList<>();
    // Foreach loop.
    for(Account account: accounts) {
        ids.add(account.getAcc());
    }
    Collections.sort(ids);
    for (int index = 0; index < ids.size(); ++index) {
        if (ids.get(index) != index + 1) {
            return index + 1;
        }
    }
    return ids.size() + 1;
}

が2、1、5 の場合はids、1、2、5 にソートされます。その後、ループは次のように比較します。

index = 0, index + 1 = 1, compare to 1, equal.
index = 1, index + 1 = 2, compare to 2, equal.
index = 2, index + 1 = 3, compare to 5, not equal, return 3.

ID が 3、2、1 の場合、それらは 1、2、3 にソートされ、唯一の違いは最後の比較になります。

index = 2, index + 1 = 3, compare to 3, equal.
return size + 1 = 4.
于 2013-06-12T18:40:46.070 に答える
0

もっと多くのコードを共有する必要がありますが、私が見てきたことから、最後のアカウントから新しいアカウントを作成していると予測されますNr +1 したがって、リストの最後のアカウントが 2 の場合、3 が作成されます ...4 など..新しいアカウントを確認して設定してくださいaccountNr = acc.size+1

于 2013-06-12T18:17:23.770 に答える