Best way to integrate values into enum (dictionary), then calculating based on user selection in form - asp.net-mvc-3

Any guidance or pointing me to an example would be much appreciated (I can't formulate a good search term on the Googleplex).
I have a model using enums that i define in a dictionary and then render on the view with #Html.RadioButtonFor, etc.
Here is an example of my model:
public PaymentPlanList PaymentPlan { get; set; }
public enum PaymentPlanList
{
PaymentPlan_One,
PaymentPlan_Two,
}
public class PaymentPlanDictionary
{
public static readonly Dictionary<PaymentPlanList, string> paymentplanDictionary = new Dictionary<PaymentPlanList, string>
{
{ PaymentPlanList.PaymentPlan_One, "One full payment in advance (receive the lowest price)." },
{ PaymentPlanList.PaymentPlan_Two, "Two payments: first payment of 50% due up front, the balance of 50% due within 30 days (increases fee by $100)." },
};
static string ConvertPaymentPlan(PaymentPlanList paymentplanlist)
{
string name;
return (paymentplanDictionary.TryGetValue(paymentplanlist, out name))
? name : paymentplanlist.ToString();
}
static void Main()
{
Console.WriteLine(ConvertPaymentPlan(PaymentPlanList.PaymentPlan_One));
Console.WriteLine(ConvertPaymentPlan(PaymentPlanList.PaymentPlan_Two));
}
}
And, for completeness, this is my view related to the above:
<p>
#Html.RadioButtonFor(m => m.PaymentPlan, "PaymentPlan_One")
One full payment in advance (receive the lowest price).
</p>
<p>
#Html.RadioButtonFor(m => m.PaymentPlan, "PaymentPlan_Two")
Two payments: first payment 50% due up front, the balance of 50% due within 30 days (increases fee by $100).
</p>
This is a quote system I have users fill out. For this particular service, say I charge $1,000.00. This is the base price. Based on user input, this price will be changed, and I want to show that to the user. So, if the user selects the first option, the price remains unchanged. If the user selects the second option, the fee is increased by $100.00.
This changes exponentially, since there are more inputs that affect the price (if selected).
Ultimately, based on the user inputs, I need to calculate the total. I am rendering a view which will display the total. I was thinking of using some #{} blocks and if/else if statements to either a) show nothing if what was selected does not increase the total, or b) showing the additional amount (e.g., $100.00), and then later showing a total.
Something like (EDITING here for clarity):
Base service: $1,000.00
Addon service1: $100.00 (only if user selects "PaymentPlan_Two" for two payments of 50% each (from the PaymentPlanList enum), otherwise hidden (and no addition of the $100.00) if user selects "PaymentPan_One" and pays in full)
Addon service2: $0.00 (this is hidden and a $0.00 or no value since the user did not select anything from a separate enum, but the value of $100.00 would be added if selected, which would make the Total $1,200.00 if it were selected; ALTERNATIVELY, how could I handle if there were 3 or more items in the list? E.g., Choice_One is $0.00, Choice_Two is $100.00 and Choice_Three is $200.00)
TOTAL: $1,100.00
Thanks for any help.

let's see if I understand your requirements correctly:
The application needs to add prices to a base price, depending on
selection of Addon services.
These selections are from a Dictionary, which is based on an Enum
Therefore we're looking to store the price against the Enum to keep the data associations in one place.
It is possible to store a single value against an Enum:
public enum PaymentPlanList
{
PaymentPlan_One = 100,
PaymentPlan_Two = 200,
}
However, I don't think this would be flexible enough for our needs - Enums only allow integers, and are commonly used this way in bitwise operations (where the values are multiples of 2).
I think a better solution here might be to use a Model-View-View-Model (MVVM) which can contain the logic about which services are available, how much they cost, and which services are valid in combination with other services.
There's an ticket pricing example (which sounds similar in concept to the domain here) on the Knockout.js home page that re-calculates a travel ticket price on the client web-page based on a user selection.

Related

What is the most appropriate data structures for these cases?

I am working on a program and I'm considering which data structure is most appropriate.
I have the following class -
public class item{
String name;
int value;
public item(String name, int value){
this.value = value;
this.name = name;
}
}
I have two cases -
To store a large amount of items and retrieve the top n amount of items while new items are being added and old items have their values updated.
To store all the items and retrieve top n amount of items without any new items being added or old items being updated.
I'm trying to achieve the best case time complexity for both cases.
I have thought about storing all items in a map for look up by their name, and in a maxheap, then removing n number of items from the top, storing them, then adding them back to the stack.
I've looked at priority queues and binary search trees as options, would either of these be more suitable? or are they any other structures that would fit either of these cases?
If name is unique you can use IDictionary. It is pretty fast for this uses

Is MNL the right model to use when the choice options vary across observations?

In a survey of 100 people, I am asking each person to choose between product A and product B. I ask each person this question 3 times, but each time I present a different set of products. Say, first time, Person 1 is asked to choose between 'Phone 1' and 'Phone 2', given certain attributes of each phone. The second time the choice is again 'Phone 1' vs. 'Phone 2', but a different set of attributes for each phone.
A person is presented three attributes associated with the two phone alternatives every time the question is asked. So, each time between Phone 1 and Phone 2, the attributes of the phone such as cost, memory and camera pixels are presented so that user can choose which set of attributes is most attractive, Phone 1's or Phone 2's.
Overall, 3*100 = 300 responses; 3 responses per person. Each time the attributes cost, memory and camera pixels presented and user asked to choose the feature set they prefer.
My goal is to analyze how users value features of a phone vs. cost of the phone.
In this scenario, can I use a MNL - even though each time I asked the person a question, I only presented two choices ? My understanding is that MNL is sued when (a) there are multiple choices and (b) the choice options do not change across observations, i.e. each person is asked to choose between multiple products, say A, B, C and A, B, C do not change across observations.
In the scenario described above, the two choices varied across the three times the same person was asked the question ? If not MNL, should I rather create a binary logit model given that user only had to choose between two options when the question was asked (even though he was asked the question three times)? If I can use binary logit, should I be concerned that the choice set of products change across observations ? or should I let the attributes defined in each of the rows address the differences in product choices across observations.
I have setup the data as follows (thinking I can do MNL but may be I should set it up differently and use another modeling approach?):
I am working on designing and analyzing similar survey but mine is related to transportation. I am at the beginning level and I am still new to the whole concept, however I will give you an advice and reference maybe it is helpful.
First point: I have come cross 3 models as following from a useful video on YouTube:
MNL refers to Multinomial Logit Model. MNL is used with
alternative-invariant regressors (for example salary of participant
in the survey, or his/her gender …).
Conditional logit model is used with alternative-invariant (gender,
salary, education level …) and alternative-variant regressors (cost
of the product, memory, camera pixel …)
Mixed logit model which uses random parameters. It is also used with
alternative-invariant (gender, salary, education level …) and
alternative-variant regressors (cost of the product, memory, camera
pixel …)
Note regarding alternative-invariant and alternative-variant regressors:
The gender of person participating in the survey will NOT vary between Product A or Product P, so it is alternative-invariant regressor. While price of product could vary between Product A and Product B so it is called alternative-variant regressors.
Based on above I assume you need to use conditional logit model or mixed logit model.
For me I couldn’t find a special function in R for the conditional logit model or mixed logit model. The same mlogit function is used, refer to the examples below for the help of mlogit package:
a pure "multinomial model"
summary(mlogit(mode ~ 0 | income, data = Fish))
a pure "conditional" model
summary(mlogit(mode ~ price + catch, data = Fish))
a "mixed" model
m <- mlogit(mode ~ price+ catch | income, data = Fish)
summary(m)
same model with charter as the reference level
m <- mlogit(mode ~ price+ catch | income, data = Fish, reflevel = "charter")
From the examples above, I think (but NOT sure) that in the Manual of mlogit package, they refer to mixed logit when you used both alternative-invariant and alternative-variant regressors. While conditional model when you have only alternative-variant regressors. On the other hand, multinomial model when you have only multinomial alternative-invariant regressors.
Second point: There is something called “panel data” when you are asking the same person to choose one product for each choice-set. Same person here means that in your model you are taking into consideration, the gender, the salary, the education level … which they will stay the same for the same person. Check this: https://en.wikipedia.org/wiki/Panel_data
To use panel techniques please refer to help in mlogit package in R. I am quoting from it the following:
“panel only relevant if rpar is not NULL and if the data are repeated observations of the same unit ; if TRUE, the mixed-logit model is estimated using panel techniques”
So in my understanding, if you want to use the panel techniques you have to use random draws because panel will be true and rpar will not be NULL.
Moreover, for example about using the panel data, please refer to the below example from “Estimation of multinomial logit models in R : The mlogit Packages” by Yves Croissant
data("Train", package = "mlogit")
Tr <- mlogit.data(Train, shape = "wide", varying = 4:11, choice = "choice", sep = "_", opposite = c("price", "time", "change", "comfort"), alt.levels=c("A", "B"), id.var ="id")
Train.ml <- mlogit(choice ~ price + time + change + comfort, Tr)
Train.mxlc <- mlogit(choice ~ price + time + change + comfort, Tr, panel = TRUE, rpar = c(time = "cn", change = "n", comfort = "ln"), correlation = TRUE, R = 100, halton = NA)
Train.mxlu <- update(Train.mxlc, correlation = FALSE)
I hope that is useful to you.

Magento Flat Rate Shipping only if chart is over a certain amount

In Magento 1.7, is there any way to show Flat Rate Shipping only if chart is over a certain amount?
By rule the only option seems to be free shipping, but my goal is to have Flat Rate if sub total in chart is xx (if sub total is lower, normal rates will be used).
Not by default. It's easy to extend the flat rate shipping carrier method though to do this. Simply create a new module, extend the flat rate shipping system.xml so that there is a new option for minimum cart price (this will allow you to configure it through admin). Extend the following class Mage_Shipping_Model_Carrier_Flatrate and override the collectRates method to check the cart total vs the admin config value. Return false if the cart value is not enough and it will not show up as an option, otherwise it will return the cost set in the admin.
Edit: Something like the following would do it. Extend the class, don't mod the core!
public function collectRates(Mage_Shipping_Model_Rate_Request $request)
{
// Get The Minimum Order Value From Admin Config & Compare To Cart Subtotal (Base Prices)
if (!$this->getConfigFlag('minimum_order') || $request->getBaseSubtotalInclTax() < $this->getConfigFlag('minimum_order')) {
return false;
} else {
return parent::collectRates($request);
}
}

Magento1.7 - Add optional fee over order

How can I add an optional 10% fee over the whole order? I want my customers to be able to choose between no fee or a optional 10% fee (with some advantages for them).
At this moment, I've tried to represent this by enabling "Free shipping" and "Flat rate".
So at System/Configuration/Shipping Methods I've put the following values:
- 'Handling Fee' => '0.10'
- 'Calculate Handling Fee'=>'Percent'
- 'Type' => 'Per order'
As a result, the generated orders have a '10 cents' fee, instead of a percentage over its value.
How does can I represent this with Magento? Should I use Flat rates?
PS: I'm testing it only with back-end, should I face any difference compared with end user?
I have achieved what I was looking for by editing app/code/core/Mage/Shipping/Model/Carrier/Flatrate.php by adding the following method
protected function _getPerorderPrice($cost, $handlingType, $handlingFee){
if ($handlingType == self::HANDLING_TYPE_PERCENT) {
$val = ($cost * $this->_numBoxes * $handlingFee);
return $val;
}
return ($cost * $this->_numBoxes) $handlingFee;
}
and by changing the line 78 to:
$shippingPrice = $request->getPackageValue();
I'm not sure if this will cause any side-effect, so please comment if you identify any :)
At this moment everything seems to be working fine.

How to send list items to view part by part in mvc3?

I have a product list and want to send them to view part by part in every page refresh, 5 per page. When all parts have been displayed start over at the beginning of the list on the next refresh. If the product list count is not dividend of 5, for example, count = 23, to come back to first. i.e. to take 15-20, then 20-2 (21, 22, 23, 1 and 2). Then, 3-8 and to continue.
I cannot use the paging rule as:
ViewBag.Result = db.Products.Skip( ( pageNo - 1 ) * 5).Take( 5 ).ToList();
Because, I will send that 5 products from all actions to views and update in every refreshing site.
I have an idea that to take random index and start, but I need to know the last sending index and in every refresh, send it to controller's current action. How can I accomplish this?
You need to keep the last sending index between the HTTP calls. Since HTTP is stateless, you need a medium to keep it in between. You may use Session Variable do to that.
Session["LastIndexSent"]= 5;
return View(someITemList);
Also try to avoid sending such Results in ViewBag. Using dynamic stuff like ViewBag/ViewData to transfer data between controller and view makes your code ugly.Use strongly typed approach.
List<Product> productList=GetProductListFromSomeWhere();
return View(productList);
And in your strongly typed view,
#model List<Product>
#foreach(var prod in Model)
{
<p>#prod.Name</p>
}

Resources