手记

JAVA入门第二季 6-1综合练习

  • 父类
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人推荐
随时随地看视频
慕课网APP

热门评论

http://img.mukewang.com/5925786900014eee03230615.jpg载人的车有,和载车的人有,数据出现错误

写的很好,看的最懂的一次代码,自己也仿制了一个,效果和老师写的一样!

看了你的后自己写的,Car类数组总是赋值不进去,还在找解决方法,晕死了

查看全部评论