JAVA實現住宿預約管理系統

這個學期學了面向對象設計與分析,讓做一個住宿預約管理系統,因爲事情有點多,做的就比較倉促,很多功能都不完善,勉強湊合着看
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

思路:

  • 功能:
    (1) 記錄訂單:分爲預約訂房、現場訂房
    (2) 修改訂單信息:分爲更換房間、記錄到達、刪除訂單
  • 使用圖形化界面顯示房間信息、訂單信息,通過不同的功能按鈕對數據進行操作,其中房間信息、訂單信息的數據存放在數據庫中,通過jdbc調用。
    在這裏插入圖片描述

細節:

  • 記錄訂單就是記錄每一個房客的信息和更新房間的狀態,房客填寫信息,根據房客想要入住的房間類型查詢該類房間的信息,若有狀態爲“空閒”的房間就將房客安排到這個房間,若沒有則訂房失敗。預約訂房和現場訂房的區別是,預約訂房得到的房間狀態爲“預約”,而現場訂房得到的房間狀態爲“入住”。
  • 修改訂單信息都是首先以房客的電話號碼爲根據查找到要修改的訂單,之所以用電話號碼爲根據是因爲電話號碼是唯一的。更換房間需要輸入想要更換的房間的房號,若該房間的狀態爲“空閒”,則更換成功,否則更換失敗,在更換成功後需要將原來的房間狀態改爲“空閒”,新的房間狀態改爲“預定”或者“入住”,將訂單中的房間改爲新房間;刪除訂單需要再次確認一遍,確認刪除後再刪除該條記錄,同時將該訂單中的房間狀態改爲“空閒”;記錄到達只需將訂單中的房間狀態改爲“入住”。

完整代碼如下:

import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class Main {
	public static void main(String[] args) {
		HotelJFrame a = new HotelJFrame() ;
 	}
}

public class Room {		//房間類
	String type ;		//房間型號
	String state ;		//當前房間狀態
	int roomNum ;		//房號
	double price ;		//價格
	Room() {}
	Room(String type, int roomNum, String state, double price) {
		this.type = type ;
		this.roomNum = roomNum ;
		this.state = state ;				
		this.price = price ;
	}
	void setState(String state) {	//修改房間狀態
		this.state = state ;
	}
	double getPrice() {				//返回價格
		return price ;
	}
	int getRoomNum() {				//返回房號
		return roomNum ;
	}
	String getType() {				//返回房間類型
		return type ;
	}
	String getState() {				//返回房間狀態
		return state ;
	}
}

public class Customer {				//房客類
	String checkInTime ;			//入住時間
	String leaveTime ;				//離開時間
	String roomType ;				//房間型號
	String phone ;					//電話號碼
	String name ;					//名字
	int covers ;					//人數
	int roomNum ;					//房間號	
	Customer(String checkInTime, String leaveTime, String roomType, String phone, String name, int covers, int roomNum) {
		this.covers = covers ;
		this.roomNum = roomNum ;
		this.checkInTime = checkInTime ;
		this.leaveTime = leaveTime ;
		this.phone = phone  ;
		this.name = name  ;
		this.roomType = roomType ;
	}
	int getCovers() {
		return covers ;
	}
	int getRoomNum() {
		return roomNum ;
	}
	String getName() {
		return name ;
	}
	String getPhone() {
		return phone ;
	}
	String getCheckInTime() {
		return checkInTime ;
	}
	String getLeaveTime() {
		return leaveTime ;
	}
	String getRoomType() {
		return roomType ;
	}
	void setCheckInTime(int covers) {
		this.covers = covers ;
	}
	void setRoomNum(int roomNum) {
		this.roomNum = roomNum ;
	}
	void setCheckInTime(String checkInTime) {
		this.checkInTime = checkInTime ;
	}
	void setLeaveTime(String leaveTime) {
		this.leaveTime = leaveTime ;
	}
	void setPhone(String phone) {
		this.phone = phone  ;
	}
	void setname(String name) {
		this.name = name  ;
	}
	void setRoomType(String roomType) {
		this.roomType = roomType ;
	}
}

public class Display {			//顯示房間、房客信息類
	Vector<Customer> customers ;		
	Vector<Room> rooms ;
	void setCustomer() {		//從數據庫導入房客信息
		customers = new Vector<Customer>() ;
		try {	Class.forName("org.apache.derby.jdbc.EmbeddedDriver") ;	}
		catch(Exception ee) {}
		try {
			Connection con = DriverManager.getConnection("jdbc:derby:data;create=false") ;
			Statement sql = con.createStatement() ;
			ResultSet rs = sql.executeQuery("SELECT * FROM bookingMessage ") ;
			while(rs.next()) {
				String name = rs.getString(1) ;
				int roomNum = rs.getInt(2) ;
				int cover = rs.getInt(3) ;
				String phone = rs.getString(4) ;
				String checkInTime = rs.getString(5) ;
				String leaveTime = rs.getString(6) ;
				String type = rs.getString(7) ;
				Customer c = new Customer(checkInTime, leaveTime, type, phone, name, cover, roomNum) ;
				customers.add(c) ;
			}
			con.close() ;
		}
		catch(Exception ee) {}
	}
	void setRoom() {			//從數據庫導入房間信息
		rooms = new Vector<Room>() ;	
		try {	Class.forName("org.apache.derby.jdbc.EmbeddedDriver") ;	}
		catch(Exception ee) {}
		try {
			Connection con = DriverManager.getConnection("jdbc:derby:data;create=false") ;
			Statement sql = con.createStatement() ;
			ResultSet rs = sql.executeQuery("SELECT * FROM roomInf ") ;
			while(rs.next()) {
				String type = rs.getString(1) ;
				int roomNum = rs.getInt(2) ;
				String state = rs.getString(3) ;
				double price = rs.getDouble(4) ;
				Room r = new Room(type, roomNum, state, price) ;
				rooms.add(r) ;
			}
			con.close() ;
		}
		catch(Exception ee) {}
	}
	int getRoomLength() {				//獲得房間數量
		return rooms.size() ;
	}
	int getCustomerLength() {			//獲得房客數量
		return customers.size() ;
	}
	Room getRoom(int i) {				//獲得房間信息
		return rooms.get(i) ;
	}
	Customer getCustomer(int i) {		//獲得房客信息
		return customers.get(i) ;
	}
}

public class HotelJFrame extends JFrame implements ActionListener {
	JButton bookingFunction ;		//預約功能
	JButton arriveFunction ;		//簽到功能
	JButton changeFunction ;		//修改功能
	JButton back ;					//返回頁面按鈕
	JPanel home ;					//主頁面
	BookingPanel bookingPanel ;		//預約面板
	ChangePanel changePanel ;		//到達面板
	int flag = 0 ;					//記錄使用了哪一種功能
	HotelJFrame() {
		setLayout(null) ;
		setBounds(300, 100, 800, 800) ;
		setVisible(true) ;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
		bookingFunction = new JButton("記錄預約") ;
		bookingFunction.addActionListener(this) ;
		bookingFunction.setBounds(200, 300, 150, 100);
		arriveFunction = new JButton("修改信息") ;
		arriveFunction.addActionListener(this) ;
		arriveFunction.setBounds(450, 300, 150, 100);
		back = new JButton("返回") ;
		back.setBounds(100, 700, 100, 50) ;
		back.addActionListener(this) ;
		back.setVisible(false) ;
		home = new JPanel() ;
		home.setLayout(null) ;
		home.setBounds(0, 0, 800, 800) ;
		home.add(bookingFunction) ;
		home.add(arriveFunction) ;
		add(back) ;
		add(home) ;		
	}
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == bookingFunction) {		//點擊“記錄預約”進入預約功能
			bookingPanel = new BookingPanel() ;
			home.setVisible(false) ;
			remove(home) ;
			add(bookingPanel) ;
			back.setVisible(true) ;
			flag = 1 ;
		}
		if(e.getSource() == arriveFunction) {		//點擊“記錄到達”進入簽到功能
			changePanel = new ChangePanel() ;
			home.setVisible(false) ;
			remove(home) ;
			add(changePanel) ;
			back.setVisible(true) ;
			flag = 2 ;
		}
		if(e.getSource() == back) {				//返回功能頁面
			if(flag == 1) {
				bookingPanel.setVisible(false) ;
				remove(bookingPanel) ;				//移除填寫預約面板
			}
			if(flag == 2) {
				changePanel.setVisible(false) ;		//移除到達面板
				remove(changePanel) ;
 			}
			home.setVisible(true) ;
			add(home) ;							//重新添加功能按鈕
			back.setVisible(false) ;
 		}
	}
}

public class BookingPanel extends JPanel implements ActionListener{
	JButton reservation ;		//預約訂房按鈕
	JButton walkIn ;			//現場訂房按鈕
	JButton back ;				//返回頁面按鈕
	JTextArea roomMessage ;		//顯示房間信息區域
	Display display ;			//信息類
	ReserveFill fill ;			//預定訂房類
	WalkFill walk ;				//現場訂房類
	int flag = 0 ;				//記錄使用了哪一種訂房方式
	BookingPanel() {
		setLayout(null) ;
		setBounds(0, 0, 800, 800) ;
		setVisible(true) ;
		reservation = new JButton("預約訂房") ;
		reservation.addActionListener(this) ;
		reservation.setBounds(200, 30, 150, 100);
		walkIn = new JButton("現場訂房") ;
		walkIn.addActionListener(this) ;
		walkIn.setBounds(450, 30, 150, 100) ;
		roomMessage = new JTextArea(12, 20) ;
		roomMessage.setBounds(250, 300, 300, 200) ;
		back = new JButton("返回") ;
		back.setBounds(600, 700, 100, 50) ;
		back.addActionListener(this) ;
		back.setVisible(false) ;
		add(back) ;
		add(walkIn) ;
		add(reservation) ;
		add(roomMessage) ;
		setInformation() ;				
	}
	void setInformation() {				//顯示出所有房間信息
		display = new Display() ;
		display.setRoom() ;				//從數據庫中讀取房間信息
		roomMessage.append("  房間      房號        價格            房間狀態\n") ;
		for(int i=0; i<display.getRoomLength(); i++) {		
			Room roomInf = display.getRoom(i) ;
			roomMessage.append(roomInf.getType()+"    "+roomInf.getRoomNum()+"       "
					+roomInf.getPrice()+"                "+roomInf.getState()) ;
			roomMessage.append("\n") ;
		}
	}
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == reservation) {	
			walkIn.setVisible(false) ;
			remove(walkIn) ;
			reservation.setVisible(false) ;		//移除功能按鈕
			remove(reservation) ;
			fill = new ReserveFill() ;	
			add(fill) ;							//添加填寫預約信息面板
			back.setVisible(true) ;
			flag = 1 ;
		}
		if(e.getSource() == walkIn) {		
			walkIn.setVisible(false) ;
			remove(walkIn) ;
			reservation.setVisible(false) ;		//移除功能按鈕
			remove(reservation) ;
			walk = new WalkFill() ;	
			add(walk) ;							//添加填寫預約信息面板
			back.setVisible(true) ;	
			flag = 2 ;
		}
		if(e.getSource() == back) {				//返回功能頁面
			if(flag == 1) {
				fill.setVisible(false) ;
				remove(fill) ;					//移除填寫預約信息面板
			}
			if(flag == 2) {
				walk.setVisible(false) ;		//移除填寫入住信息面板
				remove(walk) ;
 			}
			walkIn.setVisible(true) ;
			reservation.setVisible(true) ;
			add(reservation) ;					//重新添加功能按鈕
			add(walkIn) ;
			back.setVisible(false) ;
 		}
	}
}

