手记

java入门第二季达达租车系统代码

package immo.com;

public class Car {
private String name;
private int price;
private int passenageCapility;
private int goodCapility;
public Car(String name, int price) {
super();
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setPrice(int price) {
this.price = price;
}
public void setPassenageCapility(int passenageCapility) {
this.passenageCapility = passenageCapility;
}
public void setGoodCapility(int goodCapility) {
this.goodCapility = goodCapility;
}
public int getPrice() {
return price;
}
public int getPassenageCapility() {
return passenageCapility;
}
public int getGoodCapility() {
return goodCapility;
}
public void printinfo(){}
}
package immo.com;

public class Truck extends Car {
public Truck(String name, int price,int goodCapility) {
super(name, price);
setGoodCapility(goodCapility);
}
public void printinfo(){
System.out.println(getName()+'\t'+getPrice()+"\t"+"载货:"+getGoodCapility()+"吨");
}
}
package immo.com;

public class PassenageCar extends Car {
public PassenageCar(String name, int price,int passenageCapility) {
super(name, price);
setPassenageCapility(passenageCapility);
// TODO Auto-generated constructor stub
}
public void printinfo(){
System.out.println(getName()+'\t'+getPrice()+"\t"+"载人:"+getPassenageCapility()+"人");
}
}
package immo.com;

public class PickUp extends Car {
public PickUp(String name, int price,int passenageCapility,int goodCapility) {
super(name, price);
setPassenageCapility(passenageCapility);
setGoodCapility(goodCapility);
// TODO Auto-generated constructor stub
}
public void printinfo(){
System.out.println(getName()+'\t'+getPrice()
+"\t"+"载人:"+getPassenageCapility()+"人"+" 载货:"+getGoodCapility()+"吨");
}
}
package immo.com;
import java.util.Scanner;
public class RentCarSystem {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Car[] carsForRent={new PassenageCar("奥迪A4",500,4),new PassenageCar("马自达6",400,4),
        new PickUp("皮卡雪6",450,4,2),new PassenageCar("金龙",800,20),
        new Truck("松花江",400,4),new Truck("依维柯",1000,20)};
    System.out.println("欢迎使用达达租车系统:");
    System.out.println("您是否要租车:1是 0否");
    Scanner scan=new Scanner(System.in);
    String input=scan.next();
    if (input.equals("1")){
        System.out.println("您可租车的类型及价目表:");
        System.out.println("序号\t汽车名称\t租金\t容量");
        int i=1;
        for (Car currentCar:carsForRent){
            System.out.print(""+i+"\t");
            currentCar.printinfo();
            i++;
        }
        System.out.println("请您输入要租车的数量:");
        int number=scan.nextInt();
        String passenageCarlist="";
        String goodsCarlist="";
        int totalpassenageCapility=0;
        int totalGoodsCapility=0;
        int totalprice=0;
        int j=1;
        while(j<=number){
            System.out.printf("请您输入第%d辆车的序号:\n",j);
            int index=scan.nextInt();
            Car currentCar=carsForRent[index-1];
            if (currentCar instanceof PassenageCar){
                passenageCarlist+=(currentCar.getName()+"\t");
                totalpassenageCapility+=currentCar.getPassenageCapility();
                totalprice+=currentCar.getPrice();
            }else if (currentCar instanceof Truck){
                goodsCarlist+=(currentCar.getName()+"\t");
                totalGoodsCapility+=currentCar.getGoodCapility();
                totalprice+=currentCar.getPrice();
            }else{
                passenageCarlist+=(currentCar.getName()+"\t");
                totalpassenageCapility+=currentCar.getPassenageCapility();
                goodsCarlist+=(currentCar.getName()+"\t");
                totalGoodsCapility+=currentCar.getGoodCapility();
                totalprice+=currentCar.getPrice();
            }
            j++;
        }
        System.out.println("请您输入租车天数:");
        int daynumber=scan.nextInt();
        int totalmoney=totalprice*daynumber;
        System.out.println("您的账单:");
        System.out.println("***可载人的车有:");
        System.out.println(passenageCarlist+"共载人:"+totalpassenageCapility+"人");
        System.out.println("***可载货的车有:");
        System.out.println(goodsCarlist+"共载货:"+totalGoodsCapility+"吨");
        System.out.println("***租车总价格:"+totalmoney+"元");
        scan.close();
    }else{
        scan.close();
        System.exit(0);
    }
}

}

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