手记

Java入门第二季6-1租车项目

1.Car类
package imooc;

public class Car {
public int ID;
public String nameString;
public int rent;
public int personCapacity;
public int goodCapacity;
}

2.可载人汽车类
package imooc;

public class peopleCar extends Car{
public peopleCar(int newID, String newName, int newRent,
int newPersonCapacity) {
this.ID = newID;
this.nameString = newName;
this.rent = newRent;
this.personCapacity = newPersonCapacity;
}

}

3.可载物汽车类
package imooc;

public class Trunk extends Car{
public Trunk(int newID, String newName, int newRent,
int newGoodCapacity) {
this.ID = newID;
this.nameString = newName;
this.rent = newRent;
this.goodCapacity = newGoodCapacity;
}

}

4.皮卡,既可载人又可载物类
package imooc;

public class PikaCar extends Car {
public PikaCar(int newID,String newName,int newRent,int newPoepleCapacity,int newGoodCapacity){
this.ID=newID;
this.nameString=newName;
this.personCapacity=newPoepleCapacity;
this.rent=newRent;
this.goodCapacity=newGoodCapacity;
}
}

5.Main函数
package imooc;

import java.security.PrivateKey;
import java.util.*;

public class Main {
public static void main(String[] args) {
Car[] allCars = { new peopleCar(1, "奥迪A4", 500, 4),
new peopleCar(2, "马自达6", 400, 4),
new PikaCar(3, "皮卡雪6", 450, 4, 2),
new peopleCar(4, "金龙", 800, 20), new Trunk(5, "松花江", 400, 4),
new Trunk(6, "依维柯", 1000, 20) };
System.out.println("欢迎使用达达租车系统");
System.out.println("您是否要租车“1是0否");
Scanner inputScanner = new Scanner(System.in);// 指定输入设备为System.in
int Acess = inputScanner.nextInt();// 接收用户输入的信息并保存
System.out.println("您输入的是:" + inputScanner + "和" + Acess);
if (Acess == 1) {
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号 汽车名称 租金 容量");
for (int i = 0; i < allCars.length; i++) {
if (allCars[i] instanceof peopleCar) {
System.out.println(allCars[i].ID + " "

  • allCars[i].nameString + " " + allCars[i].rent
  • " " + allCars[i].personCapacity);
    } else if (allCars[i] instanceof Trunk) {
    System.out.println(allCars[i].ID + " "
  • allCars[i].nameString + " " + allCars[i].rent
  • " " + allCars[i].goodCapacity);
    } else if (allCars[i] instanceof PikaCar) {
    System.out.println(allCars[i].ID + " "
  • allCars[i].nameString + " " + allCars[i].rent
  • " " + allCars[i].personCapacity + " "
  • allCars[i].goodCapacity);
    }

        }
    }
    System.out.println("请输入您要租汽车的数量:");
    Scanner input = new Scanner(System.in);// 指定输入设备为System.in
    int num = input.nextInt();// 接收用户输入的信息并保存
    int sum = 0;
     Car[] newlistCars=new Car[6];
    for (int i = 0; i < num; i++) {
        System.out.println("请输入第" + (i + 1) + "辆车的序号:");
        Scanner inputID = new Scanner(System.in);
        int CarID = inputID.nextInt();
        for(int a=0;a<allCars.length;a++){
            if(CarID==allCars[a].ID){
                newlistCars[i]=new Car();
                newlistCars[i]=allCars[a];

    // newlistCars[i].ID=allCars[a].ID;
    // newlistCars[i].nameString=allCars[a].nameString;
    // newlistCars[i].rent=allCars[a].rent;
    // newlistCars[i].personCapacity=allCars[a].personCapacity;
    // newlistCars[i].goodCapacity=allCars[a].goodCapacity;
    sum=sum+newlistCars[i].rent;
    }
    }
    }
    System.out.println("请输入租车天数:");
    Scanner inputRent = new Scanner(System.in);
    int rentDay = inputRent.nextInt();
    System.out.println("您的账单:");
    System.out.println("可载人的车:");
    String[] peoplecarname=new String[6];
    int personNum=0;
    int capacity=0;
    int a=0;int b=0;
    String[] goodcarname=new String[6];
    for(int i=0;i<newlistCars.length;i++){
    if(newlistCars[i] instanceof peopleCar||newlistCars[i] instanceof PikaCar){
    peoplecarname[a]=newlistCars[i].nameString;
    personNum=personNum+newlistCars[i].personCapacity;

            if(peoplecarname[a]!=null){
                System.out.print(peoplecarname[a]+"  ");
            }
            a=a+1;
        }
    
    }
    System.out.println("共可载人:"+personNum+"人");
    System.out.println("可载物的车:");
    for(int i=0;i<newlistCars.length;i++){
    
        if(newlistCars[i] instanceof Trunk||newlistCars[i] instanceof PikaCar){
            goodcarname[b]=newlistCars[i].nameString;
            capacity=personNum+newlistCars[i].goodCapacity;
            if(goodcarname[b]!=null){
                System.out.print(goodcarname[b]+"  ");
            }
            b=b+1;
        }
    }
    System.out.println("共可载物:"+capacity+"吨");
    
    System.out.println("租车总价格:" + (sum * rentDay) + "元");

    }
    }

14人推荐
随时随地看视频
慕课网APP

热门评论

你导入的包错的,Java.Api手册没有

查看全部评论