继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

哒哒租车系统代码练习

永恒星
关注TA
已关注
手记 2
粉丝 2
获赞 16

package carSystem;

public class Car {
private int Num;
private int Rent;
String Name;
public Car(int Num,int Rent,String Name){
this.Name=Name;
this.Num=Num;
this.Rent=Rent;
}
public int getNum() {
return Num;
}
public int getRent() {
return Rent;
}
public String getName() {
return Name;
}

}


package carSystem;

public class truck extends Car {
private int Cargo;
public truck(int Num, String Name, int Rent,int Cargo) {
super(Num, Rent, Name);
// TODO Auto-generated constructor stub
this.Cargo=Cargo;
}
public int getCargo() {
return Cargo;
}

}


package carSystem;

public class bus extends Car {
private int Passenger;
public bus(int Num, String Name, int Rent, int Passenger) {
super(Num, Rent,Name);
// TODO Auto-generated constructor stub
this.Passenger=Passenger;
}
public int getPassenger() {
return Passenger;
}

}


package carSystem;

public class Pika extends Car {
private int Cargo;
private int Passenger;
public Pika(int Num, String Name,int Rent, int Passenger,int Cargo) {
super(Num, Rent, Name);
// TODO Auto-generated constructor stub
this.Cargo=Cargo;
this.Passenger=Passenger;
}
public int getCargo() {
return Cargo;
}
public int getPassenger() {
return Passenger;
}

}


package carSystem;

import java.util.Scanner;

public class Initial {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Car[] car=
            {   new bus(1,"奥迪A4",500,4),
                new bus(2,"马自达6",400,4),
                new Pika(3,"皮卡雪6",450,4,2),
                new bus(4,"金龙",800,20),
                new truck(5,"松花江",400,4),
                new truck(6,"依维柯",1000,20)};
        System.out.println("欢迎使用答答租车系统:\n您是否要租车:1是0否");
        Scanner input=new Scanner(System.in);
        int j=input.nextInt();
        if(j==0){
            System.exit(0);
        }
        System.out.println("--------------------------------------------------");
        System.out.println("序号\t汽车名称\t\t租金\t\t容量");
        for(Car cars:car){
            if(cars instanceof bus){
        System.out.println(cars.getNum()+"\t"+cars.getName()+"\t\t"+cars.getRent()+"元/天\t\t"+"载人:"+((bus)cars).getPassenger()+"人");
            }
            if(cars instanceof truck){
                System.out.println(cars.getNum()+"\t"+cars.getName()+"\t\t"+cars.getRent()+"元/天\t\t"+"载货:"+((truck)cars).getCargo()+"吨");                   
            }
            if(cars instanceof Pika){
                System.out.println(cars.getNum()+"\t"+cars.getName()+"\t\t"+cars.getRent()+"元/天\t\t"+"载人:"+((Pika)cars).getPassenger()+"人"+"载货:"+((Pika)cars).getCargo()+"吨");                  
            }
        }
        System.out.println("--------------------------------------------------");
            //input.close();
        int[] rent=new int[5];
        int[] day=new int[5];
        int[] price=new int[5];
        String[] name=new String[5]; 
        System.out.print("请输入您要租车的数量:");
            int rentNums=input.nextInt();
        System.out.println();
            int sumPrice=0;
            for(int i=0;i<rentNums;i++){
                System.out.print("请输入你要租车的序号:");
                int NO=input.nextInt();
                System.out.print("请输入你要租车的时间:");
                int time=input.nextInt();
                System.out.println();                   
                switch (NO) {
                case 1:
                    rent[0]=car[0].getRent();
                    day[0]=time;
                    name[0]=car[0].getName();
                    price[0]=rent[0]*day[0];
                    break;
                case 2:
                    rent[1]=car[1].getRent();
                    day[1]=time;
                    name[1]=car[1].getName();
                    price[1]=rent[1]*day[1];
                    break;                  
                case 3:
                    rent[2]=car[2].getRent();
                    day[2]=time;
                    name[2]=car[2].getName();
                    price[2]=rent[2]*day[2];
                    break;
                case 4:
                    rent[3]=car[3].getRent();
                    day[3]=time;
                    name[3]=car[3].getName();
                    price[3]=rent[3]*day[3];
                    break;
                case 5:
                    rent[4]=car[4].getRent();
                    day[4]=time;
                    name[4]=car[4].getName();
                    price[4]=rent[4]*day[4];
                    break;
                case 6:
                    rent[5]=car[5].getRent();
                    day[5]=time;
                    name[5]=car[5].getName();
                    price[5]=rent[5]*day[5];
                    break;
                default:
                    break;
                }
                sumPrice+=price[NO-1];
            }//FOR
            System.out.println("你的帐单:");
            System.out.println("----------------------------------------------");
           // System.out.println("******可载人的车有:");
            System.out.println("车名\t租金(元/天)\t租车时间(天)\t租价(元)");
            for(int k=0;k<5;k++){
                if(name[k]==null)continue;
                System.out.println(name[k]+"\t"+rent[k]+"\t\t"+day[k]+"\t\t"+price[k]);
            }
            System.out.println("----------------------------------------------");
            System.out.println("总价:"+sumPrice);

}//MAIN
public int price(int[] rent,int[] day){

    return rent[0]*day[0]+rent[1]*day[1]+rent[2]*day[2]+rent[3]*day[3]+rent[4]*day[4]+rent[5]*day[5]; 
}

}

打开App,阅读手记
7人推荐
发表评论
随时随地看视频慕课网APP

热门评论

为什么我在main中定义car数组时报cannot be resolved to a variabler 错误?

查看全部评论