深入.NET和C#的小型汽車租賃系統的框架


前言:寫這個小型系統之前呢,我們應該要猜測可能要用到哪些知識點。

那么對於這個小型系統:主要用到了如下的知識:

 封裝,集合(ArrayList和HashTable)和泛型和非泛型集合(泛型:List<>和非泛型:Dictioanry<>),

構造函數,方法重載,多態(繼承,抽象:抽象類和抽象方法),怎么動態綁定數據到ListView上-----------

效果圖:

 

第一步.

創建幾個類

01工具類(vehicle)(父類,超類,基類),此類為抽象類

有方法重載,構造函數

該類主要負責提供車類的屬性

屬性:

  public string Color { get; set; }
public double DailyRent { get; set; }
public string LicenseNO { get; set; }
public string Name { get; set; }
public int RentDate { get; set; }
public string RentUser { get; set; }
public int YearsOfService { get; set; }

02:創建一個汽車類(Car)(子類)

構造函數,方法重載。

03:創建一個卡車 類(Truck)(子類)

構造函數,方法重載

屬性:

  public int Load

        {

            get { return load; }

            set { load = value; }

        }

04:創建一個工具(工廠)類(VehicleUtil)

第二步.

代碼實現思路:

01:工具,父類(vehicle)

代碼如下:

 public Vehicle()
{
}
public Vehicle(string color, double dailyrent, string licenseno, string name, int yearsofservice)
{
Color
= color;
DailyRent
= dailyrent;
LicenseNO
= licenseno;
Name
= name;
YearsOfService
= yearsofservice;
}
public abstract double CalcPrice();

02:汽車類(Car)

代碼如下:

public Car()
{
}
public Car(string licenseno, string name, string color, int yearsofservice, double dailyrent)
{
Color
= color;
DailyRent
= dailyrent;
LicenseNO
= licenseno;
Name
= name;
YearsOfService
= yearsofservice;
}
public override double CalcPrice()
{
double totalPrice = 0;
totalPrice
= RentDate * DailyRent;
return totalPrice;
}

03:卡車 類(Truck)

代碼如下:

  public Truck(string licenseno, string name, string color, int yearsofservice, double dailyrent, int load)
{
Color
= color;
DailyRent
= dailyrent;
LicenseNO
= licenseno;
Name
= name;
YearsOfService
= yearsofservice;
Load
= load;
}
public override double CalcPrice()
{
double totalPrice = 0;
totalPrice
= RentDate * DailyRent;
return totalPrice;
}

04:工具(工廠)類(VehicleUtil

代碼如下:

 public static Vehicle CreateVehicle(string licenseno, string name, string color, int yearsofservice, double dailyrent, int load, string type)
{
Vehicle vehicle
= null;
switch (type)
{
case "Car":
vehicle
= new Car(licenseno, name, color, yearsofservice, dailyrent);
break;
case "Truck":
vehicle
= new Truck(licenseno, name, color, yearsofservice, dailyrent, load);
break;
default:
break;
}
return vehicle;
}

第三步.

出租事件:(btnRent_Click(object sender, EventArgs e))

關鍵代碼如下:

 if (lvlist.SelectedItems.Count == 0)
{
MessageBox.Show(
"請選擇一行");
return;
}
if (txtUser.Text == "")
{
MessageBox.Show(
"請輸入客戶名字!");
return;
}
string key = lvlist.SelectedItems[0].Text;
Vehicle vehicle
= vehicles[key];
vehicles.Remove(key);
rentvehicles.Add(key, vehicle);
PrintVehicles(vehicles, lvlist);
MessageBox.Show(
"出租成功!!");
//PrintVehicles綁定數據到listview上
public void PrintVehicles(Dictionary<string, Vehicle> listprirnt, ListView lvinfo)
{
ListViewItem lv
=null;
lvinfo.Items.Clear();
foreach (Vehicle item in listprirnt.Values)
{
if (item is Car)
{
lv
= new ListViewItem(item.LicenseNO);
lv.SubItems.Add(item.Name);
lv.SubItems.Add(item.Color.ToString());
lv.SubItems.Add(item.YearsOfService.ToString());
lv.SubItems.Add(item.DailyRent.ToString());
lvinfo.Items.Add(lv);
}
else if(item is Truck)
{
lv
= new ListViewItem(item.LicenseNO);
lv.SubItems.Add(item.Name);
lv.SubItems.Add(item.Color.ToString());
lv.SubItems.Add(item.YearsOfService.ToString());
lv.SubItems.Add(item.DailyRent.ToString());
lv.SubItems.Add(((Truck)item).Load.ToString());
lvinfo.Items.Add(lv);
}

  第四步

還車.在btnGiveMoney_Click事件中寫   

  if (txtDayNum.Text == string.Empty)
{
MessageBox.Show(
"請輸入天數!");
return;
}
string addNum = lvlistGive.SelectedItems[0].Text;
Vehicle vechile
= rentvehicles[addNum];
rentvehicles.Remove(addNum);
vechile.RentDate
= Convert.ToInt32(txtDayNum.Text);
double money = 0;
//調用計算價格方法
money
= vechile.CalcPrice();
MessageBox.Show(
"你一共要福le" + money);
vehicles.Add(addNum, vechile);
//刷新
PrintVehicles(rentvehicles, lvlistGive);
MessageBox.Show(
"成功!!");

第五步.

添加車輛在 btnIncAR_Click事件中寫

if (txtWeight.Text == "" || txtUseTime.Text == "" || txtType.Text == "" || txtID.Text == "" || txtDayRentMoney.Text == "" || cboColor.Text == "")

{
MessageBox.Show(
"請您填寫完整!");
}
else
{
string licon = txtID.Text;
string color = cboColor.Text;
double mon = Convert.ToDouble(txtDayRentMoney.Text);
int tine = Convert.ToInt32(txtUseTime.Text);
string ty = txtType.Text;
if (radJiaoCar.Checked)
{
Car ca
= new Car(licon, ty, color, tine, mon);
vehicles.Add(licon, ca);
}
if (radKaCar.Checked)
{
int load = Convert.ToInt32(txtWeight.Text);
Truck tr
= new Truck(licon, ty, color, tine, mon, load);
vehicles.Add(licon, tr);
}
MessageBox.Show(
"添加成功!");
}

注:此文沒有代碼的注釋,是因為想在看代碼的任何情況下,

也能看懂代碼的含義。

 

 


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



C#汽車租賃系統 深入.NET 第十章 汽車租賃系統 C#汽車租賃系統 完整版 ACCP 7.0 C#項目 汽車租賃系統 汽車租賃系統 汽車租賃系統 汽車租賃系統 汽車租賃系統 汽車租賃系統 汽車租賃系統
 
粤ICP备14056181号  © 2014-2020 ITdaan.com