C # Implementation of simple car rental system _c# Tutorial

Source: Internet
Author: User

Recently learned the inheritance, polymorphism, collection, design patterns, there is a car rental system, to share with you:

Let's start by looking at the effect of our system.

1. To do a project, we first analyze the project

Based on our recent knowledge, we can see that inheritance, polymorphism, aggregation, design patterns, we can all use

We have the required class and simple model of the "simple factory" plant ready

Class Diagram:

01. Vehicle Class (parent class)

 Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;

namespace Car Rental system
{
 //vehicle class Parent public
 abstract class Vehicle
 {/
  /attributes
  //car brand public
  string Licenseno {get; set;}
  
  Car name public
  string name {get; set;}
  Color public
  string Color {get; set;}
  Use time public
  int Rentdate {get; set;}
  Use person public
  string Rentuser {get; set;}
  Daily rent public
  double dailyrent {get; set;}
  Return car date public
  int Returndate {get; set;}

  Public Vehicle () {}
  //Construct 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;
 
  }

  virtual method of calculating price public
  abstract Double getnum ();
  
 }


02. Sub Class automobile (inheriting vehicle class parent)

 Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;

namespace Car Rental system
{
 //Automotive class Subclass public class
 car:vehicle {public
 car
  () {}
  //construct
  public cars (String Licenseno, string name, string color, int rentdate, double dailyrent)
   : Base (Licenseno, name, color, rentdate , dailyrent) 
  {}
  //Overriding the method of calculating the price of the parent class public
  override Double Getnum ()
  {
   //day rent * rental number of days
   double result = this. Dailyrent * this. Returndate;
   return result
  }}
 }


03. Sub class truck class inherit vehicle class parent class

 Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;

namespace Car Rental system
{
 //Subclass public
 class Truck:vehicle {/
  /payload public
  int Load {get; set;}

  Public truck () {}

  //Construct Public
  Truck (string licenseno,string name,string color,int rentdate,double dailyrent, int load)
   : Base (licenseno,name,color,rentdate,dailyrent) 
  {this
   . Load = load;
  }

  Override the method for calculating the price of the parent class public
  override Double Getnum ()
   /day rent * rent number of days
   double result = this. Dailyrent * this. Returndate;
   return result
  }}
 }


04. Factory class of "Simple Factory"

Say this factory class, is in order to in the new car warehousing, can know it is a car or a truck, instantiate different objects, easy to use

 Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;

namespace Car Rental system
{//
 factory class public
 class Vehiclefactory
 {
  ///with parameter static method public
  static Vehicle Carteshow (String Liceseno, string name, string color, int rentdate, double dailyrent, int load,string type)
  {
   // Initializes the parent class object
   Vehicle Vehicle = null;
   Switch (type)
   {
    ///According to the value coming from, instantiate the corresponding object case
    "Truck":
     vehicle = new Truck (liceseno, name, color, rentdate, Dailyrent, Load);
     break;
    Case "sedan":
     vehicle = new Car (liceseno, name, color, rentdate, dailyrent);
     break;
   Returns the instantiated object return
   vehicle;
  
  
  }
 }


2. The rest is to implement the function of the main form

In fact, the core of car rental and return is the interaction between two sets.

New car warehousing is to use the "simple factory" design pattern for the corresponding add

There's a control we haven't seen before 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 Car Rental System {public partial class Frmmain:form {public Frmmain () {InitializeComponent ();
  //Save the collection of rental vehicles dictionary<string, vehicle> vehicles=new dictionary<string,vehicle> ();

  Save rented out of the collection dictionary<string, vehicle> rentvehicles=new dictionary<string,vehicle> ();  Method of dynamically loading 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);
   }//Prepare rental vehicle public void intitle () {Truck truck = new Truck ("Jing A111", "Audi", "Red", 3,240,10);

   Car car = new Car ("Beijing A222", "BMW", "Black", 3, 240); Vehicles. ADD (truck.
   Licenseno,truck); Vehicles. ADD (car.
   Licenseno, car);
   
  Load Data New (vehicles,lvlist);
  } private void Frmmain_load (object sender, EventArgs e) {intitle (); //Click on the event that the car is triggered private void Btn_zu_click (object sender, EventArgs e) {if (lvlist).
    selecteditems.count==0) {MessageBox.Show ("Please select a row!");
   Return } if (Txt_name. text== "") {Messagebox.shoW ("Please enter your name!");

   Return
   //execute car rental. Gets the value of the car brand string carnum = Lvlist. Selecteditems[0].
   Text;

   Vehicle ve= Vehicles[carnum]; Directly to get the information to be rented into the Rentvehicles collection rentvehicles.

   ADD (Carnum,ve); Deletes the original collection vehicles.

   Remove (Carnum);
   Reload new (vehicles,lvlist);

  MessageBox.Show ("Car rental Success");
   
  private void Button1_Click (object sender, EventArgs e) {//Load leased vehicle information New (Rentvehicles,lvlist_huan); } private void Btn_ji_click (object sender, EventArgs e) {if (txt_day).
    text== "") {MessageBox.Show ("Please enter Days");
   Return } if (Lvlist_huan.
    selecteditems.count==0) {MessageBox.Show ("Please select one row");
   Return //Get the value of the car brand string carnum1 = Lvlist_huan. Selecteditems[0].
   Text;

  
   Vehicle ve = rentvehicles[carnum1]; Get the number of days to rent int num = Convert.ToInt32 (txt_day.
   Text); Ve.
   Returndate = num; Double Money=ve.
   Getnum (); DialogResult result= MessageBox.Show ("You want to pay" +money+ "Yuan", "hint", messageboxbuttons.okcancel,messageboxicon.question); if (Result==dialogresult.ok) {//directly put the information to be received into the vehicles set vehicles.

    Add (Carnum1, VE); Deletes the original collection rentvehicles.

    Remove (CARNUM1);
    Reload New (Rentvehicles, Lvlist_huan);
   MessageBox.Show ("Return car Success");
  }///refresh button private void Btn_new_click (object sender, EventArgs e) {//Reload new (vehicles, lvlist);
   //Determine if the correct way to fill out the 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; //Inbound button click event private void Btn_ruku_click (object sender, EventArgs e) {if (good ())//To judge whether the fill is correct {foreach (s Tring item in vehicles. Keys) {if (txt_id). Text==item) {MessageBox.Show ("this license plate is already in stock, please confirm!")
      ");
     Return
    }///use "Simple Factory" 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); }//Add collection vehicles. ADD (txt_id.
    Text, ve);

