最近學習了繼承,多態,集合,設計模式,有一個汽車租憑系統,給大家分享一下:
我們首先來看看我們這個系統的效果
1.做一個項目,我們首先對項目進行分析
根據我們最近學的知識,我們可以看出繼承,多態,集合,設計模式,我們都能用到
我們把所需要的類和簡單模式中的“簡單工廠”的工廠准備好
類圖:
01.車輛類(父類)
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
汽車租賃系統
{
// 車輛類 父類
public
abstract
class
Vehicle
{
//屬性
//車牌號
public
string
LicenseNo {
get
;
set
; }
//車名
public
string
Name {
get
;
set
; }
//顏色
public
string
Color {
get
;
set
; }
//使用時間
public
int
RentDate {
get
;
set
; }
//使用人
public
string
RentUser {
get
;
set
; }
//日租金
public
double
DailyRent {
get
;
set
; }
//還車日期
public
int
ReturnDate {
get
;
set
; }
public
Vehicle() { }
//構造
public
Vehicle(
string
liceseno,
string
name,
string
color,
int
rentdate,
double
dailyrent)
{
this
.Color = color;
this
.DailyRent = dailyrent;
this
.LicenseNo = liceseno;
this
.Name = name;
this
.RentDate = rentdate;
}
//計算價格的虛方法
public
abstract
double
GetNum();
}
}
02.子類汽車類 (繼承 車輛類 父類)
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
汽車租憑系統
{
//汽車類 子類
public
class
Car:Vehicle
{
public
Car() { }
//構造
public
Car(
string
licenseno,
string
name,
string
color,
int
rentdate,
double
dailyrent)
:
base
(licenseno, name, color, rentdate, dailyrent)
{ }
//重寫父類計算價格的方法
public
override
double
GetNum()
{
//日租金*租的天數
double
result =
this
.DailyRent *
this
.ReturnDate;
return
result;
}
}
}
03.子類卡車類 繼承 車輛類 父類
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
汽車租憑系統
{
//子類
public
class
Truck:Vehicle
{
//載重
public
int
Load {
get
;
set
; }
public
Truck() { }
//構造
public
Truck(
string
licenseno,
string
name,
string
color,
int
rentdate,
double
dailyrent,
int
load )
:
base
(licenseno,name,color,rentdate,dailyrent )
{
this
.Load = load;
}
//重寫父類計算價格的方法
public
override
double
GetNum()
{
//日租金*租的天數
double
result =
this
.DailyRent *
this
.ReturnDate;
return
result;
}
}
}
04.“簡單工廠”的工廠類
說這個工廠類,就是為了在新車入庫的時候,可以知道它是轎車還是卡車,實例化不同的對象,方便使用
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
汽車租賃系統
{
//工廠類
public
class
VehicleFactory
{
//帶參靜態方法
public
static
Vehicle Carteshow(
string
liceseno,
string
name,
string
color,
int
rentdate,
double
dailyrent,
int
Load,
string
type)
{
//初始化父類對象
Vehicle vehicle =
null
;
switch
(type)
{
//根據傳來的值,實例化對應的對象
case
"卡車"
:
vehicle =
new
Truck(liceseno, name, color, rentdate, dailyrent, Load);
break
;
case
"轎車"
:
vehicle =
new
Car(liceseno, name, color, rentdate, dailyrent);
break
;
}
//返回實例化對象
return
vehicle;
}
}
}
2. 剩下的就是對主窗體的功能進行實現
其實租車和還車的核心就是兩個集合之間的交互
新車入庫就是使用“簡單工廠”的設計模式進行對應添加
其中有個我們以前沒見過的控件TabControl
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
汽車租賃系統
{
public
partial
class
FrmMain : Form
{
public
FrmMain()
{
InitializeComponent();
}
//保存可租車輛的集合
Dictionary<
string
, Vehicle> vehicles=
new
Dictionary<
string
,Vehicle>();
//保存租出車的集合
Dictionary<
string
, Vehicle> rentvehicles=
new
Dictionary<
string
,Vehicle>();
//動態加載listvIEw的方法
public
void
New(Dictionary<
string
,Vehicle> list,ListVIEw lvlist)
{
ListViewItem listvIEw =
null
;
lvlist.Items.Clear();
foreach
(Vehicle item
in
list.Values)
{
if
(item
is
Car)
{
listvIEw =
new
ListVIEwItem();
listvIEw.Text = item.LicenseNo;
listvIEw.SubItems.Add(item.Name);
listvIEw.SubItems.Add(item.Color);
listvIEw.SubItems.Add(item.RentDate.ToString());
listvIEw.SubItems.Add(item.DailyRent.ToString());
}
else
if
(item
is
Truck)
{
listvIEw =
new
ListVIEwItem();
listvIEw.Text = item.LicenseNo;
listvIEw.SubItems.Add(item.Name);
listvIEw.SubItems.Add(item.Color);
listvIEw.SubItems.Add(item.RentDate.ToString());
listvIEw.SubItems.Add(item.DailyRent.ToString());
listvIEw.SubItems.Add(((Truck)item).Load.ToString());
}
lvlist.Items.Add(listvIEw);
}
}
//准備可租車輛
public
void
Intitle()
{
Truck truck =
new
Truck(
"京A111"
,
"奧迪"
,
"紅色"
,3,240,10);
Car car =
new
Car(
"京A222"
,
"寶馬"
,
"黑色"
, 3, 240);
vehicles.Add(truck.LicenseNo,truck);
vehicles.Add(car.LicenseNo, car);
//加載數據
New(vehicles,lvlist);
}
private
void
FrmMain_Load(
object
sender, EventArgs e)
{
Intitle();
}
//點擊租車觸發的事件
private
void
btn_zu_Click(
object
sender, EventArgs e)
{
if
(lvlist.SelectedItems.Count==0)
{
MessageBox.Show(
"請選中一行!"
);
return
;
}
if
(txt_name.Text==
""
)
{
MessageBox.Show(
"請輸入姓名!"
);
return
;
}
//執行租車.
//獲取車牌號的值
string
carnum = lvlist.SelectedItems[0].Text;
Vehicle ve= vehicles[carnum];
//直接把獲得要租的信息放入rentvehicles集合
rentvehicles.Add(carnum,ve);
//刪除原來的集合
vehicles.Remove(carnum);
//重新加載
New(vehicles,lvlist);
MessageBox.Show(
"租車成功"
);
}
private
void
button1_Click(
object
sender, EventArgs e)
{
//加載已出租車輛信息
New(rentvehicles,lvlist_huan);
}
private
void
btn_ji_Click(
object
sender, EventArgs e)
{
if
(txt_day.Text==
""
)
{
MessageBox.Show(
"請輸入天數"
);
return
;
}
if
(lvlist_huan.SelectedItems.Count==0)
{
MessageBox.Show(
"請選擇一行"
);
return
;
}
//獲取車牌號的值
string
carnum1 = lvlist_huan.SelectedItems[0].Text;
Vehicle ve = rentvehicles[carnum1];
//獲取租的天數
int
num = Convert.ToInt32(txt_day.Text);
ve.ReturnDate = num;
double
money=ve.GetNum();
DialogResult result= MessageBox.Show(
"你要支付"
+money+
"元"
,
"提示"
,MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
if
(result==DialogResult.OK)
{
//直接把獲得要還的信息放入vehicles集合
vehicles.Add(carnum1, ve);
//刪除原來的集合
rentvehicles.Remove(carnum1);
//重新加載
New(rentvehicles, lvlist_huan);
MessageBox.Show(
"還車成功"
);
}
}
//刷新按鈕
private
void
btn_new_Click(
object
sender, EventArgs e)
{
//重新加載
New(vehicles, lvlist);
}
//判斷填寫是否正確的方法
public
bool
Good()
{
bool
flag =
true
;
if
(txt_id.Text==
""
||txt_xing.Text==
""
||cmb_color.Text==
""
||txt_time.Text==
""
||txt_money.Text==
""
||txt_zhong.Text==
""
|| rdb_jiao.Text==
""
)
{
flag =
false
;
}
return
flag;
}
//入庫按鈕點擊事件
private
void
btn_ruku_Click(
object
sender, EventArgs e)
{
if
(Good())
//判斷填寫是否正確
{
foreach
(
string
item
in
vehicles.Keys)
{
if
(txt_id.Text==item)
{
MessageBox.Show(
"此車牌已經有庫存了,請你確認!"
);
return
;
}
}
//使用"簡單工廠"
Vehicle ve =
null
;
if
(rdb_jiao.Checked ==
true
)
{
ve = VehicleFactory.Carteshow(txt_id.Text, txt_xing.Text, cmb_color.Text,Convert.ToInt32(txt_time.Text), Convert.ToDouble(txt_money.Text), Convert.ToInt32(txt_zhong.Text), rdb_jiao.Text);
}
else
{
ve = VehicleFactory.Carteshow(txt_id.Text, txt_xing.Text, cmb_color.Text, Convert.ToInt32(txt_time.Text), Convert.ToDouble(txt_money.Text), Convert.ToInt32(txt_zhong.Text), rdb_ka.Text);
}
//添加集合
vehicles.Add(txt_id.Text, ve);
MessageBox.Show(
"入庫成功"
);
//清空所要填的值選項
txt_id.Text=
""
;
txt_xing.Text=
""
;
cmb_color.Text=
""
;
txt_time.Text=
""
;
txt_money.Text=
""
;
txt_zhong.Text =
""
;
}
else
{
MessageBox.Show(
"請完善信息的填寫!"
);
}
}
//選擇不同的按鈕
private
void
rdb_jiao_CheckedChanged(
object
sender, EventArgs e)
{
if
(rdb_jiao.Checked==
true
)
{
lab_zhong.ForeColor = Color.Red;
txt_zhong.Enabled =
false
;
}
else
{
lab_zhong.ForeColor = Color.Black;
txt_zhong.Enabled =
true
;
}
}
}
}
我們來分類看看其中的魅力代碼:
1.租車的界面功能
01.租車按鈕
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33//點擊租車觸發的事件
private
void
btn_zu_Click(
object
sender, EventArgs e)
{
//確保選中一行
if
(lvlist.SelectedItems.Count==0)
{
MessageBox.Show(
"請選中一行!"
);
return
;
}
//確保有人租車
if
(txt_name.Text==
""
)
{
MessageBox.Show(
"請輸入姓名!"
);
return
;
}
//執行租車.
//獲取車牌號的值
string
carnum = lvlist.SelectedItems[0].Text;
//根據車牌號獲得此車對象
Vehicle ve= vehicles[carnum];
//直接把獲得要租的信息放入rentvehicles集合
rentvehicles.Add(carnum,ve);
//刪除原來的集合
vehicles.Remove(carnum);
//重新加載
New(vehicles,lvlist);
MessageBox.Show(
"租車成功"
);
02.刷新按鈕
? 1 2 3 4 5 6//刷新按鈕
private
void
btn_new_Click(
object
sender, EventArgs e)
{
//重新加載
New(vehicles, lvlist);
}
這裡的刷新定義了一個方法,也就是動態加載ListVIEw的方法(Nuw方法)
這個方法有兩個參數,第一個參數傳入車輛類型集合對象,第二個傳入ListvIEw的名字
這樣的作用就是在租車和還車時都能使用此方法進行刷新,豈不妙哉!
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34//動態加載listvIEw的方法
public
void
New(Dictionary<
string
,Vehicle> list,ListVIEw lvlist)
{
//初始化LIstvIEwItem對象
ListViewItem listvIEw =
null
;
//清除ListvIEw,以免有沖突的值
lvlist.Items.Clear();
foreach
(Vehicle item
in
list.Values)
{
//判斷賦值
if
(item
is
Car)
{
listvIEw =
new
ListVIEwItem();
listvIEw.Text = item.LicenseNo;
listvIEw.SubItems.Add(item.Name);
listvIEw.SubItems.Add(item.Color);
listvIEw.SubItems.Add(item.RentDate.ToString());
listvIEw.SubItems.Add(item.DailyRent.ToString());
}
else
if
(item
is
Truck)
{
listvIEw =
new
ListVIEwItem();
listvIEw.Text = item.LicenseNo;
listvIEw.SubItems.Add(item.Name);
listvIEw.SubItems.Add(item.Color);
listvIEw.SubItems.Add(item.RentDate.ToString());
listvIEw.SubItems.Add(item.DailyRent.ToString());
listvIEw.SubItems.Add(((Truck)item).Load.ToString());
}
//關聯
lvlist.Items.Add(listvIEw);
}
}
2.還車的界面功能
01.選擇結算按鈕
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41private
void
btn_ji_Click(
object
sender, EventArgs e)
{
//確保 是否輸入天數
if
(txt_day.Text==
""
)
{
MessageBox.Show(
"請輸入天數"
);
return
;
}
//確保是否選中一行
if
(lvlist_huan.SelectedItems.Count==0)
{
MessageBox.Show(
"請選擇一行"
);
return
;
}
//獲取車牌號的值
string
carnum1 = lvlist_huan.SelectedItems[0].Text;
//根據車牌號獲得對應的車輛對象
Vehicle ve = rentvehicles[carnum1];
//獲取租的天數
int
num = Convert.ToInt32(txt_day.Text);
//給屬性使用天數賦值
ve.ReturnDate = num;
//調用計算方法(多態的應用)
double
money=ve.GetNum();
DialogResult result= MessageBox.Show(
"你要支付"
+money+
"元"
,
"提示"
,MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
if
(result==DialogResult.OK)
{
//直接把獲得要還的信息放入vehicles集合
vehicles.Add(carnum1, ve);
//刪除原來的集合
rentvehicles.Remove(carnum1);
//重新加載
New(rentvehicles, lvlist_huan);
MessageBox.Show(
"還車成功"
);
}
}
02.刷新按鈕(調用租車時寫的方法)
? 1 2 3 4 5 6private
void
button1_Click(
object
sender, EventArgs e)
{
//加載已出租車輛信息
New(rentvehicles,lvlist_huan);
}
3.新車入庫界面功能
01.入庫按鈕
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46//入庫按鈕點擊事件
private
void
btn_ruku_Click(
object
sender, EventArgs e)
{
if
(Good())
//判斷填寫是否正確
{
//車牌號是唯一的,不能重復添加已有的車牌號
foreach
(
string
item
in
vehicles.Keys)
{
if
(txt_id.Text==item)
{
MessageBox.Show(
"此車牌已經有庫存了,請你確認!"
);
return
;
}
}
//使用"簡單工廠",進行對應添加
Vehicle ve =
null
;
if
(rdb_jiao.Checked ==
true
)
{
ve = VehicleFactory.Carteshow(txt_id.Text, txt_xing.Text, cmb_color.Text,Convert.ToInt32(txt_time.Text), Convert.ToDouble(txt_money.Text), Convert.ToInt32(txt_zhong.Text), rdb_jiao.Text);
}
else
{
ve = VehicleFactory.Carteshow(txt_id.Text, txt_xing.Text, cmb_color.Text, Convert.ToInt32(txt_time.Text), Convert.ToDouble(txt_money.Text), Convert.ToInt32(txt_zhong.Text), rdb_ka.Text);
}
//添加集合
vehicles.Add(txt_id.Text, ve);
MessageBox.Show(
"入庫成功"
);
//清空所要填的值選項
txt_id.Text=
""
;
txt_xing.Text=
""
;
cmb_color.Text=
""
;
txt_time.Text=
""
;
txt_money.Text=
""
;
txt_zhong.Text =
""
;
}
else
{
MessageBox.Show(
"請完善信息的填寫!"
);
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助。