Generate non repeating numbers to add to customerNumber int variable

119 views Asked by At

I'm creating a banking app and I need to generate a customer number starting from number 1, keeping track of the number so that it won't repeat itself each time I enter the loop and store it into an int variable that I can use to collect the value and pass it to the customerNumber variable outside the loop. I've tried a few things like arraylists and arrays, but I was getting troubles in passing the values to the variable I wanted. Thanks in advance and sorry for my terrible noobishness...I'm new in programming... Here's what I've got so far:

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

There are 1 answers

1
Jef Vrijhoeven On

One thing you can do is create a singleton class, and request a number each time you need one. The singleton class keeps a list of the numbers that have been used already, and thus can return a number that has not been used before.

If you need also to generate new numbers after your application is restarted, then you can store all numbers in a file, and read that file whenever needed.

A singleton class, is a class that can have max 1 instance. You can achieve this by making the constructor private, and creating a public static method (usually called something like getInstance() ) to get an instance of this class. This getInstance() returns the ref to the only instance, and if no instance was created yet, it first creates one. Then, this only instance knows all account numbers in use (inyour case), regardless how often an instance of this class is requested. The responsibility of this class is to maintain the account nrs: create a nr, print them, save them, read them, ...

Example:

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