我找不到隐藏的错误

时间:2019-03-15 22:52:57

标签: java

我已经完成我的代码,并且可以正确打印,但是我有一个测试器类,而我的类代码的某些部分却没有打印出应有的内容,请帮忙。我已经尝试了许多方法来修复它,但是它无法正常工作。

测试器类:

AsyncLoadingCache<Location, ClimaTempoLocale> localeWeatherCache = 
    Caffeine.newBuilder().buildAsync(location -> 
        climaTempoRepository.findLocaleByCityNameAndState(location.city(), location.state()));

AsyncLoadingCache<ClimaTempoLocale, CurrentWeather> currentWeatherCache =
    Caffeine.newBuilder().buildAsync(climaTempoRepository::findCurrentWeatherByLocale);

AsyncLoadingCache<ClimaTempoLocale, WeatherForecasts> weatherForecastsCache =
    Caffeine.newBuilder().buildAsync(climaTempoRepository::findDailyForecastByLocale);

public Mono<Weather> weatherForecastByLocation(Location location) {
  var locale = Mono.fromFuture(localeWeatherCache.get(location));
  var currentWeather = Mono.fromFuture(locale.map(localeWeatherCache::get));
  var weatherForecasts = Mono.fromFuture(locale.map(weatherForecastsCache::get));

  return Mono.zip(currentWeather, weatherForecasts, (current, forecasts) ->
      Weather.buildWith(builder -> {
          builder.location = location;
          builder.currentWeather = current;
          builder.weatherForecasts = forecasts;
      }));
}

