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

JAVA 入门第二季 结课小项目 达达租车系统

慕粉4345422
关注TA
已关注
手记 1
粉丝 0
获赞 2

/*

  • HH first project
  • Copyright kept
  • */
    package CarSystem;

import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
import java.util.*;

public class CarRentalSystem {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    //Loading cars 
    Car[] cars={new van(1, "Audi", 50, 5), new van(2, "Ford", 40, 5), 
                new van(3, "Nissan",40,4), new truck(4, "Uhual1", 40, 2.0f),
                new truck(5, "Uhual2",50,3.0f),new truck(6,"Uhual3",60, 4.0f),
                new pickup(7, "pickup1", 70, 1.0f,4), new pickup(8,"pickup2",80,2.0f,4)};

    int numOfCar=cars.length;

    //Show the welcome interface 
    System.out.println("Welcome to HH CarRental System."+"\n"
                       +"Do you want to choose cars today?"+"\n"
                       +"Press 1 for continue Or any key to close the window");
    Scanner scanner= new Scanner(System.in);
    String input=scanner.next();

    if(input.equals("1")==false){
        System.out.println("Thanks for using!");
    }else if(input.equals("1")){
        System.out.println("Avaible cars for today:");
        System.out.println("ID"+"\t\t"+"Brand"+"\t\t"+"Price"+"\t\t"+"capacity");

        for (int i=0;i<numOfCar;i++){
            cars[i].print();
        }

    // Choosing interface 
    List<Integer> RentalIndex= new ArrayList<>();   

    System.out.println("How many cars do you want to rent today?");
    int numForRent=Integer.parseInt(scanner.next());

    if(numForRent==0||numForRent>8){
        System.out.println("Incorrect input, please try again");

    }else {
        for(int i=1; i<=numForRent; i++){
            System.out.println("please enter the ID of No." +i+ " choise");
            int in=Integer.parseInt(scanner.next());
            RentalIndex.add(in);
        }
        Collections.sort(RentalIndex);
        System.out.println("you want car No." + RentalIndex);
    }

    System.out.println("How many days do you need ?");
    int daysForRent=Integer.parseInt(scanner.next()); 
    // Calculate price 

    float priceTotal=0;
    float Weight=0;
    int   numOfPeople=0;
    int   array=0;

    for (int i=0; i<numForRent;i++){

        array=RentalIndex.get(i);
        priceTotal+=cars[array-1].price;

        Weight+=cars[array-1].Weight_cargo;

        numOfPeople+=cars[array-1].Num_people;
    }
    priceTotal=priceTotal*daysForRent;

    System.out.println("Your cars could loading "+Weight+" T Cargos in total!");
    System.out.println("Your cars could have "+numOfPeople+" persons!");
    System.out.println("Your total price is "+priceTotal+"USD! \n"+
            "Thanks for using!");

    }

}
}

package CarSystem;

public class Car {
int id;
String name;
float price;
float Weight_cargo;
int Num_people;

public void print(){}

}

package CarSystem;

public class truck extends Car {
//contructor
public truck(int id, String name, float price, float Weight_cargo){
this.id=id;
this.name=name;
this.price=price;
this.Weight_cargo=Weight_cargo;

}

public void print(){
System.out.println(id+"\t\t"+name+"\t\t"+price+"USD"+"\t\t"+Weight_cargo+"T Cargos");

}

}
package CarSystem;

public class van extends Car {
public van(int id, String name, float price, int Num_people){
this.id=id;
this.name=name;
this.price=price;
this.Num_people=Num_people;

}

public void print(){

System.out.println(id+"\t\t"+name+"\t\t"+price+"USD"+"\t\t"+Num_people+"Persons");

}

}

package CarSystem;

public class pickup extends Car{
public pickup(int id, String name, float price, float Weight_cargo,int Num_people){
this.id=id;
this.name=name;
this.price=price;
this.Num_people=Num_people;
this.Weight_cargo=Weight_cargo;

}

public void print(){

System.out.println(id+"\t\t"+name+"\t\t"+price+"USD"+"\t\t"+Num_people+
                 "Persons"+" and "+Weight_cargo+"T Cargos");

}

}

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