-2

I really hope u guys will be able to help me I'm trying to make a program on inheritance and arrays Amma just right the question I've tried it but I can't get the displays correct .......

create a base class called package with instance variables (packageId, weight, costperounce,senderName,senderaddress,city,recipientName, recipient city ,recipient parish). And here's what's I'm suppose to do

Use inheritance hierarchy to create an application that displays the recipients and senders addresses information. And calculate the transport cost for several packages (weight*costperounce). The application should contain an array of package objects of class TwoDayPackage and overnight Package. The transport fees for the TwoDayPackage includes a flat rate of $500.00 added to the basic calculation whilst the OverNightPackage includes a flat rate of $1500.00 added to the costperounce multiplied by the weight of the package. Loop through the array to process the package polymorphically.

For each package use properties to obtain the address information of the sender a d the recipient then print the two addresses as they would spare on a mailing labels also. All each package's cost method and print the result. Keep track of the total shipping cost for all packages in the array and display the total when the loop terminates

Plz help me I've at it for almost a week now and help will be appreciated

1 Answers1

4

perhaps this will help, I felt generous today.

A few things your assignment likely is trying to force you to learn and some links that will go through some of those topics

  1. override vs new
  2. polymorphism
  3. inheritance
  4. Array vs List
  5. Array Resizing

      class Program
    {
        static void Main(string[] args)
        {
            //the array method
            var test = new Order();
            test.AddPackage(new OvernightPackage() { WeightInOunces = 10, Sender = new Person() { Name = "Sender"}, Recipient = new Person() {Name = "Recipient"}});
            test.AddPackage(new TwoDayPackage() { WeightInOunces = 16, Sender = new Person() { Name = "Sender" }, Recipient = new Person() { Name = "Recipient" } });
            test.AddPackage(new TwoDayPackage() { WeightInOunces = 10, Sender = new Person() { Name = "Sender" }, Recipient = new Person() { Name = "Recipient" } });
            test.PrintManifestsWithArray();
    
            //the list way
            test.Packages = new List<Package>(test.PackagesArray);
            test.PrintManifests();
    
    
            Console.ReadLine();
        }
    }
    
    public class Person
    {
        public string Name { get; set; }
        public Address Address { get; set; }
    }
    
    public class Address
    {
        public string StreetAddress { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string ZipCode { get; set; }
    }
    
    public abstract class Package
    {
        protected Package()
        {
            CostPerOunce = .08;
            WeightInOunces = 0;
        }
        public string Id { get; set; }
        public int WeightInOunces { get; set; }
        public double DeliveryCost { get { return CalculateCost(); } }
        public Person Sender { get; set; }
        public Person Recipient { get; set; }
        public double CostPerOunce { get; set; }
        protected virtual double CalculateCost ()
        {
            return WeightInOunces*CostPerOunce;
        }
    }
    
    public class OvernightPackage : Package
    {
        public OvernightPackage()
        {
            CostPerOunce = .12;
        }
        protected override double CalculateCost()
        {
            var cost = base.CalculateCost();
            cost += 1500.00;
            return cost;
        }
    }
    
    public class TwoDayPackage : Package
    {
        protected override double CalculateCost()
        {
            var cost = base.CalculateCost();
            cost += 500.00;
            return cost;
        }
    }
    
    public class Order
    {
        public List<Package> Packages { get; set; }
        public Package[] PackagesArray
        {
            get { return _thePackages; }
        }
    
        private Package[] _thePackages = new Package[0];
        public void AddPackage(Package pkg)
        {
            Array.Resize(ref _thePackages, _thePackages.Length + 1);
            _thePackages[_thePackages.Length -1] = pkg;
        }
    
        public void PrintManifestsWithArray()
        {
            foreach (var package in PackagesArray)
            {
                Console.WriteLine( package.GetType() + ": " + package.DeliveryCost);
            }
        }
    
        public double TotalCost
        {
            get
            {
                return Packages.Sum(package => (double) package.DeliveryCost);
            }
        }
    
        public void PrintManifests()
        {
            foreach (var package in Packages)
            {
                Console.WriteLine(package.Sender.Name);
                Console.WriteLine(package.Recipient.Name);
                Console.WriteLine(package.DeliveryCost);
                ///so on and so forth
            }
        }
    }
    
Community
  • 1
  • 1
Rob Allen
  • 2,871
  • 2
  • 30
  • 48