示例#1
0
        public List<Pet> listPets()
        {
            List<Pet> petList = new List<Pet>();

            string path = "C:\\Listing.txt";
            //string path = "C:/Users/Andrew/Desktop/new_cse445/445Proj5/Assignment 5/WebStore/Listing.txt";

            string[] lines = System.IO.File.ReadAllLines(path);
            for (int x = 0; x < lines.Count(); x++)
            {
                string temp = "";
                Dog d;
                Cat c;
                Bird b;
                string[] p = lines[x].Split('\t');
                switch (p[0])
                {
                    case "Dog":
                        d = new Dog();
                        d.setPetType(p[0]);
                        d.setBreed(p[1]);
                        d.setColor(p[2]);
                        d.setId(p[3]);
                        d.setAge(Convert.ToInt32(p[4]));
                        d.setPrice(Convert.ToDouble(p[5]));
                        for (int y = 6; y < p.Count(); y++)
                            temp += p[y];
                        d.setDescription(temp);
                        petList.Add(d);
                        break;
                    case "Cat":
                        c = new Cat();
                        c.setPetType(p[0]);
                        c.setBreed(p[1]);
                        c.setColor(p[2]);
                        c.setId(p[3]);
                        c.setAge(Convert.ToInt32(p[4]));
                        c.setPrice(Convert.ToDouble(p[5]));
                        for (int y = 6; y < p.Count(); y++)
                            temp += p[y];
                        c.setDescription(temp);
                        petList.Add(c);
                        break;
                    case "Bird":
                        b = new Bird();
                        b.setPetType(p[0]);
                        b.setType(p[1]);
                        b.setWeight(Convert.ToDouble(p[2]));
                        b.setId(p[3]);
                        b.setAge(Convert.ToInt32(p[4]));
                        b.setPrice(Convert.ToDouble(p[5]));
                        for (int y = 6; y < p.Count(); y++)
                            temp += p[y];
                        b.setDescription(temp);
                        petList.Add(b);
                        break;
                }
            }

            return petList;
        }
示例#2
0
        public void addPet(Pet p)
        {
            string path = "c:\\Listing.txt";
            string[] lines = System.IO.File.ReadAllLines(path);
            for (int x = 0; x < lines.Count(); x++)
            {
                string[] ind = lines[x].Split('\t');
                if (ind.Count() > 3)
                {
                    if (p.getId() == ind[3])
                    {
                        return;
                    }
                }
            }
            string result = "";

            result += p.getPetType() + "\t";
            switch (p.getPetType())
            {
                case "Dog":
                    Dog d = new Dog();
                    d = (Dog)p;
                    result += d.getBreed() + "\t" + d.getColor() + "\t" + d.getId() + "\t" + d.getAge() + "\t" +
                        d.getPrice() + "\t" + d.getDescription();
                    break;
                case "Cat":
                    Cat c = new Cat();
                    c = (Cat)p;
                    result += c.getBreed() + "\t" + c.getColor() + "\t" + c.getId() + "\t" + c.getAge() + "\t" +
                        c.getPrice() + "\t" + c.getDescription();
                    break;
                case "Bird":
                    Bird b = new Bird();
                    b = (Bird)p;
                    result += b.getType() + "\t" + b.getWeight() + "\t" + b.getId() + "\t" + b.getAge() + "\t" +
                        b.getPrice() + "\t" + b.getDescription();
                    break;
            }
            Debug.WriteLine(result);
            StreamWriter w = File.AppendText(path);

                w.WriteLine(result);

            w.Close();
        }