You are on page 1of 10

Grade settings: Maximum grade: 100

Disable external file upload, paste and drop external content: Yes
Run: Yes Evaluate: Yes
Automatic grade: Yes Maximum execution time: 240 s Maximum memory used: 1.50
GiB Maximum execution file size: 128 MiB Maximum number of processes: 10000
GO-GO Parcel Service – Courier Charge Calculation

Click here to download the Code Skeleton

GO-GO Parcel Service is one of the most widely used courier services in Tamilnadu. They are in
need of a system, which can be configured easily, to adapt to the changes they bring in with the
charges for the parcel in kilograms and need to calculate the courier charge for the customers.
You being their software consultant has been approached by them to develop a software system
which can be used for managing their business.  

Service: Calculating Courier Charge for each customer

Provided Courier class with the below private attributes as a part of code skeleton

courierId int
weight int
serviceCharge ServiceChargeInfo
chargePerKg float
 

Getter and setter methods for all the above attributes has been provided as a part of code
skeleton. Courier class should be registered as a bean with the spring container via XML file.

The value for chargePerKg is populated in charges.properties file as key=value pair. Fetch the


values from property file and assign to the private attribute chargePerKg in Courier class. The
values for these attributes should be injected via setter based injection. Do not change the value
of chargePerKg in the property file.

Provided ServiceChargeInfo class with the below private attribute as a part of code skeleton

locationServiceCharge Map<String,Float>
 

Getter and setter methods for the above attribute has been provided as a part of code skeleton.

The Map should be configured in the beans.xml file with the below key-values.

Key - City name (String) Value - Service charge (float)


Coimbatore 200.0
Chennai 300.0
Madurai 150.0
 
Note: Only for the above cities service charge needs to be added with the courierCharge. The
keys are case sensitive.

ServiceChargeInfo is used only for a particular property, so ServiceChargeInfo should be


declared as an inner bean in the Courier class. ServiceChargeInfo should be injected into Courier
via setter based Injection.

Overview of Service 1:

Write a method public double calculateCourierCharge(Courier cObj,String


city)in CourierBO class that accepts a string and Courier Object. The string contains the city
name. Based on the city, service charge needs to be added with the courier charge and return the
courierCharge. If the city name is not available in the map then there is no service charge.

Formula to calculate total courierCharge is given below:

If the city is available in the map then calculation should be as follows,

courierCharge = weight * chargePerKg;

courierCharge = courierCharge + appropriateServiceCharge;

If the city is not available in the map then calculation should be as follows,

courierCharge = weight * chargePerKg;

Technical Specifications:

Component Name Method Name Input Output Exception


CourierService calculateCourierCharg Int courierId,int double - InvalidParcelWeightExceptio
e weight,String courierCharg n
city e
This Exception to be caught
and thrown back to Main
class
CourierBO calculateCourierCharg Courier cObj, double -  
e String city courierCharg
e
beans.xml Contains all the XML      
configurations related
to Service
 

Create a class called Driver with the main method and get the inputs like courierId, weight of the
parcel and city name from the user. Get the CourierService class object from the beans.xml file
and invoke the calculateCourierCharge(courierId,weight,city) which is in
the CourierService class to perform the implementation. Display the total Courier charge which
is returned from calculateCourierCharge method in CourierBO class.
Business Rules & Validations:

In CourierService  class includes the following private attribute and inject via setter based
injection.

private CourierBO cBoObj;

Getter and setter methods for the above attribute  has been provided as a part of code skeleton.

In this CourierService class, the method public double calculateCourierCharge(int courierId, int


weight,String city) accepts courierId, weight and city as the arguments .Validate the weight, if the
parcel weight is within the range get the courier object from beans.xml file and set the weight
and courierId in that object. In case the weight is not within the range, an user-defined
Exception InvalidParcelWeightException should be thrown with message “Invalid Parcel
Weight”.

Parcel weight should be greater than zero (0) and less than thousand (1000).

If the parcel weight is within given range call the method calculateCourierCharge(cObj,city) in


CourierBO class and perform the implementation.

Limitations and Constraints:

1.      Courier and ServiceChargeInfo class should be in com.spring.model package.

2.      InvalidParcelWeightException class should be in com.spring.exception package.

3.      CourierService class should be in com.spring.service package.

4.      CourierBO class should be in com.spring.bo package.

5.      Driver class should be in com.spring.main package.

6.      All of the above mentioned java classes to be configured as beans in beans.xml file

7.      ServiceChargeInfo should be declared as an inner bean in the Courier class and should be


injected into Courier via setter based Injection.

8.      CourierService should be configured as bean inside beans.xml.

9.      CourierBO should be configured as bean inside beans.xml with the bean id


as “courierBoObj“

10.  CourierBO should be injected via setter based injection inside CourierService

11.  The service charge for each city should be declared as a MAP in the beans.xml and should


be injected using setter.

12.  charges.properties file will be provided with the value for charge per Kg, fetch and assign
values for chargePerKg attribute in Courier class via setter based injection. To read this property
file use <context:property-placeholder location="classpath:charges.properties" /> in beans.xml
file.

13.  Use ONLY beans.xml for all configurations.

Sample Input Output 1:

Enter the courier ID:

101

Enter the total weight of parcel:

20

Enter the city:

Coimbatore  // City name available in Map hence Service charge Applicable

Total Courier Charge: 600.0

Sample Input Output 2:

Enter the courier ID:

103

Enter the total weight of parcel:

10

Enter the city:

Bangalore // City name available in Map hence Service charge is not Applicable

Total Courier Charge: 200.0

Sample Input Output 3:

Enter the courier ID:

210

Enter the total weight of parcel:

1012 // Weight not in given range

Enter the city:

Bangalore
Invalid Parcel Weight

Automatic evaluation[-]
Proposed grade: 100.0 / 100
Result Description
[+]Grading and Feedback

GoGoParcelService/pom.xml

1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-


instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-
4.0.0.xsd">
2 <modelVersion>4.0.0</modelVersion>
3 <groupId>GoGoParcelService</groupId>
4 <artifactId>GoGoParcelService</artifactId>
5 <version>0.0.1-SNAPSHOT</version>
6
7 <dependencies>
8
9 <dependency>
10 <groupId>org.springframework</groupId>
11 <artifactId>spring-context</artifactId>
12 <version>4.3.10.RELEASE</version>
13 </dependency>
14
15 </dependencies>
16
17 </project>
GoGoParcelService/src/main/java/com/spring/bo/CourierBO.java

1 // CourierBO
2
3 package com.spring.bo;
4
5 import com.spring.model.Courier; //importing the packages and subpackages required
6 import java.util.Map;
7 public class CourierBO {
8
9 public double calculateCourierCharge(Courier cObj,String city) {
10
11 double courierCharge=0.0;
12 //fill the code
13 courierCharge = cObj.getWeight() * cObj.getChargePerKg();
14 Map<String,Float> data = cObj.getServiceCharge().getLocationServiceCharge();
15 if(data.containsKey(city)){
16 courierCharge += data.get(city); //setting as counter
17 }
18
19 return courierCharge;
20 }
21
22 }
23
24
GoGoParcelService/src/main/java/com/spring/exception/InvalidParcelWei
ghtException.java

1 //Invalid
2 package com.spring.exception;
3
4 public class InvalidParcelWeightException extends Exception {
5
6 public InvalidParcelWeightException(String msg) {
7 super(msg); //argumentizing the string to superclass
8 //fill the code
9 }
10
11 }
GoGoParcelService/src/main/java/com/spring/main/Driver.java

1 //Driver
2 package com.spring.main;
3
4 import com.spring.exception.InvalidParcelWeightException; //importing packages and subpackages
required
5 import com.spring.service.CourierService;
6 import org.springframework.context.ApplicationContext;
7 import org.springframework.context.support.ClassPathXmlApplicationContext;
8 import java.util.Scanner;
9 public class Driver {
10
11 public static void main(String[] args) {
12
13 //fill the code
14
15 Scanner sc=new Scanner(System.in);
16 System.out.println("Enter the courier ID:");
17 int courierId = Integer.parseInt(sc.nextLine());
18 System.out.println("Enter the total weight of parcel:");
19 int weight = Integer.parseInt(sc.nextLine());
20 System.out.println("Enter the city:");
21 String city = sc.nextLine();
22
23 ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
24 CourierService courierService=(CourierService)context.getBean("courierService");
25 //catching the unexpected exceptions using try catch block
26 try{
27 System.out.println("Total Courier Charge: " +
courierService.calculateCourierCharge(courierId,weight,city));
28 }
29 catch(InvalidParcelWeightException e){
30 System.out.println(e.getMessage());
31 }
32
33 }
34
35 }
GoGoParcelService/src/main/java/com/spring/model/Courier.java

