- 父类
package gakki;
public abstract class Car {
public String name;
public int price;
public int people;
public int weight;
}
- 子类1(只载人)
package gakki;
public class Bus extends Car {
public Bus(String name,int price,int people){
this.name=name;
this.price=price;
this.people=people;
}
@Override
public String toString() {
return (name+"\t"+price+"\t"+people+"人");
}
}
- 子类2(只载货)
package gakki;
public class KaCar extends Car {
public KaCar(String name,int price,int weight){
this.name=name;
this.price=price;
this.weight=weight;
}
@Override
public String toString() {
return (name+"\t"+price+"\t"+weight+"吨");
}
}
- 子类3(既载人又载货)
package gakki;
public class PiCar extends Car {
public PiCar(String name,int price,int people,int weight){
this.name=name;
this.price=price;
this.people=people;
this.weight=weight;
}
@Override
public String toString() {
return (name+"\t"+price+"\t"+people+"人,"+weight+"吨");
}
}
- 主代码
package gakki;
import java.util.Scanner;
public class Initial5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int totalPeople=0,totalPrice=0,totalWeight=0;
String ZaiKe="",ZaiHuo="";
Car[] cars={new Bus("玛莎拉蒂", 500,4),
new Bus("大黄蜂",700,4),
new KaCar("擎天柱", 1000, 20),
new KaCar("威震天",900,30),
new PiCar("福特", 800, 2, 3),
new PiCar("雷诺",600,2,10)
};
System.out.println("欢迎使用gakki租车系统");
System.out.println("您是否要租车:1是 0否");
Scanner scanner=new Scanner(System.in);
int a = scanner.nextInt();
if(a==1){
System.out.println("你可租车的类型及其价目表");
System.out.println("序号\t汽车名称\t租金\t容量\t");
for(int i=0;i<cars.length;i++){
System.out.println((i+1)+".\t"+cars[i] );
}
System.out.print("请输入你要租汽车的数量:");
int b=scanner.nextInt();
for(int j=0;j<b;j++){
System.out.println("请输入第"+(j+1)+"辆车的序号");
int c=scanner.nextInt();
System.out.println("您租的是:"+cars[c-1].name);
totalPrice=totalPrice+cars[c-1].price;
totalPeople=totalPeople+cars[c-1].people;
totalWeight=totalWeight+cars[c-1].weight;
if(cars[c-1].people!=0){
ZaiKe=ZaiKe+cars[c-1].name+" ";
}
if(cars[c-1].weight!=0){
ZaiHuo=ZaiHuo+cars[c-1].name+" ";
}
}
System.out.print("请输入租此车的天数:");
int d=scanner.nextInt();
totalPrice=totalPrice*d;
}
System.out.println("您的账单:");
System.out.println("可载人的车有:");
System.out.println(ZaiKe+"\t共"+totalPeople+"人");
System.out.println("载货的车有:");
System.out.println(ZaiHuo+"\t共"+totalWeight+"吨");
System.out.println("总价格为:"+totalPrice);
}
}
- 运行结果
我也是初学者,如果有不理解的地方,可以一起交流。
点击查看更多内容
9人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