Home > Java > javaTutorial > Detailed process of implementing car rental system in Java

Detailed process of implementing car rental system in Java

Y2J
Release: 2017-05-08 16:00:15
Original
3364 people have browsed it

This article mainly introduces you to the steps of using Java to implement a Dada car rental system. The article gives detailed implementation ideas and sample codes, and provides complete source code at the end for everyone to learn and download. If needed Friends can refer to it, let’s take a look below.

This article introduces the use of Java to write a console version of the "Dada Car Rental System". I won't say much below, let's take a look at the detailed implementation method.

Achieve the goal

Write a console version of "Dada Car Rental System" in java

Realize function

## 1. Display all available rental vehicles


2. Select the car model and rental quantity


3. Display Car rental list, including: total amount, total cargo capacity and its type, total passenger capacity and its type

Three major analyzes

Data model analysis

Business model analysis

Display and process analysis

##Achievement effect

Car rental page

Car rental bill

##Implementation ideas

First define a Car class, which contains basic functions: car name, number of passengers, cargo capacity, and daily rental. Then create three subclasses, namely passenger car class, truck class and pickup truck class (which can carry both passengers and cargo). They all inherit

Car class. Finally, a main class is needed to start the entire system and call each subclass.

Implementation code

package com.jinger;
public abstract class Car {
 public int rent;//日租金
 public int people;//载客人数
 public int loads;//载货量
 public String name;//车名
public int getRent(){
 return rent;
}
public void setRent(int rent){
 this.rent=rent;
}
public int getPeople(){
 return people;
}
public void setPeople(int people){
 this.people=people;
}
public int getLoads(){
 return loads;
}
public void setLoads(int loads){
 this.loads=loads;
}
public String getName(){
 return name;
}
public void setName(String name){
 this.name=name;
}
}
Copy after login
Passenger car class
package com.jinger;
public class PassageCar extends Car{
 public PassageCar(String name,int people,int rent){
 this.setName(name);
 this.setPeople(people);
 this.setRent(rent);
 
 
 }
 
 public String toString(){
 return this.getName()+"\t"+this.getPeople()+"\t\t\t\t"+this.getRent();
 }
 }
Copy after login

Truck class

package com.jinger;
public class Truck extends Car {
 public Truck(String name,int loads,int rent){
 this.setName(name);
 this.setLoads(loads);
 this.setRent(rent);
 }
 
 public String toString(){
 return this.getName()+"\t\t\t"+this.getLoads()+"\t\t"+this.getRent();
 }
 }
Copy after login

Pickup truck class

package com.jinger;
public class Pickup extends Car {
 public Pickup(String name,int people,int loads,int rent){
 this.setName(name);
 this.setPeople(people);
 this.setLoads(loads);
 this.setRent(rent);
 }
 
 public String toString(){
 return this.getName()+"\t"+this.getPeople()+"\t\t"+this.getLoads()+"\t\t"+this.getRent();
 }
 }
Copy after login

Main class

package com.jinger;
import java.util.*;
public class Initial {
 public static void main(String[] args) {
 //对各类车实例化并保存到cars数组
 Car[] cars={
 new PassageCar("奥迪A4",4,500),
 new PassageCar("马自达6",4,400),
 new Pickup("皮卡雪6",4,2,450),
 new PassageCar("金龙",20,800),
 new Truck("松花江",4,400),
 new Truck("依维柯",20,1000)};
 System.out.println("****欢迎使用达达租车系统!****");
 System.out.println("****您确认租车吗?****"+"\n"+"是(请输入1) \t 否(请输入2)");
 
 Scanner in1=new Scanner(System.in);
 int is=in1.nextInt();
 if(is!=1){
 System.out.println("****欢迎下次光临!****");
 System.exit(0);
 }
 if(is==1){
 System.out.println("****您可租车的类型及价目表****");
 System.out.println("序号"+"\t车名"+"\t载客数(人)"+"\t载货量(吨)"+"\t日租金(元/天)");
 
 //使用循环方式将各类车输出
 for(int i=0;i<cars.length;i++){
 System.out.println((i+1)+"\t"+cars[i]);
 }
 
 
 
 System.out.println("****请输入您的租车数量:****");
 int num1=in1.nextInt();
 Car[] rentcar=new Car[num1];
 int price=0;//总价格
 int totalpeople=0;//总人数
 int totalloads=0;//总载货量
 
 for(int i=0;i<num1;i++){
 System.out.println("****请输入第"+(i+1)+"辆车的序号:****");
 int numx=in1.nextInt();
 rentcar[i]=cars[numx-1];
 
 }
 System.out.println("****请输入天数:****");
 int day=in1.nextInt();
 for(int i=0;i<num1;i++){
 price=price+rentcar[i].rent *day;
 }
 System.out.println("****您的账单:****");
 System.out.println("已选载人车:");
 for(int i=0;i<num1;i++){
 if(rentcar[i].people!=0){
  System.out.println(rentcar[i].name+"\t");
 }
 
 totalpeople=totalpeople+rentcar[i].people;
 }
 
 System.out.println(&#39;\n&#39;);
 System.out.println("已选载货车:");
 for(int i=0;i<num1;i++){
 if(rentcar[i].loads!=0){
  System.out.println(rentcar[i].name+"\t");
 }
  
 totalloads=totalloads+rentcar[i].loads;
 }
 
 
  System.out.println(&#39;\n&#39;);
  System.out.println("共载客:"+totalpeople+"人");
  System.out.println("共载货:"+totalloads+"吨");
  System.out.println("租车总价格:"+price+"元");
  System.out.println(&#39;\n&#39;);
  System.out.println("****感谢您的惠顾,欢迎再次光临!****");
 
 }
 }
 }
Copy after login

Harvest

The idea determines the encoding.

Programming should focus on a top-down, step-by-step design approach.

【Related recommendations】

1.
Java free video tutorial

2. Comprehensive analysis of Java annotations

3. FastJson Tutorial Manual

The above is the detailed content of Detailed process of implementing car rental system in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template