王胖子
2015-04-22 10:08
package com.demo;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Rental {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("欢迎使用答答租车系统!");
System.out.println("是否进入租车界面:");
System.out.println("1:进入 0:退出");
Scanner putInScanner = new Scanner(System.in);
int putIn = putInScanner.nextInt();
if (putIn==1) {
System.out.println("可选择车的类型及其价目:");
System.out.println("序号"+"\t"+"汽车名称"+"\t\t"+"租金(元/天)"+"\t"+"载人量(人)"+"\t"+"载货量(吨)");
Car[] cars = {new Coach(1, "奥迪A4", 500, 4,0),new Coach(2, "马自达6", 400, 4,0)
,new PickUP(3, "皮卡雪6", 450, 4,2),new Coach(4, "金龙", 800, 20,0)
,new Truck(5,"松花江",400,0,4),new Truck(6,"依维柯",1000,0,20)};
//显示价目表
for (Car car : cars) {
System.out.println(car.getId()+"\t"+car.getCarName()+"\t\t"+car.getCarRent()+"\t\t"+
car.getNumber()+"\t\t"+car.getCargoes());
}
System.out.println("请输入租车的数目:");
List<Car> carList = new ArrayList<Car>();
for (int i = 0; i < cars.length; i++) {
carList.add(cars[i]);
}
int money = 0;//总钱数
int amount = putInScanner.nextInt();
for (int i = 0; i < amount; i++) {
int oneMoney = 0;//单个的钱数
System.out.println("请输入第"+(i+1)+"辆车的序号");
int num = putInScanner.nextInt();
System.out.println("该车使用几天:");
int day = putInScanner.nextInt();
System.out.println("已成功添加:"+carList.get(num-1).getCarName());
oneMoney = carList.get(num-1).getCarRent()*day + oneMoney;
System.out.println("该车花费"+oneMoney+"元");
money = money + oneMoney;
}
System.out.println("总计金额:"+money+"元");
System.out.println("感谢您的使用,再见!");
} else {
System.out.println("已退出租车系统!");
}
}
}
package com.demo;
/**
* 抽象类:车
* @author Administrator
*
*/
public class Car {
private int id;//序号
private int number;//载人量
private int cargoes;//载货量
private String carName;//车名
private int carRent;//租金
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public int getCargoes() {
return cargoes;
}
public void setCargoes(int cargoes) {
this.cargoes = cargoes;
}
public String getCarName() {
return carName;
}
public void setCarName(String carName) {
this.carName = carName;
}
public int getCarRent() {
return carRent;
}
public void setCarRent(int carRent) {
this.carRent = carRent;
}
//id:序号;carName:汽车名称;rent:租金;number:载人量;cargoes:载货量
public Car(int id,String carName,int rent,int number,int cargoes){
this.setId(id);
this.setCarName(carName);
this.setCarRent(rent);
this.setCargoes(cargoes);
this.setNumber(number);
}
}
package com.demo;
/**
* 货车
* @author Administrator
*
*/
public class Truck extends Car{
public Truck(int id, String carName, int rent, int number, int cargoes) {
super(id, carName, rent, number, cargoes);
this.setId(id);
this.setCarName(carName);
this.setCarRent(rent);
this.setCargoes(cargoes);
this.setNumber(number);
}
}
package com.demo;
/**
* 客车
* @author Administrator
*
*/
public class Coach extends Car {
public Coach(int id, String carName, int rent, int number, int cargoes) {
super(id, carName, rent, number, cargoes);
this.setId(id);
this.setCarName(carName);
this.setCarRent(rent);
this.setCargoes(cargoes);
this.setNumber(number);
}
}
package com.demo;
/**
* 皮卡
* @author Administrator
*
*/
public class PickUP extends Car {
public PickUP(int id, String carName, int rent, int number, int cargoes) {
super(id, carName, rent, number, cargoes);
this.setId(id);
this.setCarName(carName);
this.setCarRent(rent);
this.setCargoes(cargoes);
this.setNumber(number);
}
}
输入超出范围的情况没有考虑
Java入门第二季 升级版
530561 学习 · 6091 问题
相似问题