Presentation is loading. Please wait.

Presentation is loading. Please wait.

Maximum Profit Please use speaker notes for additional information!

Similar presentations


Presentation on theme: "Maximum Profit Please use speaker notes for additional information!"— Presentation transcript:

1 Maximum Profit Please use speaker notes for additional information!

2 Maximum Profit The problem was presented as you have a maximum budget of $1800 and the hours allocated for the work is 500 hours. Tables cost $60 and take 15 hours to make and have a profit of $50. Chairs cost $25 and take 10 hours to make and have a profit of $30. Benches cost $4 and take 2 hours to make and have a profit of $6. Figure out what combination of tables, chairs and benches to make to maximize profit.

3 Private Sub Form_Load() MaxProfit = 0 cnt = 0 For t = 0 To 30 budget = 1800 - t * 60 hours = 500 - t * 15 If budget / 25 < hours / 10 Then maxchairs = budget / 25 Else maxchairs = hours / 10 End If For c = 0 To maxchairs budgetx = budget - c * 25 hoursx = hours - c * 10 If budgetx / 4 < hoursx / 2 Then b = budgetx / 4 Else b = hoursx / 2 End If profit = (t * 50) + (c * 30) + (b * 6) If profit = 1630 Then savet(cnt) = t savec(cnt) = c saveb(cnt) = b cnt = cnt + 1 End If If profit > MaxProfit Then MaxProfit = profit End If Next c Next t Profitbox.Text = MaxProfit Tablelbl.Caption = Tablelbl.Caption & savet(0) & ", " & savet(1) & ", " & savet(2) & ", " & savet(3) & ", " & savet(4) Chairlbl.Caption = Chairlbl.Caption & savec(0) & ", " & savec(1) & ", " & savec(2) & ", " & savec(3) & ", " & savec(4) Benchlbl.Caption = Benchlbl.Caption & saveb(0) & ", " & saveb(1) & ", " & saveb(2) & ", " & saveb(3) & ", " & saveb(4) Countlbl.Caption = Countlbl.Caption & cnt End Sub Dim t As Integer Dim c As Integer Dim b As Integer Dim budget As Integer Dim budgetx As Integer Dim hours As Integer Dim hoursx As Integer Dim maxchairs As Integer Dim profit As Integer Dim MaxProfit As Integer Dim savet(5) As Integer Dim savec(5) As Integer Dim saveb(5) As Integer Dim cnt As Integer The Dim statements are located in General/Declarations but in fact there is only one routine so it does not matter. When the for loops are complete, I print out the contents of the arrays with the maximum profit combinations.

4 Private Sub Form_Load() MaxProfit = 0 cnt = 0 For t = 0 To 30 budget = 1800 - t * 60 hours = 500 - t * 15 If budget / 25 < hours / 10 Then maxchairs = budget / 25 Else maxchairs = hours / 10 End If For c = 0 To maxchairs budgetx = budget - c * 25 hoursx = hours - c * 10 If budgetx / 4 < hoursx / 2 Then b = budgetx / 4 Else b = hoursx / 2 End If profit = (t * 50) + (c * 30) + (b * 6) If profit = 1630 Then savet(cnt) = t savec(cnt) = c saveb(cnt) = b cnt = cnt + 1 End If If profit > MaxProfit Then MaxProfit = profit End If Next c Next t Profitbox.Text = MaxProfit Maximum Profit With a maximum budget of $1800 and maximum hours of 500, I did the math to figure out the maximum number of tables I could make. It costs $60 to make a table so, 1800/60 = 30 It takes 15 hours to make a table so, 500/15 = 33.3 but that would be over budget so I know the maximum number of tables is 30. This results in the FOR t = 0 to 30 loop. This means that cost is the dominant number in making tables.

5 Private Sub Form_Load() MaxProfit = 0 cnt = 0 For t = 0 To 30 budget = 1800 - t * 60 hours = 500 - t * 15 If budget / 25 < hours / 10 Then maxchairs = budget / 25 Else maxchairs = hours / 10 End If For c = 0 To maxchairs budgetx = budget - c * 25 hoursx = hours - c * 10 If budgetx / 4 < hoursx / 2 Then b = budgetx / 4 Else b = hoursx / 2 End If profit = (t * 50) + (c * 30) + (b * 6) If profit = 1630 Then savet(cnt) = t savec(cnt) = c saveb(cnt) = b cnt = cnt + 1 End If If profit > MaxProfit Then MaxProfit = profit End If Next c Next t Profitbox.Text = MaxProfit Maximum Profit Whatever t (number of tables) is, we are figuring out how much budget and how many hours are left after we deduct t times cost and hours. Now we need to determine the maximum number of chairs. To do that we need to find out whether hours or budget is the deciding factor in chairs. We will now do the For loop varying c (number of chairs) from 0 to maxchairs. Whatever c is, we are figuring out how much budget and how many hours are left after we deduct c times cost and hours.

6 Private Sub Form_Load() MaxProfit = 0 cnt = 0 For t = 0 To 30 budget = 1800 - t * 60 hours = 500 - t * 15 If budget / 25 < hours / 10 Then maxchairs = budget / 25 Else maxchairs = hours / 10 End If For c = 0 To maxchairs budgetx = budget - c * 25 hoursx = hours - c * 10 If budgetx / 4 < hoursx / 2 Then b = budgetx / 4 Else b = hoursx / 2 End If profit = (t * 50) + (c * 30) + (b * 6) If profit = 1630 Then savet(cnt) = t savec(cnt) = c saveb(cnt) = b cnt = cnt + 1 End If If profit > MaxProfit Then MaxProfit = profit End If Next c Next t Profitbox.Text = MaxProfit Maximum Profit We now need to determine (after subtracting from the budget for tables and chairs) what is going to control benches b - the budget or the hours. We then use that figure to establish the value of b (number of benches). We can now calculate profit based on what we have been told the profit is for each of these. Tables has a 50 profit, chairs has a 30 profit and benches has a 6 profit. I have already figured out that the maximum profit with these particular figures is 1630. If that profit is achieved then I move the combination that achieved it to the save areas. I also set MaxProfit to that amount. That is really not needed, I could have preset it.

7 Maximum Profit - variable numbers The second view shows the scroll bar positioned at the bottom.

8 Maximum Profit - variable numbers Private Sub txtBenchPrice_LostFocus() Call EditRtn(txtBenchPrice) End Sub Private Sub txtBenchTime_LostFocus() Call EditRtn(txtBenchTime) End Sub Private Sub txtBudgtDol_LostFocus() Call EditRtn(txtBudgtDol) End Sub Private Sub txtBudgtHrs_LostFocus() Call EditRtn(txtBudgtHrs) End Sub Private Sub txtChairCost_LostFocus() Call EditRtn(txtChairCost) End Sub Private Sub txtChairPrice_LostFocus() Call EditRtn(txtChairPrice) End Sub Private Sub txtChairTime_LostFocus() Call EditRtn(txtChairTime) End Sub Private Sub txtTableCost_LostFocus() Call EditRtn(txtTableCost) End Sub Private Sub txtTablePrice_LostFocus() Call EditRtn(txtTablePrice) End Sub Private Sub txtTableTime_LostFocus() Call EditRtn(txtTableTime) End Sub

9 Maximum Profit - variable numbers “Object variables are stored as 32-bit (4-byte) addresses that refer to objects. Using the Set statement, a variable declared as an Object can have any object reference assigned to it.” Visual Basic Help Private Sub txtTableCost_LostFocus() Call EditRtn(txtTableCost) End Sub Private Sub txtTablePrice_LostFocus() Call EditRtn(txtTablePrice) End Sub Private Sub txtTableTime_LostFocus() Call EditRtn(txtTableTime) End Sub Private Sub EditRtn(CurrCtl As Object) If CurrCtl.Text <> "" Then If Not (IsNumeric(CurrCtl.Text)) Or _ Val(CurrCtl.Text) < 1 Or _ Int(Val(CurrCtl.Text)) <> Val(CurrCtl.Text) Then Beep CurrCtl.Text = "" CurrCtl.SetFocus End If End Sub If the field contains data, I want to make sure that it is numeric, positive and a whole number. This code will beep, set the field to null and give it focus if any of those criteria are not met.

