找来找去就一道题,OOD设计货物和货车的class
Glassdoor上也有相关描述:
You start off with a straightforward OOP question of designing a class with a constructor.
The company makes sensors for shipping and trucks so the class was for a Trailer and a Container.
You do some simple functions related to adding containers to the trailer,
and then you to a function where they want to know how to determine a Trailer’s weight at any given time.
Write a load(), unload() and weight() function .
试着写了一下
代码
import java.util.*;
class Container {
private double weight;
private int id;
public Container(int id, double weight) {
this.weight = weight;
this.id = id;
}
public double weight() {
return this.weight;
}
public int getId() {
return this.id;
}
}
class Trailer {
private Map<Integer, Container> cMap;
private Map<Long, Double> timeWtMap;
private double total;
public Trailer() {
total = 0;
cMap = new HashMap<>();
timeWtMap = new TreeMap<>();
}
public void load(Container ct) {
cMap.put(ct.getId(), ct);
total += ct.weight();
long timeNow = System.currentTimeMillis();
timeWtMap.put(timeNow, total);
}
public Container unload(int id) {
Container ct = cMap.get(id);
cMap.remove(id);
long timeNow = System.currentTimeMillis();
total -= ct.weight();
timeWtMap.put(timeNow, total);
return ct;
}
public double totalWeight() {
return this.total;
}
public double getTotalWeight(long timestamp) {
double sum = 0;
for (long time : timeWtMap.keySet()) {
if (time <= timestamp) {
sum = timeWtMap.get(time);
} else {
System.out.println("not yet: " + new Date(time));
return sum;
}
}
return sum;
}
public int size() {
return cMap.size();
}
}
class TMS {
public static void main(String[] args) {
Container ct1 = new Container(1, 10.0);
Container ct2 = new Container(2, 20.0);
Trailer t = new Trailer();
t.load(ct1);
long timeNow1 = System.currentTimeMillis();
System.out.println(new Date(timeNow1));
System.out.println("current total weight: " + t.totalWeight());
System.out.println("current number of containers: " + t.size());
//sleep for a second
try
{
Thread.sleep(1000);
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
t.load(ct2);
long timeNow2 = System.currentTimeMillis();
System.out.println(new Date(timeNow2));
System.out.println("current total weight: " + t.totalWeight());
System.out.println("current number of containers: " + t.size());
//sleep for a second
try
{
Thread.sleep(1000);
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
t.unload(2);
long timeNow3 = System.currentTimeMillis();
System.out.println(new Date(timeNow3));
System.out.println("current total weight: " + t.totalWeight());
System.out.println("current number of containers: " + t.size());
long timeNow = System.currentTimeMillis();
System.out.println(new Date(timeNow));
System.out.println(t.getTotalWeight(timeNow));
long timeBefore = timeNow - 800;
System.out.println(new Date(timeBefore));
System.out.println(t.getTotalWeight(timeBefore));
}
}
输出
Wed Sep 11 12:55:23 EDT 2019
current total weight: 10.0
current number of containers: 1
Wed Sep 11 12:55:25 EDT 2019
current total weight: 30.0
current number of containers: 2
Wed Sep 11 12:55:26 EDT 2019
current total weight: 10.0
current number of containers: 1
Wed Sep 11 12:55:26 EDT 2019
10.0
Wed Sep 11 12:55:25 EDT 2019
not yet: Wed Sep 11 12:55:26 EDT 2019
30.0