简体   繁体   English

Java中的2个相同的类

[英]2 identical classes in Java

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. 现在,我所做的是我制作了Customer类的另一个实例,该实例表示给定的时间,然后检查它是否与任何“真实” 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. 我需要与Customer类完全相同的方法和变量,但是我认为仅使Customer类代表随机时间而不是特定客户的时间不是一个好的设计选择。

Thank you very much! 非常感谢你!

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. 现在,我所做的是我制作了Customer类的另一个实例,该实例表示给定的时间,然后检查它是否与任何“真实” Customer相匹配。

That's not what you want to do. 那不是你想做的。

What you want to do is go through each customer with one time, and see if that time is between the enterTime and leaveTime . 您要做的是遍历每个客户一次,看看该时间是否介于enterTimeleaveTime之间。

Edited to answer the question: 编辑以回答问题:

I agree that the class is badly named. 我同意班名不好。 The information is about a customer, so I named it CustomerVisit . 该信息与客户有关,因此我将其命名为CustomerVisit

Your CustomerVisit class should look more like this: 您的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;
    }
}

Instead of storing the time as a String , you need to store it in an object made for date / time calculations, like Calendar . 除了将时间存储为String ,您还需要将其存储在用于日期/时间计算的对象中,例如Calendar

I was working on adding a SimpleDateFormat so you can display the enterTime and the leaveTime as a String. 我正在添加SimpleDateFormat以便可以将enterTimeleaveTime显示为字符串。

With the isVisit method, you can loop through your CustomerVisit instances, and see which customers, or how many customers, visited at the givenTime . 使用isVisit方法,您可以遍历CustomerVisit实例,并查看在给givenTime访问了哪些客户或多少客户。

The setVisit method takes the String from the file, and converts the String to Calendar objects. setVisit方法从文件中获取String ,并将String转换为Calendar对象。

You should not need a new type of customer for this problem. 您不需要新的客户类型来解决此问题。

When you say time I presume you mean within a certain range. 当您说时间时,我想您的意思是在一定范围内。 I would do something like this: 我会做这样的事情:

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;
     }   
}  

you need to update your Customer class like so: 您需要像这样更新Customer类:

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

As it stands currently, you have a dangerous API and one that doesn't work: 从目前的情况来看,您有一个危险的API,一个无效的API:

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

One this doesn't return an int, which is what you most likely want to do. 其中一个不返回int,这是您最可能想做的。 Also, you have not defensively coded against the case that enterTime is not a valid integer. 同样,您也没有针对enterTime不是有效整数的情况进行防御性编码。 Further most dates are treated as a long 此外,大多数日期被视为long

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有相同名称的两个相同类之间的Java差异 - Java difference between two identical classes with identical name 相同的 Java 源代码编译为二进制不同的类 - Identical Java sources compile to binary differing classes 使用第三方库在Java中映射两个相同的类 - Mapping two identical classes in java with third party library 声明许多相同的匿名类会浪费java中的内存吗? - Does declaring many identical anonymous classes waste memory in java? 如何检查两个Java类在语义上是否相同? - How can I check if two Java classes are semantically identical? 在Java中管理具有几乎相同属性的2个类的最佳方法 - Best way to manage 2 classes with nearly identical properties in Java Java的instanceof可以区分功能相同的类吗? - Can Java's instanceof differentiate functionally identical classes? Java ClassCastException,具有通过网络发送的两个不同包的相同类 - Java ClassCastException with two identical classes in different packages, sent over the network Java - 为什么System和Runtime类具有相同的方法? - Java - why System and Runtime classes have identical methods? 两个类共享相同和相似但不同的方法的 Java 设计模式 - Java design pattern for two classes sharing identical and similar but different methods
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM