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

📄 ginshopmanagement.java

📁 酒店管理软件的源代码
💻 JAVA
字号:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
import java.util.*;
import java.io.*;
import javax.swing.event.*;
import javax.swing.border.*;

public class GinshopManagement
{
  public static void main(String [] args)
  {
    MainFrame frame = new MainFrame();
    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    frame.show();
  }
}

class MainFrame extends JFrame
{
  public MainFrame()
  {
    //set MainFrame size
    Toolkit kit = Toolkit.getDefaultToolkit();
    Dimension screenSize = kit.getScreenSize();
    int screenWidth = screenSize.width;
    int screenLength = screenSize.height;
    setSize(screenWidth / 2 + 30, screenLength / 2);
    setLocation((screenWidth - getWidth()) / 2, (screenLength - getHeight()) / 2);

    setTitle("欢迎使用:酒店管理系统");

    loadData();
    initMenu();
    if(autoConnect == true)
      connectDB();
    initGUI();

    addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent event)
      {
        try
        {
          saveData();
          if (stat != null)
            stat.close();
          if (conn != null)
            conn.close();
        }
        catch(SQLException e)
        {
          e.printStackTrace();
        }
        finally
        {
          System.exit(0);
        }
      }
    });
  }

  private void initGUI()
  {
    JTabbedPane tabbedPane = new JTabbedPane();
    CheckInPreorderPanelInstance = new CheckInPreorderPanel(this);
    CheckOutPanelInstance = new CheckOutPanel(this);
    tabbedPane.addTab("入住及预定", CheckInPreorderPanelInstance);
    tabbedPane.addTab("结账退房", CheckOutPanelInstance);

    getContentPane().add(tabbedPane, BorderLayout.CENTER);
  }
  private void initMenu()
  {
    JMenu fileMenu = new JMenu("文件(F)");
    fileMenu.setMnemonic('F');
    fileMenu.add(new ConnectAction("连接数据库...", true, KeyEvent.VK_L, InputEvent.CTRL_MASK));
    fileMenu.add(new ConnectAction("配置数据库...", false, KeyEvent.VK_C, InputEvent.CTRL_MASK));

    JMenuBar menuBar = new JMenuBar();
    menuBar.add(fileMenu);
    setJMenuBar(menuBar);
  }
  public void connectDB()
  {
    try
    {
      if(stat != null)
        stat.close();
      if(conn != null)
        conn.close();
      conn = getConnection("酒店管理");
      stat = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                        ResultSet.CONCUR_UPDATABLE);
    }
    catch(IOException e)
    {
      e.printStackTrace();
    }
    catch(SQLException e)
    {
      e.printStackTrace();
      Toolkit.getDefaultToolkit().beep();
      if(JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(this, "无法连接到数据库\n"+
                                    "这可能是因为数据库文件的路径不正确\n"
                                    + "要配置路径吗?", "无法打开数据库",
                                    JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE))
      {
        formerFilePath = "";
        reconnect = true;
        showConnectDBDialog();
      }
    }
  }

  private void loadData()
  {
    Properties defaultSettings = new Properties();
    defaultSettings.put("userName", "");
    defaultSettings.put("password", "");
    defaultSettings.put("formerFilePath", "");
    defaultSettings.put("autoConnect", "false");
    settings = new Properties(defaultSettings);
    try
    {
      FileInputStream in = new FileInputStream("酒店管理.db");
      settings.load(in);
    }
    catch(IOException e)
    {
      e.printStackTrace();
    }
    userName = settings.getProperty("userName");
    password = Safety.coordinate(settings.getProperty("password"));
    formerFilePath = settings.getProperty("formerFilePath");
    autoConnect = ((settings.getProperty("autoConnect").equals("true")) ? true : false);
  }
  private void saveData()
  {
    try
    {
      FileOutputStream out = new FileOutputStream("酒店管理.db");
      settings.put("userName", userName);
      settings.put("password", Safety.chaos(password)); //加密之后再储存
      settings.put("formerFilePath", formerFilePath);
      settings.put("autoConnect", String.valueOf(autoConnect));
      settings.store(out, "GinshopManagement");
    }
    catch(IOException e)
    {
      e.printStackTrace();
    }
  }
  private void showConnectDBDialog()
  {
    if(connectDBDialog == null)
      connectDBDialog = new DataExchangeDialog(this, new ConnectDBPanel(this), "连接数据库");
    connectDBDialog.showDialog();
  }

  public void showMessage(String message)
  {
    Toolkit.getDefaultToolkit().beep();
    JOptionPane.showMessageDialog(MainFrame.this, message,
                                  "错误", JOptionPane.INFORMATION_MESSAGE);
  }
  public void reConnect()
  {
    if(reconnect)
    {
      if(CheckInPreorderPanelInstance != null)
        CheckInPreorderPanelInstance.clear();
      connectDB();
      if(CheckInPreorderPanelInstance != null)
        CheckInPreorderPanelInstance.updatehouseTypeComboBox();
    }
  }
  private Connection getConnection(String DBName)
     throws SQLException, IOException
  {
     Properties props = new Properties();
     props.setProperty("password", password);
     props.setProperty("user", userName);
     props.setProperty("url", "jdbc:odbc:" + DBName);
     System.setProperty("jdbc.drivers", "sun.jdbc.odbc.JdbcOdbcDriver");
     return
         DriverManager.getConnection(props.getProperty("url"), props);
  }

  public void showWarningMessage(String message)
  {
    Toolkit.getDefaultToolkit().beep();
    JOptionPane.showMessageDialog(MainFrame.this, message,
                                  "错误", JOptionPane.WARNING_MESSAGE);
  }
  private class ConnectAction extends AbstractAction
  {
    public ConnectAction(String name, boolean connectImmediately, int keyCode,
                                     int modifiers)
    {
      super(name);
      this.connectImmediately = connectImmediately;
      putValue(Action.ACCELERATOR_KEY,
               KeyStroke.getKeyStroke(keyCode,modifiers));
    }
    public void actionPerformed(ActionEvent event)
    {
      reconnect = connectImmediately;
      showConnectDBDialog();
    }
    private boolean connectImmediately;
  }

  private DataExchangeDialog connectDBDialog = null;
  private CheckOutPanel CheckOutPanelInstance;
  private Properties settings;
  private boolean reconnect;

  public CheckInPreorderPanel CheckInPreorderPanelInstance;
  public String formerFilePath = "";
  public String userName = "";
  public String password = "";
  public boolean autoConnect = false;

  public Connection conn = null;
  public Statement stat = null;

  public static final int NAME = 0;
  public static final int ID = 1;
  public static final int HOUSE_NUM = 2;
  public static final int TYPE = 3;
  public static final int TIME = 4;
  public static final int MONEY = 5;
}

⌨️ 快捷键说明

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