public class Order extends JPanel implements ActionListener{			//記錄訂單類
	JTextField name, cover, phone, checkInTime, leaveTime, roomType ;	//填寫訂單信息
	JButton button ;					//提交訂單信息按鈕
	Box baseBox, box1, box2 ;
	Connection con ;
	Statement sql ;
	ResultSet rs ;
	Order() {
		box1 = Box.createVerticalBox() ;			//左邊標籤欄
		box1.add(new JLabel("入住人姓名")) ;
		box1.add(Box.createVerticalStrut(10)) ;
		box1.add(new JLabel("入住人數")) ;
		box1.add(Box.createVerticalStrut(10)) ;
		box1.add(new JLabel("電話號碼")) ;
		box1.add(Box.createVerticalStrut(10)) ;
		box1.add(new JLabel("入住時間")) ;
		box1.add(Box.createVerticalStrut(10)) ;
		box1.add(new JLabel("離開時間")) ;
		box1.add(Box.createVerticalStrut(20)) ;
		box1.add(new JLabel("房間型號")) ;
		box2 = Box.createVerticalBox() ;			//右邊輸入欄
		box2.add(Box.createVerticalStrut(20)) ;
		box2.add(name = new JTextField(30)) ;
		box2.add(Box.createVerticalStrut(10)) ;
		box2.add(cover = new JTextField(30)) ;
		box2.add(Box.createVerticalStrut(10)) ;
		box2.add(phone = new JTextField(30)) ;
		box2.add(Box.createVerticalStrut(10)) ;
		box2.add(checkInTime = new JTextField(30)) ;
		box2.add(Box.createVerticalStrut(10)) ;
		box2.add(leaveTime = new JTextField(30)) ;
		box2.add(Box.createVerticalStrut(10)) ;
		box2.add(roomType = new JTextField(30)) ;
		box2.add(Box.createVerticalStrut(10)) ;
		baseBox = Box.createHorizontalBox() ;
		baseBox.add(box1) ;
		baseBox.add(Box.createHorizontalStrut(10)) ;
		baseBox.add(box2) ;
		button = new JButton("確認") ;
		button.addActionListener(this) ;
		add(baseBox) ;
		add(button) ;
		setBounds(0, 30, 800, 300) ;
		setVisible(true) ;
	}
	public void actionPerformed(ActionEvent e) {}
}

public class ReserveFill extends Order implements ActionListener{		//記錄預定訂單
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == button) {
			try {	Class.forName("org.apache.derby.jdbc.EmbeddedDriver") ;	}
			catch(Exception ee) {}
			try {
				Connection con = DriverManager.getConnection("jdbc:derby:data;create=false") ;
				Statement sql = con.createStatement() ;
				ResultSet rs = sql.executeQuery("SELECT * FROM roomInf WHERE type='"+roomType.getText()+"'") ;//首先從房間信息表中找符合條件的房間
				boolean flag = true ;			//記錄是否預定成功
				while(rs.next()) {
					String type = rs.getString(1) ;
					int number = rs.getInt(2) ;
					String state = rs.getString(3) ;
					if(state.equals("空閒")) {				//想預定的房間類型有空房就預定
						sql.executeLargeUpdate("UPDATE roomInf SET state='預定' WHERE roomNum="+number) ;		//更新房間狀態
						sql.executeUpdate("INSERT INTO bookingMessage VALUES ('"+name.getText()+
								"', "+number+", "+cover.getText()+", '"+phone.getText()+"', '"+
								checkInTime.getText()+"', '"+leaveTime.getText()+"', '"+
								type+"')") ;				//添加預定信息到數據庫中
						JOptionPane.showMessageDialog(this, "預定成功!房間號:"+number+" ",null, JOptionPane.INFORMATION_MESSAGE);
						flag = false ;
						break ;
					}
				}
				if(flag) 
					JOptionPane.showMessageDialog(this, "預定失敗!",null, JOptionPane.INFORMATION_MESSAGE);
				con.close() ;
			}
			catch(Exception ee) {}
		}
	}
}

public class WalkFill extends Order implements ActionListener{		//記錄現場訂單
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == button) {
			try {	Class.forName("org.apache.derby.jdbc.EmbeddedDriver") ;	}
			catch(Exception ee) {}
			try {
				Connection con = DriverManager.getConnection("jdbc:derby:data;create=false") ;
				Statement sql = con.createStatement() ;
				ResultSet rs = sql.executeQuery("SELECT * FROM roomInf WHERE type='"+roomType.getText()+"'") ;//首先從房間信息表中找符合條件的房間
				boolean flag = true ;			//記錄是否預定成功
				while(rs.next()) {
					String type = rs.getString(1) ;
					int number = rs.getInt(2) ;
					String state = rs.getString(3) ;
					if(state.equals("空閒")) {	//想入住的房間類型有空房就入住
						sql.executeLargeUpdate("UPDATE roomInf SET state='入住' WHERE roomNum="+number) ;		//更新房間狀態
						sql.executeUpdate("INSERT INTO bookingMessage VALUES ('"+name.getText()+
								"', "+number+", "+cover.getText()+", '"+phone.getText()+"', '"+
								checkInTime.getText()+"', '"+leaveTime.getText()+"', '"+
								type+"')") ;				//添加入住信息到數據庫中
						JOptionPane.showMessageDialog(this, "入住成功!房間號:"+number+" ",null, JOptionPane.INFORMATION_MESSAGE);
						flag = false ;
						break ;
					}
				}
				if(flag) 
					JOptionPane.showMessageDialog(this, "入住失敗!",null, JOptionPane.INFORMATION_MESSAGE);
				con.close() ;
			}
			catch(Exception ee) {}
		}
	}
}

public class ChangePanel extends JPanel implements ActionListener{
	JLabel label ;
	JTextField phone ;
	JButton arrive ;				//記錄到達
	JButton cancel ;				//刪除訂單
	JButton transfer ;				//換房間
	JTextArea customerMessage ;		//顯示房間信息區域
	JTextArea roomMessage ;			//顯示房間信息區域
	Display display ;				//信息類
	ChangePanel() {
		setLayout(null) ;
		setBounds(0, 0, 800, 800) ;
		setVisible(true) ;
		label = new JLabel("請輸入電話號碼") ;
		label.setBounds(100, 60, 100, 50) ;
		phone = new JTextField(30) ;
		phone.setBounds(210, 70, 400, 30) ;
		arrive = new JButton("記錄到達") ;
		arrive.setBounds(200, 120, 100, 45) ;
		arrive.addActionListener(this) ;
		cancel = new JButton("刪除訂單") ;
		cancel.setBounds(350, 120, 100, 45) ;
		cancel.addActionListener(this) ;
		transfer = new JButton("更換房間") ;
		transfer.setBounds(500, 120, 100, 45) ;
		transfer.addActionListener(this) ;
		customerMessage  = new JTextArea(12, 20) ;
		customerMessage.setBounds(150, 200, 500, 200) ;
		roomMessage = new JTextArea(12, 20) ;
		roomMessage.setBounds(150, 450, 500, 200) ;
		add(label) ;
		add(phone) ;
		add(arrive) ;
		add(cancel) ;
		add(transfer) ;
		add(customerMessage) ;
		add(roomMessage) ;
		setCustomerInf() ;	
		setRoomInf() ;
	}
	void setCustomerInf() {				//顯示出所有房客訂單信息
		display = new Display() ;
		display.setCustomer() ;				//從數據庫中讀取房客訂單信息
		customerMessage.append("姓名        房間          房號       入住人數        電話號碼             入住時間                離開時間\n") ;
		for(int i=0; i<display.getCustomerLength(); i++) {		
			Customer customerInf = display.getCustomer(i) ;
			customerMessage.append(customerInf.getName()+"       "+customerInf.getRoomType()+"        "
					+customerInf.getRoomNum()+"               "+customerInf.getCovers()+"               "
					+customerInf.getPhone()+" "+customerInf.getCheckInTime()+"           "
					+customerInf.getLeaveTime()) ;
			customerMessage.append("\n") ;
		}
	}
	void setRoomInf() {				//顯示出所有房間信息
		display = new Display() ;
		display.setRoom() ;				//從數據庫中讀取房間信息
		roomMessage.append("  房間      房號        價格            房間狀態\n") ;
		for(int i=0; i<display.getRoomLength(); i++) {		
			Room roomInf = display.getRoom(i) ;
			roomMessage.append(roomInf.getType()+"    "+roomInf.getRoomNum()+"       "
					+roomInf.getPrice()+"                "+roomInf.getState()) ;
			roomMessage.append("\n") ;
		}
	}
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == arrive) {			//預約者記錄到達
			SignIn signIn = new SignIn(phone.getText()) ;
			customerMessage.setText("");		//更新信息
			roomMessage.setText("") ;
			setCustomerInf() ;
			setRoomInf() ;
		}
		if(e.getSource() == cancel) {			//刪除訂單  
			Cancel cancel = new Cancel(phone.getText()) ; 
			customerMessage.setText("");		//更新信息
			roomMessage.setText("") ;
			setCustomerInf() ;
			setRoomInf() ;
		}
		if(e.getSource() == transfer) {			//更換房間
			int newRoom = Integer.parseInt(JOptionPane.showInputDialog(this,
					"請輸入要更換的房號", null, JOptionPane.INFORMATION_MESSAGE)) ;
			Change change = new Change(phone.getText(), newRoom) ;
			customerMessage.setText("");		//更新信息
			roomMessage.setText("") ;
			setCustomerInf() ;
			setRoomInf() ;
		}
	}
}

