ITPub博客

首页 > IT职业 > IT生活 > 达达租车测试类,不足之处请指点

达达租车测试类,不足之处请指点

IT生活 作者:lslzf 时间:2021-09-09 08:57:02 0 删除 编辑


package test;
import java.util.Arrays;

public class Cars {
private String name;//汽车名
private int price;//价钱
private int peopleTotal;//载人量
private int cargo;//载货量
private String type;
public void msg(int i){}
public String[] getTypes() {
String[] types = {"passenger", "truck", "cargo"};
return types;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}

public String getName(){
return this.name;
}

public void setName(String name){
this.name = name;
}

public int getPrice(){
return this.price;
}

public void setPrice(int price){
this.price = price;
}

public int getPeopleTotal(){
return this.peopleTotal;
}

public void setPeopleTotal(int peopleTotal){
this.peopleTotal = peopleTotal;
}

public int getCargo(){
return this.cargo;
}

public void setCargo(int cargo){
this.cargo = cargo;
}
}


package test;


public class passengerCar extends Cars{

public passengerCar(String name, int price, int peopleTotal) {
this.setName(name);
this.setPeopleTotal(peopleTotal);
this.setPrice(price);
this.setType("passenger");
}

public void msg(int i) {
String m = i+"t"+this.getName()+"t"+this.getPrice()+"元/天"+"tt"+"载人"+this.getPeopleTotal()+"个";
System.out.println(m);
} }


package test;


public class Truck extends Cars{
public Truck(String name, int price, int cargo) {
this.setName(name);
this.setCargo(cargo);
this.setPrice(price);
this.setType("truck");
}

public void msg(int i) {
String m = i+"t"+this.getName()+"t"+this.getPrice()+"元/天"+"tt"+"载货"+this.getCargo()+"kg";
System.out.println(m);
}
}


package test;


public class Cargo extends Cars{
public Cargo(String name, int price, int peopleTotal, int cargo) {
this.setName(name);
this.setPeopleTotal(peopleTotal);
this.setPrice(price);
this.setCargo(cargo);
this.setType("cargo");
}

public void msg(int i) {
String m = i+"t"+this.getName()+"t"+this.getPrice()+"元/天"+"tt"+"载人"+this.getPeopleTotal()+"个"+"t"+"载货"+this.getCargo()+"kg";
System.out.println(m);
}
}


package test;
import java.util.Scanner;
/**

  • @author Administrator
    /
    public class CarTest {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    Cars[] car = {
    new passengerCar("奥迪A6L", 500, 4),
    new passengerCar("马自达6", 400, 4),
    new Truck("松花江", 400, 4),
    new Truck("依维柯", 800, 20),
    new Cargo("皮卡雪6", 500, 4,2),
    new Cargo("TSL-6", 800, 3,3),
    };
    System.out.println("你是否要租车: 1是 0否");
    int p = sc.nextInt();
    if(p == 1) {
    System.out.println("你可租车的类型和租金");
    System.out.println("序号"+"t"+"汽车名称"+"t"+" 租金"+"tt"+"容量");
    for(int i = 0;i car[i].msg((i+1));
    }
    //选择车的数量
    System.out.println("请输入你要租车的数量:");
    int carCount = sc.nextInt();
    if(carCount System.out.println("数量不能小于0");
    }
    int[] carArray = new int[carCount];
    //记录选择的车
    for(int i = 0;i System.out.println("请输入你要租用的车辆序号:");
    int carNum = sc.nextInt();
    carArray[i] = carNum;
    }
    System.out.println("请输入你要租的天数:");
    int day = sc.nextInt();
    //找出选择车的信息, 统计
    System.out.println("你的账单:");
    System.out.println("选择载车的有:");
    int r = 0;
    int h = 0;
    int sum = 0;
    for(int i=0;i String type = car[carArray[i]-1].getType();
    if("passenger".equals(type) "cargo".equals(type)) {
    System.out.println(car[carArray[i]-1].getName());
    r += car[carArray[i]-1].getPeopleTotal();
    }
    sum += car[carArray[i]-1].getPrice()
    day;
    }
    System.out.println("总载客量:"+r);
    System.out.println("选择载货的有:");
    for(int i=0;i String type = car[carArray[i]-1].getType();
    if("truck".equals(type) "cargo".equals(type)) {
    System.out.println(car[carArray[i]-1].getName());
    h += car[carArray[i]-1].getCargo();
    }
    }
    System.out.println("总载货量:"+h);
    System.out.println("租车总价格:"+sum);
    }else{
    System.out.println("欢迎再次租车!");
    }
    }
    }
  • 来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1070/viewspace-2798027/ ,如需转载,请注明出处,否则将追究法律责任。

    请登录后发表评论 登录
    全部评论

    注册时间:2019-04-15

    • 博文量
      102
    • 访问量
      882