1、车的类型和价格运用抽象类来完成,创建抽象类 Car
package Dadazuche;
public abstract class Car {
int pc ;
int price;
int cap;
String type;
public abstract String carType(int i);
public abstract int price(int i);
}
2、功能性载客、载货、以及皮卡的载客+载货 ,分别用三个接口来实现
package Dadazuche;
public interface zaihuoliang {
public int zaihuo(int i);
}
package Dadazuche;
public interface zaihuozaike extends Zaikeliang, zaihuoliang {
public int zaihuo(int i);
public int zaike(int i);
}
package Dadazuche;
public interface zaihuoliang {
public int zaihuo(int i);
}
3、定义对象bus继承抽象类Car,引用接口Zaikeliang ,用来计算价格和载客量
package Dadazuche;
public class bus extends Car implements Zaikeliang {
@Override
public int zaike(int i) {
// TODO Auto-generated method stub
int audiA4=4;
int mazida6=4;
int jinlvkeche=40;
switch(i){
case 1:pc=audiA4;break;
case 2:pc=mazida6;break;
case 4:pc=jinlvkeche;break;
}
return pc;
}
@Override
public String carType(int i) {
// TODO Auto-generated method stub
String audiA4="奥迪A4";
String mazida6="马自达6";
String jinlvkeche="金旅客车";
switch(i){
case 1:type=audiA4;break;
case 2:type=mazida6;break;
case 4:type=jinlvkeche;break;
}
return type;
}
public int price(int i) {
// TODO Auto-generated method stub
int audiA4=800;
int mazida6=400;
int jinlvkeche=1000;
switch(i){
case 1:price=audiA4;break;
case 2:price=mazida6;break;
case 4:price=jinlvkeche;break;
}
return price;
}
}
4、定义对象trunk继承抽象类Car,引用接口zaihuoliang,用来计算价格和载客量
package Dadazuche;
public class truck extends Car implements zaihuoliang {
@Override
public int zaihuo(int i) {
// TODO Auto-generated method stub
//定义载货量
int songhuajiang =40;
int jiefangj6 =60;
if(i==5);cap =songhuajiang;
if(i==6);cap =jiefangj6;
return cap;
}
@Override
public String carType(int i) {
// TODO Auto-generated method stub
String songhuajiang ="松花江";
String jiefangj6 ="解放j6";
if(i==5);type =songhuajiang;
if(i==6);type =jiefangj6;
return type;
}
@Override
public int price(int i) {
// TODO Auto-generated method stub
int songhuajiang =700;
int jiefangj6 =800;
if(i==5);price =songhuajiang;
if(i==6);price =jiefangj6;
return price;
}
}
5、定义对象Pickup继承抽象类Car,引用接口zaihuozaike,用来计算价格和载客量和载货量
package Dadazuche;
public class Pickup extends Car implements zaihuozaike {
@Override
public int zaihuo(int i) {
// TODO Auto-generated method stub
//定义载货量为6吨
int pickupsnow6=6;
if(i==3){
cap =pickupsnow6;
}
return cap;
}
@Override
public int zaike(int i) {
// TODO Auto-generated method stub
int pickupsnow6=4;
if(i==3) {
pc=pickupsnow6;
}
return pc;
}
@Override
public String carType(int i) {
// TODO Auto-generated method stub
String pickupsnow6 ="雪佛兰6";
if(i==3){
type=pickupsnow6;
}
return type;
}
@Override
public int price(int i) {
// TODO Auto-generated method stub
int pickupsnow6=500;
if(i==3){
price =pickupsnow6;
}
return price;
}
}
6、主程序test实现租车功能
package Dadazuche;
import java.util.*;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
String [] Cars ={"序号 租车名称 租金 容量",
"1. 奥迪A4 800/天 载人:4人",
"2. 马自达6 400/天 载人:4人",
"3. 皮卡雪6 500/天 载人4人载货6吨",
"4. 金旅客车 1000/天 载人:40人",
"5. 松花江 700/天 载货:4吨",
"6. 解放J6 800/天 载货:6吨"};
//String type="";
System.out.println("欢迎使用达达租车系统:");
System.out.println("您是否要租车,请输入序号:1.是 0.否");
Scanner scanner =new Scanner(System.in);
int a=scanner.nextInt();
if (a==0){
System.out.println("欢迎下次使用!");
}
else if(a==1){
System.out.println("欢迎使用达达租车系统,你可以租的车辆信息如下:");
for (int i=0;i<Cars.length;i++){
System.out.println(Cars[i]);
}
}
System.out.println("请输入你要租的车辆数");
int num =scanner.nextInt();
int sumprice =0;
int sumpc =0;
int sumcap =0;
for(int j=1;j<=num;j++){
System.out.println("请输入要租的第"+j+"辆车的序号");
int No =scanner.nextInt();
//声明小汽车
Car c1 =new bus();
Zaikeliang c11 =new bus();
//声明皮卡对象
Car c3 =new Pickup();
zaihuozaike c31 =new Pickup();
//声明火车
Car c2 =new truck();
zaihuoliang c21 =new truck();
switch(No){
case 1:
case 2:
case 4:{
sumprice =sumprice+c1.price(No);
sumpc =sumpc+c11.zaike(No);
}break;
case 3:
{
sumprice =sumprice+c3.price(No);
sumpc =sumpc+c31.zaike(No);
sumcap =sumcap +c31.zaihuo(No);
}break;
case 5:
case 6:
{
sumprice =sumprice+c2.price(No);
sumcap=sumcap+c21.zaihuo(No);
}break;
}
}
System.out.println("您输入您要租车的天数");
int k =scanner.nextInt();
System.out.println("您租车的总费用为:"+sumprice*k);
System.out.println("您租车的载客总量为:"+sumpc*k);
System.out.println("您租车的载货总量为:"+sumcap*k);
}
}
总结:
总的来说写的不是很好,后来仔细思考一下 太繁琐,可以把抽象类的功能都定义为 载客和载货,这样就不用再使用接口
点击查看更多内容
5人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