lendorborrowframe.java

来自「图书管理系统:用户管理」· Java 代码 · 共 529 行 · 第 1/2 页

JAVA
529
字号
											}
											count++;
											canbook--;
											haveborrow++;
											setTitle(message+"正在借书,还可以借"+canbook+"本书.");
											thisprogress.setValue(count);thisprogress.setString(""+count+"");
											thistable.repaint();
										}
									}
									else{
										JOptionPane.showMessageDialog(this,"图书馆没有此书");
									}
									
								}
								catch(SQLException e3){
									JOptionPane.showMessageDialog(this,"计算机已与服务器断开");
								}
							}
							else{
								JOptionPane.showMessageDialog(this,"已有一本同样的书待借");
							}
						}
						else{
							JOptionPane.showMessageDialog(this,"这个读者借的书已达到最大数量","警告对话框",JOptionPane.WARNING_MESSAGE);
						}
					}
					else{
						JOptionPane.showMessageDialog(this,"请输入读者卡号进行验证");
					}
				}
				else{
					JOptionPane.showMessageDialog(this,"请输入书号");
				}
			}
			else{
				if(book.length()!=0){
					String queue="SELECT * FROM Lend_Book WHERE ReaderCardID = "+message+" AND BookID = "+book;
					String delbook="SELECT * FROM Book_Info WHERE BookID = "+book;
					try{
						ResultSet rs=sql.executeQuery(queue);
						if(rs.next()){
							ResultSet result=sql.executeQuery(delbook);
							if(result.next()){
								boolean full=true;
								for(int i=0;i<thistime.length;i++){
									if(thistime[i][0]==null){
										for(int j=0;j<5;j++){
											thistime[i][j]=result.getString(j+1);
										}
										count++;
										thisprogress.setValue(count);thisprogress.setString(""+count+"");
										full=false;
										thistable.repaint();
										break;
									}
								}
								if(full){
									extend();
									for(int j=0;j<5;j++){
										thistime[thistime.length/2][j]=result.getString(j+1);
									}
									count++;
									thisprogress.setValue(count);thisprogress.setString(""+count+"");
									thistable.repaint();
								}
							}
						}
						else{
							JOptionPane.showMessageDialog(this,"这个读者没借过这本书");
						}
					}
					catch(SQLException e2){
						JOptionPane.showMessageDialog(this,"计算机已与服务器断开");
					}
				}
				else{
					JOptionPane.showMessageDialog(this,"请输入书号");
				}
			}
		}
		else if(e.getSource()==retractbutton){
			int[] selectrow;
			int g=0;
			if((selectrow=thistable.getSelectedRows())!=null){
				for(int i=0;i<selectrow.length;i++){
					if(thistime[selectrow[i]][0]!=null){
						g++;
					}
				}
				if(g!=0){
					int sel=0;
					if(lend){
						sel=JOptionPane.showConfirmDialog(this,"撤消借这"+g+"本图书?","确认对话框",JOptionPane.YES_NO_OPTION);
					}
					else{
						sel=JOptionPane.showConfirmDialog(this,"撤消还这"+g+"本图书?","确认对话框",JOptionPane.YES_NO_OPTION);
					}
					if(sel==JOptionPane.YES_OPTION){
						for(int i=0;i<selectrow.length;i++){
							for(int j=0;j<5;j++){
								thistime[selectrow[i]][j]=null;
							}
						}
						if(lend){
							setTitle(message+"正在借书,还可以借"+(canbook-g)+"本书.");
						}
						count=count-g;
						thisprogress.setValue(count);thisprogress.setString(""+count+"");
						thistable.repaint();
					}
				}
			}
		}
		else if(e.getSource()==clearbutton){
			cardfield.setText(null);
			bookfield.setText(null);
		}
		else if(e.getSource()==commitbutton){
			int sel=0;
			if(lend){
				sel=JOptionPane.showConfirmDialog(this,"现在提交借书请求吗?","确定对话框",JOptionPane.YES_NO_OPTION);
			}
			else{
				sel=JOptionPane.showConfirmDialog(this,"现在提交还书请求吗?","确定对话框",JOptionPane.YES_NO_OPTION);
			}
			if(sel==JOptionPane.YES_OPTION){
				int m=0;
				for(int i=0;i<thistime.length;i++){
					if(thistime[i][0]!=null){
						String insertordel=null;
						if(lend){
							insertordel="INSERT INTO Lend_Book VALUES ("+"'"+message+"',"+"'"+thistime[i][0]+"',"+"'"+new Timestamp(System.currentTimeMillis())+"')";
						}
						else{
							insertordel="DELETE FROM Lend_Book WHERE ReaderCardID = "+message+" AND BookID = "+thistime[i][0];
						}
						try{
							int n=sql.executeUpdate(insertordel);
							if(n==1){
								m++;
							}
						}
						catch(SQLException e3){
							JOptionPane.showMessageDialog(this,"计算机已与服务器断开");
						}
					}
				}
				resetthistable();
				thistable.repaint();
				if(lend){
					JOptionPane.showMessageDialog(this,"成功借书"+m+"本");
				}
				else{
					JOptionPane.showMessageDialog(this,"成功还书"+m+"本");
				}
				String queue="SELECT * FROM Book_Info,Lend_Book WHERE ReaderCardID = "+"'"+message+"'"+" AND Book_Info.BookID = Lend_Book.BookID";
				try{
					int n=0;
					ResultSet rs=sql.executeQuery(queue);
					resethavetable();
					while(rs.next()){
						if(n==havetime.length){
							container.remove(havescroll);
							Object[][] temp=havetime;
							havetime=new Object[temp.length*2][5];
							havetable=new JTable(havetime,columnName);
							if(!lend){
								havetable.addMouseListener(pl);
							}
							havescroll=new JScrollPane(havetable);
							container.add(havescroll);
							havescroll.setBounds(0,240,365,120);
							for(int i=0;i<temp.length;i++){
								for(int j=0;j<temp[i].length;j++){
									havetime[i][j]=temp[i][j];
								}
							}
						}
						for(int i=0;i<5;i++){
							havetime[n][i]=rs.getString(i+1);
						}
						n++;
					}
					cardfield.setText(null);
					bookfield.setText(null);
					havetable.repaint();
					thisprogress.setValue(0);thisprogress.setString(""+0+"");
					haveprogress.setValue(n);haveprogress.setString(""+n+"");
				}
				catch(SQLException e3){
					JOptionPane.showMessageDialog(this,"计算机已与服务器断开");
				}
			}
		}
		else{
			int[] n=havetable.getSelectedRows();
			if(n!=null){
				for(int i=0;i<n.length;i++){
					if(havetime[n[i]][0]!=null){
						for(int z=0;z<thistime.length;z++){
							if(thistime[z][0]!=null){
								if(thistime[z][0].equals(havetime[n[i]][0])){
									JOptionPane.showMessageDialog(this,"在还书列表中已有一本相同的书");
									return;
								}
							}
						}
						int j;
						for(j=0;j<thistime.length;j++){
							if(thistime[j][0]==null){
								for(int g=0;g<5;g++){
									thistime[j][g]=havetime[n[i]][g];
								}
								count++;
								break;
							}
						}
						if(j==thistime.length-1){
							extend();
							for(int g=0;g<5;g++){
								thistime[j+1][g]=havetime[n[i]][g];
							}
							count++;
						}
					}
			    }
			}
			thisprogress.setValue(count);thisprogress.setString(""+count+"");
			thistable.repaint();
		}
	}
	//当返回行数多于上限时,扩展列表
	private void extend(){
		container.remove(thisscroll);
		Object[][] temp=thistime;
		thistime=new Object[temp.length*2][5];
		thistable=new JTable(thistime,columnName);
		thisscroll=new JScrollPane(thistable);
		container.add(thisscroll);
		thisscroll.setBounds(0,95,365,120);
		for(int i=0;i<temp.length;i++){
			for(int j=0;j<temp[i].length;j++){
				thistime[i][j]=temp[i][j];
			}
		}
	}
	//重新对列表赋值
	private void resetthistable(){
		for(int i=0;i<thistime.length;i++){
			for(int j=0;j<thistime[i].length;j++){
				thistime[i][j]=null;
			}
		}
	}
//	重新对列表赋值
	private void resethavetable(){
		for(int i=0;i<havetime.length;i++){
			for(int j=0;j<havetime[i].length;j++){
				havetime[i][j]=null;
			}
		}
	}
}

⌨️ 快捷键说明

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