栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

    // 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 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
    

    }
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/233747.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号