慕仙2242888
2018-11-14 17:27
//父类汽车
package com.spirit.test;
public class Car {
int number;//车辆类别
String carName;//车辆名称
int carMoney;//租金
int person;//载客量
double goods;//载货量
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getCarName() {
return carName;
}
public void setCarName(String carName) {
this.carName = carName;
}
public int getCarMoney() {
return carMoney;
}
public void setCarMoney(int carMoney) {
this.carMoney = carMoney;
}
public int getPerson() {
return person;
}
public void setPerson(int person) {
this.person = person;
}
public double getGoods() {
return goods;
}
public void setGoods(double goods) {
this.goods = goods;
}
public Car(int number,String carName,int carMoney,int person,double goods){
this.number = number;
this.carName = carName;
this.carMoney = carMoney;
this.person = person;
this.goods = goods;
}
public Car(int number,String carName,int carMoney,int person){
this.number = number;
this.carName = carName;
this.carMoney = carMoney;
this.person = person;
}
public Car(int number,String carName,int carMoney,double goods){
this.number = number;
this.carName = carName;
this.carMoney = carMoney;
this.goods = goods;
}
public void show(){
// System.out.println("车辆编号"+number+"--车辆名称"+carName+"--租金/天"+carMoney+"--载客量"+person+"--载货量"+goods);
}
}
//子类货车
package com.spirit.test;
public class Truck extends Car {
Truck(int number, String carName, int carMoney, double goods) {
super(number, carName, carMoney, goods);
}
public void show() {
System.out.println("车辆编号:" + number + "--车辆名称:" + carName + "--租金/天:"
+ carMoney + "--载货量:" + goods);
}
}
//子类皮卡
package com.spirit.test;
public class PickUP extends Car{
PickUP(int number, String carName, int carMoney,int person,double goods) {
super(number, carName, carMoney,person,goods);
}
public void show(){
System.out.println("车辆编号:"+number+"--车辆名称:"+carName+"--租金/天:"+carMoney+"--载客量:"+person+"--载货量:"+goods);
}
}
//子类客车
package com.spirit.test;
public class Bus extends Car{
Bus(int number, String carName, int carMoney,int person) {
super(number, carName, carMoney,person);
}
public void show(){
System.out.println("车辆编号:"+number+"--车辆名称:"+carName+"--租金/天:"+carMoney+"--载客量:"+person);
}
}
//
package com.spirit.test;
import java.util.Scanner;、
public class test {
static int payTotal = 0;
static int personTotal = 0;
static double goodsTotal = 0;
static String[] carryPersonCar;
static String[] carryGoodsCar;
static int personCar = 0;
static int goodsCar = 0;
static Car[] cars = { new Bus(1, "奥迪", 800, 4),
new Truck(2, "金龙", 1000, 800), new PickUP(3, "长安", 500, 2, 200) };
static int[] countCar = new int[cars.length];
static int[] countDays = new int[cars.length];
public static void leaseCar() {
System.out.println("请输入所租车编号、个数及天数");
Scanner buyCarMessage = new Scanner(System.in);
if (buyCarMessage.hasNextLine()) {
String[] buyCar = buyCarMessage.nextLine().split(" ");
for (int i = 0; i < cars.length; i++) {
int number = cars[i].getNumber();
int money = cars[i].getCarMoney();
int person = cars[i].getPerson();
double goods = cars[i].getGoods();
int buyNameNumber = Integer.parseInt(buyCar[0]);
int buyNumber = Integer.parseInt(buyCar[1]);
int buyDays = Integer.parseInt(buyCar[2]);
if (number == buyNameNumber) {
payTotal = payTotal + money * buyNumber * buyDays;
personTotal = personTotal + person * buyNumber * buyDays;
goodsTotal = goodsTotal + goods * buyNumber * buyDays;
countCar[number-1] = countCar[number-1]+buyNumber;
countDays[number-1] = countDays[number-1]+buyDays;
}
}
}
}
public static void ifCarryOn() {
for (;;) {
System.out.println("您是否还需要租用其他车辆?");
Scanner doMessage = new Scanner(System.in);
if (doMessage.hasNext()) {
String answerMessage = doMessage.next().toUpperCase();
if (answerMessage.equals("Y")) {
leaseCar();
} else {
break;
}
}
}
}
public static void main(String[] args) {
System.out.println("请问是否要租车?(Y/N)");
Scanner s = new Scanner(System.in);
if (s.hasNext()) {
String answer = s.next().toUpperCase();
// System.out.println(answer);
if (answer.equals("Y")) {
System.out.println("您可租用的汽车类型及价目表:");
for (int i = 0; i < cars.length; i++) {
cars[i].show();
}
leaseCar();
ifCarryOn();
System.out.println("您的账单:");
for(int i = 0;i<cars.length;i++){
System.out.println("****"+cars[i].getCarName()+" "+countCar[i]+"辆"+" "+countDays[i]+"天");
}
System.out.println("****"+"可载人:"+personTotal+"人");
System.out.println("****"+"可载货:"+goodsTotal+"t");
System.out.println("****"+"租车总价格为:"+payTotal+"¥");
}
}
}
}
我写了3个小时,写的比你的烂,但功能都实现了?
我感觉 private 修饰后的变量 封装才有效果
然后 没有然后了 抱歉 我一看代码就大脑空白了 只能看到这了 再看下去估计脑袋就炸了?
很好了,我都没写出来,只能看网上的答案照着敲一敲,没逻辑没思路好尴尬
哎呦 不错哦·
很不错了
我写了不止一个小时 啊~~~
这不是挺不错的吗= =我写了一小时
Java入门第二季 升级版
530562 学习 · 6091 问题
相似问题