交作业!!!

来源:12-1 综合练习

Cavon

2015-05-20 12:06

package system;

public class Dada {
 public static void main(String[] args) {
  User method = new User();
  System.out.println("欢迎使用答答租车系统:");
  if (method.getWhether()) {
   // 创建对象
   PassengerCar se1 = new PassengerCar(1, "奥迪A4", 4, 500);
   PassengerCar se2 = new PassengerCar(2, "马自达6", 4, 400);
   PassenWagon se3 = new PassenWagon(3, "皮卡雪6", 4, 2, 450);
   PassengerCar se4 = new PassengerCar(4, "金龙", 20, 800);
   Wagon se5 = new Wagon(5, "松花江", 4, 400);
   Wagon se6 = new Wagon(6, "依维柯", 20, 1000);
   method.aboutOutput(se1, se2, se3, se4, se5, se6);// 输出租车信息
   int rentNum = method.getRentNum();// 输入租车量
   int cars[] = method.getCarSequence(rentNum);// 输入要租的车的序号
   int days = method.getDays();// 输入要租的天数
   method.bill(cars, days, se1, se2, se3, se4, se5, se6);
  }
 }
}

package system;

/*
 * 所有车的父类
 */
public abstract class Car {
 private String carName;
 private int num;
 private int sequence;
 private int rent;

 public int getSequence() {
  return sequence;
 }

 public void setSequence(int sequence) {
  this.sequence = sequence;
 }

 public String getCarName() {
  return carName;
 }

 public void setCarName(String carName) {
  this.carName = carName;
 }

 public int getNum() {
  return num;
 }

 public void setNum(int num) {
  this.num = num;
 }

 public int getRent() {
  return rent;
 }

 public void setRent(int rent) {
  this.rent = rent;
 }

}

package system;

/*
 * 载客汽车
 */
public class PassengerCar extends Car {
 private int mannedNum;

 public PassengerCar(int sequence, String carName, int mannedNum, int rent) {
  this.mannedNum = mannedNum;
  setCarName(carName);
  setSequence(sequence);
  setRent(rent);
 }

 public int getMannedNum() {
  return mannedNum;
 }

 public void setMannedNum(int mannedNum) {
  this.mannedNum = mannedNum;
 }

 public void out() {
  System.out.println(getSequence() + "." + " " + getCarName() + " "
    + getRent() + "元/天 载人:" + getMannedNum() + "人");
 }
}

package system;

/*
 * 既可载货也可载人的汽车
 */
public class PassenWagon extends Car {
 private int mannedNum;
 private int limitation;

 public PassenWagon(int sequence, String carName, int mannedNum,
   int limitation, int rent) {
  this.mannedNum = mannedNum;
  this.limitation = limitation;
  setCarName(carName);
  setSequence(sequence);
  setRent(rent);
 }

 public int getMannedNum() {
  return mannedNum;
 }

 public void setMannedNum(int mannedNum) {
  this.mannedNum = mannedNum;
 }

 public int getLimitation() {
  return limitation;
 }

 public void setLimitation(int limitation) {
  this.limitation = limitation;
 }

 public void out() {
  System.out.println(getSequence() + "." + " " + getCarName() + " "
    + getRent() + "元/天 载人:" + getMannedNum() + " 载货:"
    + getLimitation() + "吨");
 }
}

package system;

/*
 * 载货汽车
 */
public class Wagon extends Car {
 private int limitation;

 public Wagon(int sequence, String carName, int limitation, int rent) {
  this.limitation = limitation;
  setCarName(carName);
  setSequence(sequence);
  setRent(rent);
 }

 public int getLimitation() {
  return limitation;
 }

 public void setLimitation(int limitation) {
  this.limitation = limitation;
 }

 public void out() {
  System.out.println(getSequence() + "." + " " + getCarName() + " "
    + getRent() + "元/天 载货:" + getLimitation() + "吨");
 }
}

package system;

import java.util.Scanner;

@SuppressWarnings("resource")
public class User {
 // 判断是否要租车
 public boolean getWhether() {
  System.out.println("您是否要租车:1是 0否");
  Scanner sc = new Scanner(System.in);
  int whether = sc.nextInt();
  if (whether == 1) {
   return true;
  } else if (whether == 0) {
   return false;
  } else {
   System.out.println("您的输入有误,请重新选择。");
   return getWhether();
  }
 }

 // 输出相关信息
 public void aboutOutput(PassengerCar a, PassengerCar b, PassenWagon c,
   PassengerCar d, Wagon e, Wagon f) {
  System.out.println("您可租车的类型及其价目表:\n序号    汽车名称    租金    容量");
  a.out();
  b.out();
  c.out();
  d.out();
  e.out();
  f.out();
 }

