如何实现train类

but5z9lq  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(271)

关闭。这个问题需要更加突出重点。它目前不接受答案。
**想改进这个问题吗?**通过编辑这篇文章更新这个问题,使它只关注一个问题。

六天前关门了。
改进这个问题
我有以下两门课:

public class Station{

    private String id;

    private String description;

    private boolean isTerminus;

    private int numberPassengersInTransit;

    public Station(String id, String desc) {
        this.id = id;
        description= desc;
    }

    public String getDescription() {
        return descrizione;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public boolean isTerminus() {
        return isTerminus;
    }

    public void setTerminus(boolean isTerminus) {
        this.isTerminus = isTerminus;
    }

    public String getId() {
        return id;
    }

    public int getNumberPassengersInTransit() {
        return numberPassengersInTransit;
    }

    public void addPassenger() {
        numberPassengersInTransit++;
    }

    public void addGroupPassengers(int num) {
        numberPassengersInTransit += num;
    }
}

public class Train {

    private String id;

    private String description;

    private Station parkingPlace;

    private int numberPassengersOnBoard;

    public final int NUMBER_MAX_PASSENGERS = 200; 

    private StringBuilder log = new StringBuilder();

    public Train(String id, String desc, Station parkingPlace) {
        this.id = id;
        descrizione = desc;
        this.parkingPlace = parkingPlace;
    }

    public Station getParkingPlace() {
        return parkingPlace;
    }

    public void setParkingPlace(Station parkingPlace) {
        this.parkingPlace = parkingPlace;
    }

    public int getNumberPassengersOnBoard() {
        return numberPassengersOnBoard;
    }

    public String getId() {
        return id;
    }

    public String getDescription() {
        return description;
    }

    public void runs(Station arrival) {

        log.append("The train "+id+" travel to the station "+parkingPlace.getDescription()+" at the station "+arrival.getDescription()+".");
        parkingPlace=arrival;
    }

    public void addPassenger() {

        if(numberPassengersOnBoard<NUMBER_MAX_PASSENGERS)
            numberPassengersOnBoard++;
        else
            System.out.println("Error");
    }

    public void addGroupPassengers(int num) {

        if(numberPassengersOnBoard+num<NUMBER_MAX_PASSENGERS)
            numberPassengersOnBoard++;
        else
            System.out.println("Error");

    }

    public String getLog() {
        return log.toString();
    }

}

测试类别:

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

public class StationTest {

    private Station sta;

    @Before
    public void setUp() throws Exception {
        sta = new Station("STRM0001","Giardinetti");
    }

    @Test
    public void testConstructorStation() {
        assertEquals("The station id was not initialized correctly", 
                "STRM0001", sta.getId());
        assertEquals("The description from the station was not initialized correctly", 
                "Giardinetti", sta.getDescription());
        assertEquals("At creation the number of passengers in transit is 0.", 
                0, sta.getNumberPassengersInTransit());
    }

    @Test
    public void testAddPassenger() {
        sta.addPassenger();
        assertEquals("After adding a passenger for the first time, the number of passengers in transit must be 1.", 
                1, sta.getNumberPassengersInTransit());
    }

    @Test
    public void testAddGroupPassengers() {
        sta.addGroupPassengers(5);
        assertEquals("After adding a group of passengers for the first time, the number of passengers in transit must be consistent.", 
                5, sta.getNumberPassengersInTransit());
    }

    @Test
    public void testAddPassengerAndThenGroupPassengers() {
        sta.addPassenger();
        sta.addGroupPassengers(3);
        assertEquals("After adding a passenger and then a group of passengers" +
        " the number of passengers in transit must be consistent.", 
                4, sta.getNumberPassengersInTransit());
    }

    @Test
    public void testAggiungiGruppoPasseggeriEPoiPasseggero() {
        sta.addGroupPassengers(2);
        sta.addPassenger();
        assertEquals("After adding a passenger and then a group of passengers" +
        " the number of passengers in transit must be consistent.", 
                3, sta.getNumberPassengersInTransit());
    }
}

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

public class TrenoTest {

    private Train trainTest;
    private Station stationTest;

    @Before
    public void setUp() throws Exception {
        trainTest = new Train("testRM001", "Yellow arrow", stationTest);
    }

    @Test
    public void testConstructorTrain() {
        assertEquals("L'id from the train was not initialized correctly", 
                "testRM001", trainTest.getId());
        assertEquals("The description from the train was not initialized correctly", 
                "Yellow arrow", trainTest.getDescription());
        assertEquals("The train station was not initialized correctly. ", 
                stationTest, trainTest.getParkingPlace());
    }

    @Test
    public void testRuns() {
        Station st1 = new Station("STRM0001","Giardinetti");
        trainTest.setParkingPlace(st1);
        Station st2 = new Station("STRM0002","Termini");
        assertEquals("The departure station does not coincide with the train station.", 
                st1, trainTest.getParkingPlace());
        trenoTest.effettuaCorsa(st2);
        assertEquals("The arrival station does not coincide with the train station.", 
                st2, trainTest.getParkingPlace());
        String log = "The testRM001 train travels from Giardinetti station to Termini station.";
        assertEquals("The log does not report correct information.", log, trainTest.getLog());
    }

    @Test
    public void testAddPassenger() {
        trainTest.addPassenger();
        assertEquals("After adding a passenger for the first time, the number of passengers in transit must be 1.", 
                1, trainTest.getNumberPassengersOnBoard());
    }

    @Test
    public void testAddGroupPassengers() {
        trainTest.addGroupPassengers(5);
        assertEquals("After adding a group of passengers for the first time, the number of passengers in transit must be consistent.", 
                5, trainTest.getNumberPassengersOnBoard());
    }

    @Test
    public void testAddPassengerAndThenGroupPassengers() {
        trainTest.addPassenger();
        trainTest.addGroupPassengers(3);
        assertEquals("After adding a passenger and then a group of passengers" +
        " the number of passengers in transit must be consistent.", 
                4, trainTest.getNumberPassengersOnBoard());
    }

    @Test
    public void testAddGroupPassengersAndThenPassenger() {
        trainTest.addGroupPassengers(2);
        trainTest.addPassenger();
        assertEquals("After adding a passenger and then a group of passengers " +
        " the number of passengers in transit must be consistent.", 
                3, trainTest.getNumberPassengersOnBoard());
    }
}

运行这两个类会出现以下错误:
在增加一名乘客和一组乘客之后,在途乘客人数必须保持一致。应为:<3>,但为:<2>。
首次增加一组旅客后,在途旅客人数必须一致。应为:<5>,但为:<1>。
在增加一名乘客和一组乘客之后,在途乘客人数必须保持一致。应为:<4>,但为:<2>。
如何修复上述三个错误?

lbsnaicq

lbsnaicq1#

public void addGroupPassengers(int num) {

    if(numberPassengersOnBoard+num<NUMBER_MAX_PASSENGERS)
        numberPassengersOnBoard++;
    else
        System.out.println("Error");

}

这部分说明,如果当前乘客人数+您要添加的组中的人数<最大乘客人数,则您只向当前组添加一名乘客。把它换成这个应该能让它工作。

public void addGroupPassengers(int num) {

    if(numberPassengersOnBoard+num<NUMBER_MAX_PASSENGERS)
        numberPassengersOnBoard+= num;
    else
        System.out.println("Error");

}

相关问题