0

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

ojus kulkarni
  • 1,877
  • 3
  • 25
  • 41
  • are you saving your generated coupons somewhere? – Sachin Gupta Mar 09 '16 at 05:27
  • yes I saving `voucherNumber` in mysql database – ojus kulkarni Mar 09 '16 at 05:31
  • then do one thing, get last saved coupon no from database, parse it and then check date/month, if it is changed then just reset your counter. – Sachin Gupta Mar 09 '16 at 05:33
  • you will have to take srNumber as a static field that will not map to an object but to a class.Check for what static variables are. – Abdullah Khan Mar 09 '16 at 05:59
  • @AbdullahWasi yes you are right , it should be static – ojus kulkarni Mar 09 '16 at 06:49
  • Why does a method called "CouponCreation" take a coupon? It really should just take what it needs to create a coupon and return a new one. Why are you using "000" and "001"? I get that you want to display it has a three digit number, but it is just a number. Leave the display until later. Should the Coupon have the "voucher number" or a "serialNumber" and know how to generate a "voucher number" from what it has? Does this need to work across instances of your application or only one instance of your application? If only one instance of your application, does it need to support multi threading? – Michael Lloyd Lee mlk Mar 09 '16 at 09:40
  • Finally, what is wrong with what you have above, what have you tried to fix it? – Michael Lloyd Lee mlk Mar 09 '16 at 09:47
  • @mlk it is only for one instance so there is no need of multithreading, yes I want to display three digits so i tried in that way, but i know it will not work so i am again asking here how to generate **srNumber**. also I am getting month and date but the only problem is about **serial number** it should detect a date change and initiate serial number counting from `001` on every new date – ojus kulkarni Mar 09 '16 at 13:36
  • 1
    Right, so lets cut the problem into a few smaller ones. First how do we [prefix a number with zeros in java](http://stackoverflow.com/questions/275711/add-leading-zeroes-to-number-in-java). – Michael Lloyd Lee mlk Mar 09 '16 at 13:42

2 Answers2

2

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

T.G
  • 1,913
  • 1
  • 16
  • 29
0

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].

Zaid Qureshi
  • 1,203
  • 1
  • 10
  • 15
  • 1
    can you please elaborate it with some code ? because i think it should. not that much complicated – ojus kulkarni Mar 09 '16 at 05:34
  • this way you always have to fill table on program startup because codes are stored in mysql db. i would suggest using right sql query to extract this number instead. – T.G Mar 09 '16 at 09:40
  • @T.G exactly I am not getting answer by doing this, but if I want to fire query to extract number then how should i do that? – ojus kulkarni Mar 09 '16 at 13:29