亲宝软件园·资讯

展开

java实现租车系统 java实现简单租车系统

浅然_ 人气:0

这也是参考了mooc上的一个基础项目,所以拿来写一下。不过我的Demo肯定有不好或者错误的地方,欢迎指出

1、项目功能/需求

a、展示所有可租车辆
b、选择车型、租车量
c、展示租车清单,包含:总金额、总载货量、总载客量

2、项目系统UML图

3、项目源码Demo

Car类

package Car.rental;

public class Car {
 private String name;
 private int rent;
 private int passenger;
 private double volume;
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 public int getRent() {
 return rent;
 }
 public void setRent(int rent) {
 this.rent = rent;
 }
 public int getPassenger() {
 return passenger;
 }
 public void setPassenger(int passenger) {
 this.passenger = passenger;
 }
 public double getVolume() {
 return volume;
 }
 public void setVolume(double volume) {
 this.volume = volume;
 }

}

Truck类

package Car.rental;

public class Truck extends Car {
 private String name;
 private int rent;
 private double volume;
 public Truck(String name,int rent,double volume)
 {
 this.name=name;
 this.rent=rent;
 this.volume=volume;
 }
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 public int getRent() {
 return rent;
 }
 public void setRent(int rent) {
 this.rent = rent;
 }
 public double getVolume() {
 return volume;
 }
 public void setVolume(double volume) {
 this.volume = volume;
 }

}

Coach类

package Car.rental;

public class Coach extends Car {

 private String name;
 private int rent;
 private int passenger;
 public Coach(String name,int rent,int passenger) {
 this.name=name;
 this.passenger=passenger;
 this.rent=rent;
 }
 public int getPassenger() {
 return passenger;
 }
 public void setPassenger(int passenger) {
 this.passenger = passenger;
 }
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 public int getRent() {
 return rent;
 }
 public void setRent(int rent) {
 this.rent = rent;
 }
}

Pickup类

package Car.rental;

public class Pickup extends Car {
 private String name;
 private int rent;
 private int passenger;
 private double volume;
 public Pickup(String name,int rent,int passenger,double volume)
 {
 this.name=name;
 this.passenger=passenger;
 this.rent=rent;
 this.volume=volume;
 }
 public int getPassenger() {
 return passenger;
 }
 public void setPassenger(int passenger) {
 this.passenger = passenger;
 }
 public double getVolume() {
 return volume;
 }
 public void setVolume(double volume) {
 this.volume = volume;
 }
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 public int getRent() {
 return rent;
 }
 public void setRent(int rent) {
 this.rent = rent;
 }

}

测试类

package Car.rental;

import java.util.Scanner;

public class main_Car {

 public static void main(String[] args) {
 // TODO Auto-generated method stub
 /*****创建车对象*******/
 /*Car tru=new Truck("拉丝货车",200,1.1);
 Car coa=new Coach("法法客车",150,20);
 Car Pic=new Pickup("无名皮卡车",300,4,0.5);*/
 Car[] Allcar= {new Truck("拉丝货车",200,1.1),new Coach("法法客车",150,20),
   new Pickup("无名皮卡车",300,4,0.5)};

 System.out.println("请问您是否要租车:1.是 2.否");
 Scanner scan=new Scanner(System.in);
 String input=scan.next();
 if(input.equals("1"))
 {
  int all_rent = 0;//总租金
  int all_passenger=0;//总载客
  double all_volume=0.0;//总载货
  System.out.println("您可租车的类型及其价目");
  System.out.println("序号\t汽车名称\t租金\t容量\t");
  for(int i=0;i<Allcar.length;i++)
  {
  if(Allcar[i] instanceof Truck)
  {
   System.out.println((i+1)+"\t"+Allcar[i].getName()+"\t"+Allcar[i].getRent()+"\t"+"载货:"+Allcar[i].getVolume());
  }
  if(Allcar[i] instanceof Coach)
  {
   System.out.println((i+1)+"\t"+Allcar[i].getName()+"\t"+Allcar[i].getRent()+"\t"+"载客:"+Allcar[i].getPassenger());
  }
  if(Allcar[i] instanceof Pickup)
  {
   System.out.println((i+1)+"\t"+Allcar[i].getName()+"\t"+Allcar[i].getRent()+"\t"+"载客:"+Allcar[i].getPassenger()+"载货:"+Allcar[i].getVolume());
  }
  }
  System.out.println("请输入您要租车的数量");
  int sum=scan.nextInt();
  int Car_order;
  for(int j=0;j<sum;j++)
  {
  System.out.print("你要租的第"+(j+1)+"辆车的序号是:");
  Car_order=scan.nextInt();
  System.out.println();
  if(Allcar[Car_order-1] instanceof Truck)
  {
   all_rent+=Allcar[Car_order-1].getRent();
   all_volume+=Allcar[Car_order-1].getVolume();
  }
  if(Allcar[Car_order-1] instanceof Coach)
  {
   all_rent+=Allcar[Car_order-1].getRent();
   all_passenger+=Allcar[Car_order-1].getPassenger();
  }
  if(Allcar[Car_order-1] instanceof Pickup)
  {
   all_rent+=Allcar[Car_order-1].getRent();
   all_passenger+=Allcar[Car_order-1].getPassenger();
   all_volume+=Allcar[Car_order-1].getVolume();
  }
  }
  System.out.println("您的总租金是:"+all_rent);
  System.out.println("您的总载货是:"+all_volume);
  System.out.println("您的总载客是:"+all_passenger);
 }

 }

}

4、项目命令行演示结果

加载全部内容

相关教程
猜你喜欢
用户评论