10 Maximum Profit - variable numbers This is the data entry screen when we start the program. The last thing to be filled in is Benches Cost. At that point, a routine is executed to show the next stage.

11 Private Sub txtBenchCost_LostFocus() Call EditRtn(txtBenchCost) If txtBenchCost.Text <> "" Then lblProfit.Visible = True lblChairProfit.Caption = txtChairPrice.Text - txtChairCost.Text lblChairProfit.Visible = True lblTableProfit.Caption = txtTablePrice.Text - txtTableCost.Text lblTableProfit.Visible = True lblBenchProfit.Caption = txtBenchPrice.Text - txtBenchCost.Text lblBenchProfit.Visible = True btnCalcMaxProfit.Visible = True btnCalcMaxProfit.SetFocus End If End Sub Maximum Profit - variable numbers After we loose focus on txtBenchCost, the profit label area are made visible and the calculate profit appears. The button to activate the next processing is also made visible and receives focus. After the next stage, this button will disappear.

12 Maximum Profit - variable numbers The next area shows the maximum profit, tells the number of combinations and shows them. Notice that the button that was pressed to cause the last stage to happen is no longer visible.

13 Private Sub btnCalcMaxProfit_Click() Dim MaxProfit As Integer, Profit As Integer Dim MaxChairs As Integer, MaxTables As Integer Dim t As Integer, c As Integer, b As Integer Dim cnt As Integer, I As Integer Dim Budget1 As Integer, Hours1 As Integer Dim Budget2 As Integer, Hours2 As Integer Dim UnitsArray() As Integer btnCalcMaxProfit.Visible = False MaxProfit = 0 cnt = 0 MaxTables = Int(txtBudgtDol.Text / txtTableCost.Text) If MaxTables > Int(txtBudgtHrs.Text / txtTableTime.Text) Then MaxTables = Int(txtBudgtHrs.Text / txtTableTime.Text) End If For t = 0 To MaxTables Budget1 = txtBudgtDol.Text - (t * txtTableCost.Text) Hours1 = txtBudgtHrs.Text - (t * txtTableTime.Text) If Budget1 / txtChairCost.Text < Hours1 / txtChairTime.Text Then MaxChairs = Int(Budget1 / txtChairCost.Text) Else MaxChairs = Int(Hours1 / txtChairTime.Text) End If Maximum Profit - variable numbers MaxTables which is the maximum number of tables that can be produced given the budgeted dollars and time constraints is first calculated by dividing the budget in dollars by the table cost. I then checked to see if that number was greater than the budgeted time divided by the time it takes to make a table. If it is I put the hours calculation into MaxTables. The For statement goes from 0 to MaxTables. MaxChairs is no determined in the same way.

14 For c = 0 To MaxChairs Budget2 = Budget1 - (c * txtChairCost.Text) Hours2 = Hours1 - (c * txtChairTime.Text) If Budget2 / txtBenchCost.Text < Hours2 / txtBenchTime.Text Then b = Int(Budget2 / txtBenchCost.Text) Else b = Int(Hours2 / txtBenchTime.Text) End If Profit = (t * lblTableProfit.Caption) _ + (c * lblChairProfit.Caption) _ + (b * lblBenchProfit.Caption) If Profit > MaxProfit Then MaxProfit = Profit cnt = 0 ReDim UnitsArray(3, 0) End If If Profit = MaxProfit Then ReDim Preserve UnitsArray(3, cnt + 1) UnitsArray(1, cnt) = c UnitsArray(2, cnt) = t UnitsArray(3, cnt) = b cnt = cnt + 1 End If Next c Next t Maximum Profit - variable numbers If the maximum profit has changed, then a new value is moved to maximum profit and the array is re-dimensioned to hold a place for chairs, tables and benches (that is why there is a 3) and 0 elements meaning that it is reset to contain a blank row. Cnt is reset to 0 since there is nothing in the array and when entering we will add to 0th element. If the Profit is = to MaxProfit then the array is re-dimensioned to add room for one more set of chair, table and bench combinations but the integrity of the data in the array is preserved with the preserve.