public class Change {
	Connection con ;
	Statement sql ;
	ResultSet rs ;
	Change(String phone, int number) {
		transfer(phone, number) ;
	}
	void transfer(String search, int newNum) {		//更新入住狀態
		try {	Class.forName("org.apache.derby.jdbc.EmbeddedDriver") ;	}
		catch(Exception e) {}
		try {
			con = DriverManager.getConnection("jdbc:derby:data;create=false") ;
			sql = con.createStatement() ;
			rs = sql.executeQuery("SELECT * FROM bookingMessage WHERE phone='"+search+"'") ;//根據電話號碼查找訂單
			rs.next() ;				//跳到下一個纔是篩選出的訂單
			String type = rs.getString(7) ;		//預定房間類型	
			int befortNum = rs.getInt(2) ;		//原來房間號
			rs = sql.executeQuery("SELECT * FROM roomInf WHERE roomNum="+newNum) ;
			rs.next() ;
			String state = rs.getString(3) ;
			if( state.equals("空閒") ) {
				sql.executeLargeUpdate("UPDATE bookingMessage SET roomNum="+newNum+" WHERE roomNum="+befortNum) ;
				sql.executeLargeUpdate("UPDATE roomInf SET state='空閒' WHERE roomNum="+befortNum) ;	//更新原來房間狀態
				sql.executeLargeUpdate("UPDATE roomInf SET state='預定' WHERE roomNum="+newNum) ;	//更新新房間狀態
				JOptionPane.showMessageDialog(new ChangePanel(), "更換成功!", null, JOptionPane.INFORMATION_MESSAGE);
			}
			else 
				JOptionPane.showMessageDialog(new ChangePanel(), "更換失敗!", null, JOptionPane.INFORMATION_MESSAGE);
			con.close() ;
		}
		catch(Exception e) {
			JOptionPane.showMessageDialog(new ChangePanel(), "更換失敗!", null, JOptionPane.INFORMATION_MESSAGE);
		}
	}
}

