简体   繁体   中英

How to assign a value to a field in different class?

In this program, I have 2 classes Application and Customer. I want to initialize Standard Fare with some value which should be entered by the user. Standard Fare field is in Customer class. I did this, but it is not showing the desired result. When Calculate function is called The Value of Standard fare is becoming zero.

When I initialize the value of STANDARD_FARE in Customer class itself, then the program is working as desired. How can I input the value given by the user to STANDARD_FARE?

Also methods like GetAge(), GetPassNo() in Application class is not returning the value of the same.

 class Application
  {
    private static int Nop ;
    private static double TotalFare=0;
    Customer cust= new Customer(); 

    static void Main(string[] args)
      {
        Application obj = new Application();                      
        Console.Write("Enter the STANDARD RATE of the tour ");
        obj.cust.StandardFare = int.Parse(Console.ReadLine());

        a:
        Console.Clear();                                             
        Console.WriteLine("Enter the number of passengers");
        Nop = int.Parse(Console.ReadLine());                       

        Application[] app = new Application[Nop];                 

        if (Nop <= 0)                                               
        {
            Console.WriteLine("Please enter a valid number of passengers");
            Console.ReadKey();                                     
            goto a;
        }

        for (int i = 0; i < Nop; i++)
        {
            app[i] = new Application();                           
            app[i].GetInformationFromCust();         
        }

        for (int j = 0; j < Nop; j++)
        {

           app[j].cust.Display();                                  

       }                                             
     }

    public int GetInformationFromCust()
    {      
         b:
        Console.Clear();                                     
        int slen = 0;                                   

        Console.WriteLine("Enter the title of the passenger");
        cust.Customer_Title = Console.ReadLine();
        Console.WriteLine("\r\nEnter passenger's First name :");
        cust.Customer_FName = Console.ReadLine();
        Console.WriteLine("\r\nEnter passenger's Last name :");
        cust.Customer_LName = Console.ReadLine();
        slen = cust.Customer_FName.Length + cust.Customer_LName.Length;    
         if (slen < 5 || slen > 15)                                       
          {
            Console.WriteLine("\r\nName should be between 5 to 15 characters, Please try again ");
            Console.ReadLine();
            goto b;
          }

        c:
        long x = 0, len = 0;
        Console.WriteLine("\r\nEnter the passport number of the passenger ");
        cust.CustomerPassNo = int.Parse(Console.ReadLine());
        x = cust.CustomerPassNo;               

         while (x > 0)
          {
            x = x / 10;                    
            ++len;
          }
        if (len != 8)                    
          {
            Console.WriteLine("\r\nInvalid passport number, passport should be of 8 digits ");
            goto c;
          }

        d:
        Console.WriteLine("\r\nEnter the age of the passenger :");
        cust.Customer_Age = int.Parse(Console.ReadLine());
          if (cust.Customer_Age < 0)                                         
          {
             Console.WriteLine("\r\nInvalid age, please enter a valid age ");
             goto d;
          } 
        cust.CalculatePrice();                                          
        return 0;
    }

    public int GetAge()
    {
        return cust.Customer_Age;
    }

    public double GetAirFare()
    {
        return cust.CustomerTicket ;
    }

    public long GetPassportNo()
    {
        return cust.CustomerPassNo;
    }

    public string GetTitle()
    {
        return cust.Customer_Title;
    }
 }
class Customer 
  {
    const double K_DISCOUNT = 0.10;                            
    const double S_DISCOUNT = 0.20;

    private double STANDARD_FARE;
    private string CustomerName { get; set; }
    private int CustomerAge;
    private string CustomerFName;
    private string CustomerLName;
    private long CustomerPassport;
    private double CustomerPrice; 
    private string CustomerTitle;
    private double KidDiscount;
    private double SeniorDiscount;

    public Customer()                                         
    {
        this.KidDiscount = K_DISCOUNT;
        this.SeniorDiscount = S_DISCOUNT;
    }

    public double StandardFare
    {
        get { return STANDARD_FARE; }
        set { STANDARD_FARE = value; }
    }

    public int Customer_Age
    {
        get { return CustomerAge; }
        set { CustomerAge = value; }
    }

    public string Customer_Title
    {
        get { return CustomerTitle; }
        set { CustomerTitle = value; }
    }

    public string Customer_FName
    {
        get { return CustomerFName; }
        set { CustomerFName = value; }
    }

    public string Customer_LName
    {
        get { return CustomerLName; }
        set { CustomerLName = value; }
    }

    public long CustomerPassNo
    {
        get { return CustomerPassport; }
        set { CustomerPassport = value; }
    }

    public double CustomerTicket
    {
        get { return CustomerPrice; }
        set { CustomerPrice = value; }
    }


    public int CalculatePrice()
    {
        if (CustomerAge < 3)
        {
            CustomerPrice = 0;
        }
        else if (CustomerAge >= 3 && CustomerAge < 18)
        {
            CustomerPrice = STANDARD_FARE - (STANDARD_FARE * KidDiscount);
        }
        else if (CustomerAge > 65)
        {
            CustomerPrice = STANDARD_FARE - (STANDARD_FARE * SeniorDiscount);
        }
        else
        {
            CustomerPrice = STANDARD_FARE;
        }
        return 0;
    }

    public void Display()
    {
      //some code here
    }
}

产量

You are populating your array app with instances of Application that still have the default STANDARD_FARE value (which is 0.0), because you have never set it on those instances . You only set it on the obj.cust instance, which you never again use . Because STANDARD_FARE is an instance variable, changes to it have no affect on other (or future) instances.

You have the same problem in reverse with all the Application.Get* functions; they are getting properties of an object ( obj.cust ) that has never had any properties set, other than StandardFare / STANDARD_FARE .

The most obvious fix is to do away with obj and obj.cust entirely - they have no use other than to be confusing - and make STANDARD_FARE a static variable (and its setter StandardFare a static property).


BTW, your naming conventions are terrible and inconsistent; if I were your grader I'd dock you points for using unclear variable names( app , nop ), and for using ALL_CAPS for non-constants ( STANDARD_FARE ). I'd also object to using a private auto-backed property ( CustomerName , which is also never used) instead of simply a private variable, for not using auto-backed properties elsewhere ( StandardFare as an explicitly-coded public getter and setter for STANDARD_FARE , etc.), and for copying constant values into non-settable instance variables ( K_DISCOUNT to KidDiscount ; just use the constant directly, or at least make KidDiscount static and add some non-private access to it). As others have mentioned, you of course should not be using goto in place of loops. I'll also mention the error-prone and inefficient checking the length of the passport number by repeated division instead of simply checking whether it's less than 99999999 (in theory, passport numbers might start with a zero, which would look like less than 8 digits after parsing, but you could also make sure it's greater than 10000000 if you want).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM