今天试着写了一个新的程序,叫做“答答租车系统”,是慕课网上的一个综合练习,戳我看原题。
项目要求截图如下:
我的代码(简单粗暴版):
Vehicle.java
public class Vehicle {
String name; //车名
int rent; //租金
int seatingCapacity; //载人量
int cargoCapacity; //载货量
//构造方法,初始化变量
public Vehicle(String newName, int newRent,
int sCapacity, int cCapacity) {
name = newName;
rent = newRent;
seatingCapacity = sCapacity;
cargoCapacity = cCapacity;
}
//打印可租车的类型及其价目表
public void Display(int i) {
System.out.print(i + " " + name + " " + rent + " ");
if (seatingCapacity != 0) {
System.out.print("载人:" + seatingCapacity + "人" + " ");
}
if (cargoCapacity != 0) {
System.out.print("载货:" + cargoCapacity + "吨");
}
System.out.print("
");
}
}
Main.java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Vehicle[] vehicles = new Vehicle[7];
vehicles[1] = new Vehicle("奥迪A4 ", 500, 4, 0);
vehicles[2] = new Vehicle("马自达6", 400, 4, 0);
vehicles[3] = new Vehicle("皮卡雪6", 450, 4, 2);
vehicles[4] = new Vehicle("金龙 ", 800, 20, 0);
vehicles[5] = new Vehicle("松花江 ", 400, 0, 4);
vehicles[6] = new Vehicle("依维柯", 1000, 0, 20);
System.out.println("欢迎使用嗒嗒租车系统!");
System.out.println("您是否需要租车?");
System.out.println("是:请输入数字1 不是:请输入数字2");
int y = 0;
if (scan.hasNextInt()) {
y = scan.nextInt();
}
//若用户输入数字1,则进入租车流程
if (y == 1) {
int i;
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号" + " " + "汽车名称" + " "
+ "租金(元/天)" + " " + "容量");
for (i = 1; i <= 6; i++) {
vehicles[i].Display(i);
}
System.out.println("请输入您要租车的数量:");
int n = 0; //记录租车的总数量
if (scan.hasNextInt()) {
n = scan.nextInt();
}
int carNO = 0, rentDays = 0; //选择的车序号和租期
int rental = 0, totalPerson = 0, totalCargo = 0;//总租金、总载人数、总载货数
int[] record = {0, 0, 0, 0, 0, 0, 0}; //记录各种车的被租数量
int[] person = {0, 0, 0, 0, 0, 0, 0}; //记录所租车是否可载人
int[] cargo = {0, 0, 0, 0, 0, 0, 0}; //记录所租车是否可载货
for (i = 1; i <= n; i++) {
System.out.println("请输入您要租的第" + i + "辆车的序号");
if (scan.hasNextInt()) {
carNO = scan.nextInt();
}
rental += vehicles[carNO].rent;
record[carNO]++;
if (vehicles[carNO].seatingCapacity != 0) {
person[i] = 1;
}
if (vehicles[carNO].cargoCapacity != 0) {
cargo[i] = 1;
}
}
System.out.println("请输入租车天数:");
if (scan.hasNextInt()) {
rentDays = scan.nextInt();
}
rental *= rentDays;
System.out.println("************************************");
System.out.print("
");
System.out.println("您本次租车的账单如下:");
System.out.print("
");
System.out.println("******你本次共租车" + n + "辆:");
System.out.print("
");
System.out.println("汽车名字 出租数量");
for (i = 1; i < 7; i++) {
if (record[i] != 0) {
System.out.println(vehicles[i].name + " " + record[i]);
}
}
System.out.print("
");
System.out.println("*****可载人的有:");
System.out.print("
");
for (i = 1; i < 7; i++) {
if (person[i] != 0) {
System.out.print(vehicles[i].name + " ");
totalPerson += vehicles[i].seatingCapacity;
}
}
System.out.print("
");
System.out.println("共可载人:" + totalPerson + "人");
System.out.print("
");
System.out.println("*****可载货的有:");
System.out.print("
");
for (i = 1; i < 7; i++) {
if (cargo[i] != 0) {
System.out.print(vehicles[i].name + " ");
totalCargo += vehicles[i].cargoCapacity;
}
}
System.out.print("
");
System.out.println("共可载货:" + totalCargo + "吨");
System.out.print("
");
System.out.println("*****租车总价格:" + rental + "元");
System.out.print("
");
System.out.println("************************************");
}
}
}