0

Hi!

I have a small design problem in my current project. I have a class that holds the information about the time a Customer was in a Shop.

public class Customer {
    private String enterTime;
    private String leaveTime;

    public void enterTimeToInt {
        Integer.parseInt(enterTime);
    } 

    public void leaveTimeToInt {
        Integer.parseInt(leaveTime);
    } 
}

Now, in my program, i came across a situation where i had to make sure if there was any customer in the shop at a given time. Now, what i did was that i made another instance of the Customer class, which represented the given time and then checked if it matches any "real" Customer. But the problem is though, that i made a customer object representing time in a more abstract sense, not a customer.

What should i do in this case? I need exactly the same methods and variables as the Customer class has, but i don't think it is a good design choice to just make a Customer class that represents random time, not a specific customer's time.

Thank you very much!

4

2 回答 2

6

现在,我所做的是我创建了 Customer 类的另一个实例,它表示给定时间,然后检查它是否匹配任何“真实”客户。

那不是你想做的。

您要做的是用一次遍历每个客户,看看该时间是否在enterTime和之间leaveTime

编辑以回答问题:

我同意这门课的名字很糟糕。该信息是关于一个客户的,所以我将其命名为CustomerVisit

你的CustomerVisit班级应该看起来更像这样:

import java.util.Calendar;

public class CustomerVisit {
    private Calendar enterTime;
    private Calendar leaveTime;


    public Calendar getEnterTime() {
        return enterTime;
    }

    public void setEnterTime(Calendar enterTime) {
        this.enterTime = enterTime;
    }

    public Calendar getLeaveTime() {
        return leaveTime;
    }

    public void setLeaveTime(Calendar leaveTime) {
        this.leaveTime = leaveTime;
    }

    public boolean isVisit(Calendar givenTime) {
        if (givenTime.after(enterTime) && givenTime.before(leaveTime)) {
            return true;
        } else {
            return false;
        }
    }

    public void setVisit(String visitString) {
        // File format is hh:mm-hh:mm
        String[] times = visitString.split("-");
        String[] enterTimeString = times[0].split(":");
        String[] leaveTimeString = times[1].split(":");

        enterTime = setCalendar(enterTimeString);
        leaveTime = setCalendar(leaveTimeString);
    }

    private Calendar setCalendar(String[] timeString) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 
                Integer.valueOf(timeString[0]));
        calendar.set(Calendar.MINUTE, 
                Integer.valueOf(timeString[1]));
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar;
    }
}

String您需要将时间存储在用于日期/时间计算的对象中,而不是将时间存储为Calendar.

我正在努力添加一个SimpleDateFormat,以便您可以将enterTime和 显示leaveTime为字符串。

使用该isVisit方法,您可以遍历您的CustomerVisit实例,并查看在givenTime.

setVisit方法String从文件中获取 ,并将 转换StringCalendar对象。

于 2013-06-02T21:35:56.273 回答
2

对于这个问题,您不应该需要新类型的客户。

当您说时间时,我认为您的意思是在一定范围内。我会做这样的事情:

public class CustomerRange  
{  
     Collection<Customer> customers;  

     ...   

     public boolean customerInShop(Date time)  
     {  
            for(Customer customer : customers)  
            {  
                  if(customer.getStart().after(time) && customer.getEnd().before(time))  
                  {
                      return true;
                  }  
            }  
            return false;
     }   
}  

您需要像这样更新您的客户类:

public class Customer  
{  
     Date start;  
     Date end;  
       ...
}  

就目前而言,您有一个危险的 API 并且一个不起作用:

public void enterTimeToInt {
        Integer.parseInt(enterTime);
    } 

一个 this 不返回 int,这是您最可能想要做的。enterTime此外,您还没有针对不是有效整数的情况进行防御性编码。此外,大多数日期被视为long

于 2013-06-02T21:38:08.477 回答