public Order(Customer cust) { this.cust = cust; OrderNum = nextOrderNum; //orderNum = nextOrderNum++; }
public static void AddCustomer(ArrayList customers, ref Customer newCust) { //method that adds a new customer to the array list unless there is a duplicate name //if there is a duplicate name, then it deletes the new customer and throws an exception. foreach (Customer cust in customers) if (cust.Fname == newCust.Fname && cust.Lname == newCust.Lname) {//already using compareTo for sorting newCust = null; throw new ArgumentException("Customer " + cust.ID + " " + cust.Fname + " " + cust.Lname + " already exists. "); } customers.Add(newCust); }
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 }
public Order() { this.cust = null; orderNum = 0; }