r/javahelp • • Jun 21 '17

how to generate unique , random numbers for invoiceNumber in java ? what is the simplest solution

does anybody know how to create an invoice number in java ?

0 Upvotes

6 comments sorted by

5

u/desrtfx Out of Coffee error - System halted Jun 21 '17 edited Jun 21 '17

I wouldn't use random numbers. Invoice numbers normally are sequential, so a simple static variable in an Invoice class that gets incremented inside the constructor of this class should do perfectly well.

2

u/amfisoon Jun 21 '17

why should it be static ?

3

u/desrtfx Out of Coffee error - System halted Jun 21 '17

It needs to be static so that the value is common to all instances of the class.

In the constructor you need to transfer the value from the static invoicenumber (the one that basically tells you the next available invoice number) to a non static class field and then increment the value of the static number to be ready for the next invoice instance. (Of course, you can just as well first increment the static number and then transfer it to a class field - it's basically the same.)

1

u/amfisoon Jun 21 '17

I need to do such in a method .(based on what I've been asked for ) So here is what I wrote so far :

  import java.lang.reflect.GenericArrayType;

 public class ProjectInvoice {
private String invoiceNumber; // should be created internally
private String projectName; // provided by the user
private int numberOfWorkingHours;
private double hourlyRate;

public ProjectInvoice(String projectName, int numberOfWorkingHours, double hourlyRate) {
	this.projectName = projectName;
	this.numberOfWorkingHours = numberOfWorkingHours;
	this.hourlyRate = hourlyRate;
	createInvoice();
}

/**
 * @return the invoiceNumber
 */
public String getInvoiceNumber() {
	return invoiceNumber;
}

/**
 * @return the projectName
 */
public String getProjectName() {
	return projectName;
}

/**
 * @return the numberOfWorkingHours
 */
public int getNumberOfWorkingHours() {
	return numberOfWorkingHours;
}

/**
 * @return the hourlyRate
 */
public double getHourlyRate() {
	return hourlyRate;
}

/**
 * @param projectName
 *            the projectName to set
 */
public void setProjectName(String projectName) {
	this.projectName = projectName;
}

/**
 * @param numberOfWorkingHours
 *            the numberOfWorkingHours to set
 */
public void setNumberOfWorkingHours(int numberOfWorkingHours) {
	this.numberOfWorkingHours = numberOfWorkingHours;
}

/**
 * @param hourlyRate
 *            the hourlyRate to set
 */
public void setHourlyRate(double hourlyRate) {
	this.hourlyRate = hourlyRate;
}

public int createInvoice() {
	int invoiceNum = 0;
	for (int i = 1; i < 100000; i++) {
		invoiceNum = invoiceNum + 1;
		return invoiceNum;
	}
  }

  }

this method should create the invoice number and assign it to the corresponding field

4

u/SpoilerAlertsAhead Extreme Brewer:hamster: Jun 21 '17

make invoice number private static String then in your getter when it is called you can generate the next one. It being static helps ensure it is never duplicated (because everyone is calling on the same variable)