    MessageBox.Show ("warehousing success"); Empty the value option you want to fill in txt_id.
    Text= ""; Txt_xing.
    Text= ""; Cmb_color.
    Text= ""; Txt_time.
    Text= ""; Txt_money.
    Text= ""; Txt_zhong. 
   Text = ""; else {MessageBox.Show ("Please complete the information!")
   "); }///select different button 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;

 }
  }
 }
}

Let's sort out the charm code:

1. The interface function of renting car

01. Car Rental Button

Click on a car rental trigger event
  private void Btn_zu_click (object sender, EventArgs e)
  {
   //ensures that a row of
   if (lvlist) is selected. selecteditems.count==0)
   
   {
    MessageBox.Show ("Please select a row!");
    return;
   }
   Make sure someone rents a car
   if (txt_name. text== "")
   {
    MessageBox.Show ("Please enter your name!");
    return;

   }
   Execute a car rental.
   Gets the value of the car brand
   string carnum = Lvlist. Selecteditems[0]. Text;
   According to the license plate number to obtain this vehicle object
   Vehicle ve= vehicles[carnum];

   Directly to get the information to be rented into the Rentvehicles collection

   rentvehicles. ADD (carnum,ve);

   Deletes the original collection
   vehicles. Remove (carnum);

   Reload
   New (vehicles,lvlist);
   MessageBox.Show ("Car rental Success");

02. Refresh Button

Refresh button
  private void Btn_new_click (object sender, EventArgs e)
  {
   //Reload
   new (vehicles, lvlist);
  }

The refresh here defines a method, that is, the method of dynamically loading ListView (NUW method)

This method has two parameters, the first parameter incoming vehicle type collection object, the second incoming ListView name

The role is to use this method to refresh when renting a car and return it, wouldn't it be wonderful!

 Dynamically loading ListView method public void New (dictionary<string,vehicle> list,listview lvlist) {//Initialize ListViewItem object
    ListViewItem listview = null; Clears the ListView to avoid conflicting values lvlist.
   Items.clear (); foreach (Vehicle item in list.)
     Values) {//Judgment assignment 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 ()); }//Association lvlist.
   Items.Add (listview);
 }
   
  }

2. Return the car interface function

01. Select the Checkout button

 private void Btn_ji_click (object sender, EventArgs e) {//Ensure that the number of days is entered if (txt_d Ay.
    text== "") {MessageBox.Show ("Please enter Days");
   Return //Ensure that a row of if (Lvlist_huan) is selected.
    selecteditems.count==0) {MessageBox.Show ("Please select one row");
   Return //Get the value of the car brand string carnum1 = Lvlist_huan. Selecteditems[0].
    Text;
  
   According to the license plate number to obtain the corresponding vehicle object Vehicle ve = Rentvehicles[carnum1]; Get the number of days to rent int num = Convert.ToInt32 (txt_day.
   Text); Assign a value to the attribute using the number of days ve.
   Returndate = num; Call the calculation method (polymorphic application) Double money=ve.
   Getnum ();
   DialogResult result= MessageBox.Show ("You have to pay" +money+ "Yuan", "hint", messageboxbuttons.okcancel,messageboxicon.question); if (Result==dialogresult.ok) {//directly put the information to be received into the vehicles set vehicles.

    Add (Carnum1, VE); Deletes the original collection rentvehicles.

    Remove (CARNUM1);
    Reload New (Rentvehicles, Lvlist_huan);
   MessageBox.Show ("Return car Success"); }
   

  }

02. Refresh button (call the method written when renting a car)

private void Button1_Click (object sender, EventArgs e)
  {
   //Load leased vehicle information
   New (Rentvehicles,lvlist_huan);
   
  }

3. New Car Storage interface function

01. Storage button

 Inbound button click event private void Btn_ruku_click (object sender, EventArgs e) {if (good ())//To determine whether the correct fill in {//car brand is unique and cannot be Repeat to add the existing license plate number foreach (string item in vehicles. Keys) {if (txt_id). Text==item) {MessageBox.Show ("this license plate is already in stock, please confirm!")
      ");
     Return
    }///using "Simple Factory", make corresponding add 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); }//Add collection vehicles. ADD (txt_id.
    Text, ve);

    MessageBox.Show ("warehousing success"); Empty the value option you want to fill in txt_id.
    Text= ""; Txt_xing.
    Text= ""; Cmb_color.
    Text= ""; Txt_time.
    Text= ""; Txt_money.
    Text= ""; Txt_zhonG.text = ""; else {MessageBox.Show ("Please complete the information!")
   ");
 }
   

  }

The above is the entire content of this article, I hope to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.