예제 #1
0
    public static void Main()
    {
        Bank         bank = new Bank();
        InputWrapper iw   = new InputWrapper();
        string       cmd;

        Console.WriteLine("Enter command, quit to exit");
        cmd = iw.getString("> ");
        while (!cmd.Equals("quit"))
        {
            if (cmd.Equals("open"))
            {
                AccountType type;
                string      stype = iw.getString("account type: ");
                switch (stype)
                {
                case "checking":
                    type = AccountType.Checking;
                    break;

                case "savings":
                    type = AccountType.Savings;
                    break;

                default:
                    type = AccountType.Invalid;
                    break;
                }
                if (type == AccountType.Invalid)
                {
                    Console.WriteLine("Valid account types are checking/savings");
                    continue;
                }
                decimal bal   = iw.getDecimal("starting balance: ");
                string  owner = iw.getString("owner: ");
                int     id    = bank.AddAccount(type, bal, owner);
                Console.WriteLine("Account opened, id = {0}", id);
            }
            else if (cmd.Equals("close"))
            {
                int id = iw.getInt("account id: ");
                bank.DeleteAccount(id);
            }
            else if (cmd.Equals("show"))
            {
                ShowArray(bank.GetAccounts());
            }
            else if (cmd.Equals("account"))
            {
                int     id  = iw.getInt("account id: ");
                Account acc = bank.FindAccount(id);
                Atm.ProcessAccount(acc);
            }
            else
            {
                help();
            }
            cmd = iw.getString("> ");
        }
    }
예제 #2
0
    public static void Main(string[] args)
    {
        InputWrapper iw = new InputWrapper();

        Console.WriteLine("Enter command, 'quit' to exit.");
        string cmd = iw.getString("> ");

        while (!cmd.Equals("quit"))
        {
            if (cmd.Equals("Length"))
            {
                Console.WriteLine(cmd);
                break;
            }
            else if (cmd.Equals("new"))
            {
                Console.WriteLine(cmd);
                break;
            }
            else if (cmd.Equals("show"))
            {
                Console.WriteLine(cmd);
                break;
            }
            else
            {
                cmd = iw.getString(">> ");
            }
        }
    }
예제 #3
0
    static void Main()
    {
        Console.WriteLine("Enter time in format hh:mm");
        Console.WriteLine("Press carriage return to quit");
        InputWrapper iw  = new InputWrapper();
        string       str = iw.getString("time: ");

        try
        {
            while (str != "")
            {
                Time t = (Time)str;
                t.Show();
                str = iw.getString("time: ");
            }
            Console.WriteLine("Goodbye");
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        //finally
        //{
        //    Console.WriteLine("Enter time in formt hh:mm again");
        //    InputWrapper iw1 = new InputWrapper();
        string str1 = iw.getString("time: ");
        //}
    }
예제 #4
0
    public static void Main(String[] args)
    {
        string[] names = new string[10];
        int      count = 0;

        names[count++] = "John";
        names[count++] = "Jane";
        names[count++] = "Todd";

        string cmd = " ";

        while (cmd != "quit")
        {
            InputWrapper iw = new InputWrapper();
            Console.WriteLine("Enter command, quit to exit.");
            cmd = iw.getString("> ");
            Console.WriteLine("the command {0} was entered", cmd);

            switch (cmd)
            {
            case "add":
                string name = iw.getString("names: ");
                names[count++] = name;
                Console.WriteLine(name);
                break;

            case "forward":
                for (int i = 0; i < names.Length; i++)
                {
                    Console.WriteLine(names[i]);
                }
                break;

            case "backward":
                Array.Reverse(names);
                Console.WriteLine("Backward");
                PrintIndexAndValues(names);
                break;

            //    case "find":
            //        int j = 0;
            //        string target = iw.getString("Search: ");
            //        while (target != names[j])
            //        {
            //            break;
            //        }
            //        Console.WriteLine(names[j]);
            //        break;
            case "remove":
                Console.WriteLine("Remove");
                break;

            default:
                Console.WriteLine("Available commands include: forward, backward, add, find, remove");
                break;
            }
            cmd = iw.getString("> ");
        }
    }
예제 #5
0
    static void Main(string[] args)
    {
        InputWrapper iw   = new InputWrapper();
        string       name = " ";
        string       spouse;
        ArrayList    MyMarriedFriends = new ArrayList();

        while (true)
        {
            Console.WriteLine("Enter friend's name, quit to exit");
            name = iw.getString("> ");
            if (name.Equals("quit"))
            {
                break;
            }
            Console.WriteLine("Enter friend's spouse's name");
            spouse = iw.getString("> ");
            Friend node = new Friend(name, spouse);
            MyMarriedFriends.Add(node);
        }
        foreach (object o in MyMarriedFriends)
        {
            // todo: what errors need to be handled here?
            try
            {
                if (name != null)
                {
                    Friend f = (Friend)o;
                    Console.WriteLine(f.ToString());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                //Console.WriteLine("Please input valid names");
            }
        }
        MyMarriedFriends.Sort(); // default sort
        Console.WriteLine("\nSorted");
        foreach (object o in MyMarriedFriends)
        {
            // todo: what errors need to be handled here?
            try
            {
                if (MyMarriedFriends[0] is IComparable)
                {
                    Friend f = (Friend)o;
                    Console.WriteLine(f.ToString());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
        Console.Write("Enter <cr> to end program");
        Console.ReadLine();
    }
예제 #6
0
    public static void Main()
    {
        // Initialize accounts and show starting state
        accounts = new ArrayList();
        Console.WriteLine("accounts.Count = {0}", accounts.Count);
        Console.WriteLine("accounts.Capacity = {0}", accounts.Capacity);
        AddAccount(100, "Bob", 1);
        AddAccount(200, "Mary", 2);
        AddAccount(300, "Charlie", 3);
        ShowAccounts(accounts);
        Console.WriteLine("accounts.Count = {0}", accounts.Count);
        Console.WriteLine("accounts.Capacity = {0}", accounts.Capacity);
        // Command processing loop
        InputWrapper iw = new InputWrapper();
        string       cmd;

        Console.WriteLine("Enter command, quit to exit");
        cmd = iw.getString("> ");
        while (!cmd.Equals("quit"))
        {
            try
            {
                if (cmd.Equals("show"))
                {
                    ShowAccounts(accounts);
                }
                else if (cmd.Equals("enum"))
                {
                    ShowEnum(accounts);
                }
                else if (cmd.Equals("remove"))
                {
                    int id = iw.getInt("id: ");
                    RemoveAccount(id);
                }
                else if (cmd.Equals("add"))
                {
                    decimal balance = iw.getDecimal("balance: ");
                    string  owner   = iw.getString("owner: ");
                    int     id      = iw.getInt("id: ");
                    AddAccount(balance, owner, id);
                }
                else
                {
                    help();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                if (e.InnerException != null)
                {
                    Console.WriteLine(e.InnerException.Message);
                }
            }
            cmd = iw.getString("> ");
        }
    }
예제 #7
0
    public static void Main(string[] args)
    {
        Account acc = new Account(100);

        ShowBalance(acc);
        string       cmd;
        InputWrapper iw = new InputWrapper();

        Console.WriteLine("Enter command, quit to exit");
        cmd = iw.getString("> ");
        while (!cmd.Equals("quit"))
        {
            try
            {
                if (cmd.Equals("deposit"))
                {
                    decimal amount = iw.getDecimal("amount: ");
                    acc.Deposit(amount);
                    ShowBalance(acc);
                }
                else if (cmd.Equals("withdraw"))
                {
                    decimal amount = iw.getDecimal("amount: ");
                    try
                    {
                        acc.Withdraw(amount);
                    }
                    catch (BalanceException e)
                    {
                        Console.WriteLine("You are short {0:C}", e.Shortage);
                        Console.WriteLine("Please make a deposit");
                        decimal supplemental = iw.getDecimal("amount: ");
                        acc.Deposit(supplemental);
                        acc.Withdraw(amount);                           // try again
                    }
                    finally
                    {
                        ShowBalance(acc);
                    }
                }
                else if (cmd.Equals("show"))
                {
                    ShowBalance(acc);
                }
                else
                {
                    help();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            cmd = iw.getString("> ");
        }
    }
예제 #8
0
    public static void Main()
    {
        InputWrapper iw = new InputWrapper();

        Console.WriteLine("Enter a string, empty string to terminate");
        string email = iw.getString("email: ");

        while (!email.Equals(""))
        {
            // Extract string to first '@' character
            int index = email.IndexOf('@', 0);
            if (index != -1)
            {
                string user = email.Substring(0, index);
                // Extract string to first '.' character
                int start = index + 1;
                index = email.IndexOf('.', index);
                if (index != -1)
                {
                    string domain = email.Substring(start, index - start);
                    // Check that suffix is legal
                    string suffix = email.Substring(index + 1);
                    if (suffix == "com" || suffix == "org" || suffix == "edu")
                    {
                        // Check that user and domain are alphanumeric
                        if (IsAlphaNum(user) && IsAlphaNum(domain))
                        {
                            Console.WriteLine("user = {0}", user);
                            Console.WriteLine("domain = {0}", domain);
                            Console.WriteLine("suffix = {0}", suffix);
                        }
                        else
                        {
                            Console.WriteLine("User and domain must be alphanumeric");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Legal suffixes are com, org and edu");
                    }
                }
                else
                {
                    Console.WriteLine("No period found in email address");
                }
            }
            else
            {
                Console.WriteLine("No at symbol found in email address");
            }
            email = iw.getString("email: ");
        }
    }
예제 #9
0
    public static void ProcessAccount(Account acc)
    {
        ShowBalance(acc);
        string       cmd;
        InputWrapper iw = new InputWrapper();

        Console.WriteLine("Enter command, quit to exit");
        cmd = iw.getString(acc.Prompt);
        while (!cmd.Equals("quit"))
        {
            try
            {
                if (cmd.Equals("deposit"))
                {
                    decimal amount = iw.getDecimal("amount: ");
                    acc.Deposit(amount);
                    ShowBalance(acc);
                }
                else if (cmd.Equals("withdraw"))
                {
                    decimal amount = iw.getDecimal("amount: ");
                    acc.Withdraw(amount);
                    ShowBalance(acc);
                }
                else if (cmd.Equals("owner"))
                {
                    string owner = iw.getString("new owner name: ");
                    acc.Owner = owner;
                    show(acc);
                }
                else if (cmd.Equals("show"))
                {
                    show(acc);
                }
                else
                {
                    accountHelp();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                if (e.InnerException != null)
                {
                    Console.WriteLine(e.InnerException.Message);
                }
            }
            cmd = iw.getString(acc.Prompt);
        }
    }
예제 #10
0
    static void Main()
    {
        Console.WriteLine("Enter time in format hh:mm");
        Console.WriteLine("Press 'Enter' to quit");
        InputWrapper iw  = new InputWrapper();
        string       str = iw.getString("time: ");

        while (str != "")
        {
            Time t = (Time)str;
            t.Show();
            str = iw.getString("time: ");
        }
        Console.WriteLine("Goodbye");
    }
예제 #11
0
    public static void Main(string[] args)
    {
        InputWrapper iw   = new InputWrapper();
        string       name = iw.getString("Enter your name: ");

        Console.WriteLine("Hello, " + name);
    }
예제 #12
0
    static void Main(string[] args)
    {
        //TODO: use command loop to show and find policies
        Policy[] policy = new Policy[3];
        policy[0] = new HomeOwnerLine
                        (33, "Duplex", "98001", 1, 300000, 2000000, "Kathy", "applied for",
                        "2016-5-6", "2017-5-6");
        policy[1] = new RentersLine
                        (45, "SingleFamily", "98000", 55, 2, 30000, 200000, "Rachel", "Bound", "2013-5-6", "2014-5-6");
        policy[2] = new MotorCycleLine
                        ("343525335vkj", "TwoWheel",
                        3, 20000, 20000, "Jay", "Bound", "2013-5-6", "2014-5-6");
        policy[0].ShowPolicy();            //works//
        Console.WriteLine(policy[0].Name); //works

        InputWrapper iw = new InputWrapper();
        string       cmd;

        Console.WriteLine("Enter the id to find the policy");
        cmd = iw.getString("> ");

        for (int i = 0; i < policy.Length; i++)
        {
            if (cmd.Equals(policy[i].Id.ToString()))
            {
                policy[i].ShowPolicy();
            }
        }
    }
예제 #13
0
    public static void Main()
    {
        InputWrapper iw = new InputWrapper();

        Console.WriteLine("Enter command, quit to exit");
        string cmd = iw.getString("> ");

        while (!cmd.Equals("quit"))
        {
            //Console.WriteLine("command is " + cmd);
            switch (cmd)
            {
            case "add":
                Console.WriteLine("add command");
                break;

            case "forward":
                Console.WriteLine("forward command");
                break;

            case "reverse":
                Console.WriteLine("reverse command");
                break;

            case "find":
                Console.WriteLine("find command");
                break;

            case "remove":
                Console.WriteLine("remove command");
                break;

            default:
                Console.WriteLine("The following commands are available:");
                Console.WriteLine("   add       add a contact");
                Console.WriteLine("   forward   show contacts in forward order");
                Console.WriteLine("   reverse   show contacts in reverse order");
                Console.WriteLine("   find      find a contact");
                Console.WriteLine("   remove    remove a contact");
                Console.WriteLine("   quit      exit the program");
                break;
            }
            cmd = iw.getString("> ");
        }
    }
예제 #14
0
    static void Main(string[] args)
    {
        InputWrapper iw   = new InputWrapper();
        string       name = " ";
        string       spouse;
        ArrayList    MyMarriedFriends = new ArrayList();

        while (true)
        {
            Console.WriteLine("Enter friend's name, quit to exit");
            name = iw.getString("> ");
            if (name.Equals("quit"))
            {
                break;
            }
            Console.WriteLine("Enter friend's spouse's name");
            spouse = iw.getString("> ");
            Friend node = new Friend(name, spouse);
            MyMarriedFriends.Add(node);
        }
        foreach (object o in MyMarriedFriends)
        {
            Friend f = o as Friend;
            if (f == null)
            {
                throw new Exception("Invalid object in ArrayList");
            }
            Console.WriteLine(f.ToString());
        }
        MyMarriedFriends.Sort();         // default sort
        Console.WriteLine("\nSorted");
        foreach (object o in MyMarriedFriends)
        {
            Friend f = o as Friend;
            if (f == null)
            {
                throw new Exception("Invalid object in ArrayList");
            }
            Console.WriteLine(f.ToString());
        }
        Console.Write("Enter <cr> to end program");
        Console.ReadLine();
    }
예제 #15
0
    public static void Main(string[] args)
    {
        InputWrapper iw = new InputWrapper();

        char[]   spaces = { '@', '.' };
        string[] elements;
        Console.WriteLine("Enter string, enter to exit.  ");
        string email = iw.getString("email: ");

        while (!email.Equals(""))
        {
            elements = email.Split(spaces);
            foreach (string ele in elements)
            {
                Console.WriteLine(ele);
            }
            email = iw.getString("email: ");
        }
    }
예제 #16
0
    public static void Main()
    {
        InputWrapper iw = new InputWrapper();

        char[]   spaces = { '@', '.' };
        string[] elements;
        Console.WriteLine("Enter a string, empty string to end");
        string email = iw.getString("email: ");

        while (!email.Equals(""))
        {
            elements = email.Split(spaces);
            foreach (string ele in elements)
            {
                Console.WriteLine(ele);
            }
            email = iw.getString("email: ");
            int index = email.IndexOf('@', 0);
            if (index != -1)
            {
                string user  = email.Substring(0, index);
                int    start = index + 1;
                index = email.IndexOf('.', index);
                if (index != -1)
                {
                    string domain = email.Substring(start, index - start);
                    string suffix = email.Substring(index + 1);
                    if (suffix == "com" || suffix == "org" || suffix == "edu")
                    {
                        Console.WriteLine("user = {0}", user);
                        Console.WriteLine("domain = {0}", domain);
                        Console.WriteLine("suffix = {0}", suffix);
                        break;
                    }
                    else
                    {
                        Console.WriteLine("Legal suffixes are .com, .org, .edu");
                        break;
                    }
                }
            }
        }
    }
예제 #17
0
    static void Main(string[] args)
    {
        InputWrapper iw   = new InputWrapper();
        string       name = " ";
        string       spouse;
        ArrayList    MyMarriedFriends = new ArrayList();

        while (true)
        {
            Console.WriteLine("Enter friend's name, quit to exit");
            name = iw.getString("> ");
            if (name.Equals("quit"))
            {
                break;
            }
            Console.WriteLine("Enter friend's spouse's name");
            spouse = iw.getString("> ");
            Friend node = new Friend(name, spouse);
            MyMarriedFriends.Add(node);
        }
        foreach (object o in MyMarriedFriends)
        {
            Friend f = (Friend)o;
            Console.WriteLine(f.ToString());
        }
        MyMarriedFriends.Sort();         // default sort
        Console.WriteLine("\nSorted by friend");
        foreach (object o in MyMarriedFriends)
        {
            Friend f = (Friend)o;
            Console.WriteLine(f.ToString());
        }
        MyMarriedFriends.Sort(new SpouseComparer());         // alternate sort
        Console.WriteLine("\nSorted by Spouse");
        foreach (object o in MyMarriedFriends)
        {
            Friend f = (Friend)o;
            Console.WriteLine(f.ToString());
        }
        Console.Write("Enter <cr> to end program");
        Console.ReadLine();
    }
예제 #18
0
    public static void Main(string[] args)
    {
        Account acc = new Account(100);

        ShowBalance(acc);
        string       cmd;
        InputWrapper iw = new InputWrapper();

        Console.WriteLine("Enter command, quit to exit");
        cmd = iw.getString("> ");
        while (!cmd.Equals("quit"))
        {
            try
            {
                if (cmd.Equals("deposit"))
                {
                    decimal amount = iw.getDecimal("amount: ");
                    acc.Deposit(amount);
                    ShowBalance(acc);
                }
                else if (cmd.Equals("withdraw"))
                {
                    decimal amount = iw.getDecimal("amount: ");
                    acc.Withdraw(amount);
                    ShowBalance(acc);
                }
                else if (cmd.Equals("show"))
                {
                    ShowBalance(acc);
                }
                else
                {
                    help();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            cmd = iw.getString("> ");
        }
    }
예제 #19
0
    public static void Main()
    {
        InputWrapper iw = new InputWrapper();

        char[]   seps = { '@', '.' };
        string[] pieces;
        Console.WriteLine("Enter a string, empty string to terminate");
        string email = iw.getString("email: ");

        while (!email.Equals(""))
        {
            //Console.WriteLine(email);
            pieces = email.Split(seps);
            foreach (string part in pieces)
            {
                Console.WriteLine(part);
            }
            email = iw.getString("email: ");
        }
    }
예제 #20
0
    public static void Main()
    {
        Bank         bank = new Bank();
        InputWrapper iw   = new InputWrapper();
        string       cmd;

        Console.WriteLine("Enter command, quit to exit");
        cmd = iw.getString("> ");
        while (!cmd.Equals("quit"))
        {
            if (cmd.Equals("open"))
            {
                decimal bal   = iw.getDecimal("starting balance: ");
                string  owner = iw.getString("owner: ");
                int     id    = bank.AddAccount(bal, owner);
                Console.WriteLine("Account opened, id = {0}", id);
            }
            else if (cmd.Equals("close"))
            {
                int id = iw.getInt("account id: ");
                bank.DeleteAccount(id);
            }
            else if (cmd.Equals("show"))
            {
                ShowArray(bank.GetAccounts());
            }
            else if (cmd.Equals("account"))
            {
                int     id  = iw.getInt("account id: ");
                Account acc = bank.FindAccount(id);
                Atm.ProcessAccount(acc);
            }
            else
            {
                help();
            }
            cmd = iw.getString("> ");
        }
    }
예제 #21
0
    public static void ProcessAccount(Account acc)
    {
        Console.WriteLine("balance = {0}", acc.Balance);
        string       cmd;
        InputWrapper iw = new InputWrapper();

        Console.WriteLine("Enter command, quit to exit");
        cmd = iw.getString(acc.Prompt);
        while (!cmd.Equals("quit"))
        {
            if (cmd.Equals("deposit"))
            {
                decimal amount = iw.getDecimal("amount: ");
                acc.Deposit(amount);
                Console.WriteLine("balance = {0}", acc.Balance);
            }
            else if (cmd.Equals("withdraw"))
            {
                decimal amount = iw.getDecimal("amount: ");
                acc.Withdraw(amount);
                Console.WriteLine("balance = {0}", acc.Balance);
            }
            else if (cmd.Equals("owner"))
            {
                string owner = iw.getString("new owner name: ");
                acc.Owner = owner;
                show(acc);
            }
            else if (cmd.Equals("show"))
            {
                show(acc);
            }
            else
            {
                accountHelp();
            }
            cmd = iw.getString(acc.Prompt);
        }
    }
예제 #22
0
        static void Main(string[] args)
        {
            string       cmd = "";
            InputWrapper iw  = new InputWrapper();

            Console.WriteLine("Enter command, quit to exit");
            //cmd = iw.getString("> ")
            //.ToUpper();
            //while (cmd != "quit")
            while (!cmd.Equals("quit"))
            {
                cmd = iw.getString("> ").ToLower().TrimEnd();
                {
                    //Console.WriteLine("Enter command, quit to exit");
                    switch (cmd)
                    {
                    case "Add":
                        Console.WriteLine("Add is invoked");
                        break;

                    case "Forward":
                        Console.WriteLine("Forward is invoked");
                        break;

                    case "Reverse":
                        Console.WriteLine("Reverse is invoked");
                        break;

                    case "Find":
                        Console.WriteLine("Find is invoked");
                        break;

                    case "Remove":
                        Console.WriteLine("Remove is invoked");
                        break;

                    default:
                        Console.WriteLine("Add is to add new contact to the contact list");
                        Console.WriteLine("Forward is to add new contact to the contact list");
                        Console.WriteLine("Reverse is to add new contact to the contact list");
                        Console.WriteLine("Find is to add new contact to the contact list");
                        Console.WriteLine("Remove is to add new contact to the contact list");
                        break;
                    }
                }
            }
            //cmd = iw.getString(">");
        }
예제 #23
0
    public static int Main(string[] args)
    {
        InputWrapper iw = new InputWrapper();     // get an input reader (read from stdin)
        int          i  = iw.getInt("enter integer: ");

        Console.WriteLine("i = {0}", i);
        double d = iw.getDouble("enter double: ");

        Console.WriteLine("d = {0}", d);
        decimal dec = iw.getDecimal("enter decimal: ");

        Console.WriteLine("dec = {0}", dec);
        string s = iw.getString("enter string: ");

        Console.WriteLine("s = {0}", s);
        return(0);
    }
예제 #24
0
    public static void Main()
    {
        string[] names = new string[10];
        int      count = 0;
        int      index;
        string   target;

        names[count++] = "Tom";
        names[count++] = "Dick";
        names[count++] = "Harry";
        InputWrapper iw = new InputWrapper();

        Console.WriteLine("Enter command, quit to exit");
        string cmd = iw.getString("> ");

        while (!cmd.Equals("quit"))
        {
            //Console.WriteLine("command is " + cmd);
            switch (cmd)
            {
            case "add":
                //Console.WriteLine("add command");
                string name = iw.getString("name: ");
                names[count++] = name;
                break;

            case "forward":
                //Console.WriteLine("forward command");
                //foreach (string n in names)
                //	Console.WriteLine(n);
                for (int i = 0; i < count; i++)
                {
                    Console.WriteLine(names[i]);
                }
                break;

            case "reverse":
                //Console.WriteLine("reverse command");
                for (int i = count - 1; i >= 0; i--)
                {
                    Console.WriteLine(names[i]);
                }
                break;

            case "find":
                //Console.WriteLine("find command");
                target = iw.getString("target name: ");
                index  = Search(names, count, target);
                if (index != -1)
                {
                    Console.WriteLine("{0} found at {1}", target, index);
                }
                else
                {
                    Console.WriteLine("{0} not found", target);
                }
                break;

            case "remove":
                //Console.WriteLine("remove command");
                target = iw.getString("remove name: ");
                index  = Search(names, count, target);
                if (index != -1)
                {
                    // Move names to fill hole
                    for (int i = index; i < count - 1; i++)
                    {
                        names[i] = names[i + 1];
                    }
                    count--;
                    Console.WriteLine("{0} has been removed", target);
                }
                else
                {
                    Console.WriteLine("{0} not found", target);
                }
                break;

            default:
                Console.WriteLine("The following commands are available:");
                Console.WriteLine("   add       add a contact");
                Console.WriteLine("   forward   show contacts in forward order");
                Console.WriteLine("   reverse   show contacts in reverse order");
                Console.WriteLine("   find      find a contact");
                Console.WriteLine("   remove    remove a contact");
                Console.WriteLine("   quit      exit the program");
                break;
            }
            cmd = iw.getString("> ");
        }
    }
예제 #25
0
    public static void Main()
    {
        string[] names = new string[10];
        int      count = 0;

        //string target;
        names [count++] = "Mike";
        names[count++]  = "Pete";
        names[count++]  = "Sally";

        Console.WriteLine(names.Length);
        //instantiate InputWrapper object
        InputWrapper iw = new InputWrapper();
        //use getString method to prompt for command
        String cmd = iw.getString("> ");

        Console.WriteLine(cmd);
        while (cmd != "quit")
        {
            switch (cmd)
            {
            case "add":
                string name = iw.getString("names: ");
                names[count++] = name;
                Console.WriteLine(name);
                break;

            case "forward":
                for (int i = 0; i < names.Length; i++)
                {
                    Console.WriteLine(names[i]);
                }
                break;

            case "backward":
                Array.Reverse(names);
                //Console.WriteLine("Backward");
                PrintIndexAndValues(names);
                break;

            case "find":
                int    j      = 0;
                string target = iw.getString("Search: ");
                while (target != names[j])
                {
                    break;
                }
                Console.WriteLine(names[j]);
                break;

            case "remove":
                Console.WriteLine("Remove");
                break;

            default:
                Console.WriteLine("Available commands include: forward, backward, add, find, remove");
                break;
            }
            cmd = iw.getString("> ");
        }
    }
예제 #26
0
    public static void Main()
    {
        // Initialize accounts and show starting state
        Account[] accounts = new Account[3];
        accounts[0] = new CheckingAccount(100, "Bob", 0);
        accounts[1] = new SavingsAccount(200, "Mary", 1);
        accounts[2] = new CheckingAccount(300, "Charlie", 2);
        ShowAccounts(accounts);
        acc = accounts[0];
        SetInterfaces();
        Console.WriteLine(istat.GetStatement());
        // Command processing loop
        InputWrapper iw = new InputWrapper();
        string       cmd;

        Console.WriteLine("Enter command, quit to exit");
        cmd = iw.getString("> ");
        while (!cmd.Equals("quit"))
        {
            try
            {
                if (cmd.Equals("show"))
                {
                    ShowAccounts(accounts);
                }
                else if (cmd.Equals("account"))
                {
                    int id = iw.getInt("account id: ");
                    acc = accounts[id];
                    SetInterfaces();
                    Console.WriteLine(istat.GetStatement());
                }
                else if (cmd.Equals("deposit"))
                {
                    decimal amount = iw.getDecimal("amount: ");
                    iacc.Deposit(amount);
                    ShowBalance(istat);
                }
                else if (cmd.Equals("withdraw"))
                {
                    decimal amount = iw.getDecimal("amount: ");
                    iacc.Withdraw(amount);
                    ShowBalance(istat);
                }
                else if (cmd.Equals("statement"))
                {
                    Console.WriteLine(istat.GetStatement());
                }
                else if (cmd.Equals("post"))
                {
                    istat.Post();
                    Console.WriteLine(istat.GetStatement());
                }
                else if (cmd.Equals("month"))
                {
                    istat.MonthEnd();
                    Console.WriteLine(istat.GetStatement());
                }
                else if (cmd.Equals("fee"))
                {
                    if (ichk == null)
                    {
                        Console.WriteLine("IChecking is not supported");
                    }
                    else
                    {
                        Console.WriteLine("fee = {0:C}", ichk.Fee);
                    }
                }
                else if (cmd.Equals("interest"))
                {
                    if (isav == null)
                    {
                        Console.WriteLine("ISavings is not supported");
                    }
                    else
                    {
                        Console.WriteLine("interest = {0:C}", isav.Interest);
                    }
                }
                else if (cmd.Equals("rate"))
                {
                    if (isav == null)
                    {
                        Console.WriteLine("ISavings is not supported");
                    }
                    else
                    {
                        Console.WriteLine("rate = {0}", isav.Rate);
                    }
                }
                else
                {
                    help();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                if (e.InnerException != null)
                {
                    Console.WriteLine(e.InnerException.Message);
                }
            }
            cmd = iw.getString("> ");
        }
    }
예제 #27
0
    public static void Main()
    {
        Account   acc;
        IChecking ichk;
        ISavings  isav;

        // Initialize accounts and show starting state
        Account[] accounts = new Account[3];
        accounts[0] = new CheckingAccount(100, "Bob", 0);
        accounts[1] = new SavingsAccount(200, "Mary", 1);
        accounts[2] = new CheckingAccount(300, "Charlie", 2);
        ShowAccounts(accounts);
        acc = accounts[0];
        Console.WriteLine(acc.GetStatement());
        // Command processing loop
        InputWrapper iw = new InputWrapper();
        string       cmd;

        Console.WriteLine("Enter command, quit to exit");
        cmd = iw.getString("> ");
        while (!cmd.Equals("quit"))
        {
            try
            {
                if (cmd.Equals("show"))
                {
                    ShowAccounts(accounts);
                }
                else if (cmd.Equals("account"))
                {
                    int id = iw.getInt("account id: ");
                    acc = accounts[id];
                    Console.WriteLine(acc.GetStatement());
                }
                else if (cmd.Equals("deposit"))
                {
                    decimal amount = iw.getDecimal("amount: ");
                    acc.Deposit(amount);
                    ShowBalance(acc);
                }
                else if (cmd.Equals("withdraw"))
                {
                    decimal amount = iw.getDecimal("amount: ");
                    acc.Withdraw(amount);
                    ShowBalance(acc);
                }
                else if (cmd.Equals("statement"))
                {
                    Console.WriteLine(acc.GetStatement());
                }
                else if (cmd.Equals("fee"))
                {
                    // cast to interface and check for exception
                    try
                    {
                        ichk = (IChecking)acc;
                        Console.WriteLine("fee = {0:C}", ichk.Fee);
                    }
                    catch (InvalidCastException e)
                    {
                        Console.WriteLine("IChecking in not supported");
                        Console.WriteLine(e.Message);
                    }
                }
                else if (cmd.Equals("interest"))
                {
                    // use C# "is" operator
                    if (acc is ISavings)
                    {
                        isav = (ISavings)acc;
                        Console.WriteLine("interest = {0:C}", isav.Interest);
                    }
                    else
                    {
                        Console.WriteLine("ISavings in not supported");
                    }
                }
                else if (cmd.Equals("rate"))
                {
                    // use C# "as" operator
                    isav = acc as ISavings;
                    if (isav != null)
                    {
                        //isav = (ISavings) acc;	// cast not necessary
                        Console.WriteLine("rate = {0}", isav.Rate);
                    }
                    else
                    {
                        Console.WriteLine("ISavings in not supported");
                    }
                }
                else
                {
                    help();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                if (e.InnerException != null)
                {
                    Console.WriteLine(e.InnerException.Message);
                }
            }
            cmd = iw.getString("> ");
        }
    }
예제 #28
0
    public static void Main(string[] args)
    {
        string cmd;
        string buffer;
        string buffer2;

        iw     = new InputWrapper();
        buffer = iw.getString("Enter string to work on: ");
        Console.WriteLine("Enter command, 'quit' to exit");
        cmd = iw.getString("> ");
        while (!cmd.Equals("quit"))
        {
            if (cmd.Equals("length"))
            {
                Console.WriteLine("length = " + buffer.Length);
            }
            else if (cmd.Equals("new"))
            {
                buffer = iw.getString("new string: ");
            }
            else if (cmd.Equals("show"))
            {
                show(buffer);
            }
            else if (cmd.Equals("upper"))
            {
                buffer = buffer.ToUpper();
                show(buffer);
            }
            else if (cmd.Equals("lower"))
            {
                buffer = buffer.ToLower();
                show(buffer);
            }
            else if (cmd.Equals("append"))
            {
                buffer2 = iw.getString("Enter second string: ");
                buffer  = buffer + buffer2;
                show(buffer);
            }
            else if (cmd.Equals("substring"))
            {
                substring(buffer);
            }
            else if (cmd.Equals("index"))
            {
                index(buffer);
            }
            else if (cmd.Equals("char"))
            {
                character(buffer);
            }
            else if (cmd.Equals("reverse"))
            {
                buffer = reverse(buffer);
            }
            else
            {
                help();
            }
            cmd = iw.getString("> ");
        }
    }
예제 #29
0
    public static void Main()
    {
        // Initialize strings and show starting state
        list = new List <string>(4);
        ShowCount();
        AddString("Bob");
        AddString("Mary");
        AddString("Charlie");
        ShowList(list);
        ShowCount();
        // Command processing loop
        InputWrapper iw = new InputWrapper();
        string       cmd;

        Console.WriteLine("Enter command, quit to exit");
        cmd = iw.getString("> ");
        while (!cmd.Equals("quit"))
        {
            try
            {
                if (cmd.Equals("show"))
                {
                    ShowList(list);
                }
                else if (cmd.Equals("array"))
                {
                    ShowArray(list);
                }
                else if (cmd.Equals("enum"))
                {
                    ShowEnum(list);
                }
                else if (cmd.Equals("add"))
                {
                    string str = iw.getString("string: ");
                    AddString(str);
                }
                else if (cmd.Equals("remove"))
                {
                    string str = iw.getString("string: ");
                    RemoveString(str);
                }
                else if (cmd.Equals("removeat"))
                {
                    int index = iw.getInt("index: ");
                    RemoveAt(index);
                }
                else if (cmd.Equals("index"))
                {
                    string str = iw.getString("string: ");
                    ShowIndex(str);
                }
                else if (cmd.Equals("count"))
                {
                    ShowCount();
                }
                else if (cmd.Equals("sort"))
                {
                    Sort();
                }
                else
                {
                    help();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                if (e.InnerException != null)
                {
                    Console.WriteLine(e.InnerException.Message);
                }
            }
            cmd = iw.getString("> ");
        }
    }
예제 #30
0
    public static void Main()
    {
        string       cmd;
        InputWrapper iw = new InputWrapper();

        Console.WriteLine("Enter command, quit to exit");
        cmd = iw.getString(": ");
        while (!cmd.Equals("quit"))
        {
            if (cmd.Equals("create"))
            {
                size  = iw.getInt("size: ");
                array = new int[size];
            }
            else if (cmd.Equals("destroy"))
            {
                array = null;
                size  = 0;
            }
            else if (cmd.Equals("squares"))
            {
                for (int i = 0; i < size; i++)
                {
                    array[i] = i * i;
                }
                show();
            }
            else if (cmd.Equals("reverse"))
            {
                int[] temp = new int[size];
                for (int i = 0; i < size; i++)
                {
                    temp[i] = array[size - 1 - i];
                }
                array = temp;
                show();
            }
            else if (cmd.Equals("show"))
            {
                show();
            }
            else if (cmd.Equals("sort"))
            {
                if (array != null)
                {
                    Array.Sort(array);
                    show();
                }
            }
            else if (cmd.Equals("random"))
            {
                Random rand = new Random();
                for (int i = 0; i < size; i++)
                {
                    array[i] = rand.Next(100);
                }
                show();
            }
            else if (cmd.Equals("foreach"))
            {
                if (array != null)
                {
                    foreach (int x in array)
                    {
                        Console.Write("{0} ", x);
                    }
                    Console.WriteLine();
                }
            }
            else if (cmd.Equals("search"))
            {
                int target = iw.getInt("target: ");
                int index  = Array.BinarySearch(array, target);
                if (index < 0)
                {
                    Console.WriteLine("{0} not found", target);
                }
                else
                {
                    Console.WriteLine("{0} found at index {1}", target, index);
                }
            }
            else
            {
                help();
            }
            cmd = iw.getString(": ");
        }
    }