Create a Delivery class for a delivery service. The class contains fields to hol
ID: 3906689 • Letter: C
Question
Create a Delivery class for a delivery service. The class contains fields to hold the following: A delivery number that contains eight digits. The first four digits represent the year, and the last four digits represent the delivery number. For example, the 76th delivery in 2011 has a complete delivery number of 20110076. A code representing the delivery area. A local delivery is code 1, and a long-distance delivery is code 2. A weight, in pounds, of the item to be delivered. The fee for the delivery, as follows: Distance Weight Fee ($) 1 Under 5 pounds 12.00 1 5 to 20 pounds 16.50 1 Over 20 pounds 22.00 2 Under 5 pounds 35.00 2 5 pounds or more 47.95 Create a constructor for the Delivery class that accepts arguments for the year, delivery number within the year, delivery distance code, and weight of the package. The constructor determines the eight-digit delivery number and delivery fee. Also include a method that displays every Delivery object field. Save the file as Delivery.java.
Explanation / Answer
import java.util.Scanner;
public class Delivery {
static String year,dno,dcode,full_code;
static double weight,fee;
//CONSTRUCTOR OF DELIVERY CLASS
public Delivery(String year,String dno,String dcode,double weight)
{
//FOR DETERMINING EIGHT DIGIT CODE OF PaCkage
if(dno.length()==1)
{
full_code=year.concat("000");
full_code=full_code.concat(dno);
}
else if(dno.length()==2)
{
full_code=year.concat("00");
full_code=full_code.concat(dno);
}
else if(dno.length()==3)
{
full_code=year.concat("0");
full_code=full_code.concat(dno);
}
else
{
full_code=year.concat(dno);
}
//FOR FINDING DELIVERY FEE
if(dcode.equals("1")) //.....FOR CODE AREA NUMBER 1....
{
if(weight<5.00)
fee=12.00;
else if(weight>=5.00 && weight<=20.00)
fee=16.50;
else if(weight>20.00)
fee=22.00;
}
else if(dcode.equals("2")) //...FOR CODE AREA NUMBER 2
{
if(weight<5.00)
fee=35.00;
else if(weight>=5.00)
fee=47.95;
}
//CALLING DISPLAY METHOD
Display(year,dno,dcode,weight,full_code,fee);
}
//DISPLAY METHOD FOR DISPLAYING ALL THE DELIVERY OBJECT FIELDS
public void Display(String year,String dno,String dcode,double weight,String full_code,double fee)
{
System.out.println("___________________________________________");
System.out.println("Year="+year);
System.out.println("Delivery number="+dno);
System.out.println("Area Code="+dcode);
System.out.println("Weight="+weight);
System.out.println("___________________________________________");
System.out.println("THE DELIVER NUMBER="+full_code);
System.out.println("DELIVERY FEE="+fee);
System.out.println("___________________________________________");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
//INPUT YEAR OF DELIVERY
System.out.println("Enter year of delivery");
year=sc.nextLine();
//INPUT DELIVERY NUMBER FOR THE YEAR
System.out.println("Enter delivery number of the year");
dno=sc.nextLine();
//INPUT CODE FOR THE DELIVERY AREA
System.out.println("Enter delivery area code- Code 1=local delivery Code 2=long-distance delivery");
dcode=sc.nextLine();
//INPUT WEIGHT OF PACKAGE
System.out.println("Enter weight of package");
weight=sc.nextDouble();
//ASSIGNING VALUES TO THE CONSTRUCTOR
Delivery d=new Delivery(year,dno,dcode,weight);
}
}