Reputation: 1907
I am trying to create a coupon in following format :
{month} / {date} / {serialNumber}
I am selecting month and date from UI, so I am getting month and date properly but my problem is :
serialNumber should auto Increment and
It should again start from zero for a new date
what i tried is :
private static Integer srNumber = 000;
public Coupon CouponCreation(Coupon coupon) {
String voucherNumber;
srNumber += srNumber + 001;
voucherNumber = (coupon.getTransactionDate().getMonth() + 1)
+ "/" + coupon.getTransactionDate().getDate()
+ "/" + srNumber;
coupon.setVoucherNumber(voucherNumber);
return coupon;
}
Class Coupon contains all getter and setter methods
In above code I want to generate srNumber like: 001, 002, 003 and so on
But if coupon.getTransactionDate().getDate()
changed then srNumber will start again from 001, 002 and voucher number should be
08/02/001, 08/02/002 and so on
Upvotes: 0
Views: 1670
Reputation: 1921
MySQL approach
table definition
CREATE TABLE v (
month int,
day int,
orderNum int);
some startup values
INSERT INTO v VALUES (1,1,1);
INSERT INTO v VALUES (1,1,2);
INSERT INTO v VALUES (1,1,3);
INSERT INTO v VALUES (1,2,1);
INSERT INTO v VALUES (1,2,2);
INSERT INTO v VALUES (1,3,1);
Now inputing values with auto generate
INSERT INTO v SELECT month,day,COUNT(orderNum)+1 FROM v WHERE month=1 AND day=1;
INSERT INTO v SELECT month,day,COUNT(orderNum)+1 FROM v WHERE month=1 AND day=1;
INSERT INTO v SELECT month,day,COUNT(orderNum)+1 FROM v WHERE month=1 AND day=1;
but above example will turn our life in nightmare if there will be some more columns.
INSERT INTO v VALUES(1,1,(SELECT COUNT(orderNum)+1 FROM v AS v2 WHERE month=1 AND day=1));
INSERT INTO v VALUES(1,2,(SELECT COUNT(orderNum)+1 FROM v AS v2 WHERE month=1 AND day=2));
INSERT INTO v VALUES(1,3,(SELECT COUNT(orderNum)+1 FROM v AS v2 WHERE month=1 AND day=3));
INSERT INTO v VALUES(1,4,(SELECT COUNT(orderNum)+1 FROM v AS v2 WHERE month=1 AND day=4));
INSERT INTO v VALUES(1,4,(SELECT COUNT(orderNum)+1 FROM v AS v2 WHERE month=1 AND day=4));
INSERT INTO v VALUES(1,1,(SELECT COUNT(orderNum)+1 FROM v AS v2 WHERE month=1 AND day=1));
example above is what you are looking for. Don't forget to specify table alias (v2 in example above), otherwise insert will fail.
if you now execute
SELECT * FROM v ORDER BY month, day
then you will see, that it also works for starting new sequence for every next day
If you store those code as a single string, then you have to do some string spiting to work it out. To avoid that, i suggest to change it to 3 fields to make in better and then join it into string in application
Upvotes: 2
Reputation: 1213
Any easy way to do this would be to create a 2D int array, 12x31, and each cell would store a serial number. So when you get your date, convert it day and month, and use the serial number and increment. So if your coupon is Jan, 10. You do serialArray[0][10]
to get the Serial. and increment after assigning it the voucher.
If you would like you can make it 3D, for the year too.
For the 2D, month and date, it would look like this assuming, getMonth() and getDate() return ints:
int[][] serialArray = new int [12][31];
int srNumber=serialArray[coupon.getTransactionDate().getMonth()][coupon.getTransactionDate().getDate()]++;
Serial Array would initialize to 0 at the start of the program. Or maybe you could even read a file into it. This is how it would be if you want a separate serial code for each date.
EDIT This code is for if your vouchers will be going back and fourth between dates and you dont want to reset your serial number. If you just want to reset your serial number each time you encounter a new date. You can store the previous date.
Also you can add a 3rd dimension to keep the year too so [YY][MM][DD].
Upvotes: 0