纯萌新 我感觉我的代码好繁琐 有很多地方需要优化 改进
package project4;
public abstract class Car {
public abstract void price();
}
package project4;
public class Aodi extends Car {
public int num = 1;
public String name = "奥迪A4";
public int rent = 500;
public int manned = 4;
@Override
public void price() {
// TODO Auto-generated method stub
System.out.println( +num+". "+name+" "+rent+"/天 载人:" +manned + "人" );
}
}
package project4;
public class Mazida extends Car {
public int num = 2;
public String name = "马自达6";
public int rent = 400;
public int manned = 4;
@Override
public void price() {
// TODO Auto-generated method stub
System.out.println( +num+". "+name+" "+rent+"/天 载人:" +manned + "人" );
}
}
package project4;
public class Pikaxue extends Car {
public int num = 3;
public String name = "皮卡雪6";
public int rent = 450;
public int manned = 4;
public float cargo = 2.0f;
@Override
public void price() {
// TODO Auto-generated method stub
System.out.println( +num+". "+name+" "+rent+"/天 载人:" +manned + "载货:"+ cargo +"吨" );
}
}
package project4;
public class Jinlong extends Car {
public int num = 4;
public String name = "金龙";
public int rent = 800;
public int manned = 20;
@Override
public void price() {
// TODO Auto-generated method stub
System.out.println( +num+". "+name+" "+rent+"/天 载人:" +manned + "人" );
}
}
package project4;
public class Songhuajiang extends Car {
public int num = 5;
public String name = "松花江";
public int rent = 400;
public float cargo = 4.0f;
@Override
public void price() {
// TODO Auto-generated method stub
System.out.println( +num+". "+name+" "+rent+"/天 载货:" +cargo + "吨" );
}
}
package project4;
public class Yiweike extends Car {
public int num = 6;
public String name = "依维柯";
public int rent = 1000;
public float cargo = 20.0f;
@Override
public void price() {
// TODO Auto-generated method stub
System.out.println( +num+". "+name+" "+rent+"/天 载货:" +cargo + "吨" );
}
}
package project4;
import java.util.Scanner;
public class Initclass {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("欢迎使用达达租车系统");
System.out.println("您是否要租车:1是 0否");
Scanner input = new Scanner(System.in);//键盘输入
int a = input.nextInt();
if(a==0)
{
System.out.println("谢谢您的光临");
}
else if(a==1)
{
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号 汽车名称 租金 容量");
Aodi car1 = new Aodi();
car1.price();
Mazida car2 = new Mazida();
car2.price();
Pikaxue car3 = new Pikaxue();
car3.price();
Jinlong car4 = new Jinlong();
car4.price();
Songhuajiang car5 = new Songhuajiang();
car5.price();
Yiweike car6 = new Yiweike();
car6.price();
System.out.println("请输入您要租汽车的数量");
int n = input.nextInt();
int[] numbers = new int[100];//建立数组保存汽车序号
for(int i=1;i<=n;i++)//循环输入客户所选汽车序号
{
System.out.println("请输入第"+i+"辆车的序号");
int numb = input.nextInt();
if(numb<0||numb>6)
{
System.out.println("输入序号错误");
break;
}
numbers[i] = numb;
}
System.out.println("请输入租车天数");
int day = input.nextInt();
System.out.println("您的账单:");
System.out.println("***可载人的车有:");
int j;
int ma=0;
int money=0;
for(j=1;j<=n;j++)
{
if(numbers[j] == car1.num)
{
ma = ma + car1.manned;
System.out.print(car1.name);
money = money +car1.rent * day;
}
if(numbers[j] == car2.num)
{
ma = ma + car2.manned;
System.out.print(car2.name);
money = money +car2.rent * day;
}
if(numbers[j] == car3.num)
{
ma = ma + car3.manned;
System.out.print(car3.name);
money = money +car3.rent * day;
}
if(numbers[j] == car4.num)
{
ma = ma + car4.manned;
System.out.print(car4.name);
money = money +car4.rent * day;
}
System.out.print(" ");
}
System.out.println("共载人:"+ ma +"人");
System.out.println("***载货的车有:");
int k;
float weight=0;
for(k=1;k<=n;k++)
{
if(numbers[k] == car3.num)
{
weight = weight + car3.cargo;
System.out.print(car3.name);
}
if(numbers[k] == car5.num)
{
weight = weight + car5.cargo;
System.out.print(car5.name);
money = money + car5.rent * day;
}
if(numbers[k] == car6.num)
{
weight = weight + car6.cargo;
System.out.print(car6.name);
money = money + car6.rent * day;
}
System.out.print(" ");
}
System.out.println("共载货:"+weight);
System.out.println("***租车总价值:"+ money + "元");
}
System.out.println("欢迎您的下次光临");
}
}