一开始看到不止的题目没有头绪,参照了前辈们的答案才做出来,修修改改了好久总算达成了跟课程老师的题目差不多的要求,和自己加的一些交互代码。
帮小弟提下意见或者建议 谢谢啦
package com.imooc; /*车 父类*/ public class Car { protected String carName; //车名 protected int money; //价格 protected int seatNum; //座位数 protected double weigthNum; //载货量 直接用了double类型 private double toMoney=0; //总价格 private int toSeat=0; //总座位 private int toWeigth=0; //总载重 private int day=1; //租车天数 public Car(String newCarName,int newMoney,int newSeatNum,double NewWeigthNum) { this.carName=newCarName; this.money=newMoney; this.seatNum=newSeatNum; this.weigthNum=NewWeigthNum; } public Car() { } public String getCarName() { return carName; } public int getMoney() { return money; } public int getSeatNum() { return seatNum; } public double getWeigthNum() { return weigthNum; } public double getToMoney() { return toMoney; } public int getToSeat() { return toSeat; } public int getToWeigth() { return toWeigth; } public int getDay() { return day; } public void show() { if(weigthNum<=0) { System.out.println("\t"+carName+"\t"+money+"元/天\t载人:"+seatNum+"人"); } else if(seatNum<=0) { System.out.println("\t"+carName+"\t"+money+"元/天\t载货:"+weigthNum+"吨"); }else { System.out.println("\t"+carName+"\t"+money+"元/天\t载人:"+seatNum+"人 载货:"+weigthNum+"吨"); } } }
package com.imooc; /*客车 子类*/ public class PassCar extends Car { public PassCar(String newCarName,int newMoney,int newSeatNum){ super.carName=newCarName; super.money=newMoney; super.seatNum=newSeatNum; } }
package com.imooc; /*皮卡车 子类*/ public class PickupCar extends Car { public PickupCar(String newCarName,int newMoney,int newSeatNum,double weigthNum){ super.carName=newCarName; super.money=newMoney; super.seatNum=newSeatNum; super.weigthNum=weigthNum; } }
package com.imooc; /*货车 子类*/ public class TruckCar extends Car { public TruckCar(String newCarName,int newMoney,double weigthNum){ super.carName=newCarName; super.money=newMoney; super.weigthNum=weigthNum; } }
package com.imooc;
import java.util.*;
/*运行类*/
public class Initail {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("***欢迎使用答答租车系统***");
System.out.println("您是否要租车?输入数字\t1是,2否"); //判断是否要租车
Car carVa=new Car();
int toSeat=carVa.getToSeat(); //定义车总座位数
double toWeigth=carVa.getWeigthNum(); //定义值车总载重数
double toMoney=carVa.getMoney(); //定义总价格
int day=carVa.getDay();
int carType=0; //定义选择的车序号
int maxC=999; //定义最大租车数量
for(int tf=3;tf>=0;tf--) {
int confirmInt=input.nextInt(); //输入数字判断是否要租车
if(confirmInt==1) {
tf=-1; //结束外层租车需求循环判断 18行
Car car[]={
new PassCar("奥迪A4",500,4),
new PassCar("马自达6",400,4),
new PassCar("金龙",800,20),
new PickupCar("皮卡雪6",450,4,2),
new TruckCar("松花江",400,4),
new TruckCar("依维柯",1000,20),
};
System.out.println("序号"+"\t"+"汽车名称"+"\t"+"租金"+"\t"+"容量");
for(int c=0,d=1;c<car.length;c++,d++) { //显示租车列表
System.out.print(d+".");
car[c].show();
}
int carNum=3; //定义租车数量
System.out.println("请输入您要租车的数量");
for(int tfNum=3;tfNum>=0;tfNum--) { //循环最大租车数量
carNum=input.nextInt();
String carN[]=new String[carNum]; //定义数组,保存选择后的车辆-车名
int carS[]=new int[carNum]; //定义数组,保存选择后的车辆-座位数
double carW[]=new double[carNum]; //定义数组,保存选择后的车辆-载重数
List<String> listS = new ArrayList<String>();
List<String> listW = new ArrayList<String>();
if(carNum>0&&carNum<=maxC) { //判断租车数量.小于max=999, 进入选车循环
tfNum=-1;
for(int i=1,tfNo=3;i<=carNum;) {
System.out.println("请输入第"+i+"辆车的序号,输入"+"[ 0 ]"+"直接进入下一步租车天数界面");
carType=input.nextInt(); //选择车序号
if(carType<=car.length&&carType>0) { //如果输入的车序号小于车辆数据总长度
tfNo=3;
toMoney=toMoney+car[carType-1].getMoney(); //计算总价格
toSeat=toSeat+car[carType-1].getSeatNum(); //计算总座位/载人数
toWeigth=toWeigth+car[carType-1].getWeigthNum(); //计算总载重数
carS[i-1]=car[carType-1].getSeatNum(); //保存我们选择的车座位数为数组
carW[i-1]=car[carType-1].getWeigthNum();//保存我们选择的车载重为数组
carN[i-1]=car[carType-1].getCarName(); //保存我们选择的车名为数组
i++;
if(i<carNum) {
//显示租车列表
System.out.println("序号"+"\t"+"汽车名称"+"\t"+"租金"+"\t"+"容量");
for(int c=0,d=1;c<car.length;c++,d++) {
System.out.print(d+".");
car[c].show();
}
}
}
else {
if(tfNo>0&&carType!=0) {
//显示租车列表
System.out.println("序号"+"\t"+"汽车名称"+"\t"+"租金"+"\t"+"容量");
for(int c=0,d=1;c<car.length;c++,d++) {
System.out.print(d+".");
car[c].show();
}
System.out.println();
System.out.println("输入错误!此序号的车辆不存在,还可输入"+tfNo+"次。");
tfNo--;
}
else if(tfNo<=0) {
System.out.println("输入的租车数量,错误次数已达3次,退出租车界面");
break;
}
}
//判断车辆完选择之后
if(i>carNum||carType==0) {
System.out.println("请输入租车天数:");
day=input.nextInt();
if(day>0) { //判断租车天数
System.out.println("您的账单:");
System.out.println("***可载人的车有:");
for(int s=0;s<carN.length;s++) {
if(!listS.contains(carN[s])) { //判断重复值
if(carS[s]>0) { //判断座位数大于0
listS.add(carN[s]); //加到list
}
}
}
System.out.print(listS); //输出去重复值后的车
if(toSeat<=0) { //判断有没有选择客车
System.out.println("无");
}else {
System.out.println("\t总载人:"+toSeat+"人");
}
System.out.println("***可载货的车有:");
for(int w=0;w<carN.length;w++) {
if(!listW.contains(carN[w])) { //判断重复值
if(carW[w]>0) { //判断座位数大于0
listW.add(carN[w]); //加到list
}
}
}
System.out.print(listW); //输出 去重复值 后的车
if(toWeigth<=0) { //判断有没有选择货车
System.out.println("无");
}else {
System.out.println("\t总载货:"+toWeigth+"吨");
}
System.out.println("租车总价格:"+toMoney*day+"元");
}else {
int tfDay=3;
System.out.println("租车天数错误,请重新输入:");
while(tfDay>=0) {
day=input.nextInt();
tfDay--;
}
if(tfDay<=0) {
System.out.println("输入租车天数错误次数已达最大值,默认给您选择租车天数为1天");
day=1;
}
}
if(carType==0) {
break;
}
}
}
}
else{
if(tfNum>0) {
System.out.println("输入错误!租车数量需要在1-"+maxC+"之间,还可输入"+tfNum+"次。");
System.out.println("请重新输入租车数量:");
}
else if(tfNum<=0) {
System.out.println("输入的租车数量,错误次数已达3次,退出租车界面");
break;
}
}
}
}
else if(confirmInt==2) {
System.out.println("您选择了否,感谢使用!");
break;
}
else if(confirmInt!=1||confirmInt!=2){
if(tf>0) {
System.out.println("输入错误!还可输入"+tf+"次。");
System.out.println("输入数字选择您是否要租车? 1是,2否");
}
else if(tf<=0) {
System.out.println("输入需求错误次数已到达3次,退出租车界面。");
}
}
}
}
}
欢迎参考我的学习笔记,包括第一季到第三季全部代码~
https://www.jianshu.com/p/43f45dd0b22f
真长长
package java22;
import java.util.*;
import java.util.Scanner;
public class RentCar {
public static void main(String[] args) {
System.out.println("欢迎使用答答租车系统:");
System.out.println("您是否要租车:1是 0否");
boolean withDraw2 = true,withDraw3 = true,withDraw4 = true;//输入异常时退出用
boolean isprint1= false,
isprint2= false,
isprint3_1= false,
isprint3_2 = false,
isprint4= false,
isprint5= false,
isprint6= false;
Scanner strRent = new Scanner(System.in); //第1次输入,是否租车
if(strRent.hasNext()){
String Rent = strRent.nextLine();
if(Rent.equals("1")){
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号 汽车名称 租金 容量");
System.out.println("1. 奥迪A4 500元/天 载人:4人");//载人 1-4人,2-4人,3-4人,4-20人
System.out.println("2. 马自达6 400元/天 载人:4人");//载货 3-2吨,5-4吨,6-20吨
System.out.println("3. 皮卡雪6 450元/天 载人:4 载货:2吨");
System.out.println("4. 金龙 800元/天 载人:20人");
System.out.println("5. 松花江 400元/天 载货:4吨");
System.out.println("6. 依维柯 1000元/天 载货:20吨");
System.out.println("请输入您要租汽车的数量:");
Scanner strRentNumber = new Scanner(System.in); //第2次输入,租几辆
if(strRentNumber.hasNext()){
String RentNumber = strRentNumber.nextLine();
for (int i = 0; i < RentNumber.length(); i++){
if(withDraw2 == true){
if (!Character.isDigit(RentNumber.charAt(i))){
System.out.println("非法输入!");
withDraw2 = false;
break;
}
}else{break;}
}
if(withDraw2 == true){
int rentNumber = Integer.parseInt(RentNumber);
int[] rentCar = new int[rentNumber + 1];
for(int i = 0; i<rentNumber+1;i++){
rentCar[i] = 0;
}
int j = 1,i = 1;
while(j <= rentNumber){
if(withDraw3 == true){
System.out.println("请输入第"+j+"辆车的序号:");
Scanner strCarType = new Scanner(System.in);//第3次输入,租哪辆车
if(strCarType.hasNext()){
String CarType = strCarType.nextLine();
for (i = 0; i < CarType.length(); i++){
if (!Character.isDigit(CarType.charAt(i))){
System.out.println("非法输入!");
withDraw3 = false;
break;
}
}
if(withDraw3 == true){
int carType = Integer.parseInt(CarType);
rentCar[j] = carType;
if(rentCar[j] < 1 || rentCar[j] > 6)
{
System.out.println("非法输入!");
System.out.println( "没有" + rentCar[j] + "型车。");
System.out.println("再见!");
withDraw3 = false;
}
}
}
}else{break;}
j++;
}
if(withDraw3 == true){
double cost = 0.0,load = 0.0;
int manned = 0;
for(i = 1; i <= rentNumber;i++){
if(rentCar[i] > 0){
switch (rentCar[i]){
case 0:break;
case 1:cost += 500;manned += 4;break;
case 2:cost += 400;manned += 4;break;
case 3:cost += 450;manned += 4;load += 2;break;
case 4:cost += 800;manned += 20;break;
case 5:cost += 400;load += 4;break;
case 6:cost += 1000;load += 20;break;
default:break;
}
}
}
System.out.println("请输入租车天数:");//第4次输入,租车天数
Scanner strDays = new Scanner(System.in);
if(strDays.hasNext()){
String RentDays = strDays.nextLine();
for ( i = 0; i < RentDays.length(); i++){
if (!Character.isDigit(RentDays.charAt(i))){
System.out.println("非法输入!");
withDraw4 = false;
break;
}
}
if(withDraw4 == true){
int rentDays = Integer.parseInt(RentDays);
cost *= rentDays;
}
}
if(withDraw4 == true){
System.out.println("您的账单:");
System.out.println("**可载人的车有:");
if(manned == 0){
System.out.println("无");
}
else{
for(i = 1;i <= rentNumber;i++){
if(rentCar[i] > 0){
if (rentCar[i] == 1||rentCar[i] == 2||rentCar[i] == 3||rentCar[i] == 4){
switch(rentCar[i]){
case 1:
if(isprint1 == false){
System.out.print("奥迪A4" + " ");
isprint1 = true;
break;
}
case 2:
if(isprint2 == false){
System.out.print("马自达6" + " ");
isprint2 = true;
break;
}
case 3:
if(isprint3_1 == false){
System.out.print("皮卡雪6" + " ");
isprint3_1 = true;
break;
}
case 4:
if(isprint4 == false){
System.out.print("金龙" + " ");
isprint4 = true;
break;
}
default:break;
}
}
}
}
}
System.out.println("共载人:" + manned);
System.out.println("**载货的车有:");
if(load == 0){
System.out.println("无");
}
else{
for(i = 1;i <= rentNumber;i++){
if(rentCar[i] > 0){
if (rentCar[i] == 3||rentCar[i] == 5||rentCar[i] == 6){
switch(rentCar[i]){
case 3:
if(isprint3_2 == false){
System.out.print("皮卡雪6" + " ");
isprint3_2 = true;
break;
}
case 5:
if(isprint5 == false){
System.out.print("松花江" + " ");
isprint5 = true;
break;
}
case 6:
if(isprint6 == false){
System.out.print("依维河" + " ");
isprint6 = true;
break;
}
default:break;
}
}
}
}
}
System.out.println("共载货:" + load + "吨");
System.out.println("**租车总价格:" + cost +"元");
System.out.println("账单计算结束.感谢您的光临,祝您生活愉快!");
}
}
}
}
}else if(Rent.equals("0"))
{
System.out.println("ByeBye!");
}else{
System.out.println("非法输入!再见!");
}
}
}
}
我来粘个非对象程序的
哇,真长