當前位置:首頁 » 優惠多多 » java優惠碼生成
擴展閱讀
寧波奧德賽優惠價格 2021-03-15 14:26:02
丹尼斯購物卡能掛失么 2021-03-15 14:25:58
淘寶購物指紋驗證失敗 2021-03-15 14:24:44

java優惠碼生成

發布時間: 2021-02-06 00:11:58

A. 求助大神,要求用java代碼寫一個序列生成器

importjava.util.HashMap;
importjava.security.KeyException;
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;

/**
*類<code>Key</code>是一個資料庫主鍵生成器,用序列號的方式來產生資料庫中需要的主鍵值。
*<p>
*<code>Key</code>目前支持的資料庫包括Oracle的所有版本、MySql的3.x以上的版本
*以及所有支持max()函數的資料庫,支持欄位類型僅為數字類型的主鍵,對於字元及其它類型的主鍵尚不提供支持。
*<p>
*在使用時只需提供表名、欄位名(主鍵)以及到資料庫的JDBC連接,如果想要獲得message表的id欄位的下一個主鍵值時:
*<p>
*<blockquote>
*
*<pre>
*java.sql.Connectionconn=...;
*org.shaoye.common.sql.Keykey=org.shaoye.common.sql.Key.getInstance();
*intkeyValue=key.getNextKey("message","id",conn);
*Stringsql="insertintomessage(id,...)values("+keyValue+",...)";
*//執行插入操作...
*</pre>
*
*</blockquote>
*<p>
*
*@author令少爺([email protected])
*@sincemagic0.1
*/
publicfinalclassKey{

/**
*key的最大值,默認為9223372036854775807,即long類型的最大值
*/
privatelongmax=9223372036854775807L;

/**
*key的最小值,默認為1
**/
privatelongmin=1L;

/**
*Key的唯一實例,通過getInstance()方法獲得
**/
privatestaticKeykeygen=newKey();

/**
*KeyInfo類的實例列表,默認容量為5個
**/
privateHashMap<String,KeyInfo>keyList=newHashMap<String,KeyInfo>(5);//keyInfo
//列表

/**
*私有的默認構造方法,防止外部構造類的實例
**/
privateKey(){
}

/**
*獲得Key的唯一實例
**/
publicstaticKeygetInstance(){
returnkeygen;
}

/**
*用指定的表和欄位獲得key的下一個值,主鍵的值不得超過2147483647
*
*@paramtableName
*資料庫中的表名,表中必須有一個數字主鍵
*@paramkeyName
*表(tableName)中的欄位名
*@paramconn
*JDBC連接,如果欲獲得的key是第一次取值,則必須保證conn能連接到資料庫
*@returnkey的下一個主鍵的int值
*@throws<code>KeyException</code>
*如果表名或欄位名不存在、訪問資料庫錯誤或key的值大於2147483647時拋出
*/
publicintgetNextKey(StringtableName,StringkeyName,Connectionconn)
throwsKeyException{
longvalue=getNextKeyLong(tableName,keyName,conn);
if(value>2147483647L){
thrownewKeyException(
"Key'svaluetoobig,!");
}
return(newLong(value)).intValue();
}

/**
*用指定的表和欄位獲得key的下一個值,最大為9223372036854775807
*@paramtableName資料庫中的表名,表中必須有一個數字主鍵
*@paramkeyName表(tableName)中的欄位名
*@paramconnJDBC連接,如果欲獲得的key是第一次取值,則必須保證conn能連接到資料庫
*@returnkey的下一個主鍵的long值
*@throws<code>KeyException</code>如果表名或欄位名不存在或訪問資料庫錯誤時拋出
*/
publiclonggetNextKeyLong(StringtableName,StringkeyName,Connectionconn)
throwsKeyException{
KeyInfokeyinfo;
Stringitem=tableName+"."+keyName;
try{
if(keyList.containsKey(item)){
keyinfo=(KeyInfo)keyList.get(item);
}else{
keyinfo=newKeyInfo(tableName,keyName,conn);
keyList.put(item,keyinfo);
}
returnkeyinfo.getNextKey();
}catch(SQLExceptionsqle){
thrownewKeyException(sqle);
}
}

/**
*用指定的"表<code>.</code>欄位"形式的字元串獲得key的下一個值,主鍵的值不得超過2147483647
*@paramtableDotField"表.欄位"形式的字元串,如:message.id
*@paramconnJDBC連接,如果欲獲得的key是第一次取值,則必須保證conn能連接到資料庫
*@returnkey的下一個主鍵的int值
*@throws<code>KeyException</code>如果表名或欄位名不存在、訪問資料庫錯誤或key的值大於2147483647時拋出
*/
publicintgetNextKey(StringtableDotField,Connectionconn)
throwsKeyException{
longvalue=getNextKeyLong(tableDotField,conn);
if(value>2147483647L){
thrownewKeyException(
"Key'svaluetoobig,!");
}
return(newLong(value)).intValue();
}

/**
*用指定的"表<code>.</code>欄位"形式的字元串獲得key的下一個值,最大為9223372036854775807
*@paramtableDotField"表.欄位"形式的字元串,如:message.id
*@paramconnJDBC連接,如果欲獲得的key是第一次取值,則必須保證conn能連接到資料庫
*@returnkey的下一個主鍵的int值
*@throws<code>KeyException</code>如果表名或欄位名不存在或訪問資料庫錯誤時拋出
*/
publiclonggetNextKeyLong(StringtableDotField,Connectionconn)
throwsKeyException{
intdot_index=tableDotField.indexOf(".");
if(tableDotField.indexOf(".")<1){
thrownewKeyException("UnknownKey'"+tableDotField+"'!");
}
Stringtab=tableDotField.substring(0,dot_index);
Stringkey=tableDotField.substring(dot_index);
returngetNextKeyLong(tab,key,conn);
}

/**
*用指定的表和欄位獲得key的當前值,主鍵的值不得超過2147483647
*@paramtableName資料庫中的表名,表中必須有一個數字主鍵
*@paramkeyName表(tableName)中的欄位名
*@paramconnJDBC連接,如果欲獲得的key是第一次取值,則必須保證conn能連接到資料庫
*@returnkey的當前int值
*@throws<code>KeyException</code>
*如果表名或欄位名不存在、訪問資料庫錯誤或key的值大於2147483647時拋出
*/
publicintgetCurrentKey(StringtableName,StringkeyName,Connectionconn)
throwsKeyException{
longvalue=getCurrentKeyLong(tableName,keyName,conn);
if(value>2147483647L){
thrownewKeyException(
"Key'svaluetoobig,!");
}
return(newLong(value)).intValue();
}

/**
*用指定的表和欄位獲得key的當前值,最大為9223372036854775807
*
*@paramtableName
*資料庫中的表名,表中必須有一個數字主鍵
*@paramkeyName
*表(tableName)中的欄位名
*@paramconn
*JDBC連接,如果欲獲得的key是第一次取值,則必須保證conn能連接到資料庫
*@returnkey的當前long值
*@throws<code>KeyException</code>如果表名或欄位名不存在或訪問資料庫錯誤時拋出
*/
publiclonggetCurrentKeyLong(StringtableName,StringkeyName,
Connectionconn)throwsKeyException{
KeyInfokeyinfo;
Stringitem=tableName+"."+keyName;
try{
synchronized(keyList){
if(keyList.containsKey(item)){
keyinfo=(KeyInfo)keyList.get(item);
}else{
keyinfo=newKeyInfo(tableName,keyName,conn);
keyList.put(item,keyinfo);
}
}
returnkeyinfo.getCurrentKey();
}catch(SQLExceptionsqle){
thrownewKeyException(sqle);
}
}

/**
*用指定的"表<code>.</code>欄位"形式的字元串獲得key的當前值,主鍵的值不得超過2147483647
*
*@paramtableDotField
*"表.欄位"形式的字元串,如:message.id
*@paramconn
*JDBC連接,如果欲獲得的key是第一次取值,則必須保證conn能連接到資料庫
*@returnkey的當前int值
*@throws<code>KeyException</code>如果表名或欄位名不存在、訪問資料庫錯誤或key的值
*大於2147483647時拋出
*/
publicintgetCurrentKey(StringtableDotField,Connectionconn)
throwsKeyException{
longvalue=getCurrentKeyLong(tableDotField,conn);
if(value>2147483647L){
thrownewKeyException(
"Key'svaluetoobig,!");
}
return(newLong(value)).intValue();
}

/**
*用指定的"表<code>.</code>欄位"形式的字元串獲得key的當前值,最大為9223372036854775807
*
*@paramtableDotField
*"表.欄位"形式的字元串,如:message.id
*@paramconn
*JDBC連接,如果欲獲得的key是第一次取值,則必須保證conn能連接到資料庫
*@returnkey的當前int值
*@throws<code>KeyException</code>如果表名或欄位名不存在或訪問資料庫錯誤時拋出
*/
publiclonggetCurrentKeyLong(StringtableDotField,Connectionconn)
throwsKeyException{
intdot_index=tableDotField.indexOf(".");
if(tableDotField.indexOf(".")<1){
thrownewKeyException("UnknownKey'"+tableDotField+"'!");
}
Stringtab=tableDotField.substring(0,dot_index);
Stringkey=tableDotField.substring(dot_index);
returngetCurrentKeyLong(tab,key,conn);
}
}

