/*
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools Templates
- and open the template in the editor.
*/
package test;
import java.util.Arrays;
/*
汽车父类 - @author Administrator
*/
public class Cars {
private String name;//汽车名
private int price;//价钱
private int peopleTotal;//载人量
private int cargo;//载货量
private String type;
public void msg(int i){}
public String[] getTypes() {
String[] types = {"passenger", "truck", "cargo"};
return types;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
/**- 获取汽车名
- @return string
*/
public String getName(){
return this.name;
}
/** - 设置汽车名
- @param name
*/
public void setName(String name){
this.name = name;
}
/** - 获取价钱
- @return
*/
public int getPrice(){
return this.price;
}
/** - 设置价钱
- @param price
*/
public void setPrice(int price){
this.price = price;
}
/** - 获取载人量
- @return
*/
public int getPeopleTotal(){
return this.peopleTotal;
}
/** - 设置载人量
- @param peopleTotal
*/
public void setPeopleTotal(int peopleTotal){
this.peopleTotal = peopleTotal;
}
/** - 获取载货量
- @return
*/
public int getCargo(){
return this.cargo;
}
/** - 设置载货量
-
@param cargo
*/
public void setCargo(int cargo){
this.cargo = cargo;
}
}/*
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools Templates
- and open the template in the editor.
*/
package test;
/*
载人类
- @author Administrator
*/
public class passengerCar extends Cars{
/**- 构造
- @param name
- @param price
- @param peopleTotal
*/
public passengerCar(String name, int price, int peopleTotal) {
this.setName(name);
this.setPeopleTotal(peopleTotal);
this.setPrice(price);
this.setType("passenger");
}
/** - 打印信息
*/
public void msg(int i) {
String m = i+"\t"+this.getName()+"\t"+this.getPrice()+"元/天"+"\t\t"+"载人"+this.getPeopleTotal()+"个";
System.out.println(m);
}
/*
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools Templates
- and open the template in the editor.
*/
package test;
/*
载货量
- @author Administrator
*/
public class Truck extends Cars{
public Truck(String name, int price, int cargo) {
this.setName(name);
this.setCargo(cargo);
this.setPrice(price);
this.setType("truck");
}
/**-
打印信息
*/
public void msg(int i) {
String m = i+"\t"+this.getName()+"\t"+this.getPrice()+"元/天"+"\t\t"+"载货"+this.getCargo()+"kg";
System.out.println(m);
}
}
-
打印信息
/*
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools Templates
- and open the template in the editor.
*/
package test;
/*
载人,载货量
- @author Administrator
*/
public class Cargo extends Cars{
public Cargo(String name, int price, int peopleTotal, int cargo) {
this.setName(name);
this.setPeopleTotal(peopleTotal);
this.setPrice(price);
this.setCargo(cargo);
this.setType("cargo");
}
/**-
打印信息
*/
public void msg(int i) {
String m = i+"\t"+this.getName()+"\t"+this.getPrice()+"元/天"+"\t\t"+"载人"+this.getPeopleTotal()+"个"+"\t"+"载货"+this.getCargo()+"kg";
System.out.println(m);
}
}
-
打印信息
/*
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools Templates
- and open the template in the editor.
*/
package test;
import java.util.Scanner;
/** - @author Administrator
/
public class CarTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Cars[] car = {
new passengerCar("奥迪A6L", 500, 4),
new passengerCar("马自达6", 400, 4),
new Truck("松花江", 400, 4),
new Truck("依维柯", 800, 20),
new Cargo("皮卡雪6", 500, 4,2),
new Cargo("TSL-6", 800, 3,3),
};
System.out.println("你是否要租车: 1是 0否");
int p = sc.nextInt();
if(p == 1) {
System.out.println("你可租车的类型和租金");
System.out.println("序号"+"\t"+"汽车名称"+"\t"+" 租金"+"\t\t"+"容量");
for(int i = 0;i < car.length;i++){
car[i].msg((i+1));
}
//选择车的数量
System.out.println("请输入你要租车的数量:");
int carCount = sc.nextInt();
if(carCount <=0 ){
System.out.println("数量不能小于0");
}
int[] carArray = new int[carCount];
//记录选择的车
for(int i = 0;i < carCount; i++) {
System.out.println("请输入你要租用的车辆序号:");
int carNum = sc.nextInt();
carArray[i] = carNum;
}
System.out.println("请输入你要租的天数:");
int day = sc.nextInt();
//找出选择车的信息, 统计
System.out.println("你的账单:");
System.out.println("选择载车的有:");
int r = 0;
int h = 0;
int sum = 0;
for(int i=0;i<carArray.length;i++) {
String type = car[carArray[i]-1].getType();
if("passenger".equals(type) "cargo".equals(type)) {
System.out.println(car[carArray[i]-1].getName());
r += car[carArray[i]-1].getPeopleTotal();
}
sum += car[carArray[i]-1].getPrice()day;
}
System.out.println("总载客量:"+r);
System.out.println("选择载货的有:");
for(int i=0;i<carArray.length;i++) {
String type = car[carArray[i]-1].getType();
if("truck".equals(type) "cargo".equals(type)) {
System.out.println(car[carArray[i]-1].getName());
h += car[carArray[i]-1].getCargo();
}
}
System.out.println("总载货量:"+h);
System.out.println("租车总价格:"+sum);
}else{
System.out.println("欢迎再次租车!");
}
}
}
热门评论
不能用equals , equals的本意是比较对象的类型是否一致 而不是先判断内容是否一致
if("passenger".equals(type) "cargo".equals(type)) //这儿为啥eclipse显示错的错误如下/*Multiple markers at this line
- Cannot invoke equals(String) on the primitive type
boolean
- Syntax error on token ""cargo"", delete this token
- Line breakpoint:test [line: 50] - main(String[])
- Syntax error on token ""cargo"", delete this token*/