-3

バンキング アプリを作成しています。番号 1 から始まる顧客番号を生成し、ループに入るたびに繰り返されないように番号を追跡し、使用できる int 変数に格納する必要があります。値を収集し、ループの外で customerNumber 変数に渡します。配列リストや配列など、いくつか試してみましたが、必要な変数に値を渡す際に問題が発生していました。事前に感謝し、私のひどい初心者で申し訳ありません...私はプログラミングの初心者です...これまでのところ、次のとおりです。

import java.util.ArrayList;

public class Bank{

    public void addCustomer(String name, int telephone, String email, String    profession) {
        ArrayList customerList = new ArrayList();
        Customer customer = new Customer();
        customerList.add(customer);
    }
}

public class Customer{
    private String name;
    private int telephone;
    private String email;
    private String profession;
    private int customerNumber;

    public Customer() {

    }
}

public class Menu {
    Scanner sc = new Scanner(System.in);
    Bank bank = new Bank();


        private void createCustomer() {
        String name, email, profession;
        int telephone, customerNumber;

        System.out.println("Enter the customer's number: ");
        name = sc.nextLine();
        System.out.println("Enter the customer's telephone: ");
        telephone = sc.nextInt();
        System.out.println("Enter the customer's email: ");
        email = sc.nextLine();
        System.out.println("Enter the customer's profession: ");
        profession = sc.nextLine();
            bank.addCustomer(name, telephone, email, profession);
    }
}
4

1 に答える 1

0

できることの 1 つは、シングルトン クラスを作成し、番号が必要になるたびに番号を要求することです。シングルトン クラスは、既に使用されている数値のリストを保持しているため、以前に使用されていない数値を返すことができます。

アプリケーションの再起動後に新しい数値も生成する必要がある場合は、すべての数値をファイルに保存し、必要なときにそのファイルを読み取ることができます。

シングルトン クラスは、最大 1 つのインスタンスを持つことができるクラスです。これを実現するには、コンストラクターをプライベートにし、このクラスのインスタンスを取得するパブリック静的メソッド (通常は getInstance() などと呼ばれます) を作成します。この getInstance() は、参照を唯一のインスタンスに返します。インスタンスがまだ作成されていない場合は、最初にインスタンスを作成します。次に、このクラスのインスタンスが要求される頻度に関係なく、このインスタンスのみが使用中のすべてのアカウント番号を認識します (あなたの場合)。このクラスの責任は、アカウント nrs を維持することです: nr を作成し、それらを印刷し、それらを保存し、それらを読み取ります...

例:

private AccoutnNr singleInstance;

private AccountNr(){
}

public AccountNr getInstance(){
    if (singleInstance == null) {
        singleInstance = new AccountNr();
    }

    return singleInstance;
}

public int getAccountNr{
    // do whatever is needed to create an account nr
}

more methods if you need to do more than creating account numbers
于 2015-06-24T20:53:18.047 に答える