我这堂课有问题。

  public class StoreTester
  {
  public static void main(String[] args)
  {
    //1. First Constructor and toString
    Store store1 = new Store("grocery");
    System.out.println("1. \nOUTPUT: Grocery store is open from 10am to 
    9pm");
    System.out.println("YOUR OUTPUT: "+store1);
    System.out.println();

    //2. Second constructor and toString
    Store store2 = new Store("pet", 8, 18);
    System.out.println("2.\nOUTPUT: Pet store is open from 8am to 6pm");
    System.out.println("YOUR OUTPUT: "+store2);
    System.out.println();

    //3. getType
    Store store3 = new Store ("grocery", 0, 23);
    System.out.println("3.\nOUTPUT: grocery");
    System.out.println("YOUR OUTPUT: "+store3.getType());
    System.out.println();

    //4. getNumHoursOpen
    Store store4 = new Store ("pet", 7, 20);
    System.out.println("4.\nOUTPUT: 13");
    System.out.println("YOUR OUTPUT: "+store4.getNumHoursOpen());
    System.out.println();

    //5. isOpen (true - store is open)
    Store store5 = new Store ("grocery");
    System.out.println("5.\nOUTPUT: true");
    System.out.println("YOUR OUTPUT: "+store5.isOpen(12));
    System.out.println();

    //6. isOpen (false - store is closed)
    Store store6 = new Store ("pet", 12,20);
    System.out.println("6.\nOUTPUT: false");
    System.out.println("YOUR OUTPUT: "+store6.isOpen(11));
    System.out.println();

    //7a. setType - invalid
    Store store7 = new Store("pet");
    System.out.println("7a.\nOUTPUT: false");
    System.out.println("YOUR OUTPUT: "+store7.setType("card"));
    System.out.println();

    //7b. type not changed
    System.out.println("7b.\nOUTPUT: pet");
    System.out.println("YOUR OUTPUT: "+store7.getType());
    System.out.println();

    //8a. setType - valid
    Store store8 = new Store("pet");
    System.out.println("8a.\nOUTPUT: true");
    System.out.println("YOUR OUTPUT: "+store8.setType("grocery"));
    System.out.println();

    //8b. type changed
    System.out.println("8b.\nOUTPUT: grocery");
    System.out.println("YOUR OUTPUT: "+store8.getType());
    System.out.println();

    //9. convertTime - am
    System.out.println("9.\nOUTPUT: 11am");
    System.out.println("YOUR OUTPUT: "+Store.convertTime(11));
    System.out.println();

    //10. convertTime - pm
    System.out.println("10.\nOUTPUT: 5pm");
    System.out.println("YOUR OUTPUT: "+Store.convertTime(17));
    System.out.println();

    //11a. setHours - valid
    Store store11 = new Store("grocery");
    System.out.println("11a.\nOUTPUT: true");
    System.out.println("YOUR OUTPUT: "+store11.setHours(5,23));
    System.out.println();

    //11b. Check if new Hours were set
    System.out.println("11b.\nOUTPUT: Grocery store is open from 5am to 
    11pm");
    System.out.println("YOUR OUTPUT: " + store11);
    System.out.println();

    //12a. setHours - invalid
    Store store12 = new Store("pet");
    System.out.println("12a.\nOUTPUT: false");
    System.out.println("YOUR OUTPUT: "+store12.setHours(11,24));
    System.out.println();

    //12b. Hours should not have changed
    System.out.println("12b.\nOUTPUT: Pet store is open from 10am to 9pm");
    System.out.println("YOUR OUTPUT: " + store12);
    System.out.println();


    //13a. setHours - invalid
    Store store13 = new Store("pet");
    System.out.println("13a.\nOUTPUT: false");
    System.out.println("YOUR OUTPUT: "+store13.setHours(12,4));
    System.out.println();

    //13b. Hours should not have changed because open is after close
    System.out.println("13b.\nOUTPUT: Pet store is open from 10am to 9pm");
    System.out.println("YOUR OUTPUT: " + store13);
    System.out.println();
   }
   }  

这是它的“打印”,因为您会看到一些与他们应打印的内容不匹配。

// type of store (String)
// opening time (int)
// closing time (int)
private String type;
private int opening;
private int closing;
private int hours;


// First constructor: One parameter(store type), opening time set to 10 (10am) and closing time set to 21 (9pm)
public Store(String type)
{
opening = 10;
closing = 21;

}
// Second constructor: 3 parameters: store type, opening time and closing time
public Store(String _type, int _openTime, int _closeTime)
{
type    = _type;
opening = _openTime;
closing = _closeTime;
}

// Return the type of store
public String getType()
{
return type;
}
// Return the number of hours open.
// For example, if the store opens at 10 and closes at 18, the number of hours open is 8
public int getNumHoursOpen()
{
hours = closing - opening;
return hours;
}

// If the store is open at currentTime, return true
// If the store is not open at currentTime, return false
public boolean isOpen(int currentTime)
{
if((currentTime >= opening) || (currentTime < closing))
{
  return true;
}
else 
{
  return false;
}
}

// If newType is Grocery or Pet (not case sensitive), set the type and return true;
// Otherwise, the type remains unchanged and false is returned.
public boolean setType(String newType)
{
  type = newType;
  return true;

}

// if newOpen and newClose are both in the range [0,23] and the opening time is before the closing time,
// set the open and close times to the new times and return true.
// Otherwise, no times are changed and return false
public boolean setHours(int newOpen, int newClose)
{
  opening = newOpen;
closing = newClose;
return true;
}

// Converts timeToConvert from 24 hour time to 12 hour time.
// For example, convertTime(5) will return the value 5am
// convertTime(14) will return the value 2pm.
// Note: This method is static.   That means that if you are testing this 
method from another program, your method
// call will be similar to:   Store.convertTime(15);
// If you want to call this method from within the Store class, you can call 
it like normal.  For example: convertTime(14)
public static String convertTime(int timeToConvert)
{
if (timeToConvert >= 13){
  timeToConvert = timeToConvert % 12;
}
return timeToConvert + "";
}

// toString - returns <store type> store is open from <open time> to <close time>
// For example: grocery store is open from 10am to 11pm
// Note: call your convertTime method to convert the time to am and pm
// Since the convertTime method is in the same class, it can be called by just using the name of the method and the time to convert
// For example: convertTime(15)  -- the 15 can be replaced by any int 
variable.  A string will be returned from the convertTime method

public String toString()
{

return type;

}
}

2 个答案:

答案 0 :(得分:0)

isOpen(int time)的条件错误。您使用或||,而应使用和&&,否则,如果第一个条件为true,则不会检查第二个条件,因此将标记所有大于关闭时间的值确实如此。

将其更改为

public boolean isOpen(int currentTime)
{
    if((currentTime >= opening) && (currentTime < closing))
    {
        return true;
    }
    else 
    {
        return false;
    }

}

案例1编写得不好。您正在尝试直接打印对象;它应该返回类似于对象实例代码的内容。您需要的是传递给toString方法的东西。而且,仅接收1个类型为String的参数的构造函数不会分配Type,而只分配打开和关闭。应该是

public Store(String type)
{
    this.opening = 10;
    this.closing = 21;
    this.type = type;
}

答案 1 :(得分:-2)

代码中有很多小错误。很难单独解释所有这些。检查这些课程并与您的课程进行比较。

public class StoreTester {

    public static void main(String[] args) {
        //1. First Constructor and toString
        Store store1 = new Store("Grocery");
        System.out.println("1. \nOUTPUT: Grocery store is open from 10am to 9pm");
        System.out.println("YOUR OUTPUT: " + store1);
        System.out.println();

        //2. Second constructor and toString
        Store store2 = new Store("Pet", 8, 18);
        System.out.println("2.\nOUTPUT: Pet store is open from 8am to 6pm");
        System.out.println("YOUR OUTPUT: " + store2);
        System.out.println();

        //3. getType
        Store store3 = new Store("grocery", 0, 23);
        System.out.println("3.\nOUTPUT: grocery");
        System.out.println("YOUR OUTPUT: " + store3.getType());
        System.out.println();

        //4. getNumHoursOpen
        Store store4 = new Store("pet", 7, 20);
        System.out.println("4.\nOUTPUT: 13");
        System.out.println("YOUR OUTPUT: " + store4.getNumHoursOpen());
        System.out.println();

        //5. isOpen (true - store is open)
        Store store5 = new Store("grocery");
        System.out.println("5.\nOUTPUT: true");
        System.out.println("YOUR OUTPUT: " + store5.isOpen(12));
        System.out.println();

        //6. isOpen (false - store is closed)
        Store store6 = new Store("pet", 12, 20);
        System.out.println("6.\nOUTPUT: false");
        System.out.println("YOUR OUTPUT: " + store6.isOpen(11));
        System.out.println();

        //7a. setType - invalid
        Store store7 = new Store("pet");
        System.out.println("7a.\nOUTPUT: false");
        System.out.println("YOUR OUTPUT: " + store7.setType("card"));
        System.out.println();

        //7b. type not changed
        System.out.println("7b.\nOUTPUT: pet");
        System.out.println("YOUR OUTPUT: " + store7.getType());
        System.out.println();

        //8a. setType - valid
        Store store8 = new Store("pet");
        System.out.println("8a.\nOUTPUT: true");
        System.out.println("YOUR OUTPUT: " + store8.setType("grocery"));
        System.out.println();

        //8b. type changed
        System.out.println("8b.\nOUTPUT: grocery");
        System.out.println("YOUR OUTPUT: " + store8.getType());
        System.out.println();

        //9. convertTime - am
        System.out.println("9.\nOUTPUT: 11am");
        System.out.println("YOUR OUTPUT: " + Store.convertTime(11));
        System.out.println();

        //10. convertTime - pm
        System.out.println("10.\nOUTPUT: 5pm");
        System.out.println("YOUR OUTPUT: " + Store.convertTime(17));
        System.out.println();

        //11a. setHours - valid
        Store store11 = new Store("grocery");
        System.out.println("11a.\nOUTPUT: true");
        System.out.println("YOUR OUTPUT: " + store11.setHours(5, 23));
        System.out.println();

        //11b. Check if new Hours were set
        System.out.println("11b.\nOUTPUT: Grocery store is open from 5am to 11pm");
        System.out.println("YOUR OUTPUT: " + store11);
        System.out.println();

        //12a. setHours - invalid
        Store store12 = new Store("pet");
        System.out.println("12a.\nOUTPUT: false");
        System.out.println("YOUR OUTPUT: " + store12.setHours(11, 24));
        System.out.println();

        //12b. Hours should not have changed
        System.out.println("12b.\nOUTPUT: Pet store is open from 10am to 9pm");
        System.out.println("YOUR OUTPUT: " + store12);
        System.out.println();


        //13a. setHours - invalid
        Store store13 = new Store("pet");
        System.out.println("13a.\nOUTPUT: false");
        System.out.println("YOUR OUTPUT: " + store13.setHours(12, 4));
        System.out.println();

        //13b. Hours should not have changed because open is after close
        System.out.println("13b.\nOUTPUT: Pet store is open from 10am to 9pm");
        System.out.println("YOUR OUTPUT: " + store13);
        System.out.println();
    }
}

public class Store {

    // type of store (String)
    // opening time (int)
    // closing time (int)
    private String type;
    private int opening;
    private int closing;
    private int hours;

    // First constructor: One parameter(store type), opening time set to 10 (10am) and closing time set to 21 (9pm)
    public Store(String type) {
        this.type = type;
        opening = 10;
        closing = 21;

    }

    // Second constructor: 3 parameters: store type, opening time and closing time
    public Store(String _type, int _openTime, int _closeTime) {
        type = _type;
        opening = _openTime;
        closing = _closeTime;
    }

    // Return the type of store
    public String getType() {
        return type;
    }

    // Return the number of hours open.
    // For example, if the store opens at 10 and closes at 18, the number of hours open is 8
    public int getNumHoursOpen() {
        hours = closing - opening;
        return hours;
    }

    // If the store is open at currentTime, return true
    // If the store is not open at currentTime, return false
    public boolean isOpen(int currentTime) {
        if ((currentTime >= opening) && (currentTime < closing)) {
            return true;
        } else {
            return false;
        }
    }

    // If newType is Grocery or Pet (not case sensitive), set the type and return true;
    // Otherwise, the type remains unchanged and false is returned.
    public boolean setType(String newType) {
        if("grocery".equalsIgnoreCase(newType) || "pet".equalsIgnoreCase(newType)) {
            type = newType;
            return true;
        }
        return false;
    }

    // if newOpen and newClose are both in the range [0,23] and the opening time is before the closing time,
    // set the open and close times to the new times and return true.
    // Otherwise, no times are changed and return false
    public boolean setHours(int newOpen, int newClose) {
        if(newOpen < newClose && newClose < 24) {
            opening = newOpen;
            closing = newClose;
            return true;
        }
        return false;
    }

    // Converts timeToConvert from 24 hour time to 12 hour time.
    // For example, convertTime(5) will return the value 5am
    // convertTime(14) will return the value 2pm.
    // Note: This method is static.   That means that if you are testing this
    // method from another program, your method
    // call will be similar to:   Store.convertTime(15);
    // If you want to call this method from within the Store class, you can call
    // it like normal.  For example: convertTime(14)
    public static String convertTime(int timeToConvert) {
        if (timeToConvert > 12) {
            timeToConvert = timeToConvert - 12;
            return timeToConvert + "pm";
        }
        return timeToConvert + "am";
    }

    // toString - returns <store type> store is open from <open time> to <close time>
    // For example: grocery store is open from 10am to 11pm
    // Note: call your convertTime method to convert the time to am and pm
    // Since the convertTime method is in the same class, it can be called by just using the name of the method and the time to convert
    // For example: convertTime(15)  -- the 15 can be replaced by any int
    // variable.  A string will be returned from the convertTime method
    @Override
    public String toString() {
        String time = convertTime(closing);
        return type + " store is open from " + opening + "am to " + time;
    }
}