Beispiel #1
0
        /// <summary>
        /// Konstruktor okna
        /// </summary>
        /// <param name="carExpensesApp">Instance jádra aplikace</param>
        /// <param name="user">Aktuálně přihlášený uživatel</param>
        public ExpensesForm(CarExpensesApp carExpensesApp, User user)
        {
            InitializeComponent();

            this.carExpensesApp = carExpensesApp;
            this.user = user;
            this.selectedCar = null;

            disableElements();
            reloadUserCars();
        }
Beispiel #2
0
        /// <summary>
        /// Výpis všech aut přihlášeného uživatele - uživatel vybírá šipkami nahoru a dolu, entrem potvrzuje, escape ruší nabídku
        /// </summary>
        /// <returns>Stav, zda se výběr podařil (true/false)</returns>
        static bool selectCar()
        {
            CarResponse response = application.getUserCars();
            Console.WriteLine(response.message);
            if (response.success == false || response.carList.Count == 0)
            {
                keyToContinue();
                return false;
            }

            Console.WriteLine();

            List<Car> list = response.carList;

            ConsoleKeyInfo key;

            int selected = 0;
            Car selectedCar = null;
            int currentLineCursor = Console.CursorTop;

            do
            {
                Console.SetCursorPosition(0, currentLineCursor);

                int i = 0;
                foreach(Car _car in list)
                {
                    if (i == selected)
                    {
                        Console.Write("> ");
                        selectedCar = _car;
                    }
                    else
                    {
                        Console.Write("  ");
                    }
                    Console.WriteLine(_car.name);
                    i++;
                }

                key = Console.ReadKey();
                if (key.Key == ConsoleKey.UpArrow)
                {
                    if (selected>0) selected--;
                }
                else if (key.Key == ConsoleKey.DownArrow)
                {
                    if (selected < list.Count-1) selected++;
                }
                else if (key.Key == ConsoleKey.Escape)
                {
                    return false;
                }
            }
            while (key.Key != ConsoleKey.Enter);

            Console.WriteLine();

            car = selectedCar;
            return true;
        }
Beispiel #3
0
        /// <summary>
        /// Vstupní bod aplikace
        /// </summary>
        static void Main(string[] args)
        {
            Console.Write(startText);

            try
            {
                initApp();
                keyToContinue();
            Start:
                Console.Clear();
                Console.Write(startText);

                switch (menu(new string[] { "Login", "Register", "Exit" }))
                {
                    case 0: // Escape
                        goto Start;
                    case 1: // Login
                        if (login())
                            goto MainMenu;
                        else
                            goto Start;
                    case 2: // Register
                        register();
                        goto Start;
                    case 3: // Exit
                        exit(0);
                        break;
                }
                goto Start;

            MainMenu:
                Console.Clear();
                Console.Write(mainMenuText);
                switch ( menu(new string[] { "Select car", "Add new car", "Logout", "Exit" }) )
                {
                    case 0: // Escape
                        goto MainMenu;
                    case 1: // Select car
                        if (selectCar())
                            goto CarMenu;
                        else
                            goto MainMenu;
                    case 2: // Add new car
                        addCar();
                        goto MainMenu;
                    case 3: // Logout
                        if (logout())
                            goto Start;
                        else
                            goto MainMenu;
                    case 4: // Exit
                        exit(0);
                        break;
                }
                goto MainMenu;

            CarMenu:
                Console.Clear();
                Console.Write(getCarMenuText());
                switch (menu(new string[] { "Add gas", "Add service", "History of gas", "History of service", "Back", "Logout", "Exit" }))
                {
                    case 0: // Escape
                        car = null;
                        goto MainMenu;
                    case 1: // Add gas
                        addGas();
                        goto CarMenu;
                    case 2: // Add service
                        addService();
                        goto CarMenu;
                    case 3: // History of gas
                        writeGasStats();
                        goto CarMenu;
                    case 4: // History of service
                        writeServiceStats();
                        goto CarMenu;
                    case 5: // Back
                        car = null;
                        goto MainMenu;
                    case 6: // Logout
                        if (logout())
                            goto Start;
                        else
                            goto CarMenu;
                    case 7: // Exit
                        exit(0);
                        break;
                }
                goto CarMenu;

                exit(0);
            }
            catch (CarExpensesException ex)
            {
                Console.WriteLine(ex.Message);
                exit(1);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Vkládá do databáze nové auto.
        /// Vše je potřebné se nachází v objektu auta (parametr <paramref name="userId">userId</paramref>),
        /// včetné ID uživatele, ke kterému se vztahuje.
        /// </summary>
        /// <param name="car">Nové auto pro vložení do databáze</param>
        /// <returns>Úspěch vložení</returns>
        /// <exception cref="CarExpensesDatabaseException">Při chybě práce s databází</exception>
        public bool addCar(Car car)
        {
            using (OracleCommand cmdInsert = new OracleCommand())
            {
                try
                {
                    string sqlInsert = "INSERT INTO \"car\" (\"user_id\", \"car_model_id\", \"bought_year\", \"cost\", \"name\") ";
                    sqlInsert += "values (:p_user_id, :p_car_model_id, :p_bought_year, :p_cost, :p_name)";

                    cmdInsert.CommandText = sqlInsert;
                    cmdInsert.Connection = connection;

                    OracleParameter pUserId = new OracleParameter();
                    pUserId.OracleDbType = OracleDbType.Decimal;
                    pUserId.Value = car.userId;
                    pUserId.ParameterName = "p_user_id";

                    OracleParameter pCarModelId = new OracleParameter();
                    pCarModelId.OracleDbType = OracleDbType.Decimal;
                    pCarModelId.Value = car.carModelId;
                    pCarModelId.ParameterName = "p_car_model_id";

                    OracleParameter pBoughtYear = new OracleParameter();
                    pBoughtYear.OracleDbType = OracleDbType.Decimal;
                    pBoughtYear.Value = car.boughtYear;
                    pBoughtYear.ParameterName = "p_bought_year";

                    OracleParameter pCost = new OracleParameter();
                    pCost.OracleDbType = OracleDbType.Decimal;
                    pCost.Value = car.cost;
                    pCost.ParameterName = "p_cost";

                    OracleParameter pName = new OracleParameter();
                    pName.Value = car.name;
                    pName.ParameterName = "p_name";

                    cmdInsert.Parameters.Add(pUserId);
                    cmdInsert.Parameters.Add(pCarModelId);
                    cmdInsert.Parameters.Add(pBoughtYear);
                    cmdInsert.Parameters.Add(pCost);
                    cmdInsert.Parameters.Add(pName);

                    if ( cmdInsert.ExecuteNonQuery() > 0 )
                    {
                        return true;
                    }
                    else return false;
                }
                catch (OracleException ex)
                {
                    switch (ex.Number)
                    {
                        case 1:
                            throw new CarExpensesDatabaseException("Car ID " + car.id + " already exists.");
                        case 2291:
                            throw new CarExpensesDatabaseException("Car Model ID " + car.carModelId + " doesn't exist.");
                        case 12545:
                            throw new CarExpensesDatabaseException("The database is unavailable.");
                        default:
                            throw new CarExpensesDatabaseException("Database error: " + ex.Message.ToString());
                    }
                }
                catch (CarExpensesDatabaseException ex)
                {
                    throw new CarExpensesDatabaseException(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    throw new CarExpensesDatabaseException("Unexpected error: " + ex.Message.ToString());
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// Získává všechny auta patřící uživateli specifikovaného parametrem <paramref name="userId">userId</paramref>
        /// </summary>
        /// <param name="userId">ID uživatele</param>
        /// <returns>Seznam aut daného uživatele</returns>
        /// <exception cref="CarExpensesDatabaseException">Při chybě práce s databází</exception>
        public List<Car> getUserCars(int userId)
        {
            List<Car> carList = new List<Car>();

            using (OracleCommand cmd = new OracleCommand())
            {
                try
                {
                    cmd.Connection = connection;
                    cmd.CommandText = "SELECT * FROM \"car\" WHERE \"user_id\" = :p_user_id";
                    cmd.CommandType = System.Data.CommandType.Text;

                    OracleParameter pUserId = new OracleParameter();
                    pUserId.OracleDbType = OracleDbType.Decimal;
                    pUserId.Value = userId;
                    pUserId.ParameterName = "p_user_id";

                    cmd.Parameters.Add(pUserId);

                    using (OracleDataReader dr = cmd.ExecuteReader())
                    {
                        if (dr.HasRows)
                        {
                            while (dr.Read())
                            {
                                Car car = new Car();
                                car.id = Convert.ToInt32(dr["id"].ToString());
                                car.userId = Convert.ToInt32(dr["user_id"].ToString());
                                car.carModelId = Convert.ToInt32(dr["car_model_id"].ToString());
                                car.boughtYear = Convert.ToInt32(dr["bought_year"].ToString());
                                car.cost = Convert.ToInt32(dr["cost"].ToString());
                                car.name = dr["name"].ToString();
                                carList.Add(car);
                            }
                        }

                        return carList;
                    }
                }
                catch (OracleException ex)
                {
                    switch (ex.Number)
                    {
                        case 12545:
                            throw new CarExpensesDatabaseException("The database is unavailable.");
                        default:
                            throw new CarExpensesDatabaseException("Database error: " + ex.Message.ToString());
                    }
                }
                catch (CarExpensesDatabaseException ex)
                {
                    throw new CarExpensesDatabaseException(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    throw new CarExpensesDatabaseException("Unexpected error: " + ex.Message.ToString());
                }
            }
        }
Beispiel #6
0
 /// <summary>
 /// Konstruktor okna
 /// </summary>
 /// <param name="carExpensesApp">Instance jádra aplikace</param>
 /// <param name="car">Auto, ke kterému se oprava přidává</param>
 public AddServiceForm(CarExpensesApp carExpensesApp, Car car)
 {
     InitializeComponent();
     this.carExpensesApp = carExpensesApp;
     this.car = car;
 }
Beispiel #7
0
 private void comboSelectCar_SelectionChangeCommitted(object sender, EventArgs e)
 {
     selectedCar = (Car)comboSelectCar.SelectedItem;
     reloadCarInfo();
     reloadGas();
     reloadService();
     reloadOtherExpense();
     enableElements();
 }
Beispiel #8
0
        /// <summary>
        /// Přidání nového auta k aktuálně přihlášenému uživateli
        /// </summary>
        /// <param name="carModelId">Identifikace typu auta</param>
        /// <param name="name">Jměno auta</param>
        /// <param name="boughtYear">Rok zakoupení auta</param>
        /// <param name="cost">Cena auta</param>
        /// <returns>Objekt <see cref="Response"/>, kde je uložen stav akce a zpráva.</returns>
        /// <exception cref="CarExpensesException">Při chybě aplikace</exception>
        public Response addCar(int carModelId, string name, int boughtYear, int cost)
        {
            if (notLogged()) return new Response(false, "You are NOT logged in.");

            if (boughtYear < 1900 || boughtYear > DateTime.Today.Year)
                return new Response(false, "Bought year must be between " + 1900 + " and " + DateTime.Today.Year + ".");

            Response response = new Response();
            try
            {
                Car car = new Car();
                car.userId = user.id;
                car.carModelId = carModelId;
                car.boughtYear = boughtYear;
                car.cost = cost;
                car.name = name;

                if (carDAO.addCar(car) == true)
                {
                    response.message = "Car was successfuly added.";
                    response.success = true;
                }
            }
            catch (CarExpensesDatabaseException ex)
            {
                response.success = false;
                response.message = ex.Message.ToString();
            }
            return response;
        }