为了账号安全,请及时绑定邮箱和手机立即绑定

达达租车系统代码分享

标签:
Java

父类
public class Car {
private String name;
private int passengers;
private int goods;
private int price;
public Car(){
}
public Car(String name,int passengers,int goods,int price){
this.setName(name);
this.setPassengers(passengers);
this.setGoods(goods);
this.setPrice(price);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPassengers() {
return passengers;
}
public void setPassengers(int passengers) {
this.passengers = passengers;
}
public int getGoods() {
return goods;
}
public void setGoods(int goods) {
this.goods = goods;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public void Display(){
System.out.println(name+"\t "+passengers+"\t "+goods+"\t "+price);
}
}

子类:Auto, PickUp,Limo,Trunk,Hammer
public class Auto extends Car {
public Auto(String name,int passengers,int goods,int price){
super(name,passengers,goods,price);
}
public void Display(){
System.out.println("I am Auto Car!");
}
}

主程序
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;

public class dadaRent {

private static Scanner input;
public static List<Car> allCar;
public static List<Car> rentCar;
public dadaRent(){
    dadaRent.allCar = new ArrayList<Car>();
    dadaRent.rentCar = new ArrayList<Car>();
}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    dadaRent ddr = new dadaRent();
    boolean flag = true;
    System.out.println("Welcome to DaDaRent !");        
    while(flag)
    {           
        System.out.println("Do you want to Rent Cars or Leave? Yes OR No ?");
        input = new Scanner(System.in);
        String choose = input.next();
        if(choose.equals("no"))     
        {   
            System.out.println("谢谢光临!");
            flag = false;
            break;
        }
        if(choose.equals("yes"))
        {
            try{                
                 System.out.println("Here are the Car List:");
                 ddr.AddCars();
                 ddr.DisplayCars(allCar);
                 System.out.println("How many cars do you want to rent?");
                 int num =input.nextInt();
                 int [] rentNum = new int[num];
                 for(int i=0;i<num;i++)
                 {
                    System.out.println("请输入第"+(i+1)+"辆车编号:");
                    int j = input.nextInt();
                    rentNum[i] =j-1;                
                 }
                 System.out.println("下面是你选择的车辆:");
                 ddr.AddRent(rentNum);
                 ddr.DisplayCars(rentCar);
                 for(Car obj:rentCar)
                 {  
                        obj.Display();
                 }
                 System.out.println("Please input rent days:");
                 int rentDay  =input.nextInt();
                 System.out.println("Your tocal Price is: "+ddr.TotoalPrice(rentDay));                   
              }
                catch(InputMismatchException e)
                {
                   e.printStackTrace(); 
                }
        }
        else
            System.out.println("Input Error! Try Again!");              
    }

}

public void AddCars(){
    Car [] carArray= {new PickUp("皮卡",4,20,450),new Auto("奥迪",4,19,500),new Limo("劳斯莱斯",6,25,2000),
            new Trunk("卡车",2,30,200),new Hammer("悍马",2,40,1000)};
    allCar.addAll(Arrays.asList(carArray));
}

public void AddRent(int []rentNum){     
    for(int i=0;i<rentNum.length;i++){  
        rentCar.add(allCar.get(rentNum[i]));
    }
}

public static void DisplayCars(List<Car> cars){
    System.out.println("编号\t车型\t    载客\t    载货/tons\t租金RMB/day\t");
    for(int i=0;i<cars.size();i++)
    {
        System.out.println(" "+(i+1)+"\t"+cars.get(i).getName()+"\t     "+cars.get(i).getPassengers()+
                "\t       "+cars.get(i).getGoods()+"\t     "+cars.get(i).getPrice());
    }
}

public int TotoalPrice(int rentDay){
    int priceSum =0;
    for(int i=0;i<rentCar.size();i++){
        priceSum = priceSum+rentCar.get(i).getPrice();
    }
    return priceSum*rentDay;
}

}

点击查看更多内容
4人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消