/**
*內部類,用來存儲主鍵信息
**/
classKeyInfo{
privatelongmax=9223372036854775807L;
privatelongmin=1L;
privatelongnextKey;
privateStringtableName;
privateStringkeyName;
privateConnectionconn=null;

/**
*keyInfo對象初始化
*
*@throwsKeyException
*/
KeyInfo(StringtableName,StringkeyName,Connection_conn)
throwsSQLException,KeyException{
this.tableName=tableName;
this.keyName=keyName;
this.conn=_conn;
retrieveFromDB();
}

intgetMax(){
return(newLong(max)).intValue();
}

longgetMaxLong(){
returnmax;
}

intgetMin(){
return(newLong(min)).intValue();
}

longgetMinLong(){
returnmin;
}

/**
*取下一鍵值
*/
intgetNextKey(){
return(newLong(getNextKeyLong())).intValue();
}

/**
*取下一鍵值
*/
(){
nextKey++;
returnnextKey;
}

/**
*取當前鍵值
*/
synchronizedintgetCurrentKey(){
return(newLong(nextKey)).intValue();
}

/**
*取當前鍵值
*/
(){
returnnextKey;
}

/**
*從資料庫中取當前最大值
*
*@throwsKeyException
*/
voidretrieveFromDB()throwsSQLException,KeyException{
PreparedStatementpstmt=null;
ResultSetrs=null;
Stringsql="selectmax("+keyName+")from"+tableName;
try{
pstmt=conn.prepareStatement(sql);
}catch(Exceptionex){
thrownewKeyException("Can'tconnectDataBase!");
}
try{
rs=pstmt.executeQuery();
}catch(SQLExceptionsqle){
if(pstmt!=null)
pstmt.close();
thrownewKeyException("'"+keyName+"'or'"+tableName
+"'isn'texistinDataBase!",sqle);
}
try{
if(rs.next()){
nextKey=rs.getLong(1);
if(nextKey<min){
nextKey=min;
}
}else{
nextKey=min;
}
}catch(SQLExceptionsqle){
throw(sqle);
}finally{
if(rs!=null)
rs.close();
if(pstmt!=null)
pstmt.close();
}
}
}

