查看: 1504|回复: 0

[Java学习] 嗒嗒坐车系统 一个父类car三个子类bus,truck,pickup和测试类test

发表于 2018-2-2 08:00:02

//父类car
package rentsystem;

public class car {
//首先定义父类的属性:车类的 名称 和 租金。
public String carname;
public double carrent;

}
//子类bus
package rentsystem;

public class Bus extends car {
//定义客车的私有载客量 ,所以要用private修饰
private int personload;
//有参的构造方法定义Bus的三个属性:名称,载客量,每天的租金;
public Bus(String carname, int personload, double carrent) {
this.carname = carname;
this.personload = personload;
this.carrent = carrent;
}
//
public int getPersonload() {
return personload;
}
//用toString方法,将输出的值转化成字符串
public String toString() {
return carname + "\t" + carrent + "元/天\t" + personload + "人\t";
}
}
//子类truck
package rentsystem;

public class Truck extends car {
//定义货车的私有载货量,用 private修饰符
private float goodload;
//有参的构造方法定义,truck的三个属性:名称,载货量,每天的租金。
public Truck(String carname,int goodload,double carrent){
this.carname=carname;
this.goodload=goodload;
this.carrent=carrent;
}
public float getgoodload(){
return goodload;
}
//用toString方法,将输出的值转换成字符串输出。
public String toString(){
return carname+"\t"+carrent+"元/天\t"+goodload+"頓\t";
}
}
//子类pickup
package rentsystem;

public class pickup extends car {
//定义皮卡的私有属性载客量和载货量
private int personload;
private float goodload;
//有参构造方法定义pickup的四个属性:名称,载客量,载货量,每天的租金;
public pickup(String carname, int personload, float goodload, double carrent) {
this.carname = carname;
this.personload = personload;
this.goodload = goodload;
this.carrent = carrent;
}

  1. public int getpersonload() {
  2. return personload;
  3. }
  4. public float getgoodload() {
  5. return goodload;
  6. }
  7. //用toString方法将输出的值转换成字符串输出
  8. public String toString(){
  9. return carname+"\t"+carrent+"元/天\t"+personload+"人\t"+goodload+"吨\t";
  10. }
复制代码

}
//测试类test
package rentsystem;

import java.util.Scanner;

public class test {

  1. public static void main(String[] args) {
  2. // TODO Auto-generated method stub
  3. car[] cars = { new Bus("奧迪A4", 4, 500), new Bus("马自达6", 4, 400), new pickup("皮卡雪", 4, 2, 450),
  4. new Bus("金龍", 20, 800), new Truck("松花江", 4, 400), new Truck("依維柯", 20, 1000) };
  5. Scanner input = new Scanner(System.in);
  6. System.out.println("*****歡迎您使用嗒嗒租車系統******");
  7. System.out.println("請問您是否租車");
  8. System.out.println("是:輸入1 否:輸入任意鍵退出");
  9. if (input.nextInt() == 1) {
  10. System.out.println("您可租車的類型以及價目表");
  11. System.out.println("序号\t汽车名称\t租金\t容量\t");
  12. for (int i = 0; i < cars.length; i++) {
  13. System.out.println((i + 1) + ("\t") + cars[i]);
  14. }
  15. System.out.println("请输入您想要租汽车的数量");
  16. int carnum = input.nextInt();
  17. // 创建一新的数组carTotal,用来保存客户所选的车型,数组类型为Car
  18. car[] carTotal = new car[carnum];
  19. for (int j = 1; j <= carnum; j++) {
  20. System.out.println("请输入第" + j + "辆车的序号");
  21. int num = input.nextInt();
  22. // 将用户所选的车的序号对应的车型放入数组carTotal中
  23. carTotal[j - 1] = cars[num - 1];
  24. }
  25. System.out.println("请输入租车天数:");
  26. int carDay = input.nextInt();
  27. System.out.println("您的账单:");
  28. // 调用方法-总载客数
  29. totalperson(carTotal);
  30. // 调用方法-总载货量
  31. totalgood(carTotal);
  32. // 调用方法-总价格
  33. totalcarrent(carTotal, carDay);
  34. } else {
  35. System.out.println("输入错误,退出");
  36. }
  37. // 关闭输入函数
  38. input.close();
  39. }
  40. public static void totalperson(car[] carTotal) {// 总载客量方法
  41. int k = 0;// 定义总载客量 k
  42. System.out.println("*****可载人的车辆有:");
  43. for (int i = 0; i < carTotal.length; i++) {
  44. // 确认值的类型是否为Bus类型
  45. if (carTotal[i] instanceof Bus) {
  46. // 如果是Bus类型,则获取数组中对应的值
  47. k += ((Bus) carTotal[i]).getPersonload();
  48. System.out.println(((Bus) carTotal[i]).carname + "\t");
  49. } else if (carTotal[i] instanceof pickup) {
  50. k += ((pickup) carTotal[i]).getpersonload();
  51. System.out.println(((pickup) carTotal[i]).carname + "\t");
  52. }
  53. }
  54. System.out.println("共载客数:" + k + "人");
  55. }
  56. public static void totalgood(car[] carTotal) {// 总载货量方法
  57. float m = 0f;
  58. System.out.println("*****可载货的车有:");
  59. for (int i = 0; i < carTotal.length; i++) {
  60. if (carTotal[i] instanceof Truck) {
  61. m += ((Truck) carTotal[i]).getgoodload();
  62. System.out.println(((Truck) carTotal[i]).carname + "\t");
  63. } else if (carTotal[i] instanceof pickup) {
  64. m += ((pickup) carTotal[i]).getgoodload();
  65. System.out.println(((pickup) carTotal[i]).carname + "\t");
  66. }
  67. }
  68. System.out.println("共载货车有:" + m + "辆");
  69. }
  70. public static void totalcarrent(car[] carTotal, int carDay) {// 总费用
  71. double n = 0;// 定义总费用 n
  72. for (int i = 0; i < carTotal.length; i++) {
  73. n += carTotal[i].carrent;
  74. }
  75. n *= carDay;
  76. System.out.println("租车的总费用为:" + n);
  77. }
复制代码

}



回复

使用道具 举报