车的父类
package com.imooc;
//所有车的父类
public class Car {
String name; //车名
int price; //租金
int busload; //载人的数量
float cargo; //载客量
int id; //序列号
public void Car(int id,String name,float cargo,int price) {
this.id=id;
this.name=name;
this.cargo=cargo;
this.price=price;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price=price;
}
}
货车接口
package com.imooc;
//货车接口
public interface cargo {
void trnsport();
}
客车接口
package com.imooc;
//客车接口
public interface passenger {
void carry();
}
客车类
package com.imooc;
//客车类
public class Pessengers extends Car implements passenger {
public Pessengers(int id,String name,int busload,float price) {
System.out.println(id+","+name+busload+"人"+" "+price+"元、天 载人");
}
@Override
public void carry() {
// TODO Auto-generated method stub
}
@Override
public int getPrice() {
// TODO Auto-generated method stub
return super.getPrice();
}
@Override
public void setPrice(int price) {
// TODO Auto-generated method stub
super.setPrice(price);
}
}
客车类
package com.imooc;
//客车类
public class Pessengers extends Car implements passenger {
public Pessengers(int id,String name,int busload,float price) {
System.out.println(id+","+name+busload+"人"+" "+price+"元、天 载人");
}
@Override
public void carry() {
// TODO Auto-generated method stub
}
@Override
public int getPrice() {
// TODO Auto-generated method stub
return super.getPrice();
}
@Override
public void setPrice(int price) {
// TODO Auto-generated method stub
super.setPrice(price);
}
}
皮卡车类
package com.imooc;
//皮卡车类
public class pickUp extends Car implements passenger, cargo {
public pickUp(int id,String name,int busload,float cargo,float price) {
System.out.println(id+","+name+ " "+busload+"人 ,载货 "+cargo+"吨"+" "+price);
}
@Override
public void trnsport() {
// TODO Auto-generated method stub
}
@Override
public int getPrice() {
// TODO Auto-generated method stub
return super.getPrice();
}
@Override
public void setPrice(int price) {
// TODO Auto-generated method stub
super.setPrice(price);
}
@Override
public void carry() {
// TODO Auto-generated method stub
}
}
货车类
package com.imooc;
//货车类
public class Track extends Car implements cargo {
public Track(int id,String name,float cargo,float price) {
System.out.println(id+","+name+" "+cargo+"吨"+" "+price);
}
@Override
public void trnsport() {
// TODO Auto-generated method stub
}
@Override
public int getPrice() {
// TODO Auto-generated method stub
return super.getPrice();
}
@Override
public void setPrice(int price) {
// TODO Auto-generated method stub
super.setPrice(price);
}
}
租车系统
package com.imooc;
import java.util.Scanner;
public class rentSystem {
Scanner sc=new Scanner(System.in);
private Car[] car=new Car[]{
new Pessengers(1,"奥迪A4",4,500),
new Pessengers(2,"马自达",4,450),
new pickUp(3,"皮卡雪",4,2,200),
new Pessengers(4,"金龙",4,800),
new Track(5,"松花江",4,400),
new Track(6,"依维柯",20,1000)
};
public int sum=0;
int choose (int num) {
int n=0;
for(int i = 0 ; i < num ; i ++)
{
System.out.println("请输入第"+(i + 1)+"辆车的序号: ");
int x = sc.nextInt()-1;
switch(x)
{
case 0:
n = car[0].getPrice();
break;
case 1:
n = car[1].getPrice();
break;
case 2:
n = car[2].getPrice();
break;
case 3:
n = car[3].getPrice();
break;
case 4:
n = car[4].getPrice();
break;
case 5:
n = car[5].getPrice();
break;
}
sum+=n;
}
return sum;
}
}
测试类
package com.imooc;
import java.util.Scanner;
//测试类
public class Test {
public static void main(String[] args) {
int total;
int num;
int sum;
Scanner sc = new Scanner(System.in);
System.out.println("欢迎使用DD租车");
System.out.println("您是否要租车?1:是 0:否");
int answer = sc.nextInt();
if ( answer == 1) {
System.out.println("您可租车的类型及其价目表");
System.out.println("序号 汽车名称 租金 容量");
rentSystem rt = new rentSystem();
System.out.println("请输入您要租的汽车数量");
num = sc.nextInt();
sum = rt.choose(num);
System.out.println("请输入您要租的天数");
int day = sc.nextInt();
total = day*sum;
System.out.println("最后要租用的费用是:" + total);
answer ++;
}
else
{
System.out.println("这家伙很懒啥也没看。");
}
}
}
小白参考大神的作品跟着写的~