15 lblMaxProfit.Caption = "The maximum profit is " & MaxProfit lblMaxProfit.Visible = True lblComboCnt.Caption = "of chairs, tables and benches " _ & "that will produce the maximum profit." If cnt > 0 Then If cnt > 1 Then lblComboCnt.Caption = "There are " & cnt & " combinations " & lblComboCnt.Caption & " They are:" Else lblComboCnt.Caption = "There is 1 combination " & lblComboCnt.Caption & " It is:" End If lblComboCnt.Visible = True If cnt > 0 Then lblTableHdr.Visible = True For I = 0 To cnt - 1 txtTableDisp.Text = txtTableDisp.Text _ & Space(7 - Len(Str(UnitsArray(1, I)))) & UnitsArray(1, I) _ & Space(15 - Len(Str(UnitsArray(2, I)))) & UnitsArray(2, I) _ & Space(14 - Len(Str(UnitsArray(3, I)))) & UnitsArray(3, I) _ & Chr(13) & Chr(10) Next I txtTableDisp.Visible = True End If End Sub Maximum Profit - variable numbers When the FOR loops are complete which they were in the last slide. The information about the MaxProfit is written out and the data from the array is displayed to show the combinations that were stored.

16 Maximum Profit - variable numbers If Profit > MaxProfit Then MaxProfit = Profit cnt = 0 ReDim UnitsArray(2, 0) End If If Profit = MaxProfit Then UnitsArray(0, cnt) = c UnitsArray(1, cnt) = t UnitsArray(2, cnt) = b cnt = cnt + 1 ReDim Preserve UnitsArray(2, cnt) End If 2,0 sets it up with essentially a 3 by 1 dimension which is exactly what I need to enter my first combination. Now the data goes into the 0th, 1st and 2nd. The first row will have cnt = 0. Then I added 1 to cnt and redimensioned the array in preparation for the next element. Note that in doing this I preserved what was already in the array.

17 Maximum Profit - variable numbers If cnt > 0 Then lblTableHdr.Visible = True For I = 0 To cnt - 1 txtTableDisp.Text = txtTableDisp.Text _ & Space(7 - Len(Str(UnitsArray(0, I)))) & UnitsArray(0, I) _ & Space(15 - Len(Str(UnitsArray(1, I)))) & UnitsArray(1, I) _ & Space(14 - Len(Str(UnitsArray(2, I)))) & UnitsArray(2, I) _ & Chr(13) & Chr(10) Next I txtTableDisp.Visible = True End If Because I changed the dimensions of the array I now change the 1, 2,3 to 0, 1, 2 in this code.

18 Maximum Profit - variable numbers Private Sub txtTableCost_Validate(KeepFocus As Boolean) Call EditRtn(txtTableCost, KeepFocus) End Sub Private Sub txtTablePrice_Validate(KeepFocus As Boolean) Call EditRtn(txtTablePrice, KeepFocus) End Sub Private Sub txtTableTime_Validate(KeepFocus As Boolean) Call EditRtn(txtTableTime, KeepFocus) End Sub Private Sub EditRtn(CurrCtl As Object, KeepFocus As Boolean) If CurrCtl.Text = "" Or _ CurrCtl.Text <> "" And _ (Not (IsNumeric(CurrCtl.Text)) Or _ Val(CurrCtl.Text) < 1 Or _ Int(Val(CurrCtl.Text)) <> Val(CurrCtl.Text)) Then Beep CurrCtl.Text = "" KeepFocus = True End If End Sub KeepFocus allows me to keep the focus on the field.

19 Properties continued Properties

20

21 lblMaxProfit.Caption = "The maximum profit is " & MaxProfit lblMaxProfit.Visible = True lblComboCnt.Caption = "of chairs, tables and benches " _ & "that will produce the maximum profit." If cnt > 0 Then If cnt > 1 Then lblComboCnt.Caption = "There are " & cnt & " combinations " & lblComboCnt.Caption & " They are:" Else lblComboCnt.Caption = "There is 1 combination " & lblComboCnt.Caption & " It is:" End If Output I put in initial captions in the properties to simply show what would go in these areas.


Download ppt "Maximum Profit Please use speaker notes for additional information!"

Similar presentations


Ads by Google