花了4天才搞定!第一次啊,我的思路是,汽车是个抽象的父类拥有基本的属性
2个接口,一个载人,一个载货;然后是3个子类,一个是客车继承父类汽车+载人接口;一个是货车继承父类汽车+载货接口;一个是皮卡继承父类+载人+载货2个接口;
代码如下:
父类汽车:
/*
- 车辆是个抽象类,
*/
package com.zuche;
public abstract class Car {
public int Num ;// 车辆编号
public String name; //车辆名字
public double price;// 车辆租金
}
2个接口:
package com.zuche;
//车辆载货属性的接口
public interface Cargo {
//载货量属性
public double catgoNum = 0;
}
package com.zuche;
//车辆载人属性的接口
public interface CarryPeople {
public int carry= 0 ;
}
3个子类:
package com.zuche;
//客车属于车具有载人属性
public class Passengercar extends Car implements CarryPeople {
public void passGercar(int Num ,String name,double price,int carry){
System.out.println(Num+". "+name+" "+price+"元/天 ,载人:"+carry+"人");
}
}
package com.zuche;
//皮卡属于车 具有载人和载货的属性
public class Pickup extends Car implements Cargo, CarryPeople {
public void pickup(int Num ,String name,double price,int carry,double cargoNum){
System.out.println(Num+". "+name+" "+price+"元/天 ,载人:"+carry+"人,载货:"+cargoNum+"吨");
}
}
package com.zuche;
//火车属于车具有载货的属性
public class Truck extends Car implements Cargo {
public void truck(int Num ,String name,double price,double catgoNum){
System.out.println(Num+". "+name+" "+price+"元/天 ,载货:"+catgoNum+"吨");
}
}
主程序:
package com.zuche;
import java.util.Scanner;
import java.util.*;
public class Zuchexitong {
public static void main(String[] args) {
System.out.println("欢迎使用嗒嗒租车系统:");
System.out.println("您是否要租车请按:1是,0否");
Scanner input = new Scanner(System.in);
int i = input.nextInt();
if(i == 1){
System.out.println("您可租车的类型及其价目表:");
System.out.print("序号 汽车名称 租金 ");
System.out.println(" 容量");
// TODO 自动生成的方法存根
Passengercar Audi = new Passengercar();
Audi.passGercar(1,"奥迪A4",500,5);
Passengercar MzDa = new Passengercar();
MzDa.passGercar(2,"马自达6",400,5);
Pickup piku = new Pickup();
piku.pickup(3,"皮卡雪6",450,5,2);
Passengercar Jinlong = new Passengercar();
Jinlong.passGercar(4, "金龙 ", 800, 20);
Truck SongHJ = new Truck();
SongHJ.truck(5, "松花江 ", 400, 4);
Truck YiWeKe = new Truck();
YiWeKe.truck(6, "依维柯 ", 1000, 20);
System.out.println("请输入您要的汽车数量:");
Scanner jnput = new Scanner(System.in);
int j = jnput.nextInt();
int[] nums =new int[j];
int k=0;
int f =1;
do{
System.out.println("请输入第"+f+"辆车的序号");
Scanner knput = new Scanner(System.in);
nums[k]=knput.nextInt();
if(nums[k]<7&&nums[k]>0){
f++;
k++;
}else{
System.out.println("请重新输入:");
System.out.println("请输入第"+f+"辆车的序号");
Scanner rnput = new Scanner(System.in);
nums[k]=rnput.nextInt();
f++;k++;
}
}while(k<j);
System.out.println("请输入租车天数:");
Scanner day = new Scanner(System.in);
int days = day.nextInt();
System.out.println("您的账单:");
System.out.println("***可载人的车有:");
int one=0,two =0, three =0,four =0,fife =0,six = 0;
for(int l=0;l<k;l++){
switch(nums[l]){
case 1 :
one++;
break;
case 2 :
two++;
break;
case 3:
three++;
break;
case 4:
four++;
break;
case 5:
fife++;
break;
case 6:
six++;
break;
}
}
if(one>0)
System.out.print("\t奥迪A4 X"+one);
if(two>0)
System.out.print("\t马自达 X"+two);
if(three>0)
System.out.print("\t皮卡雪 X"+three);
if(four>0)
System.out.print("\t金龙 X"+four);
int people= (one+two+three)*5+four*20;
System.out.println("\t共载人数为:"+people+"人");
System.out.println("***可载货的车有:");
if(three>0)
System.out.print("\t皮卡雪 X"+three);
if(fife>0)
System.out.print("\t松花江 X"+fife);
if(six>0)
System.out.print("\t松花江 X"+six);
int dun =three*2+fife*4+six*20;
System.out.println("\t共载货:"+dun+"吨");
int Price= (one*500+two*400+three*450+four*800+fife*400+six*1000)*days;
System.out.println("***租车总价格:"+Price+"元,共"+days+"天。");
System.out.println("******谢谢惠顾!******");
}else System.out.println("欢迎下次再来!");
}
}
运行结果
共同学习,写下你的评论
评论加载中...
作者其他优质文章