java实现租车系统
程序员文章站
2024-02-22 12:33:16
今天用java编写了一个租车系统,过程中主要遇到的两个问题:
1、输出数组信息问题:
在得到cars[]数组后,要生成租车信息表,目前有两种思路:一是用循环输出;二是用...
今天用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" + "欢迎下次光临!------------------------"); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。