 // 获得租车量
 public int getRentNum() {
  System.out.println("请输入您要租车的数量:");
  Scanner sc = new Scanner(System.in);
  int rentNum = sc.nextInt();
  if (rentNum <= 0) {
   System.out.println("您的输入有误,请重新输入。");
   rentNum = getRentNum();
  }
  return rentNum;
 }

 // 得到租车的序号
 public int[] getCarSequence(int rentNum) {
  int cars[] = new int[rentNum];
  for (int i = rentNum, j = 1; i > 0; i--, j++) {
   System.out.println("请输入第" + j + "辆车的序号:");
   if (j <= 0 || j > 6) {
    System.out.println("您的输入有误,请重新输入。");
    return getCarSequence(rentNum);
   }
   Scanner sc = new Scanner(System.in);
   cars[j - 1] = sc.nextInt();
  }
  return cars;
 }

 // 得到租车的天数
 public int getDays() {
  int days;
  System.out.println("请输入租车的天数:");
  Scanner sc = new Scanner(System.in);
  days = sc.nextInt();
  if (days <= 0) {
   System.out.println("您的输入有无,请重新输入。");
   return getDays();
  }
  return days;
 }

 // 得到账单
 public void bill(int cars[], int days, PassengerCar a, PassengerCar b,
   PassenWagon c, PassengerCar d, Wagon e, Wagon f) {
  mannedCars(cars, a, b, c, d);// 获取可载人的车的列表
  carryCars(cars, c, e, f);// 获取可载货的车的列表
  sumRent(cars, days, a, b, c, d, e, f);// 获取租车总价格
 }

 // 得到租车总价格
 public void sumRent(int cars[], int days, PassengerCar a, PassengerCar b,
   PassenWagon c, PassengerCar d, Wagon e, Wagon f) {
  int sum = 0;
  for (int car : cars) {
   switch (car) {
   case 1:
    sum += a.getRent();
    break;
   case 2:
    sum += b.getRent();
    break;
   case 3:
    sum += c.getRent();
    break;
   case 4:
    sum += d.getRent();
    break;
   case 5:
    sum += e.getRent();
    break;
   case 6:
    sum += f.getRent();
    break;
   default:
    break;
   }
  }
  sum *= days;
  System.out.print("***租车总价格:" + sum);
 }

 // 得到载货的车的信息
 public void carryCars(int cars[], PassenWagon c, Wagon e, Wagon f) {
  System.out.println("****可载货的车有:");
  int sum = 0;
  for (int car : cars) {
   switch (car) {
   case 3:
    System.out.print(" " + c.getCarName());
    sum += c.getLimitation();
    break;
   case 5:
    System.out.print(" " + e.getCarName());
    sum += e.getLimitation();
    break;
   case 6:
    System.out.print(" " + f.getCarName());
    sum += f.getLimitation();
    break;
   default:
    break;
   }
  }
  System.out.println(" 共载货:" + sum + "吨");
 }

 // 得到载人的车的信息
 public void mannedCars(int cars[], PassengerCar a, PassengerCar b,
   PassenWagon c, PassengerCar d) {
  System.out.println("您的账单:\n***可载人的车有:");
  int sum = 0;
  for (int car : cars) {
   switch (car) {
   case 1:
    System.out.print(" " + a.getCarName());
    sum += a.getMannedNum();
    break;
   case 2:
    System.out.print(" " + b.getCarName());
    sum += b.getMannedNum();
    break;
   case 3:
    System.out.print(" " + c.getCarName());
    sum += c.getMannedNum();
    break;
   case 4:
    System.out.print(" " + d.getCarName());
    sum += d.getMannedNum();
    break;
   default:
    break;
   }
  }
  System.out.println(" 共载人:" + sum + "人");
 }
}

写回答 关注

1回答

  • Cavon
    2015-05-20 12:09:17

    package system;

    /*
     * 所有车的父类
     */
    public abstract class Car {
     private String carName;
     private int num;
     private int sequence;
     private int rent;

     public int getSequence() {
      return sequence;
     }

     public void setSequence(int sequence) {
      this.sequence = sequence;
     }

     public String getCarName() {
      return carName;
     }

     public void setCarName(String carName) {
      this.carName = carName;
     }

     public int getNum() {
      return num;
     }

     public void setNum(int num) {
      this.num = num;
     }

     public int getRent() {
      return rent;
     }

     public void setRent(int rent) {
      this.rent = rent;
     }

    }

Java入门第二季 升级版

课程升级!以终为始告别枯燥,在开发和重构中体会Java面向对象编程的奥妙

530654 学习 · 6091 问题

查看课程

相似问题