(1) 定义一个车的父类;设置各种共有的属性(价格,序号,车名,容量);
public class Car {
protected int price;
protected int id; //序号 在租用界面给用户选择用;
protected String name; //车名
protected int volume; //货车载火量;
protected int people; //客车载人量;
public Car() {
}
public Car(int id, String name, int volume, int price) {
this.price = price;
this.id = id;
this.name = name;
this.volume = volume;
}
}
(2) 定义两个接口,分别是载货、载人;提供给不同车类来实现它的功能;
public interface cargo {
void transport();
}
///载人
public interface passenger {
void carry();
}
(3)创建货车类,继承父类Car,实现接口Cargo;
public class Track extends Car implements cargo {
public Track(){
}
public Track(int id,String name,int volume,int price)
{
super(id,name,volume,price);
System.out.println(id+","+" "+name+" "+price+"元/天 载人"+volume+"人");
}
///实现cargo接口的方法.
public void transport() {
}
}
(4)普通的客车
///普通的客车 继承父类Car,pessenger.接口.
public class Pessengers extends Car implements passenger {
public Pessengers() {
super();
}
public Pessengers(int id, String name, int volume, int price) {
super(id, name, volume, price);
System.out.println(id+","+" "+name+" "+price+"元/天 载人"+volume+"人");
}
///实现pessenger接口就要实现它所有的方法
public void carry() {
}
}
(5)pickup类;
///集货车与客车功能于一身;要实现两个接口的方法;
public class pickup extends Car implements cargo, passenger {
public pickup() {
super();
}
public pickup(int id, String name, int volume,int people, int price) {
super(id, name, volume,price);
this.people=people;
System.out.println(id+","+" "+name+" "+price+"元/天 载人:"+people+"人 载货:"+volume+"吨");
}
public void carry() {
}
public void transport() {
}
}
(6)主界面,租车系统类;
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,"皮卡雪6",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++) //6
{
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;
}
}
(7)测试类
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("这家伙很懒啥也没看。 ");
}
}
}
点击查看更多内容
18人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