public class Cancel {
	Connection con ;
	Statement sql ;
	ResultSet rs ;
	Cancel(String phone) {
		del(phone) ;
	}
	void del(String search) {		//刪除訂單
		try {	Class.forName("org.apache.derby.jdbc.EmbeddedDriver") ;	}
		catch(Exception e) {}
		try {
			con = DriverManager.getConnection("jdbc:derby:data;create=false") ;
			sql = con.createStatement() ;
			rs = sql.executeQuery("SELECT * FROM bookingMessage WHERE phone='"+search+"'") ;//根據電話號碼查找訂單
			rs.next() ;							//跳到下一個纔是篩選出的訂單
			int flag = JOptionPane.showConfirmDialog(new ChangePanel(), "確認刪除該訂單?", null, JOptionPane.YES_NO_CANCEL_OPTION) ;
			if(flag == JOptionPane.YES_OPTION) {
				int roomNum = rs.getInt(2) ;		//從訂單裏獲得房間號
				sql.executeLargeUpdate("DELETE FROM bookingMessage WHERE phone='"+search+"'") ;	
				sql.executeLargeUpdate("UPDATE roomInf SET state='空閒' WHERE roomNum="+roomNum) ;	//更新房間狀態
				JOptionPane.showMessageDialog(new ChangePanel(), "刪除成功!", null, JOptionPane.INFORMATION_MESSAGE);
			}
			con.close() ;
		}
		catch(Exception e) {
			System.out.print(e) ;
			JOptionPane.showMessageDialog(new ChangePanel(), "刪除失敗!", null, JOptionPane.INFORMATION_MESSAGE);
		}
	}
}

public class SignIn {
	Connection con ;
	Statement sql ;
	ResultSet rs ;
	SignIn(String phone) {
		sign(phone) ;
	}
	void sign(String search) {		//更新入住狀態
		try {	Class.forName("org.apache.derby.jdbc.EmbeddedDriver") ;	}
		catch(Exception e) {}
		try {
			con = DriverManager.getConnection("jdbc:derby:data;create=false") ;
			sql = con.createStatement() ;
			rs = sql.executeQuery("SELECT * FROM bookingMessage WHERE phone='"+search+"'") ;//根據電話號碼查找訂單
			rs.next() ;				//跳到下一個纔是篩選出的訂單
			int roomNum = rs.getInt(2) ;		//從訂單裏獲得房間號
			sql.executeLargeUpdate("UPDATE roomInf SET state='入住' WHERE roomNum="+roomNum) ;	//更新房間狀態
			JOptionPane.showMessageDialog(new ChangePanel(), "入住成功!", null, JOptionPane.INFORMATION_MESSAGE);
			con.close() ;
		}
		catch(Exception e) {
			JOptionPane.showMessageDialog(new ChangePanel(), "入住失敗!", null, JOptionPane.INFORMATION_MESSAGE);
		}
	}
}

總結:

  • 把所有需要輸出的客房信息、訂單信息分別創建相應的類,並將其作爲對象放到一個Display類裏,從而可以在每個需要顯示信息的地方直接調用一個Display的類即可,減少了代碼的重複
  • 在設計功能的時候要先把最重要的功能選出來,再以此爲基礎進行拓展,這樣可以把一些類似的功能總結到同一個大的功能裏
  • 在使用數據庫創建表時,表中的字段要根據客房類和訂單類中的屬性進行創建
  • 不同的類功能需要更明確一些,不要把太多的功能都放在同一個類裏實現
  • 查找訂單需要根據一個可以唯一確定的信息來查找,例如電話號碼、身份證號等

在最開始寫代碼的時候比較迷,寫的很混亂,但之後看了一下面向對象的課本,結合着UML,稍微有了一點感覺,突然開竅了知道怎麼寫,本來這樣的代碼有點在煉油渣的感覺,之前也做個很多類似的東西,感覺不會有什麼收穫,但結合學的知識之後對面向對象有了更深一點的理解,也逐漸感受到了UML中的各種圖對打代碼的用處和好處,所以上課學的東西都不是沒用的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章