Exemple #1
0
        private void btnAddCone_Click(object sender, EventArgs e)
        {
            Cone cone;
            int  scoops = 0;

            //int custID = 0;

            try {
                if (Convert.ToInt32(maskCustID2.Text) != custID)
                {
                    custID = Convert.ToInt32(maskCustID2.Text);

                    foreach (Customer cust in customers)
                    {
                        if (cust.ID == custID)
                        {
                            //ctOrder.Cust = cust;
                            ctOrder = new Order(cust);
                            break;
                        }
                    }
                }
            }
            catch (FormatException) {
                MessageBox.Show("Please input customer ID. ", "Error! ");
                return;
            }

            if (radioIceCream.Checked == true)
            {
                cone = new IceCream();
            }
            else if (radioYogurt.Checked == true)
            {
                cone = new Yogurt();
            }
            else
            {
                MessageBox.Show("Check either Ice Cream or Yogurt. ", "Error! ");
                return;
            }

            try{
                scoops = Convert.ToInt32(maskScoops.Text);
            }catch (FormatException) {
                MessageBox.Show("Number of scoops must be entered. ", "Error! ");
                return;
            }

            if (scoops == 0)
            {
                MessageBox.Show("Number of scoops must be a non zero number. ", "Error! ");
                return;
            }

            cone.NumScoops = scoops;

            if (listFlavor.SelectedIndex == -1)
            {
                MessageBox.Show("Please select a flavor. ", "Error! ");
                return;
            }

            cone.ConeFlavor = listFlavor.SelectedItem.ToString();

            try {
                ctOrder += cone;
            }
            catch (ArgumentException ex) {
                MessageBox.Show(ex.Message, "Error! ");
                return;
            }

            txtOrderInfo.Text = ctOrder.ToString().Replace("\n", "\r\n");
        }//end handler
        public void readFromFile(String fname, ArrayList customerList)
        {
            if (fname2 != fname) {//restart the process if the file changes
                firstRun = true;
                fname2 = fname;
            }

            try {
                if (firstRun) {
                    infile = new FileStream(fname, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    //reader = new StreamReader(infile);

                    firstRun = false;
                }

                String str = infile.readLine();
                String[] tokens = str.Split(new char[] { ',' });

                if (String.Equals(tokens[0], "Order", StringComparison.OrdinalIgnoreCase)) {
                    //then we are reading an order

                    //Order,orderNum,customerNum,numCones
                    this.OrderNum = Convert.ToInt32(tokens[1]);

                    int customerNum = Convert.ToInt32(tokens[2]);//read customer number

                    foreach(Customer cust in customerList)//match customer number to customer
                        if (cust.ID == customerNum) {
                            this.cust = cust;
                            break;
                        }

                    if(this.cust == null)
                        throw new IOException("Customer in input file not found. ");

                    int numCones = Convert.ToInt32(tokens[3]);

                    if (numCones > 10)
                        throw new IOException("An order contains more than 10 cones. ");

                    for (int i = 0; i < numCones; i++) {
                        try {
                            //try to read an ice cream cone
                            IceCream cream = new IceCream();
                            cream.readFromFile(infile);

                            this.addCone(cream);
                        }
                        catch (ConeTypeMismatchException) {
                            //else read a yogurt cone
                            Yogurt yogurt = new Yogurt();
                            yogurt.readFromFile(infile);

                            this.addCone(yogurt);
                        }
                    }//end loop
                }
                else
                    throw new IOException("Order object not encountered in file. ");//non-cone data
            }
            catch (System.IO.FileNotFoundException) { throw; }//file not found
            catch (System.NullReferenceException) {
                //something is wrong with the input file, or most likely end of file
                infile.Close();//close the file

                infile = null;//deallocate memory

                firstRun = true;
                throw;//inform calling method there are no more records
            }
            catch (FormatException) { throw; }//input is not in the right format
        }
Exemple #3
0
        static String fname2;//used to determine if the file has been changed for some reason

        public void readFromFile(String fname, ArrayList customerList) {

            if (fname2 != fname) {//restart the process if the file changes
                firstRun = true;
                fname2 = fname;
            }

            try {
                if (firstRun) {
                    infile = new FileStream(fname, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    //reader = new StreamReader(infile);

                    firstRun = false;
                }

                String str = infile.readLine();
                String[] tokens = str.Split(new char[] { ',' });

                if (String.Equals(tokens[0], "Order", StringComparison.OrdinalIgnoreCase)) {
                    //then we are reading an order
                    
                    //Order,orderNum,customerNum,numCones
                    this.OrderNum = Convert.ToInt32(tokens[1]);

                    int customerNum = Convert.ToInt32(tokens[2]);//read customer number

                    foreach(Customer cust in customerList)//match customer number to customer
                        if (cust.ID == customerNum) {
                            this.cust = cust;
                            break;
                        }
                    
                    if(this.cust == null)
                        throw new IOException("Customer in input file not found. ");

                    int numCones = Convert.ToInt32(tokens[3]);

                    if (numCones > 10)
                        throw new IOException("An order contains more than 10 cones. ");

                    for (int i = 0; i < numCones; i++) {
                        try {
                            //try to read an ice cream cone
                            IceCream cream = new IceCream();
                            cream.readFromFile(infile);

                            this.addCone(cream);
                        }
                        catch (ConeTypeMismatchException) {
                            //else read a yogurt cone
                            Yogurt yogurt = new Yogurt();
                            yogurt.readFromFile(infile);

                            this.addCone(yogurt);
                        }
                    }//end loop
                }
                else
                    throw new IOException("Order object not encountered in file. ");//non-cone data
            }
            catch (System.IO.FileNotFoundException) { throw; }//file not found
            catch (System.NullReferenceException) {
                //something is wrong with the input file, or most likely end of file
                infile.Close();//close the file

                infile = null;//deallocate memory

                firstRun = true;
                throw;//inform calling method there are no more records
            }
            catch (FormatException) { throw; }//input is not in the right format
        }//end method