初学java, 希望能得到各位的指正。感谢!
/**
- 客(轿)车抽象类。
- @author Administrator
-
*/
public abstract class CarryPassengers {
private String autoName; //客车品牌名称属性
private int passengers; //乘员属性//无参构造方法:
public CarryPassengers() {
}
//有参构造方法:
public CarryPassengers(String autoName, int passengers) {
super();
this.autoName = autoName;
this.passengers = passengers;
}//各属性的getter/setter方法:
public String getAutoName() {
return autoName;
}public void setAutoName(String autoName) {
this.autoName = autoName;
}public int getPassengers() {
return passengers;
}public void setPassengers(int passengers) {
this.passengers = passengers;
}//抽象的租金计算方法:
public abstract void calculateRental(int number);
}
/**
- 货车抽象类。
- @author Administrator
-
*/
public abstract class CarryCargo {
private String autoName; //货车品牌名称属性;
private double cargo; //载货量属性;//无参构造方法;
public CarryCargo() {}
//带参的构造方法:
public CarryCargo(String autoName, double cargo) {
super();
this.autoName = autoName;
this.cargo = cargo;
}//各属性的getter/setter方法。
public String getAutoName() {
return autoName;
}public void setAutoName(String autoName) {
this.autoName = autoName;
}public double getCargo() {
return cargo;
}public void setCargo(double cargo) {
this.cargo = cargo;
}//抽象的租金计算方法。
public abstract void calculateRental(int number);
}
输/**
- 接口。供皮卡类车型实现载客货。
- @author Administrator
-
*/
public interface IAuto {void passengersAndcargo();
}
/**
- 轿车(客车)类。 继承Carrypassengers类。
- @author Administrator
-
*/
public class SaloonCar extends CarryPassengers {//租金属性:
private double rental;//租金的getter/setter方法。
public double getRental() {
return rental;
}public void setRental(double rental) {
this.rental = rental;
}//无参构造方法。
public SaloonCar() {}
//带参的构造方法。
public SaloonCar(String autoName, int passengers, double rental) {
super(autoName, passengers); //品牌和乘员属性由父类完成赋值。
this.rental = rental; //租金赋值。
}//重写的确toString方法。
public String toString() {
return "客车 1\t" + getAutoName() + "\t" + getPassengers() + "\t\t"- getRental();
}
//实现的租金计算方法。
public void calculateRental(int number) {System.out.println("轿车租金金额为:"+number*rental );
}
- getRental();
}
/**
* 货车类。继承Carrycargo类。
* @author Administrator
*
*/
public class Coach extends CarryCargo {
//租金属性
private double rental;
//无参的构造方法。
public Coach() {
}
//租金的getter/setter方法。
public double getRental() {
return rental;
}
public void setRental(double rental) {
this.rental = rental;
}
//带参的构造方法。
public Coach(String autoName, double cargo,double rental) {
super(autoName, cargo); //由父类实现赋值。
this.rental=rental; //租金赋值。
}
//重写的toString方法。
public String toString() {
return "货车 2\t" + getAutoName() + "\t\t" + getCargo() + "\t"
+ getRental();
}
//租金计算方法。
public void calculateRental(int number) {
System.out.println("货车租金金额为:" + number * rental);
}
}
/**
- 皮卡实现接口IAuto.
- @author Administrator
-
*/
public class Pickup implements IAuto {
private String autoName;
private int passengers;
private double cargo;
private double rental;public void passengersAndcargo() {
}
public Pickup() {
}
public Pickup(String autoName, int passengers, double cargo, double rental) {
this.autoName = autoName;
this.passengers = passengers;
this.cargo = cargo;
this.rental = rental;
}public String getAutoName() {
return autoName;
}public void setAutoName(String autoName) {
this.autoName = autoName;
}public int getPassengers() {
return passengers;
}public void setPassengers(int passengers) {
this.passengers = passengers;
}public double getCargo() {
return cargo;
}public void setCargo(double cargo) {
this.cargo = cargo;
}public double getRental() {
return rental;
}public void setRental(double rental) {
this.rental = rental;
}public String toString() {
return "皮卡 3\t" + getAutoName() + "\t" + getPassengers() + "\t"- getCargo() + "\t" + getRental();
}
public void calculateRental(int number) {
System.out.println("皮卡车租金金额为:" + number * rental);
} - getCargo() + "\t" + getRental();
}
/**
* 主程序。
*/
import java.util.Scanner;
public class TestRentalCar {
public static void main(String[] args) {
Login.display();
Decide.enter();
CarryPassengers salooncar1=new SaloonCar("奔驰",5,300);
CarryCargo cargo1=new Coach("沃尔沃",15.00,400);
Pickup pickup1=new Pickup("长城",5,2.00,200);
CarList.header();
System.out.println(salooncar1.toString());
System.out.println(cargo1.toString());
System.out.println(pickup1.toString());
CarList.type();
Scanner selectNum = new Scanner(System.in);
int select = selectNum.nextInt();
CarList.number();
int count=selectNum.nextInt();
switch (select){
case 1:
salooncar1.calculateRental(count);
break;
case 2:
cargo1.calculateRental(count);
break;
case 3:
pickup1.calculateRental(count);
break;
}
}
}
/**
- 租车程序入口Login.
- @author Administrator
-
*/
public class Login {
public static void display() {
System.out.println("**欢迎光临答答租车**");
System.out.println();
System.out.print("是否进入租车系统?(Y/N)");}
}
/**
* 租车程序列表头显示。及车辆选择信息提示。
* @author Administrator
*
*/
public class CarList {
public static void header() {
System.out.println();
System.out.println("***各型车辆列表信息***");
System.out.println();
System.out.println("\t车辆品牌\t载客人数\t载货吨位\t每日租金");
}
public static void type(){
System.out.println();
System.out.print("请选择租车类型:(1-3)");
}
public static void number(){
System.out.println();
System.out.print("请输入所需车辆数量:");
}
}
/**
- 租车程序入口条件判断。使程序可以正常退出。
*/
import java.io.IOException;
import java.util.Scanner;
public class Decide {
public static void enter() {
char acc;
try {
acc = (char) System.in.read();
if (acc == 'N' || acc == 'n') {
System.exit(0);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}