1 package com.spring.model;
2
3 public class Courier {
4
5 private int courierId;
6 private int weight;
7 private float chargePerKg;
8 private ServiceChargeInfo serviceCharge;
9
10 public ServiceChargeInfo getServiceCharge() {
11 return serviceCharge;
12 }
13 public void setServiceCharge(ServiceChargeInfo serviceCharge) {
14 this.serviceCharge = serviceCharge;
15 }
16 public int getCourierId() {
17 return courierId;
18 }
19 public void setCourierId(int courierId) {
20 this.courierId = courierId;
21 }
22 public int getWeight() {
23 return weight;
24 }
25 public void setWeight(int weight) {
26 this.weight = weight;
27 }
28
29 public float getChargePerKg() {
30 return chargePerKg;
31 }
32 public void setChargePerKg(float chargePerKg) {
33 this.chargePerKg = chargePerKg;
34 }
35
36 }
37
GoGoParcelService/src/main/java/com/spring/model/ServiceChargeInfo.j
ava

1 package com.spring.model;
2
3 import java.util.Map;
4
5 public class ServiceChargeInfo {
6
7 private Map<String,Float> locationServiceCharge;
8
9 public Map<String, Float> getLocationServiceCharge() {
10 return locationServiceCharge;
11 }
12
13 public void setLocationServiceCharge(Map<String, Float> locationServiceCharge) {
14 this.locationServiceCharge = locationServiceCharge;
15 }
16 }
17
GoGoParcelService/src/main/java/com/spring/service/CourierService.java

1 //CourierService
2
3 package com.spring.service;
4
5 import com.spring.bo.CourierBO; //importing packages and subpackages required
6 import com.spring.exception.InvalidParcelWeightException;
7 import com.spring.model.Courier;
8 import org.springframework.context.ApplicationContext;
9 import org.springframework.context.support.ClassPathXmlApplicationContext;
10 public class CourierService {
11
12 private CourierBO cBoObj;
13
14 public CourierBO getcBoObj() {
15 return cBoObj;
16 }
17
18 public void setcBoObj(CourierBO cBoObj) {
19 this.cBoObj = cBoObj;
20 }
21
22 public double calculateCourierCharge(int courierId,int weight,String city)throws
InvalidParcelWeightException {
23
24 double courierCharge=0.0;
25 //fill your code
26 if(weight>0 && weight<1000)
27 {
28 ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
29 Courier cObj=(Courier)context.getBean("courier");
30 cObj.setCourierId(courierId);
31 cObj.setWeight(weight);
32 courierCharge = cBoObj.calculateCourierCharge(cObj, city);
33 }
34 else
35 {
36 throw new InvalidParcelWeightException("Invalid Parcel Weight");
37 }
38 return courierCharge;
39 }
40
41 }
42
43
GoGoParcelService/src/main/resources/beans.xml

1 <beans xmlns="http://www.springframework.org/schema/beans"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
3 xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
4 xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
5 xmlns:task="http://www.springframework.org/schema/task"
6 xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-
3.2.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-
3.2.xsd">
7 <context:property-placeholder location="classpath:charges.properties" />
8
9
10 <bean id="courierBoObj" class="com.spring.bo.CourierBO" />
11 <bean id="courierService" class="com.spring.service.CourierService">
12 <property name="cBoObj" ref="courierBoObj" />
13
14 </bean>
15
16
17
18 <bean id="courier" class="com.spring.model.Courier">
19 <property name="courierId" value="123"/>
20 <property name="weight" value="45"/>
21 <property name="chargePerKg" value="${chargePerKg}"></property>
22 <property name="serviceCharge">
23 <bean class="com.spring.model.ServiceChargeInfo">
24 <property name="locationServiceCharge">
25 <map>
26 <entry key="Coimbatore" value="200.0"/>
27 <entry key="Chennai" value="300.0"/>
28 <entry key="Madurai" value="150.0"/>
29 </map>
30 </property>
31 </bean>
32 </property>
33
34 </bean>
35
36
37
38
39 </beans>
GoGoParcelService/src/main/resources/charges.properties

1 chargePerKg=20.0
Grade
Reviewed on Wednesday, 10 February 2021, 1:41 AM by Automatic grade
Grade 100 / 100
Assessment report
[+]Grading and Feedback

You might also like