using BookManagementProgram.XtraForm;
using BookManagementProgram.Model;
using BookManagementProgram.Repository;
using System;
using System.Windows.Forms;
using DevExpress.XtraTabbedMdi;
using BookManagementProgram.UserControls;
using BookManagementProgram.BookControls;
using BookManagementProgram.RentalControls;
using DevExpress.XtraBars;
namespace BookManagementProgram
{
public partial class Main : DevExpress.XtraEditors.XtraForm
{
private User user = BookUserRepository.user;
private XtraTabbedMdiManager mdiManager;
public Main()
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterScreen;
switch (user.authorityNo)
{
case 0:
user_page.Visible = true;
book_page.Visible = true;
rental_page.Visible = true;
break;
case 1:
book_page.Visible = true;
rental_page.Visible = true;
break;
case 2:
rental_page.Visible = true;
break;
}
}
// 홈, 사용자, 도서, 렌탈, 연체자,렌탈기록 조회버튼
private void MenuItemClickEvent(object sender, ItemClickEventArgs e)
{
MenuRun(e.Item.Hint, e.Item.Caption);
}
// 비밀번호 변경, 유저 등록, 유저 검색, 도서 추가, 도서 검색, 렌탈등록, 반납등록 버튼
private void ShowDialog_Event(object sender, ItemClickEventArgs e)
{
switch (e.Item.Hint)
{
case "FindByPassword":
(new FindByPassword()).ShowDialog();
break;
case "UserAddForm":
(new UserAddForm()).ShowDialog();
break;
case "UserSearchForm":
(new UserSearchForm()).ShowDialog();
break;
case "BookAddForm":
(new BookAddForm()).ShowDialog();
break;
case "BookSearchForm":
(new BookSearchForm()).ShowDialog();
break;
case "AddRentalForm":
(new AddRentalForm()).ShowDialog();
break;
case "ReturnBookForm":
(new ReturnBookForm()).ShowDialog(); ;
break;
}
}
// 폼 종료
private void Main_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
// 폼 종료 버튼
private void home_close_btn_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
Application.Exit();
}
// 메인 폼 로드
private void Main_Load(object sender, EventArgs e)
{
ChildFormAdd();
MenuRun(home_btn.Hint, home_btn.Caption);
}
// MID Child Form추가
private void ChildFormAdd()
{
mdiManager = new XtraTabbedMdiManager();
mdiManager.MdiParent = this;
mdiManager.HeaderButtons = DevExpress.XtraTab.TabButtons.Close;
mdiManager.HeaderLocation = DevExpress.XtraTab.TabHeaderLocation.Top;
mdiManager.PageAdded += MdiManager_ChildFormAdd;
mdiManager.ClosePageButtonShowMode = DevExpress.XtraTab.ClosePageButtonShowMode.InActiveTabPageAndTabControlHeader;
}
// MDI 설정
private void MdiManager_ChildFormAdd(object sender, MdiTabPageEventArgs e)
{
XtraMdiTabPage page = e.Page;
}
// 메뉴 아이템에 따라 MID Child 생성
private void MenuRun(string Menu, string FormTitle)
{
string nameSpace;
foreach (Form item in this.MdiChildren)
{
if (Menu == item.Name)
{
item.Close();
}
}
Form frm = null;
if (Menu.Equals("UserSelectForm"))
{
nameSpace = "BookManagementProgram.UserControls";
System.Reflection.Assembly assembly = System.Reflection.Assembly.Load("BookManagementProgram");
frm = (Form)assembly.CreateInstance(string.Format("{0}.{1}", nameSpace, Menu));
}
else if(Menu.Equals("BookSelectForm"))
{
nameSpace = "BookManagementProgram.BookControls";
System.Reflection.Assembly assembly = System.Reflection.Assembly.Load("BookManagementProgram");
frm = (Form)assembly.CreateInstance(string.Format("{0}.{1}", nameSpace, Menu));
}
else if (Menu.Equals("SelectRentalForm"))
{
nameSpace = "BookManagementProgram.RentalControls";
System.Reflection.Assembly assembly = System.Reflection.Assembly.Load("BookManagementProgram");
frm = (Form)assembly.CreateInstance(string.Format("{0}.{1}", nameSpace, Menu));
}
else if (Menu.Equals("SelectDelinquentForm"))
{
nameSpace = "BookManagementProgram.RentalControls";
System.Reflection.Assembly assembly = System.Reflection.Assembly.Load("BookManagementProgram");
frm = (Form)assembly.CreateInstance(string.Format("{0}.{1}", nameSpace, Menu));
}
else if (Menu.Equals("SelectRentalHistory"))
{
nameSpace = "BookManagementProgram.RentalControls";
System.Reflection.Assembly assembly = System.Reflection.Assembly.Load("BookManagementProgram");
frm = (Form)assembly.CreateInstance(string.Format("{0}.{1}", nameSpace, Menu));
}
else if (Menu.Equals("MainPageForm"))
{
nameSpace = "BookManagementProgram.MainControls";
System.Reflection.Assembly assembly = System.Reflection.Assembly.Load("BookManagementProgram");
frm = (Form)assembly.CreateInstance(string.Format("{0}.{1}", nameSpace, Menu));
}
if (frm != null)
{
frm.Text = FormTitle;
frm.MdiParent = this;
frm.Show();
}
else
{
MessageBox.Show("개발중이거나 존재하지 않는 화면입니다.", "오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
}
}
using BookManagementProgram.Repository;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BookManagementProgram.MainControls
{
public partial class MainPageForm : Form
{
BookRepository bookRepository = null;
public MainPageForm()
{
InitializeComponent();
bookRepository = new BookRepository();
DataTable dt = bookRepository.GetRandomBook();
if(dt != null)
{
pictureBox1.Load(dt.Rows[0]["book_saveImagePath"].ToString());
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
book_no_txt.Text = dt.Rows[0]["book_no"].ToString();
book_title_txt.Text = dt.Rows[0]["book_title"].ToString();
book_writer_txt.Text = dt.Rows[0]["book_writer"].ToString();
book_category_txt.Text = dt.Rows[0]["category_name"].ToString();
book_description_txt.Text = dt.Rows[0]["book_description"].ToString();
book_rentalcount_txt.Text = dt.Rows[0]["book_rentalcounts"].ToString();
}
}
}
}
USE [BookManagement]
GO
/****** Object: StoredProcedure [dbo].[BOOK_BOOK_S6] Script Date: 2021-08-25 오전 11:44:42 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <문민승>
-- Create date: <2021.08.24>
-- Description: <도서관리프로그램, 메인화면에 보여줄 랜덤한 책 정보>
-- =============================================
ALTER PROCEDURE [dbo].[BOOK_BOOK_S6]
AS
BEGIN
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
IF NOT EXISTS
(
SELECT TOP 1 book_no, book_title, book_writer, category_name, book_description, book_rentalcounts, book_saveImagePath
FROM Book_Book_Info I
INNER JOIN Book_Book_Category C
ON I.book_category = C.category_no
ORDER BY NEWID()
)
BEGIN
RAISERROR('존재하지 않는 데이터 입니다.',16,1)
RETURN
END
SELECT TOP 1 book_no, book_title, book_writer, category_name, book_description, book_rentalcounts, book_saveImagePath
FROM Book_Book_Info I
INNER JOIN Book_Book_Category C
ON I.book_category = C.category_no
ORDER BY NEWID();
END
using BookManagementProgram.Model;
using BookManagementProgram.Repository;
using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace BookManagementProgram.BookControls
{
public partial class BookAddForm : Form
{
BookRepository bookRepository = null;
public BookAddForm()
{
InitializeComponent();
bookRepository = new BookRepository();
this.StartPosition = FormStartPosition.CenterParent;
// 카테고리 콤보 박스
book_category_cmb.DataSource = new BindingSource(bookRepository.GetCategoryList(), null);
book_category_cmb.DisplayMember = "Key";
book_category_cmb.ValueMember = "Value";
// 위치 콤보 박스
book_location_cmb.DataSource = new BindingSource(bookRepository.GetBookLocationList(), null);
book_location_cmb.DisplayMember = "Key";
book_location_cmb.ValueMember = "Value";
}
// 취소 버튼
private void cancel_btn_Click(object sender, EventArgs e)
{
this.Close();
}
// 도서 등록
private void add_book_btn_Click(object sender, EventArgs e)
{
if(CheckInput())
{
Book book = CreateBookInstance();
if (bookRepository.AddBook(book))
{
MessageBox.Show("도서 등록에 성공하셨습니다.");
this.Close();
return;
}
MessageBox.Show("도서 등록에 실패하셨습니다.");
}
}
// Book 객체 생성
private Book CreateBookInstance()
{
Book book = new Book();
book.bookTitle = book_title_txt.Text;
book.bookWriter = book_writer_txt.Text;
book.bookCategory_no = (int)book_category_cmb.SelectedValue;
book.bookDescription = book_description_txt.Text;
book.bookPrice = Convert.ToInt32(book_price_txt.Text);
book.bookCompany = book_company_txt.Text;
book.bookDate = book_date_txt.Text;
book.bookPages = Convert.ToInt32(book_pages_txt.Text);
book.bookOriginalImagePath = book_image_txt.Text;
book.bookSavenameImaegPath = SaveBookImageFIle();
book.bookLocationNo = (int)book_location_cmb.SelectedValue;
book.bookRentalNo = 1;
return book;
}
// 텍스트박스 입력 확인
private bool CheckInput()
{
bool check = false;
if (string.IsNullOrEmpty(book_title_txt.Text))
{
MessageBox.Show("책 제목을 입력해주세요.");
book_title_txt.Focus();
}
else if (string.IsNullOrEmpty(book_writer_txt.Text))
{
MessageBox.Show("책 저자를 입력해주세요.");
book_writer_txt.Focus();
}
else if (string.IsNullOrEmpty(book_description_txt.Text))
{
MessageBox.Show("도서 설명을 입력해주세요.");
book_description_txt.Focus();
}
else if (string.IsNullOrEmpty(book_price_txt.Text) || CheckNumeric(book_price_txt.Text))
{
MessageBox.Show("가격을 입력해주세요.");
book_price_txt.Focus();
}
else if (string.IsNullOrEmpty(book_company_txt.Text))
{
MessageBox.Show("출판사를 입력해주세요.");
book_company_txt.Focus();
}
else if (string.IsNullOrEmpty(book_date_txt.Text) || CheckDatePattern(book_date_txt.Text))
{
MessageBox.Show("출판날짜를 입력해주세요.(2021.01.01)");
book_date_txt.Focus();
}
else if (string.IsNullOrEmpty(book_pages_txt.Text) || CheckNumeric(book_pages_txt.Text))
{
MessageBox.Show("총 페이지 수를 입력해주세요.");
book_pages_txt.Focus();
}
else if (string.IsNullOrEmpty(book_image_txt.Text))
{
MessageBox.Show("이미지를 선택해주세요.");
book_image_btn.Focus();
}
else
{
check = true;
}
return check;
}
// 출판일 확인 정규식
private bool CheckDatePattern(string date)
{
string pattern = @"^(1[0-9][0-9][0-9]|20\d{2})\.(0[0-9]|1[0-2])\.(0[1-9]|[1-2][0-9]|3[0-1])$";
if (!Regex.IsMatch(date, pattern))
{
return true;
}
else
{
return false;
}
}
// 숫자인지 확인
private bool CheckNumeric(string value)
{
int result = -1;
if(Int32.TryParse(value, out result))
{
if(result > 0)
{
return false;
}
}
return true;
}
// 책 이미지 불러오기 버튼
private void book_image_btn_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "이미지 불러오기";
openFileDialog.FileName = "";
openFileDialog.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";
book_image_txt.Text = string.Empty;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
book_image_txt.Text = openFileDialog.FileName;
pictureBox1.Image = new Bitmap(openFileDialog.FileName);
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
}
}
// 이미지 파일 저장
private string SaveBookImageFIle()
{
//string dir = @"C:\Users\jjh\Desktop\mms\BookManagerForm\BookImages";
string dir = $@"{System.Environment.CurrentDirectory}\Images";
DirectoryInfo di = new DirectoryInfo(dir);
if(di.Exists == false)
{
di.Create();
}
Image image = Image.FromFile(book_image_txt.Text);
bool bExist = true;
int fileCount = 0;
string fileName = string.Empty;
string dirMapPath = string.Empty;
if (image != null)
{
fileName = book_image_txt.Text.Split('\\').Last();
string strName = fileName.Substring(0, fileName.LastIndexOf("."));
string strExt = fileName.Substring(fileName.LastIndexOf("."));
while (bExist)
{
dirMapPath = dir;
string pathCombine = System.IO.Path.Combine(dirMapPath, fileName);
if (System.IO.File.Exists(pathCombine))
{
fileCount++;
fileName = $"{strName}({fileCount}){strExt}";
}
else
{
bExist = false;
}
}
image.Save($@"{dir}\{fileName}");
MessageBox.Show("이미지 저장이 되었습니다");
}
return $@"{dir}\{fileName}";
}
}
}
https://github.com/minseung-moon/CSharpStudyRepository/tree/main/BookManagementProgram