手记

Java入门第二季,鄙人解答,献丑了

主程序

import java.util.Scanner;
/**
 * Created by romanten on 2017/11/4.
 */
public class Login {
    public static void main(String[] args) {
        System.out.println("欢迎使用哒哒租车系统!");
        System.out.println("请问您是否要租车:1是 0否");
        Scanner input = new Scanner(System.in);
        int isHire = input.nextInt();
        if (isHire==0) {
            System.out.println("好吧,欢迎再来!");
            System.exit(0);
        }else{
            System.out.println("您可租车的类型及其价目表:");
            System.out.println("序号\t汽车名称\t租金\t容量");
            Car[] forHire={new Bus("奥迪A4",500,4),
                    new Bus("马自达",400,4),
                    new PickUp("皮卡雪",450,4,2),
                    new Bus("金龙",800,20),
                    new Truck("松花江",400,4),
                    new Truck("依维柯",1000,20)};
            int i=1;
            for(Car current:forHire){ //foreach
                if(current instanceof Bus){
                    System.out.print(i++);
                    current.print_name();
                    current.print_rent();
                    System.out.println("载人量(人):"+((Bus)current).getManCapacity());
                }
                else if(current instanceof Truck){
                    System.out.print(i++);
                    current.print_name();
                    current.print_rent();
                    System.out.println("载货量(吨):"+((Truck)current).getLoadCapacity());
                }else{
                    System.out.print(i++);
                    current.print_name();
                    current.print_rent();
                    System.out.println("载人(人)"+((PickUp)current).getManCapacity()+
                            " 载货(吨):"+((PickUp)current).getLoadCapacity());
                }
            }
            System.out.print("请输入您要租车的数量:");
            int count=input.nextInt();
            int[] rentCar=new int[count];
            double sum_rent=0;
            double sum_man=0;
            double sum_load=0;
            for(i=count;i>0;i--){
                System.out.println("请输入第"+(count-i+1)+"辆车的序号:");
                rentCar[count-i]=input.nextInt();
            }
            System.out.println("请输入租车天数:");
            int rentDay=input.nextInt();
            System.out.println("您的账单:\n***可载人的车有:");
            for(i=0;i<count;i++){
                if((forHire[(rentCar[i]-1)] instanceof Bus)) {
                    forHire[(rentCar[i]-1)].print_name();
                    sum_man+=((Bus)forHire[(rentCar[i]-1)]).getManCapacity();
                    sum_rent+=((Bus)forHire[(rentCar[i]-1)]).rent;
                }
                if((forHire[(rentCar[i]-1)] instanceof PickUp)) {
                    forHire[(rentCar[i]-1)].print_name();
                    sum_man+=((PickUp)forHire[(rentCar[i]-1)]).getManCapacity();
                    sum_rent+=((PickUp)forHire[(rentCar[i]-1)]).rent;
                }
            }
            System.out.println("共载人:"+sum_man+"人");
            System.out.println("***可载货的车有:");
            for(i=0;i<count;i++){
                if((forHire[(rentCar[i]-1)] instanceof Truck)) {
                    forHire[(rentCar[i]-1)].print_name();
                    sum_load+=((Truck)forHire[(rentCar[i]-1)]).getLoadCapacity();
                    sum_rent+=((Truck)forHire[(rentCar[i]-1)]).rent;
                }
                if((forHire[(rentCar[i]-1)] instanceof PickUp)) {
                    forHire[(rentCar[i]-1)].print_name();
                    sum_load+=((PickUp)forHire[(rentCar[i]-1)]).getLoadCapacity();
                    //sum_rent+=((PickUp)forHire[(rentCar[i]-1)]).rent;
                    //皮卡即载货又载人。但是价格只算一次
                }
            }
            System.out.println("共载货:"+sum_load+"吨");
            System.out.println("***总价格:"+(sum_rent*rentDay)+"元");
        }
    }
}
4人推荐
随时随地看视频
慕课网APP