父类
public class Car {
private String name;
private int passengers;
private int goods;
private int price;
public Car(){
}
public Car(String name,int passengers,int goods,int price){
this.setName(name);
this.setPassengers(passengers);
this.setGoods(goods);
this.setPrice(price);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPassengers() {
return passengers;
}
public void setPassengers(int passengers) {
this.passengers = passengers;
}
public int getGoods() {
return goods;
}
public void setGoods(int goods) {
this.goods = goods;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public void Display(){
System.out.println(name+"\t "+passengers+"\t "+goods+"\t "+price);
}
}
子类:Auto, PickUp,Limo,Trunk,Hammer
public class Auto extends Car {
public Auto(String name,int passengers,int goods,int price){
super(name,passengers,goods,price);
}
public void Display(){
System.out.println("I am Auto Car!");
}
}
主程序
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
public class dadaRent {
private static Scanner input;
public static List<Car> allCar;
public static List<Car> rentCar;
public dadaRent(){
dadaRent.allCar = new ArrayList<Car>();
dadaRent.rentCar = new ArrayList<Car>();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
dadaRent ddr = new dadaRent();
boolean flag = true;
System.out.println("Welcome to DaDaRent !");
while(flag)
{
System.out.println("Do you want to Rent Cars or Leave? Yes OR No ?");
input = new Scanner(System.in);
String choose = input.next();
if(choose.equals("no"))
{
System.out.println("谢谢光临!");
flag = false;
break;
}
if(choose.equals("yes"))
{
try{
System.out.println("Here are the Car List:");
ddr.AddCars();
ddr.DisplayCars(allCar);
System.out.println("How many cars do you want to rent?");
int num =input.nextInt();
int [] rentNum = new int[num];
for(int i=0;i<num;i++)
{
System.out.println("请输入第"+(i+1)+"辆车编号:");
int j = input.nextInt();
rentNum[i] =j-1;
}
System.out.println("下面是你选择的车辆:");
ddr.AddRent(rentNum);
ddr.DisplayCars(rentCar);
for(Car obj:rentCar)
{
obj.Display();
}
System.out.println("Please input rent days:");
int rentDay =input.nextInt();
System.out.println("Your tocal Price is: "+ddr.TotoalPrice(rentDay));
}
catch(InputMismatchException e)
{
e.printStackTrace();
}
}
else
System.out.println("Input Error! Try Again!");
}
}
public void AddCars(){
Car [] carArray= {new PickUp("皮卡",4,20,450),new Auto("奥迪",4,19,500),new Limo("劳斯莱斯",6,25,2000),
new Trunk("卡车",2,30,200),new Hammer("悍马",2,40,1000)};
allCar.addAll(Arrays.asList(carArray));
}
public void AddRent(int []rentNum){
for(int i=0;i<rentNum.length;i++){
rentCar.add(allCar.get(rentNum[i]));
}
}
public static void DisplayCars(List<Car> cars){
System.out.println("编号\t车型\t 载客\t 载货/tons\t租金RMB/day\t");
for(int i=0;i<cars.size();i++)
{
System.out.println(" "+(i+1)+"\t"+cars.get(i).getName()+"\t "+cars.get(i).getPassengers()+
"\t "+cars.get(i).getGoods()+"\t "+cars.get(i).getPrice());
}
}
public int TotoalPrice(int rentDay){
int priceSum =0;
for(int i=0;i<rentCar.size();i++){
priceSum = priceSum+rentCar.get(i).getPrice();
}
return priceSum*rentDay;
}
}
热门评论
题主我运行了一遍 提出几个能改进的 第一:显示车型号的时候排版乱了 第二:如果我要租两辆车,第二辆车输入的编号不合法的情况下程序出错 第三:如果我继续租车,第二次显示车型号的时候会显示两遍车的型号 发现的暂时这么多 希望你天天开心,我也是在学JAVA 共同进步哈