java实现租车系统 java实现租车系统
chao2016 人气:0今天用JAVA编写了一个租车系统,过程中主要遇到的两个问题:
1、输出数组信息问题:
在得到cars[]数组后,要生成租车信息表,目前有两种思路:一是用循环输出;二是用Arrays.toString()输出数组信息。
用toString()方法输出数组输出……@……形式的哈希码地址,这里需要对toString()方法进行重写,在数组涉及到的类中进行重写。
不过用第二种方法输出的其实还是一个数组,形式如图所示。那么问题来了——还有没有更好的输出方法呢?
2、父类方法不能访问子类成员变量:
本来在父类Car中写好的getPersonCapacity()和getGoodCapacity()方法似乎不能访问子类中的personCapacity和goodCapacity 这两个成员变量,导致调用参数时始终为0;所以在各子类方法中又独立加上了前面两个方法,问题得以解决。
运行效果图:
代码如下:
package rentCarSys; /* * 总共有三种车型:载人Auto,载货Van,载人载货Pickup * Car 为这三种车型的父类 * 有4种属性: * 编号 = number * 品牌 = brand * 租金/天 = fee * 载人容量 = personCapacity * 载货容量 = goodCapacity */ public class Car { int number; String brand; double fee; int personCapacity; double goodCapacity; public Car(int number, String brand, double fee){ //构造方法 this.number = number; this.brand = brand; this.fee = fee; } public int getNumber(){ return number; } public String getBrand(){ return brand; } public double getFee(){ return fee; } public int getPersonCapacity(){ return personCapacity; } public double getGoodCapacity(){ return goodCapacity; } }
package rentCarSys; /* * Auto为载人汽车,除了Car中的属性之外还有载人容量 personCapacity */ public class Auto extends Car{ private int personCapacity; public Auto(int number, String brand, double fee, int personCapacity) { super(number, brand, fee); this.personCapacity = personCapacity; } public int getPersonCapacity() { return personCapacity; } @Override public String toString() { return number + "\t" + brand + "\t" + fee + "元/天\t" + personCapacity + "人\n"; } }
package rentCarSys; /* * Van为载货汽车,除了Car中的属性之外还有载货容量 goodCapacity */ public class Van extends Car{ private double goodCapacity; public Van(int number, String brand, double fee, double goodCapacity) { super(number, brand, fee); this.goodCapacity = goodCapacity; } public double getGoodCapacity(){ return goodCapacity; } public String toString() { return number + "\t" + brand + "\t" + fee + "元/天\t" + goodCapacity + "吨" + "\n"; } }
package rentCarSys; /* * Pickup为载人载货汽车,除了Car中的属性之外还有载人容量 personCapacity,载货容量goodCapacity */ public class Pickup extends Car{ private int personCapacity; private double goodCapacity; public Pickup(int number, String brand, double fee, int personCapacity, double goodCapacity) { super(number, brand, fee); this.personCapacity = personCapacity; this.goodCapacity = goodCapacity; } public int getPersonCapacity() { return personCapacity; } public double getGoodCapacity(){ return goodCapacity; } @Override public String toString() { return number + "\t" + brand + "\t" + fee + "元/天\t" + personCapacity + "人\t" + goodCapacity + "吨\n"; } }
package rentCarSys; import java.util.Arrays; import java.util.Scanner; public class Login { public static void main(String[] args){ Scanner input = new Scanner(System.in); Car[] cars = new Car[6]; System.out.print("欢迎使用答答租车系统:"); System.out.print("您是否要租车?1、是 2、否(请输入1或2)"); int input1 = input.nextInt(); if (input1 == 1){ System.out.println("下面是所有车的信息:"); cars[0] = new Auto(1, "奥迪A4", 500.0, 4); cars[1] = new Auto(2, "马自达6", 400.0, 4); cars[2] = new Pickup(3, "皮卡雪6", 450.0, 4, 2); cars[3] = new Auto(4, "金龙", 800.0, 20); cars[4] = new Van(5, "松花江", 400.0, 4); cars[5] = new Van(6, "依维柯", 1000.0, 20); System.out.println("序号\t" + "汽车名称\t" + "租金\t\t" + "容量(载人/载货)"); System.out.println(Arrays.toString(cars)); // for(int i = 0; i < cars.length; i++){ // System.out.println("编号:"+ (i+1) +" 品牌:"+ cars[i].getBrand() // +" 租金:"+ cars[i].getFee() +"/天 载客量:"+ cars[i].getPersonCapacity()+"人" // +" 载货量:"+ cars[i].getGoodCapacity()+"吨" ); // } }else{ System.out.println("谢谢使用,再见!"); } System.out.print("请输入你要租几种车:"); int rentNum = input.nextInt(); //selected用来保存客户选中了什么车型,以及每种车型的辆数,与car数组是对应关系 int[] selected = new int[6]; for (int i = 1; i <= rentNum; i++){ System.out.println("请输入第" + i + "种车型的序号:" ); int nums = input.nextInt() - 1; System.out.println(cars[nums].getBrand() +"总共需要多少辆:"); int num = input.nextInt(); selected[nums] = num; } System.out.println("请输入租车天数:"); int daysNum = input.nextInt(); System.out.println("您的账单:--------------------------"); double total = 0; for (int i = 0; i < cars.length; i++){ if (selected[i] !=0 ){ System.out.println(selected[i] + "辆" + cars[i].getBrand() + " 总共载客量:"+selected[i]*cars[i].getPersonCapacity()+"人"+ " 总共载货量:"+selected[i]*cars[i].getGoodCapacity()+"吨"+ " "+daysNum+"天单项费用:"+selected[i]*cars[i].getFee()*daysNum+"元"); total += selected[i]*cars[i].getFee()*daysNum; } } System.out.println("租车总费用:" + total + "元" + "\n" + "欢迎下次光临!------------------------"); } }
加载全部内容