📄 hotel.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.sql.*;
public class Hotel extends JFrame{
String url="jdbc:odbc:Registry";
Connection con;
Statement stmt;
ResultSet rs;
private JPanel p1,p2,p3;
private JLabel jlwelcome,jlname,jlpassword;
private JTextField jtfname;
private JPasswordField jpfpw;
private JButton jbconfirm,jbclear,jbexit;
private String un,pw;
public Hotel(){
super("Please enter your username and password");
p1=new JPanel();
p1.setLayout(new FlowLayout());
p1.setBackground(Color.white);
ImageIcon welcomicon=new ImageIcon("welcome.jpg");
jlwelcome=new JLabel(welcomicon);
p1.add(jlwelcome);
p2=new JPanel();
p2.setLayout(new GridLayout(2,2,30,5));
jlname=new JLabel("Please enter your user name");
jlpassword=new JLabel("Please enter your password");
jtfname=new JTextField(15);
jpfpw=new JPasswordField(15);
p2.add(jlname);
p2.add(jtfname);
p2.add(jlpassword);
p2.add(jpfpw);
p3=new JPanel();
p3.setLayout(new GridLayout(1,3));
jbconfirm=new JButton("Login");
jbconfirm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
un=jtfname.getText();
pw=jpfpw.getText();
if(un.equals("")||pw.equals("")){
JOptionPane.showMessageDialog(null, "You didn't enter the name or license");
}
else if(un.equals("mwtc")||pw.equals("mwtc")){
renting application = new renting();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(false);
}
else{
JOptionPane.showMessageDialog(null, "You enter the wrong user name or password");
}
}
});
jbclear=new JButton("Clear");
jbclear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
jtfname.setText("");
jpfpw.setText("");
}
});
jbexit=new JButton("Exit");
jbexit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.exit(0);
}
});
p3.add(jbconfirm);
p3.add(jbclear);
p3.add(jbexit);
Container contentPane = getContentPane();
contentPane.setLayout(new BoxLayout(contentPane,BoxLayout.Y_AXIS));
contentPane.add(p1);
contentPane.add(p2);
contentPane.add(p3);
setSize(600, 450);
setVisible(true);
}
public static void main(String args[]){
Hotel application = new Hotel();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class renting extends JFrame{
String url="jdbc:odbc:Registry";
Connection con;
Statement stmt;
ResultSet rs;
private JPanel jp1,jp2,jp3,jp35,jp36,jp4,jp5,jp55,jp6;
private JLabel jltitle,roomlabel,jlroomid,jlreservetime,jldays,jlcname,jlcssn;
private JTextField jtfroomid,jtfreservetime,jtfdays,jtfcname,jtfcssn;
private JButton jbmanageroom,jbmanagecustomer,jbconfirm,jbcancel,jbreserve,jborderr,jborderc,jbshowall;
private JTable roomtable;
private JComboBox jcbroomtype;
private JScrollPane jsp;
private ImageIcon roomicon;
private String order;
private String roomid,stringroomtype="Single";
private String stringreservetime,stringdays,roomstatus;
private String cname,cssn;
private String []roomtype={"Single","Double","Luxury"};
private String squery=null;
private GregorianCalendar calendar;
private boolean except;
public renting(){
calendar=new GregorianCalendar();
final int thisYear=calendar.get(Calendar.YEAR);
final int thisMonth=calendar.get(Calendar.MONTH)+1;
final int today=calendar.get(Calendar.DAY_OF_MONTH);
jp1=new JPanel();
jltitle=new JLabel("Please select the room the customer want");
jp1.setLayout(new FlowLayout());
jp1.add(jltitle);
jp2=new JPanel();
jp2.setLayout(new GridLayout(7,1));
jp2.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Select room type"),
BorderFactory.createEmptyBorder(5,5,5,5)));
jcbroomtype=new JComboBox(roomtype);
jcbroomtype.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
stringroomtype = (String)jcbroomtype.getSelectedItem();
roomicon=new ImageIcon(stringroomtype+".jpg");
jp3.remove(roomlabel);
roomlabel=new JLabel(roomicon);
jp3.add(roomlabel);
jp3.setVisible(false);
jp3.setVisible(true);
}
});
jlroomid=new JLabel("Enter Room Id");
jlreservetime=new JLabel("He will reserve it after how many days?");
jldays=new JLabel("Enter Duration Time");
jtfroomid=new JTextField(10);
jtfreservetime=new JTextField(10);
jtfdays=new JTextField(10);
jp2.add(jcbroomtype);
jp2.add(jlroomid);
jp2.add(jtfroomid);
jp2.add(jlreservetime);
jp2.add(jtfreservetime);
jp2.add(jldays);
jp2.add(jtfdays);
jp3=new JPanel();
jp3.setLayout(new FlowLayout());
jp3.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Room Picture"),
BorderFactory.createEmptyBorder(5,5,5,5)));
roomicon=new ImageIcon(stringroomtype+".jpg");
roomlabel=new JLabel(roomicon);
jp3.add(roomlabel);
jp35=new JPanel();
jp35.setLayout(new GridLayout(4,1));
jp35.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Customer Information"),
BorderFactory.createEmptyBorder(5,5,5,5)));
jlcname=new JLabel("Enter Customer's Name");
jlcssn=new JLabel("Enter Customer's SSN");
jtfcname=new JTextField(20);
jtfcssn=new JTextField(20);
jp35.add(jlcname);
jp35.add(jtfcname);
jp35.add(jlcssn);
jp35.add(jtfcssn);
jp36=new JPanel();
jp36.setLayout(new BoxLayout(jp36,BoxLayout.Y_AXIS));
jp36.add(jp3);
jp36.add(jp35);
jp4=new JPanel();
jp4.setLayout(new GridLayout(1,2,40,0));
jp4.add(jp2);
jp4.add(jp36);
jp5=new JPanel();
jp5.setLayout(new GridLayout(1,5));
jbmanageroom=new JButton("Manage Room");
jbmanageroom.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
rmanage application = new rmanage();
}
});
jbmanagecustomer=new JButton("Manage Customer");
jbmanagecustomer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Cmanage application = new Cmanage();
}
});
jbconfirm=new JButton("Rent Today");
jbconfirm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try{
roomid=jtfroomid.getText();
stringdays=jtfdays.getText();
cname=jtfcname.getText();
cssn=jtfcssn.getText();
int ex=1/(int)Double.parseDouble(stringdays);
except=false;
}catch(Exception e){
except=true;
JOptionPane.showMessageDialog(null, "You didn't enter the renting days");
}
if (except==false){
if(roomid.equals("")||cname.equals("")||cssn.equals("")){
JOptionPane.showMessageDialog(null, "You didn't enter the required values");
}
else{
String squery="SELECT ROOM_STATUS FROM rooms WHERE ROOM_ID LIKE "+"\'"+roomid+"\'";
try {
con=DriverManager.getConnection(url);
stmt=con.createStatement();
rs=stmt.executeQuery(squery);
while (rs.next()){
roomstatus=rs.getString("ROOM_STATUS");
}
}catch(SQLException ex) {
System.err.println("SQLException: "+ex.getMessage());
}
if (roomstatus.equals("occupied")||roomstatus.equals("reserved")){
JOptionPane.showMessageDialog(null, "This room is not available today");
}
else{
Object[] options={"Yes","Cancel"};
JFrame frame=new JFrame();
int n=JOptionPane.showOptionDialog(frame,"Are you sure want to rent\n","Choose",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,options,options[0]);
if(n==JOptionPane.OK_OPTION){
int durationdays=(int)Double.parseDouble(stringdays);
calendar.add(Calendar.DAY_OF_MONTH,durationdays);
int returnYear=calendar.get(Calendar.YEAR);
int returnMonth=calendar.get(Calendar.MONTH)+1;
int returnday=calendar.get(Calendar.DAY_OF_MONTH);
calendar=new GregorianCalendar();
String update1="update rooms set ROOM_STATUS ="+"\'"+"occupied"+"\'"+" where ROOM_ID like " +"\'"+roomid+"\'";
String update2="update rooms set CUSTOMER_NAME ="+"\'"+cname+"\'"+" where ROOM_ID like " +"\'"+roomid+"\'";
String update3="update rooms set DURATION = "+"\'"+stringdays+"\'"+" where ROOM_ID like " +"\'"+roomid+"\'";
String update4="update rooms set CUSTOMER_SSN ="+"\'"+cssn+"\'"+" where ROOM_ID like " +"\'"+roomid+"\'";
String update5="update rooms set RESERVE_TIME ="+"\'"+today+"/"+thisMonth+"/"+thisYear+"\'"+" where ROOM_ID like " +"\'"+roomid+"\'";
String update6="update rooms set RETURN_TIME ="+"\'"+returnday+"/"+returnMonth+"/"+returnYear+"\'"+" where ROOM_ID like " +"\'"+roomid+"\'";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(java.lang.ClassNotFoundException e){
System.err.print("ClassNotFoundException:");
System.err.println(e.getMessage());
}
try {
con=DriverManager.getConnection(url);
stmt=con.createStatement();
stmt.executeUpdate(update1);
stmt.executeUpdate(update2);
stmt.executeUpdate(update3);
stmt.executeUpdate(update4);
stmt.executeUpdate(update5);
stmt.executeUpdate(update6);
stmt.close();
con.close();
}catch(SQLException ex) {
System.err.println("SQLException: "+ex.getMessage());
}
jp6.setVisible(false);
jp6.remove(jsp);
Vector dataVector = new Vector();
Vector headVector=new Vector(4);
squery = "select * from rooms";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(java.lang.ClassNotFoundException e){
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try {
con=DriverManager.getConnection(url);
stmt=con.createStatement();
rs=stmt.executeQuery(squery);
while(rs.next()) {
String i = rs.getString("ROOM_ID");
String t = rs.getString("ROOM_TYPE");
String s = rs.getString("ROOM_STATUS");
String c = rs.getString("CUSTOMER_NAME");
Vector rowVector = new Vector();
rowVector.addElement(i);
rowVector.addElement(t);
rowVector.addElement(s);
rowVector.addElement(c);
dataVector.addElement(rowVector);
}
headVector.addElement("ROOM_ID");
headVector.addElement("ROOM_TYPE");
headVector.addElement("ROOM_STATUS");
headVector.addElement("CUSTOMER_NAME");
stmt.close();
con.close();
} catch(SQLException ex) {
System.err.println("SQLException: "+ex.getMessage());
}
JTable table = new JTable(dataVector, headVector);
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
jsp = new JScrollPane(table, v, h);
jp6.add(jsp);
jp6.setVisible(true);
}
}
}
}
}
});
jbreserve=new JButton("Reserve");
jbreserve.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try{
roomid=jtfroomid.getText();
stringreservetime=jtfreservetime.getText();
stringdays=jtfdays.getText();
cname=jtfcname.getText();
cssn=jtfcssn.getText();
int ex=1/(int)Double.parseDouble(stringdays);
except=false;
}catch(Exception e){
except=true;
JOptionPane.showMessageDialog(null, "You didn't enter the renting days");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -