Shun 发表于 2021-10-5 17:24:14

Java实现答答租车系统

这篇文章主要为大家详细介绍了Java实现答答租车系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了java实现答答租车系统的具体代码,供大家参考,具体内容如下
项目需求:

基本界面需求:

and:

最后是把账单打印出来:

个人代码实现
基本思路:考虑到车辆之间的共性,设置一个父类car, 子类mannedcar(载人), truck(载货),bothcary(既载人又载货),三者继承父类car的price, name属性, getname()方法, 同时重写getpersonnum, getgoodsnum方法。
car.java:


package car;

public abstract class car {
protected int price;
protected string name;
protected int getprice() {
    return price;
}
protected string getname() {
    return name;
}
public int getpersonnum() {
    // todo auto-generated method stub
    return 0;
}
public int getgoodsnum() {
    // todo auto-generated method stub
    return 0;
}

}
mannedcar.java:


package car;

public class mannedcar extends car {
private int personnum;

public mannedcar() {
    this.personnum = 0;
    this.price = 0;
    this.name = "";
}

public mannedcar(int personnum, int price, string name) {
    this.personnum = personnum;
    this.price = price;
    this.name = name;
}

@override
public int getpersonnum() {
    return personnum;
}
}
truck.java:


package car;

public class truck extends car{
private int goodsnum;

public truck() {
    this.price = 0;
    this.goodsnum = 0;
    this.name = "";
}

public truck(int price, int goodsnum, string name) {
    this.price = price;
    this.goodsnum = goodsnum;
    this.name = name;
}

@override
public int getgoodsnum() {
    return goodsnum;
}
}
bothcarry.java:


package car;

public class bothcarry extends car {
private int personnum;
private int goodsnum;

public bothcarry() {
    this.personnum = 0;
    this.goodsnum = 0;
    this.name = "";
    this.price = 0;
}

public bothcarry(int price, int personnum,
      int goodsnum, string name) {
    this.personnum = personnum;
    this.goodsnum = goodsnum;
    this.price = price;
    this.name = name;
}

public int getpersonnum() {
    return personnum;
}

public int getgoodsnum() {
    return goodsnum;
}
}
系统:
carsystem.java:


package car;

import java.util.scanner;
import java.util.arraylist;

public class carsystem {

private static string goodbyeinfo = "欢迎再次使用本系统,再见!";
private static int dayborrow;

public static void beginsystem() {
    carsystem.systemwelcome();
    scanner scanner = new scanner(system.in);
    string usercommand = scanner.next();


    switch(usercommand){
    case "1":
      carsystem.getuserinput();
      break;
    case "0":
      system.out.println(goodbyeinfo);
      break;
    default:
      system.out.println("输入错误..end running..");
      system.exit(0);
      break;
    }
}

public static void systemwelcome() {
    system.out.println("欢迎使用答答租车系统:");
    system.out.println("您是否要租车: 1-是 0-否");
}

public static void getuserinput() {
    int numcarborrow = 0;
    arraylist<car> carlist = new arraylist<car>(6);
    carlist.add(new mannedcar(4,500,"奥迪a4"));
    carlist.add(new mannedcar(4,400,"马自达6"));
    carlist.add(new bothcarry(450,4,2,"皮卡雪6"));
    carlist.add(new mannedcar(20,800,"金龙"));
    carlist.add(new truck(400,4,"松花江"));
    carlist.add(new truck(1000,20,"依维河"));

    system.out.println("请输入您要租汽车的数量:");
    scanner sr = new scanner(system.in);

    numcarborrow = sr.nextint();

    int[] carnumlist = new int;
    for(int i=0;i<numcarborrow;i++) {
      system.out.println("请输入第" + (i+1) + "辆车的序号:");
      if (sr.hasnext()) {
      carnumlist = sr.nextint();
      }
    }
    system.out.println("请输入租车天数:");
    if (sr.hasnext()) {
      dayborrow = sr.nextint();
    }
    sr.close();

    stringbuilder manned = new stringbuilder();
    int numofmanned = 0;
    stringbuilder goods = new stringbuilder();
    int numofgoods = 0;
    int totalcost = 0;

    for(int i = 0;i < carnumlist.length;i++) {
      if(carnumlist>0 && carnumlist < 3 || carnumlist==4) {
      manned.append("");
      manned.append(carlist.get(carnumlist-1).getname());
      numofmanned += carlist.get(carnumlist-1).getpersonnum();
      }
      else if(carnumlist==3) {
      manned.append("");
      manned.append(carlist.get(carnumlist-1).getname());
      goods.append("");
      goods.append(carlist.get(carnumlist-1).getname());
      numofmanned += carlist.get(carnumlist-1).getpersonnum();
      numofgoods += carlist.get(carnumlist-1).getgoodsnum();
      }
      else {
      goods.append("");
      goods.append(carlist.get(carnumlist-1).getname());
      numofgoods += carlist.get(carnumlist-1).getgoodsnum();
      }
      totalcost += carlist.get(carnumlist-1).getprice();
    }
    //output
    system.out.println("您的账单:\n***可载人的车有:");
    system.out.println(manned + "共载人: " + numofmanned);
    system.out.println("***载货的车有:\n" + goods + "共载货 : " + numofgoods + "吨");
    system.out.println("***租车总价格: " + totalcost * dayborrow + "元");

}

}
主程序:


package car;

public class carsystemtest {

public static void main(string[] args) {
    // todo auto-generated method stub
    carsystem.beginsystem();
}

}
运行结果:


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持CodeAE代码之家。
原文链接:https://blog.csdn.net/Joseph_Cherry/article/details/60530978

http://www.zzvips.com/article/176040.html
页: [1]
查看完整版本: Java实现答答租车系统