【C#】汽车租赁系统设计与实现

2024-06-18 12:37

本文主要是介绍【C#】汽车租赁系统设计与实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目的:

设计一个简单的汽车租赁系统,包含以下功能:

  1. 添加车辆:用户可以添加新的车辆到系统中,包括车辆的品牌、型号、车牌号、日租金等信息。
  2. 查找车辆:用户可以通过车牌号或者品牌来查找车辆,并显示该车辆的租赁信息。
  3. 租车:用户可以根据车辆的品牌和租赁天数来租车,系统会计算租车的费用。
  4. 归还车辆:用户可以归还已租赁的车辆,系统会更新车辆的租赁状态并计算租金。

分析:

  • 使用面向对象的方式实现,包括至少两个类(例如 Car 和 CarRentalSystem)。
  • 使用合适的数据结构(例如列表)来存储车辆信息和租赁记录。
  • 提供命令行界面,不需要图形用户界面。

实现:

car类:

using System;public class Car
{public string Brand { get; set; }public string Model { get; set; }public string LicensePlate { get; set; }public decimal DailyRent { get; set; }public bool IsRented { get; set; }public Car(string brand, string model, string licensePlate, decimal dailyRent){Brand = brand;Model = model;LicensePlate = licensePlate;DailyRent = dailyRent;IsRented = false;}public override string ToString(){return $"Brand: {Brand}, Model: {Model}, License Plate: {LicensePlate}, Daily Rent: {DailyRent:C}, Status: {(IsRented ? "Rented" : "Available")}";}
}

CarRentalSystem 类:

using System;
using System.Collections.Generic;
using System.Linq;public class CarRentalSystem
{private List<Car> cars;public CarRentalSystem(){cars = new List<Car>();}public void AddCar(string brand, string model, string licensePlate, decimal dailyRent){cars.Add(new Car(brand, model, licensePlate, dailyRent));Console.WriteLine("Car added successfully.");}public void FindCar(string searchCriteria){var foundCars = cars.Where(car => car.LicensePlate.Contains(searchCriteria) || car.Brand.Contains(searchCriteria)).ToList();if (foundCars.Count > 0){foreach (var car in foundCars){Console.WriteLine(car);}}else{Console.WriteLine("No cars found.");}}public void RentCar(string brand, int days){var availableCar = cars.FirstOrDefault(car => car.Brand == brand && !car.IsRented);if (availableCar != null){availableCar.IsRented = true;decimal totalCost = availableCar.DailyRent * days;Console.WriteLine($"Car rented successfully. Total cost: {totalCost:C}");}else{Console.WriteLine("No available cars found for the specified brand.");}}public void ReturnCar(string licensePlate){var rentedCar = cars.FirstOrDefault(car => car.LicensePlate == licensePlate && car.IsRented);if (rentedCar != null){rentedCar.IsRented = false;Console.WriteLine("Car returned successfully.");}else{Console.WriteLine("Car not found or already returned.");}}
}

主程序:

using System;public class Program
{public static void Main(){CarRentalSystem carRentalSystem = new CarRentalSystem();while (true){Console.WriteLine("\nCar Rental System");Console.WriteLine("1. Add Car");Console.WriteLine("2. Find Car");Console.WriteLine("3. Rent Car");Console.WriteLine("4. Return Car");Console.WriteLine("5. Exit");Console.Write("Enter your choice: ");string choice = Console.ReadLine();switch (choice){case "1":Console.Write("Enter brand: ");string brand = Console.ReadLine();Console.Write("Enter model: ");string model = Console.ReadLine();Console.Write("Enter license plate: ");string licensePlate = Console.ReadLine();Console.Write("Enter daily rent: ");decimal dailyRent = decimal.Parse(Console.ReadLine());carRentalSystem.AddCar(brand, model, licensePlate, dailyRent);break;case "2":Console.Write("Enter license plate or brand to search: ");string searchCriteria = Console.ReadLine();carRentalSystem.FindCar(searchCriteria);break;case "3":Console.Write("Enter brand to rent: ");string rentBrand = Console.ReadLine();Console.Write("Enter number of days: ");int days = int.Parse(Console.ReadLine());carRentalSystem.RentCar(rentBrand, days);break;case "4":Console.Write("Enter license plate to return: ");string returnLicensePlate = Console.ReadLine();carRentalSystem.ReturnCar(returnLicensePlate);break;case "5":return;default:Console.WriteLine("Invalid choice. Please try again.");break;}}}
}

完整代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace windows期末text1
{public class Car{public string Brand { get; set; }public string Model { get; set; }public string LicensePlate { get; set; }public decimal DailyRent { get; set; }public bool IsRented { get; set; }public Car(string brand, string model, string licensePlate, decimal dailyRent){Brand = brand;Model = model;LicensePlate = licensePlate;DailyRent = dailyRent;IsRented = false;}public override string ToString(){return $"Brand: {Brand}, Model: {Model}, License Plate: {LicensePlate}, Daily Rent: {DailyRent:C}, Status: {(IsRented ? "Rented" : "Available")}";}}public class CarRentalSystem{private List<Car> cars;public CarRentalSystem(){cars = new List<Car>();}public void AddCar(string brand, string model, string licensePlate, decimal dailyRent){cars.Add(new Car(brand, model, licensePlate, dailyRent));Console.WriteLine("Car added successfully.");}public void FindCar(string searchCriteria){var foundCars = cars.Where(car => car.LicensePlate.Contains(searchCriteria) || car.Brand.Contains(searchCriteria)).ToList();if (foundCars.Count > 0){foreach (var car in foundCars){Console.WriteLine(car);}}else{Console.WriteLine("No cars found.");}}public void RentCar(string brand, int days){var availableCar = cars.FirstOrDefault(car => car.Brand == brand && !car.IsRented);if (availableCar != null){availableCar.IsRented = true;decimal totalCost = availableCar.DailyRent * days;Console.WriteLine($"Car rented successfully. Total cost: {totalCost:C}");}else{Console.WriteLine("No available cars found for the specified brand.");}}public void ReturnCar(string licensePlate){var rentedCar = cars.FirstOrDefault(car => car.LicensePlate == licensePlate && car.IsRented);if (rentedCar != null){rentedCar.IsRented = false;Console.WriteLine("Car returned successfully.");}else{Console.WriteLine("Car not found or already returned.");}}}internal class Program{static void Main(string[] args){CarRentalSystem carRentalSystem = new CarRentalSystem();while (true){Console.WriteLine("\nCar Rental System");Console.WriteLine("1. Add Car");Console.WriteLine("2. Find Car");Console.WriteLine("3. Rent Car");Console.WriteLine("4. Return Car");Console.WriteLine("5. Exit");Console.Write("Enter your choice: ");string choice = Console.ReadLine();switch (choice){case "1":Console.Write("Enter brand: ");string brand = Console.ReadLine();Console.Write("Enter model: ");string model = Console.ReadLine();Console.Write("Enter license plate: ");string licensePlate = Console.ReadLine();Console.Write("Enter daily rent: ");decimal dailyRent = decimal.Parse(Console.ReadLine());carRentalSystem.AddCar(brand, model, licensePlate, dailyRent);break;case "2":Console.Write("Enter license plate or brand to search: ");string searchCriteria = Console.ReadLine();carRentalSystem.FindCar(searchCriteria);break;case "3":Console.Write("Enter brand to rent: ");string rentBrand = Console.ReadLine();Console.Write("Enter number of days: ");int days = int.Parse(Console.ReadLine());carRentalSystem.RentCar(rentBrand, days);break;case "4":Console.Write("Enter license plate to return: ");string returnLicensePlate = Console.ReadLine();carRentalSystem.ReturnCar(returnLicensePlate);break;case "5":return;default:Console.WriteLine("Invalid choice. Please try again.");break;}}}}
}

实现结果:

以上就是完整的代码及实现。

实验小结

通过此次实验,掌握了以下知识和技能:

  • 面向对象编程的基本概念,包括类和对象的使用。
  • 使用列表存储和管理数据。
  • 实现类的方法以完成特定功能。
  • 设计和实现简单的命令行用户界面。
  • 编写和调试C#程序。

重难点分析

重点
  1. 面向对象编程概念

    • 理解类和对象的概念,以及如何定义类的属性和方法。
    • 掌握构造函数的使用,初始化对象时设置初始值。
  2. 列表数据结构

    • 使用列表存储多个对象。
    • 熟练掌握列表的添加、查找和遍历操作。
  3. 方法的实现

    • 编写方法实现特定功能,例如添加车辆、查找车辆、租车和归还车辆。
    • 在方法中处理数据并进行必要的计算,例如租车费用的计算。
难点
  1. 查找和筛选

    • 如何高效地在列表中查找符合特定条件的对象。
    • 使用 LINQ 提高查找和筛选操作的效率和代码可读性。
  2. 状态管理

    • 正确管理车辆的租赁状态,避免出现状态不一致的情况。
    • 归还车辆时,确保车辆状态更新正确。
  3. 用户输入处理

    • 处理用户输入的有效性,例如租赁天数应为正整数,日租金应为正数。
    • 提供适当的错误提示和输入提示,提高用户体验。

实验报告

实验名称

汽车租赁系统设计与实现

实验目的

通过设计和实现一个简单的汽车租赁系统,掌握C#面向对象编程的基本知识,熟悉类和对象的使用、列表数据结构的操作,以及简单的命令行界面设计。

实验内容
  1. 设计并实现 Car 类,用于表示车辆的基本信息。
  2. 设计并实现 CarRentalSystem 类,用于管理车辆的添加、查找、租赁和归还功能。
  3. 实现一个简单的命令行界面,让用户可以与系统进行交互。
实验环境
  • 操作系统:Windows 11
  • 编程语言:C#
  • 开发工具:Visual Studio 2022
实验步骤
  1. 设计 Car

    • 创建一个新类 Car,包含品牌、型号、车牌号、日租金和租赁状态等属性。
    • 实现 Car 类的构造函数,用于初始化车辆信息。
    • 重写 ToString 方法,用于输出车辆信息。
  2. 设计 CarRentalSystem

    • 创建一个新类 CarRentalSystem,使用列表存储车辆信息。
    • 实现添加车辆、查找车辆、租车和归还车辆的方法。
    • 在添加车辆方法中,将新车辆添加到列表中。
    • 在查找车辆方法中,根据车牌号或品牌查找并输出车辆信息。
    • 在租车方法中,根据品牌查找可用车辆并计算租赁费用。
    • 在归还车辆方法中,根据车牌号查找已租赁的车辆并更新其租赁状态。
  3. 实现命令行界面

    • 编写主程序,通过命令行界面与用户进行交互。
    • 提供菜单选项,让用户可以选择不同的操作。
    • 根据用户的选择调用相应的方法,完成相应的功能。

这篇关于【C#】汽车租赁系统设计与实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1072014

相关文章

C#中读取XML文件的四种常用方法

《C#中读取XML文件的四种常用方法》Xml是Internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具,下面我们就来看看C#中读取XML文件的方法都有哪些吧... 目录XML简介格式C#读取XML文件方法使用XmlDocument使用XmlTextReader/XmlTextWr

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

如何通过Python实现一个消息队列

《如何通过Python实现一个消息队列》这篇文章主要为大家详细介绍了如何通过Python实现一个简单的消息队列,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录如何通过 python 实现消息队列如何把 http 请求放在队列中执行1. 使用 queue.Queue 和 reque

Python如何实现PDF隐私信息检测

《Python如何实现PDF隐私信息检测》随着越来越多的个人信息以电子形式存储和传输,确保这些信息的安全至关重要,本文将介绍如何使用Python检测PDF文件中的隐私信息,需要的可以参考下... 目录项目背景技术栈代码解析功能说明运行结php果在当今,数据隐私保护变得尤为重要。随着越来越多的个人信息以电子形

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import

前端原生js实现拖拽排课效果实例

《前端原生js实现拖拽排课效果实例》:本文主要介绍如何实现一个简单的课程表拖拽功能,通过HTML、CSS和JavaScript的配合,我们实现了课程项的拖拽、放置和显示功能,文中通过实例代码介绍的... 目录1. 效果展示2. 效果分析2.1 关键点2.2 实现方法3. 代码实现3.1 html部分3.2

Java深度学习库DJL实现Python的NumPy方式

《Java深度学习库DJL实现Python的NumPy方式》本文介绍了DJL库的背景和基本功能,包括NDArray的创建、数学运算、数据获取和设置等,同时,还展示了如何使用NDArray进行数据预处理... 目录1 NDArray 的背景介绍1.1 架构2 JavaDJL使用2.1 安装DJL2.2 基本操

最长公共子序列问题的深度分析与Java实现方式

《最长公共子序列问题的深度分析与Java实现方式》本文详细介绍了最长公共子序列(LCS)问题,包括其概念、暴力解法、动态规划解法,并提供了Java代码实现,暴力解法虽然简单,但在大数据处理中效率较低,... 目录最长公共子序列问题概述问题理解与示例分析暴力解法思路与示例代码动态规划解法DP 表的构建与意义动