手记

Java开发之用接口实现载物载人(自己瞎实现的)

package imooc.tf;

public abstract class Car {
public String name;
public int rent;
public int person;
public int goods;
public String toString() {
return (". " + name +" "+ rent +"元/天 " + " 载人:" + person + "人" + " 载物:" + goods +"吨");
}
}
package imooc.tf;

public class Caryy_cargo_car extends Car {
public Caryy_cargo_car(String name,int rent,int goods) {
this.name = name;
this.rent = rent;
this.goods = goods;
}
public String toString() {
return (". " + name + " "+rent +"元/天 " + " 载物:" + goods +"吨");
}
}
package imooc.tf;

public class Carry_person_car extends Car {
public Carry_person_car(String name, int rent,int person) {
this.name = name;
this.rent = rent;
this.person = person;
}
public String toString() {
return (". " + name +" "+ rent +"元/天 " + " 载人:" + person + "人" );
}
}
package imooc.tf;

public class Carry_all_car extends Car {
public Carry_all_car(String name,int rent,int person,int goods) {
this.name = name;
this.rent = rent;
this.person = person;
this.goods = goods;
}

}
package imooc.tf;
import java.util.Scanner;

public class test {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Car car[] = {new Carry_person_car("奥迪A4", 500, 4),
                 new Carry_person_car("马自达6", 400, 4),
                 new Carry_all_car("皮卡雪6", 450, 4, 2),
                 new Carry_person_car("金龙         ", 800, 20),
                 new Caryy_cargo_car("松花江      ", 400, 4),
                 new Caryy_cargo_car("依维柯    ", 1000, 20)};
    System.out.println("欢迎使用答答租车系统:");
    System.out.println("您是否要租车:1是,0否");
    Scanner input = new Scanner(System.in);
    int answer =input.nextInt();
    if(answer == 1) {
        System.out.println("您可租车的类型及价目表:");

    System.out.println("序号 汽车名称     租金            容量");
    for(int i = 0;i < car.length;i++) {
        System.out.println((i+1) + "" + car[i]);
    }
    System.out.println("请输入您要的租车数量:");
    int num = input.nextInt();
    int[] count_person = new int[200];//载人数
    int[] count_goods = new int[200];//载货物数
    int[] count_rent =new int[200];//总价格
    String[] name_person = new String[200];//载人车
    String[] name_goods = new String[200];//载物车
    for(int i = 0;i < num;i++) {
        System.out.println("请输入第" + (i+1)+"辆车的序号:");
        int serial_num = input.nextInt();
        count_person[i] = car[serial_num-1].person;
        count_goods[i] = car[serial_num-1].goods;
        count_rent[i] = car[serial_num-1].rent;
        if(serial_num <= 4) name_person[i] = car[serial_num-1].name;
        if(serial_num >= 5 || serial_num == 3) name_goods[i] = car[serial_num-1].name;
    }
    System.out.println("请输入租车天数:");
    int rent_day = input.nextInt();
    System.out.println("您的账单:");
    System.out.println("***可载人的车有:");
    int all_person = 0;
    for(int i = 0;i < num;i++) {
        all_person += count_person[i];
    }
    for(int i = 0;i < num;i++) {
        if(name_person[i] != null) System.out.print(name_person[i] + "  ");

    }
    System.out.println("共载人:" + all_person + "人");
    System.out.println("***可载物的车有:");
    int all_goods = 0;
    for(int i = 0;i < num;i++) {
        all_goods += count_goods[i];
    }
    for(int i = 0;i < num;i++) {
        if(name_goods[i] != null) System.out.print(name_goods[i] + "  ");
    }
    System.out.println("共载货:" + all_goods + "吨");
    int all_rent = 0;
    for(int i = 0;i < num;i++) {
        all_rent += count_rent[i] * rent_day; 
    }
    System.out.println("***租车总价格:" + all_rent + "元");

    }else if(answer == 0) {
        System.out.println("感谢您的点击,希望下次再来");
    }
    input.close();
}

}

2人推荐
随时随地看视频
慕课网APP