⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tuipiao.java

📁 一般的延时子程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	    		JOptionPane.showMessageDialog(null,"请输入退票数",
	    		                              "错误信息",JOptionPane.ERROR_MESSAGE);
	    		return;
	    	}
	    	
	    	//tuipiaoshu over the tickets
	    	if (cTuiPiaoShu != 0 && cTuiPiaoShu > Integer.parseInt(childNum))
	    	{
	    		JOptionPane.showMessageDialog(null,"退票数大于已定票数,请按\"查询\"按钮查看信息",
	    		                              "错误信息",JOptionPane.ERROR_MESSAGE);
	    		return;
	    	}
	    	
	    	if (aTuiPiaoShu != 0 && aTuiPiaoShu > Integer.parseInt(adultNum))
	    	{
	    		JOptionPane.showMessageDialog(null,"退票数大于已定票数,请按\"查询\"按钮查看信息",
	    		                              "错误信息",JOptionPane.ERROR_MESSAGE);
	    		return;
	    	}
	    	
	    	operationForTuiPiao(cTuiPiaoShu,aTuiPiaoShu);
	    }
		
	}
	//********************************************************************
	
	//operation after returning
	private void operationForTuiPiao(int childTuiPiaoShu,int adultTuiPiaoShu)
	{
		int newChildNum = Integer.parseInt(childNum) - childTuiPiaoShu;
		int newAdultNum = Integer.parseInt(adultNum) - adultTuiPiaoShu;
		float tuiPiaoCost = caculateTuiPiaoCost(childTuiPiaoShu,adultTuiPiaoShu);
		float newCost = Float.parseFloat(cost) - tuiPiaoCost;
		
		try
		{
			RandomAccessFile raf = new RandomAccessFile(file,"rw");
			
			raf.seek(this.locationOfRecord);
			
			//input the message after modified
			raf.writeUTF(dingdanNum);
			raf.writeUTF(name);
		    raf.writeUTF(idNum);
		    raf.writeUTF(flight1);
		    raf.writeUTF(flight2);
		    raf.writeUTF(ticketType);
		    raf.writeUTF(leaveTime1);
		    raf.writeUTF(leaveTime2);
		    raf.writeUTF(String.valueOf(newChildNum));
		    raf.writeUTF(String.valueOf(newAdultNum));
		    raf.writeUTF(String.valueOf(newCost));
		    
		    int totalTuiPiaoShu = childTuiPiaoShu + adultTuiPiaoShu;
		    int sign=returnTime();
		    
		    seatInfo.tuiPiao(flight1,leaveTime1,totalTuiPiaoShu);
		    
		    //when there's the other flight
		    if (flight2.length() != 0)
		       seatInfo.tuiPiao(flight2,leaveTime2,totalTuiPiaoShu);
		       
		    if (newChildNum == 0 && newAdultNum == 0)
		       JOptionPane.showMessageDialog(null,"恭喜你退票成功!"+"\n"+"该订单号已作废!"+"\n"+"你将获得"+tuiPiaoCost +
		                                     "的退票钱"+"\n"+"欢迎你再次选择我们!",
		                                     "退票成功",JOptionPane.INFORMATION_MESSAGE);	
		    else
		       if(sign!=0)
		         JOptionPane.showMessageDialog(null,"恭喜你退票成功!"+"\n"+"你现在剩余"+newChildNum+
		                                     "张儿童票和"+newAdultNum+"张成人票"+"\n"+"你将获得"+tuiPiaoCost +
		                                     "的退票钱"+"\n"+"欢迎你再次选择我们!","退票成功",JOptionPane.INFORMATION_MESSAGE);
		
		    //clean the textField
		    rewrite();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	//********************************************************************
	
	//caculate the returning money
	private float caculateTuiPiaoCost(int childTuiPiaoShu,int adultTuiPiaoShu)
	{
		float tuiPiaoCost = 0;
		int sign=0;
		
		try
		{
			//get message about the flight
			String sqlString = "select childFare,adultFare from flight where flight='"+flight1+"'";
			ResultSet rs = sqlBean.executeQuery(sqlString);
			
			float childFare1 = 0;
			float adultFare1 = 0;
			while(rs.next())
			{
			    childFare1 = rs.getFloat(1);
			    adultFare1 = rs.getFloat(2);	
			}
			
			float childFare2 = 0;
			float adultFare2 = 0;
			if (flight2.length() != 0)
			{
				String sqlString2 = "select childFare,adultFare from flight where flight='"+flight2+"'";
			    ResultSet rs2 = sqlBean.executeQuery(sqlString2);
			    
			    while(rs2.next())
				{
				    childFare1 = rs2.getFloat(1);
				    adultFare2 = rs2.getFloat(2);	
				}
			}
			
			sign=returnTime();
			
			if (flight2.length() == 0){
				
			  if(sign==0)
			    JOptionPane.showMessageDialog(null,"抱歉,您现在不能退票!","错误信息",JOptionPane.ERROR_MESSAGE);
			  if(sign==1)
			    tuiPiaoCost = (childFare1*childTuiPiaoShu + adultFare1*adultTuiPiaoShu) * (float)0.65;
			  if(sign==2)
				tuiPiaoCost = (childFare1*childTuiPiaoShu + adultFare1*adultTuiPiaoShu) * (float)0.7;
			  
			}
			  
			else
			{
			  if(sign==0)
			    JOptionPane.showMessageDialog(null,"抱歉,退票时间已过,您现在不能退票!","错误信息",JOptionPane.ERROR_MESSAGE);
			  if(sign==1)
			    tuiPiaoCost = ( (childFare1 + childFare2)*childTuiPiaoShu +
				                (adultFare1 + adultFare2)*adultTuiPiaoShu ) * (float)0.65;
			  if(sign==2)
				tuiPiaoCost = ( (childFare1 + childFare2)*childTuiPiaoShu +
				                (adultFare1 + adultFare2)*adultTuiPiaoShu ) * (float)0.7;
			}	
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		return tuiPiaoCost;		
	}
	//********************************************************************
	
	public String getCurrentTime(){
		//Get the instance for the class Calendar which used to get the present time
    	Calendar cal = Calendar.getInstance();
    	
    	   //Because there are two Date classes(java.util.Date--java.sql.Date)
    	   //So we should designate the full name for the java.util.Date class 
    	cal.setTime(new java.util.Date());
    	
    	   //Get the present year,month,day
    	String year = String.valueOf(cal.get(Calendar.YEAR));
    	String month = String.valueOf(cal.get(Calendar.MONTH) + 1);
    	String day = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
    	String hour = String.valueOf(cal.get(Calendar.HOUR_OF_DAY));
    	//String minute = String.valueOf(cal.get(Calendar.MINUTE));
    	//String second = String.valueOf(cal.get(Calendar.SECOND));
    	return year.concat(month.concat(day.concat(hour)));
	}
	
	//determine wether can return ticket right now
	public int returnTime(){
		
	    int y=Integer.parseInt(year);
		int m=Integer.parseInt(month);
		int d=Integer.parseInt(day);
		int h=Integer.parseInt(hour);
				
		String st=getCurrentTime();
		int cy=Integer.parseInt(st.substring(0,4));
		int cm=Integer.parseInt(st.substring(4,6));
		int cd=Integer.parseInt(st.substring(6,8));
		int ch=Integer.parseInt(st.substring(8,10));
		
		//compare flight time and current time
		if(y-cy==1)
		  cm=m-1;
		  
		else if(y!=cy)   
		  return 0;
		  
		if(m-cm==1){
		      		switch(m){
		      			case 1:  
		      			case 3:  
		      			case 5: 
		      			case 7:  
		      			case 8:  
		      			case 10:  
		      			case 12:   cd=cd-31; break;
		      			case 2:    {
		      				         if(isLeapYear(cy))   cd=cd-29;
		      			             else  cd=cd-28;
		      			             break;
		      			           }
		      			case 4:  
		      			case 6:  
		      			case 9:  
		      			case 11:   cd=cd-30;   break;
		      			  
		      		}
		  }
		  else if(m!=cm)  return 0;
		  //if the flight is leave the day after tomorrow day,the return can't perform
		  if(d==cd){
		  	if(h-ch<2)  return 0;
		    else   return 1;
		  }
		    
		  else 
		      {
		         if(d-cd<5&&d-cd>0)   return 1;
		         else if(d-cd>5)   return 2;
		         else return 0;
		      }
		  	
		  	  
	}
	
	//wether the year is leap year
	public boolean isLeapYear(int year){
		if(year%4==0&&year%100!=0&&year%400==0)
		  return true;
		else return false;
	}
	//********************************************************************
	
	//get the clients' information
	private boolean getClientInfo(String dingdan,String id)
	{
		RandomAccessFile raf = null;
		
		try
		{				
		    raf = new RandomAccessFile(file,"rw");
		    
		    boolean isDingDanExist = false;
		    boolean isIDRight = false;
		    
		    long tempLocation = 0;
		    raf.seek(0);
		   
		   //find message in file
		   try{
		   	
		   	 //if can't continue reading,throw exception
		   	 while (raf.getFilePointer() < raf.length())
		    {
			    //record current position
			    tempLocation = raf.getFilePointer();
			    
			    dingdanNum = raf.readUTF();
			    name = raf.readUTF();
			    idNum = raf.readUTF();
			    flight1 = raf.readUTF();
			    flight2 = raf.readUTF();
			    ticketType = raf.readUTF();
			    leaveTime1 = raf.readUTF();
			    leaveTime2 = raf.readUTF();
			    childNum = raf.readUTF();
			    adultNum = raf.readUTF();
			    cost = raf.readUTF();
			    
			    if (dingdanNum.equals(dingdan))
			    {
			    	isDingDanExist = true;
			    	
			    	if (idNum.equals(id))
			    	{
			    		isIDRight = true;
			    		this.locationOfRecord = tempLocation;
			    		break;
			    	}
			    	
			    	else 
			    	{
			    		isIDRight = false;
			    		break;
			    	}
			    }		    
		    }
		   }
		   catch(Exception e){
		   	
		   }
		    
		    //display the fist leave time and the second time
		    if (isDingDanExist == true && isIDRight == true)
		    {
		    	String year2="";
		    	String month2="";
		    	String day2="";
		    	String hour2="";
		    	
		    	if (leaveTime1.length() != 0)
		    	{
		    		year = leaveTime1.substring(0,4);
		    	    month = leaveTime1.substring(4,6);
		    	    day = leaveTime1.substring(6,8);
		    	    hour=leaveTime1.substring(8,10);
		    	    
		    	    leaveTime1 = year.concat("-").concat(month).concat("-").concat(day);
		    	}
		    	
		    	if (leaveTime2.length() != 0)
		    	{
		    		year2 = leaveTime2.substring(0,4);
		    	    month2 = leaveTime2.substring(4,6);
		    	    day2 = leaveTime2.substring(6,8);
		    	    hour2 =leaveTime1.substring(8,10);
		    	    
		    	    leaveTime2 = year2.concat("-").concat(month2).concat("-").concat(day2);
		    	}
		    	
		    	if (Integer.parseInt(childNum) == 0 && Integer.parseInt(adultNum) == 0)
		    	{
		    		JOptionPane.showMessageDialog(null,"该订单号已经无效!",
		    		                              "错误信息",JOptionPane.ERROR_MESSAGE);
		    		return false;
		    	}
		    	
		    	return true;
		    	
		    }
		        
		    
		    if (isDingDanExist == true && isIDRight == false)
		    {
		    	JOptionPane.showMessageDialog(null,"身份证号不正确","错误信息",JOptionPane.ERROR_MESSAGE);
		    	return false;
		    }		      
		       
		    if (isDingDanExist == false)
		    {
		    	JOptionPane.showMessageDialog(null,"订单号不存在","错误信息",JOptionPane.ERROR_MESSAGE);
		    	return false;
		    	
		    }		   	
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				//close the file
				raf.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		
		return false;	
	}
    //********************************************************************	

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -