达达租车系统(java)

目标:

控制台版本租车系统实现








代码实现:


首先创建父类Car类,包含:车名、载客数、载货量、日租金。接着创建三个子类,分别是载人类、货车类和皮卡类(既能载客又能载货),都继承Car类。最后需要一个主函数,用于开启整个系统,调用每个小类。



父类Car类

package dada;
public abstract class Car {
public int rent;//日租金
public int peoplenum;//载客人数
public int goodsnum;//载货量
public String name;//车名
public int getRent(){
return rent;
}
public void setRent(int rent){
this.rent=rent;
}
public int getPeoplenum(){
return peoplenum;
}
public void setPeoplenum(int people){
this.peoplenum=people;
}
public int getGoodsnum(){
return goodsnum;
}
public void setGoodsnum(int loads){
this.goodsnum=loads;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
}


子类——客车类PeopleCar

package dada;
import dada.PeopleCar;
public class PeopleCar extends Car {
public PeopleCar(String name,int peoplenum,int rent){
this.setName(name);
this.setPeoplenum(peoplenum);
this.setRent(rent);  
}
  
public String toString(){
return this.getName()+"\t"+this.getPeoplenum()+"\t\t\t\t"+this.getRent();
}
}


子类——载货车Trunk

package dada;
import dada.Truck;
public class Truck extends Car {//载货车
       public Truck(String name,int goodsnum,int rent){
       this.setName(name);
       this.setGoodsnum(goodsnum);
   
       this.setRent(rent);
       }
   public String toString(){
   return this.getName()+"\t\t\t"+this.getGoodsnum()+"\t\t\t\t"+this.getRent();
   }
}


子类——皮卡车类Pickup

package dada;
import dada.Pickup;
public class Pickup extends Car {
          public Pickup(String name,int goodsnum,int peoplenum,int rent){
           this.setName(name);
           this.setGoodsnum(goodsnum);
           this.setRent(rent);
           this.setPeoplenum(peoplenum);
          }
          public String toString(){
       return this.getName()+"\t"+this.getGoodsnum()+"\t"+this.getRent();
       }
}


主函数类

package dada;
import java.util.*;
public class Test {


public static void main(String[] args) {
// TODO Auto-generated method stub
       //对各类车实例化并保存到cars数组
Car[] cars={
new PeopleCar("奥迪A41",4,500),
new PeopleCar("马自达6",4,400),
new Pickup("皮卡雪",2,4,450),
new PeopleCar("金龙",20,800),
new Truck("松花江",4,400),
new Truck("依维柯",20,1000)
};
System.out.println("****欢迎使用达达租车系统");
System.out.println("***确认租车请按1,不租车请按2******");
Scanner in1 = new Scanner(System.in);
int count = in1.nextInt();
if(count!=1){
System.out.println("***欢迎下次光临!***");
System.exit(0);
}else if (count ==1){
System.out.println("您可租车型及价目表");
System.out.println("序号"+"\t车名"+"\t\t载客数(人)"+"\t载货量(吨)"+"\t日租金(元/天)");
//输出实例化的各类车
for(int i =0;i<cars.length;i++){
System.out.println((i+1)+"\t"+cars[i]);
}
}


//输入想租的车
System.out.println("请输入您的租车数量");
  int carcount = in1.nextInt();
  //建立新的数组
Car[] RCars = new Car[carcount]; 
  int loads = 0;
  int totalrent = 0;
  int totalpeople = 0;
  
  for(int i =0;i<carcount;i++){
  System.out.println("请输入第"+(i+1)+"辆车的序号");
int xuhao = in1.nextInt();
RCars[i]=cars[xuhao-1];
  }


  System.out.println("请输入天数");
  int days = in1.nextInt();
  for(int i=0;i<carcount;i++){
  totalrent+=RCars[i].rent*days;
  }
  System.out.println("您的账单");
  System.out.println("已选载人车");
  for(int i=0 ;i<carcount;i++){
  if(RCars[i].peoplenum!=0)
               System.out.println(RCars[i].name);
  totalpeople+=RCars[i].peoplenum;
  }
  System.out.println("\n");
  
  System.out.println("已选载货车");
  for(int i=0;i<carcount;i++){
  if(RCars[i].goodsnum!=0)
  System.out.println(RCars[i].name);
  loads += RCars[i].goodsnum;
  }
  System.out.println("\n");
  System.out.println("共载客"+totalpeople+"人");
  System.out.println("共载货"+loads+"吨");
  System.out.println("租车总价格"+totalrent+"元");
  System.out.println("\n");
  System.out.println("感谢您的惠顾,期待下次光临");
}
}



实现结果:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值