B. java里注冊時候邀請碼是按什麼演算法生成的,還有邀請碼只用一次怎麼限制

存放資料庫啊,有A B 和驗證碼,不就是一一對應了嗎,邀請嗎用一次查一下資料庫,有就不能使用

C. java怎麼設計查找快要到期的優惠券

Date d1 = new Date();//當前的時間,java.util.Date類
System.out.println(d1.getHours());
d1.setTime(d1.getTime()+60*60l*1000);//這個是給你演示如何加1個小時,你進行比較的時候,將兩個都getTime,得專出屬的是毫秒,你相減自然就知道是否快到期了
System.out.println(d1.getHours());

D. 用java寫出商品打折程序

嘗試解答一下,這個裡面還有老年和教師的判斷就省略,主要是打折的代碼

importjava.util.Scanner;

classTest{
publicstaticvoidmain(String[]args){
Scannersc=newScanner(System.in);

System.out.println("請輸入消費的金額:");
doubleoldprice=sc.nextDouble();
doublenewprice;
if(oldprice>=2000){
newprice=oldprice*0.85;
}elseif(oldprice>=1000&&oldprice<2000){
newprice=oldprice*0.9;
}else
newprice=oldprice;
Judgejud=newJudge();
if(jud.isTeacher()||jud.isOldman())
newprice=newprice*0.95;

System.out.println("打折後的價格是:"+newprice);
System.out.println("折扣的程度是:"+newprice/oldprice);
}

}
classJudge{
publicbooleanisTeacher(){
booleanisTeacher=false;
returnisTeacher;
}
publicbooleanisOldman(){
booleanisOld=false;
returnisOld;
}
}

E. 怎麼批量生成優惠券的編碼

用軟體

F. java打折程序,有幾個不明白,能幫忙寫下源代碼嗎

import java.util.*;
public class dazhe{
static void main(String[] arg0){
Scanner in=new Scanner(System.in);
String huiyuan;double jiner;
System.out.println("***************打折系統**************:");
System.out.println("請輸入是否是會員:是(y)/否(其他字元)");
huiyuan=in.next();
System.out.println("請輸入購物金額");
jiner=in.nextDouble();
System.out.println((int)jiner);
if(huiyuan=="y")
{
if(jiner>=100&jiner<=200)
{ jiner=jiner*0.8;
System.out.println("實付金額:");
System.out.println((int)jiner);}
else if(jiner>200)
{
jiner=jiner*0.75;
System.out.println("實付金額:");
System.out.println((int)jiner);
}

else if(jiner>=100)
{
jiner=jiner*0.85;
System.out.println("實付金額:");
System.out.println(jiner);}
}
else {System.out.println("實付金額:");
System.out.println(jiner);}

}

}

G. java 生成十位數獎品兌換碼,該怎麼處理

//生成十位數獎品兌換碼
publicstaticvoidtest13()throwsException{
intcount=10;
Stringstr="";
StringBuildersb=newStringBuilder();
Randomr=newRandom(System.currentTimeMillis());
for(inti=0;i<count;i++){
intd=r.nextInt(62);
sb.append(str.charAt(d));
}
System.out.println(sb.toString());
}

H. 團購網生成優惠券/優惠碼的演算法是什麼

我觀察就是隨即生成。
因為這種優惠券造假成分不是很大,所以沒有必要在這方面下功夫

I. 淘寶優惠券代碼怎麼生成

這個優惠券是提取不了的,只能再次購買時可以附加上去
這樣就可以優惠的