设计物流系统(面向对象的设计)。讲述不同的类以及它们之间的关系。这不是系统设计问题,所以这个问题的范围只是定义不同的类(带有它的属性和方法)
提问: Adobe
解决方案:假设我们要设计一个具有以下基本功能的物流系统:
• 系统可以接受订单将其运送到指定目的地。
• 订单将是一个项目列表,并且每个订单都需要处理成本。
• 用户必须自己注册才能使用本系统。
• 用户可以跟踪他/她的订单。
• 订单将通过自行车或卡车运送,但单一车辆只能运送一个订单。
面试中会问这些类型的问题,以判断候选人的面向对象设计技能。所以,首先我们应该考虑类。
主要课程将是:
1. 用户
2. 项目
3. 车辆
4. 地点
5. 付款明细
6. 订购
7. 物流系统
User 类适用于用户/客户/客户,他们将被收取交付物品的费用。
Java
public class User {
private int userId;
private String name;
private Location address;
private String mobNo;
private String emailId;
public int getUserId()
{
return userId;
}
public void setUserId(int userId)
{
this.userId = userId;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Location getAddress()
{
return address;
}
public void setAddress(Location address)
{
this.address = address;
}
public String getMobNo()
{
return mobNo;
}
public void setMobNo(String mobNo)
{
this.mobNo = mobNo;
}
public String getEmailId()
{
return emailId;
}
public void setEmailId(String emailId)
{
this.emailId = emailId;
}
}
Java
public class Item {
private String name;
private int price;
private int volume;
private int weight;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getPrice()
{
return price;
}
public void setPrice(int price)
{
this.price = price;
}
public int getVolume()
{
return volume;
}
public void setVolume(int volume)
{
this.volume = volume;
}
public int getWeight()
{
return weight;
}
public void setWeight(int weight)
{
this.weight = weight;
}
}
Java
public class Location {
private Double longitude;
private Double latitude;
public Double getLongitude()
{
return longitude;
}
public void setLongitude(Double longitude)
{
this.longitude = longitude;
}
public Double getLatitude()
{
return latitude;
}
public void setLatitude(Double latitude)
{
this.latitude = latitude;
}
}
Java
public class Vehicle {
private int id;
private String vehicleNo;
private int capacity;
private Location currentPosition;
private VehicleStatus currentStatus;
public Vehicle(int id, String vehicleNo, int capacity)
{
this.id = id;
this.vehicleNo = vehicleNo;
this.capacity = capacity;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getVehicleNo()
{
return vehicleNo;
}
public void setVehicleNo(String vehicleNo)
{
this.vehicleNo = vehicleNo;
}
public int getCapacity()
{
return capacity;
}
public void setCapacity(int capacity)
{
this.capacity = capacity;
}
public Location getCurrentPosition()
{
return currentPosition;
}
public void setCurrentPosition(Location currentPosition)
{
this.currentPosition = currentPosition;
}
public VehicleStatus getCurrentStatus()
{
return currentStatus;
}
public void setCurrentStatus(VehicleStatus currentStatus)
{
this.currentStatus = currentStatus;
}
}
Java
public class Bike extends Vehicle {
private final static int capacityofBike = 10;
public Bike(int id, String vehicleNo)
{
super(id, vehicleNo, capacityofBike);
}
}
Java
public class Truck extends Vehicle {
private final static int capacityofTruck = 100;
public Truck(int id, String vehicleNo)
{
super(id, vehicleNo, capacityofTruck);
}
}
Java
public enum VehicleStatus {
FREE,
BUSY,
NOT_WORKING;
}
Java
public enum OrderPriority {
LOW,
MEDIUM,
HIGH;
}
Java
public enum OrderStatus {
DELIVERED,
PROCESSING,
CANCELLED;
}
Java
public enum PaymentMode {
NET_BANKING,
CREDIT_CARD,
DEBIT_CARD;
}
Java
public enum PaymentStatus {
PAID,
UNPAID;
}
Java
public class PaymentDetails {
private PaymentMode paymentMode;
private String transactionId;
private int amount;
private PaymentStatus paymentStatus;
private String cardNumber;
public PaymentMode getPaymentMode()
{
return paymentMode;
}
public void setPaymentMode(PaymentMode paymentMode)
{
this.paymentMode = paymentMode;
}
public String getTransactionId()
{
return transactionId;
}
public void setTransactionId(String transactionId)
{
this.transactionId = transactionId;
}
public int getAmount()
{
return amount;
}
public void setAmount(int amount)
{
this.amount = amount;
}
public PaymentStatus getPaymentStatus()
{
return paymentStatus;
}
public void setPaymentStatus(PaymentStatus paymentStatus)
{
this.paymentStatus = paymentStatus;
}
public String getCardNumber()
{
return cardNumber;
}
public void setCardNumber(String cardNumber)
{
this.cardNumber = cardNumber;
}
}
Java
import java.util.Date;
import java.util.List;
public class Order {
private int orderId;
private OrderPriority priority_of_order;
private User sender;
private Location destination;
private PaymentDetails paymentDetails;
private List- items;
private Long totalWeight;
private OrderStatus currentStatus;
private Date timeOfOrderPlaced;
private Date timeOfOrderDelivery;
private Vehicle vehicleOfThisOrder;
public int getOrderId()
{
return orderId;
}
public void setOrderId(int orderId)
{
this.orderId = orderId;
}
public OrderPriority getPriority_of_order()
{
return priority_of_order;
}
public void setPriority_of_order(OrderPriority priority_of_order)
{
this.priority_of_order = priority_of_order;
}
public User getSender()
{
return sender;
}
public void setSender(User sender)
{
this.sender = sender;
}
public Location getDestination()
{
return destination;
}
public void setDestination(Location destination)
{
this.destination = destination;
}
public PaymentDetails getPaymentDetails()
{
return paymentDetails;
}
public void setPaymentDetails(PaymentDetails paymentDetails)
{
this.paymentDetails = paymentDetails;
}
public List
- getItems()
{
return items;
}
public void setItems(List
- items)
{
this.items = items;
}
public Long getTotalWeight()
{
return totalWeight;
}
public void setTotalWeight(Long totalWeight)
{
this.totalWeight = totalWeight;
}
public OrderStatus getCurrentStatus()
{
return currentStatus;
}
public void setCurrentStatus(OrderStatus currentStatus)
{
this.currentStatus = currentStatus;
}
public Date getTimeOfOrderPlaced()
{
return timeOfOrderPlaced;
}
public void setTimeOfOrderPlaced(Date timeOfOrderPlaced)
{
this.timeOfOrderPlaced = timeOfOrderPlaced;
}
public Date getTimeOfOrderDelivery()
{
return timeOfOrderDelivery;
}
public void setTimeOfOrderDelivery(Date timeOfOrderDelivery)
{
this.timeOfOrderDelivery = timeOfOrderDelivery;
}
public Vehicle getVehicleOfThisOrder()
{
return vehicleOfThisOrder;
}
public void setVehicleOfThisOrder(Vehicle vehicleOfThisOrder)
{
this.vehicleOfThisOrder = vehicleOfThisOrder;
}
}
Java
import java.util.ArrayList;
import java.util.List;
public class LogisticsSystem {
private List orders;
private List vehicles;
private List users;
public LogisticsSystem()
{
this.orders = new ArrayList();
this.vehicles = new ArrayList();
this.users = new ArrayList();
}
public void takeAnOrder(Order order)
{
System.out.println("Adding an order to the system");
orders.add(order);
}
public void processOrder(Order order)
{
System.out.println("Processing an order of the system");
}
public Location trackOrder(int orderId)
{
System.out.println("Tracking an order of the system");
Location location = null;
// location = findLocationOfGivenOrder();
return location;
}
public void cacelOrder(Order order)
{
System.out.println("Going to cancell an order of the system");
}
public void registerNewUser(User user)
{
System.out.println("Registering a new user to the system");
users.add(user);
}
public List getOrders()
{
return orders;
}
public void setOrders(List orders)
{
this.orders = orders;
}
public List getVehicles()
{
return vehicles;
}
public void setVehicles(List vehicles)
{
this.vehicles = vehicles;
}
public List getUsers()
{
return users;
}
public void setUsers(List users)
{
this.users = users;
}
}
Java
package com.shashi.oodesign;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Apptest {
public static void main(String[] args)
{
User user = new User();
user.setUserId(1);
user.setName("Shashi");
user.setEmailId("shashi@gmail.com");
Item item1 = new Item();
item1.setName("item1");
item1.setPrice(20);
Item item2 = new Item();
item2.setName("item2");
item2.setPrice(40);
List- items = new ArrayList
- ();
items.add(item1);
items.add(item2);
PaymentDetails paymentDetails = new PaymentDetails();
paymentDetails.setAmount(100);
paymentDetails.setPaymentMode(PaymentMode.CREDIT_CARD);
paymentDetails.setCardNumber("12345678");
Location destination = new Location();
destination.setLatitude(73.23);
destination.setLongitude(132.34);
Order order = new Order();
order.setOrderId(1);
order.setItems(items);
order.setCurrentStatus(OrderStatus.PROCESSING);
order.setDestination(destination);
order.setPaymentDetails(paymentDetails);
order.setTimeOfOrderDelivery(new Date());
LogisticsSystem logisticsSystem = new LogisticsSystem();
logisticsSystem.registerNewUser(user);
logisticsSystem.takeAnOrder(order);
logisticsSystem.processOrder(order);
}
}
Item 类表示将要运送的项目。一个订单将包含一个项目列表。
Java
public class Item {
private String name;
private int price;
private int volume;
private int weight;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getPrice()
{
return price;
}
public void setPrice(int price)
{
this.price = price;
}
public int getVolume()
{
return volume;
}
public void setVolume(int volume)
{
this.volume = volume;
}
public int getWeight()
{
return weight;
}
public void setWeight(int weight)
{
this.weight = weight;
}
}
位置类仅表示地址。
Java
public class Location {
private Double longitude;
private Double latitude;
public Double getLongitude()
{
return longitude;
}
public void setLongitude(Double longitude)
{
this.longitude = longitude;
}
public Double getLatitude()
{
return latitude;
}
public void setLatitude(Double latitude)
{
this.latitude = latitude;
}
}
Vehicle 类表示将用于运送/交付订单的车辆。
它将有两种类型:1. 自行车和 2. 卡车
Java
public class Vehicle {
private int id;
private String vehicleNo;
private int capacity;
private Location currentPosition;
private VehicleStatus currentStatus;
public Vehicle(int id, String vehicleNo, int capacity)
{
this.id = id;
this.vehicleNo = vehicleNo;
this.capacity = capacity;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getVehicleNo()
{
return vehicleNo;
}
public void setVehicleNo(String vehicleNo)
{
this.vehicleNo = vehicleNo;
}
public int getCapacity()
{
return capacity;
}
public void setCapacity(int capacity)
{
this.capacity = capacity;
}
public Location getCurrentPosition()
{
return currentPosition;
}
public void setCurrentPosition(Location currentPosition)
{
this.currentPosition = currentPosition;
}
public VehicleStatus getCurrentStatus()
{
return currentStatus;
}
public void setCurrentStatus(VehicleStatus currentStatus)
{
this.currentStatus = currentStatus;
}
}
自行车只有 10 个单位的容量。
Java
public class Bike extends Vehicle {
private final static int capacityofBike = 10;
public Bike(int id, String vehicleNo)
{
super(id, vehicleNo, capacityofBike);
}
}
卡车只有 100 个单位的容量(比自行车多 10 倍)。
Java
public class Truck extends Vehicle {
private final static int capacityofTruck = 100;
public Truck(int id, String vehicleNo)
{
super(id, vehicleNo, capacityofTruck);
}
}
车辆当前状态的枚举:
Java
public enum VehicleStatus {
FREE,
BUSY,
NOT_WORKING;
}
OrderPriority 的枚举:
Java
public enum OrderPriority {
LOW,
MEDIUM,
HIGH;
}
OrderStatus 的枚举:
Java
public enum OrderStatus {
DELIVERED,
PROCESSING,
CANCELLED;
}
PaymentMode 的枚举:
Java
public enum PaymentMode {
NET_BANKING,
CREDIT_CARD,
DEBIT_CARD;
}
PaymentStatus 的枚举:
Java
public enum PaymentStatus {
PAID,
UNPAID;
}
PaymentDetails 类包含与付款相关的信息,以便在任何情况下取消订单,都可以轻松退款。
Java
public class PaymentDetails {
private PaymentMode paymentMode;
private String transactionId;
private int amount;
private PaymentStatus paymentStatus;
private String cardNumber;
public PaymentMode getPaymentMode()
{
return paymentMode;
}
public void setPaymentMode(PaymentMode paymentMode)
{
this.paymentMode = paymentMode;
}
public String getTransactionId()
{
return transactionId;
}
public void setTransactionId(String transactionId)
{
this.transactionId = transactionId;
}
public int getAmount()
{
return amount;
}
public void setAmount(int amount)
{
this.amount = amount;
}
public PaymentStatus getPaymentStatus()
{
return paymentStatus;
}
public void setPaymentStatus(PaymentStatus paymentStatus)
{
this.paymentStatus = paymentStatus;
}
public String getCardNumber()
{
return cardNumber;
}
public void setCardNumber(String cardNumber)
{
this.cardNumber = cardNumber;
}
}
Order 类包含一个订单的所有信息。所有字段都是不言自明的。
Java
import java.util.Date;
import java.util.List;
public class Order {
private int orderId;
private OrderPriority priority_of_order;
private User sender;
private Location destination;
private PaymentDetails paymentDetails;
private List- items;
private Long totalWeight;
private OrderStatus currentStatus;
private Date timeOfOrderPlaced;
private Date timeOfOrderDelivery;
private Vehicle vehicleOfThisOrder;
public int getOrderId()
{
return orderId;
}
public void setOrderId(int orderId)
{
this.orderId = orderId;
}
public OrderPriority getPriority_of_order()
{
return priority_of_order;
}
public void setPriority_of_order(OrderPriority priority_of_order)
{
this.priority_of_order = priority_of_order;
}
public User getSender()
{
return sender;
}
public void setSender(User sender)
{
this.sender = sender;
}
public Location getDestination()
{
return destination;
}
public void setDestination(Location destination)
{
this.destination = destination;
}
public PaymentDetails getPaymentDetails()
{
return paymentDetails;
}
public void setPaymentDetails(PaymentDetails paymentDetails)
{
this.paymentDetails = paymentDetails;
}
public List
- getItems()
{
return items;
}
public void setItems(List
- items)
{
this.items = items;
}
public Long getTotalWeight()
{
return totalWeight;
}
public void setTotalWeight(Long totalWeight)
{
this.totalWeight = totalWeight;
}
public OrderStatus getCurrentStatus()
{
return currentStatus;
}
public void setCurrentStatus(OrderStatus currentStatus)
{
this.currentStatus = currentStatus;
}
public Date getTimeOfOrderPlaced()
{
return timeOfOrderPlaced;
}
public void setTimeOfOrderPlaced(Date timeOfOrderPlaced)
{
this.timeOfOrderPlaced = timeOfOrderPlaced;
}
public Date getTimeOfOrderDelivery()
{
return timeOfOrderDelivery;
}
public void setTimeOfOrderDelivery(Date timeOfOrderDelivery)
{
this.timeOfOrderDelivery = timeOfOrderDelivery;
}
public Vehicle getVehicleOfThisOrder()
{
return vehicleOfThisOrder;
}
public void setVehicleOfThisOrder(Vehicle vehicleOfThisOrder)
{
this.vehicleOfThisOrder = vehicleOfThisOrder;
}
}
主类(LogisticsSystem),存储用户、订单和车辆的所有信息。它有像 takeAnOrder()、processOrder() 等方法。
Java
import java.util.ArrayList;
import java.util.List;
public class LogisticsSystem {
private List orders;
private List vehicles;
private List users;
public LogisticsSystem()
{
this.orders = new ArrayList();
this.vehicles = new ArrayList();
this.users = new ArrayList();
}
public void takeAnOrder(Order order)
{
System.out.println("Adding an order to the system");
orders.add(order);
}
public void processOrder(Order order)
{
System.out.println("Processing an order of the system");
}
public Location trackOrder(int orderId)
{
System.out.println("Tracking an order of the system");
Location location = null;
// location = findLocationOfGivenOrder();
return location;
}
public void cacelOrder(Order order)
{
System.out.println("Going to cancell an order of the system");
}
public void registerNewUser(User user)
{
System.out.println("Registering a new user to the system");
users.add(user);
}
public List getOrders()
{
return orders;
}
public void setOrders(List orders)
{
this.orders = orders;
}
public List getVehicles()
{
return vehicles;
}
public void setVehicles(List vehicles)
{
this.vehicles = vehicles;
}
public List getUsers()
{
return users;
}
public void setUsers(List users)
{
this.users = users;
}
}
这是测试应用程序的测试类。
Java
package com.shashi.oodesign;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Apptest {
public static void main(String[] args)
{
User user = new User();
user.setUserId(1);
user.setName("Shashi");
user.setEmailId("shashi@gmail.com");
Item item1 = new Item();
item1.setName("item1");
item1.setPrice(20);
Item item2 = new Item();
item2.setName("item2");
item2.setPrice(40);
List- items = new ArrayList
- ();
items.add(item1);
items.add(item2);
PaymentDetails paymentDetails = new PaymentDetails();
paymentDetails.setAmount(100);
paymentDetails.setPaymentMode(PaymentMode.CREDIT_CARD);
paymentDetails.setCardNumber("12345678");
Location destination = new Location();
destination.setLatitude(73.23);
destination.setLongitude(132.34);
Order order = new Order();
order.setOrderId(1);
order.setItems(items);
order.setCurrentStatus(OrderStatus.PROCESSING);
order.setDestination(destination);
order.setPaymentDetails(paymentDetails);
order.setTimeOfOrderDelivery(new Date());
LogisticsSystem logisticsSystem = new LogisticsSystem();
logisticsSystem.registerNewUser(user);
logisticsSystem.takeAnOrder(order);
logisticsSystem.processOrder(order);
}
}