手记

滴滴出租的系统代码记录

package daDaTaxi;
import java.util.Scanner;
public class RentalSystem {
    Car[] carsForRent={new Audi("Audi-A4",500,4),new Mazda("Mazda",400,4),
            new PickUp("PickUpx",450,4,2.0),new Trunk("Trunk",400,3.0)};
    public void rentalList(int num){    
    switch(num){
    case 1:
        System.out.println("Car rental type and price list:");
        System.out.println("Num\tCarname\t\trent\t\tcapacity");
        int i=1;
        for(Car currentCar: carsForRent){
            if(currentCar instanceof Audi){
                System.out.println(i+"\t"+currentCar.getName()+"\t\t"+currentCar.getRent()+"$/d\t"+currentCar.getPeopleCapacity()+"/p");                    
            }
            if(currentCar instanceof Mazda){
                System.out.println(i+"\t"+currentCar.getName()+"\t\t"+currentCar.getRent()+"$/d\t"+currentCar.getPeopleCapacity()+"/p");                    
            }
            if(currentCar instanceof PickUp){
                System.out.println(i+"\t"+currentCar.getName()+"\t\t"+currentCar.getRent()+"$/d\t"+currentCar.getPeopleCapacity()+"/p and"+currentCar.getCargoCapacity()+"/ton");                   
            }
            if(currentCar instanceof Trunk){
                System.out.println(i+"\t"+currentCar.getName()+"\t\t"+currentCar.getRent()+"$/d\t"+currentCar.getCargoCapacity()+"/ton");                   
            }
            i++;
        }
        break;
    case 2:System.out.println("Thanks your usage!");break;
    default:System.out.println("error!");;
    }
    }
    @SuppressWarnings({ "resource" })
    public int[] carRentalType(int num){
        int[] cars=new int[num];
        for(int i=1;i<=num;i++){
            System.out.print("Please input "+i+"th car typical number:\t");         
            Scanner in = new Scanner(System.in);            
            cars[i-1]=in.nextInt();
            System.out.println();
        }
        return cars;
    }
    public void carRentalCost(double day, int num,int[] carNumType ){
        int peopleCapacity=0;
        double cargoCapacity=0.;
        double money=0;
        for(int i=0;i<num;i++){
            if (carNumType[i]==1){
                peopleCapacity=carsForRent[0].getPeopleCapacity()+peopleCapacity;
                money=carsForRent[0].getRent()*day+money;
            }
            if (carNumType[i]==2){
                peopleCapacity=carsForRent[1].getPeopleCapacity()+peopleCapacity;
                money=carsForRent[1].getRent()*day+money;
            }
            if (carNumType[i]==3){
                peopleCapacity=carsForRent[2].getPeopleCapacity()+peopleCapacity;
                cargoCapacity=carsForRent[2].getCargoCapacity()+cargoCapacity;
                money=carsForRent[2].getRent()*day+money;
            }
            if (carNumType[i]==4){
                peopleCapacity=carsForRent[3].getPeopleCapacity()+peopleCapacity;
                money=carsForRent[3].getRent()*day+money;
            }
        }
        System.out.println("The total number of vehicles that can be done:\t"+peopleCapacity+"/p");
        System.out.println("Total vehicle load:\t"+cargoCapacity+"/ton");
        System.out.println("ToTal costs:\t"+money+"$"); 
    }
}

package daDaTaxi;
import java.util.Scanner;
public class ChoseTaxi {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        RentalSystem rcar=new RentalSystem();
        System.out.println("Welcome to use DaDa-Texi system!");
        System.out.println("Wether you want to rent cars: 1.Yes   2.No");
        Scanner in1 = new Scanner(System.in);       
        rcar.rentalList(in1.nextInt()); 
        System.out.print("Please input the rental number:\t");      
        Scanner in2 = new Scanner(System.in);
        int num=in2.nextInt();
        System.out.println();
        int[] carNumType=rcar.carRentalType(num);
        System.out.print("Please input the days of using car:\t");      
        Scanner day = new Scanner(System.in);
        rcar.carRentalCost(day.nextInt(), num,carNumType);
    }
}

package daDaTaxi;

public  class Car {
 String name;
 double rent;
 int peopleCapacity;
 double cargoCapacity;
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public double getRent() {
    return rent;
}
public void setRent(double rent) {
    this.rent = rent;
}
public int getPeopleCapacity() {
    return peopleCapacity;
}
public void setPeopleCapacity(int peopleCapacity) {
    this.peopleCapacity = peopleCapacity;
}
public double getCargoCapacity() {
    return cargoCapacity;
}
public void setCargoCapacity(double cargoCapacity) {
    this.cargoCapacity = cargoCapacity;
}

}

package daDaTaxi;

public class Audi extends Car {
    public  Audi(String name, double rent,int peopleCapacity) {
        this.name=name;
        this.rent=rent;
        this.peopleCapacity=peopleCapacity;
    }
}

package daDaTaxi;

public class Mazda extends Car {
    public  Mazda(String name, double rent,int peopleCapacity) {
        this.name=name;
        this.rent=rent;
        this.peopleCapacity=peopleCapacity;
    }
}

package daDaTaxi;

public class PickUp extends Car {
    public  PickUp(String name, double rent,int peopleCapacity,double cargoCapacity) {
        this.name=name;
        this.rent=rent;
        this.peopleCapacity=peopleCapacity;
        this.cargoCapacity=cargoCapacity;
    }
}

package daDaTaxi;

public class Trunk extends Car {
    public  Trunk(String name, double rent,double cargoCapacity) {
        this.name=name;
        this.rent=rent;
        this.cargoCapacity=cargoCapacity;
    }

}

```输入代码
1人推荐
随时随地看视频
慕课网APP