14-Jun-15 Program Examples The Good, the Bad, and the Ugly.

19
Mar 16, 2 022 Program Examples The Good, the Bad, and the Ugly
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    221
  • download

    1

Transcript of 14-Jun-15 Program Examples The Good, the Bad, and the Ugly.

Apr 18, 2023

Program Examples

The Good, the Bad, and the Ugly

Pre comments

Many of these examples are not Java, so if the syntax is a bit strange, don’t worry about that

Some of these examples work, some don’t Except as noted, the examples are from

http://www.thedailywtf.com(I may have made a few minor editing changes)

Password checker

private void txtHostname_KeyPress(object sender, KeyPressEventArgs e) { StringBuilder sb = new StringBuilder(); sb.Append("SELECT Passwd FROM [Users] WHERE Username='"); sb.Append(this.txtUsername.Text + "'");

String password = GetPassword(sb.ToString());

for (int i = 0; i < (sender as TextBox).Text.Length; i++) { if (password[i] == (sender as TextBox).Text[i]) { this.lblError.Text = ""; } else this.lblError.Text = "Incorrect Password!"; if (i == (sender as TextBox).Text.Length) { if (password[i] == (sender as TextBox).Text[i]) { LogUserIn(this.txtUsername.Text); } } }}

Assorted tests if (Math.max(1, 2) == 2) return;

If Not IsPostBack Then If Not IsPostBack Then LoadReport() End IfEnd If

IF 0 > 0BEGIN SELECT 'Equipment is in use‘ RETURNEND

More tests If Not(blnIsDirty = True) Then

' Do NothingElse UpdateAccountRecordEnd If

//return whether a double is negativebool IsNegative(double n){ string nStr = n.ToString(); if (nstr.IndexOf('-', 0, 1)) return true; return false;}

Access testIf ((mintAccessLevelForm = NO_ACCESS) Or _ ((mintOperationMode = OP_NEW) And (mintAccessLevelForm = READ_ACCESS) And _ (mintOperationMode = OP_NEW) And (mintAccessLevelIndividuals = READ_ACCESS) And _ (mintOperationMode = OP_NEW) And (mintAccessLevelAddressAlias = READ_ACCESS) And _ (mintOperationMode = OP_NEW) And (mintAccessLevelContracts = READ_ACCESS) And _ (mintOperationMode = OP_NEW) And (mintAccessLevelCostCenter = READ_ACCESS) And _ (mintOperationMode = OP_NEW) And (mintAccessLevelCorrespondence = READ_ACCESS) And _ (mintOperationMode = OP_NEW) And (mintAccessLevelAccountProfile = READ_ACCESS) And _ (mintOperationMode = OP_NEW) And (mintAccessLevelInstructions = READ_ACCESS) And _ (mintOperationMode = OP_NEW) And (mintAccessLevelFBOVendors = READ_ACCESS) And _ (mintOperationMode = OP_NEW) And (mintAccessLevelFBOChains = READ_ACCESS) And _ (mintOperationMode = OP_NEW) And (mintAccessLevelCatering = READ_ACCESS) And _ (mintOperationMode = OP_NEW) And (mintAccessLevelGround = READ_ACCESS) And _ (mintOperationMode = OP_NEW) And (mintAccessLevelAccountProfileOSR = READ_ACCESS)) Or _ ((mintOperationMode = OP_NEW) And (mintAccessLevelForm <> FULL_ACCESS) Or _ (mintOperationMode = OP_NEW) And (mintAccessLevelIndividuals <> FULL_ACCESS) Or _ (mintOperationMode = OP_NEW) And (mintAccessLevelAddressAlias <> FULL_ACCESS) Or _ (mintOperationMode = OP_NEW) And (mintAccessLevelContracts <> FULL_ACCESS) Or _ (mintOperationMode = OP_NEW) And (mintAccessLevelCostCenter <> FULL_ACCESS)Or _ (mintOperationMode = OP_NEW) And (mintAccessLevelCorrespondence <> FULL_ACCESS) Or _ (mintOperationMode = OP_NEW) And (mintAccessLevelAccountProfile <> FULL_ACCESS) Or _ (mintOperationMode = OP_NEW) And (mintAccessLevelInstructions <> FULL_ACCESS) Or _ (mintOperationMode = OP_NEW) And (mintAccessLevelFBOVendors <> FULL_ACCESS) Or _ (mintOperationMode = OP_NEW) And (mintAccessLevelFBOChains <> FULL_ACCESS) Or _ (mintOperationMode = OP_NEW) And (mintAccessLevelCatering <> FULL_ACCESS) Or _ (mintOperationMode = OP_NEW) And (mintAccessLevelGround <> FULL_ACCESS) Or _ (mintOperationMode = OP_NEW) And (mintAccessLevelAccountProfileOSR <> FULL_ACCESS))) Then DisplayNoAccessMessage Exit FunctionEnd If

Interesting loopfor (int i = 0; i < 12; i++) { switch(i) { case 0: list = paymentMethod; logger.debug("Payment method set."); break; case 1: list = productCategory; logger.debug("Product category set."); break; ... // I've omitted similar cases 2 through 10 case 11: list = productWaight; logger.debug("Product weight set."); default: list = new ArrayList(); logger.debug("Unknown value"); break; }}

Setting up a Choice (drop-down list)

annualMileageChoice = new Choice()annualMileageChoice.addItem(CHO_PLEASE_ENTER);annualMileageChoice.addItem("500")annualMileageChoice.addItem("1000")annualMileageChoice.addItem("1500")annualMileageChoice.addItem("2000")annualMileageChoice.addItem("2500")annualMileageChoice.addItem("3000")annualMileageChoice.addItem("3500");…annualMileageChoice.addItem("44000");annualMileageChoice.addItem("44500");annualMileageChoice.addItem("45000");

Case testing

More cases

Random int invoiceId = new Random().nextInt();

hi all,

IntTemp = Int((255 * Rnd()) + 1)

I used above ASP.NET code. Problem is in " Rnd() "Rnd() value is changing everytime.

What is the alternative for Rnd()? OR How will stop Rnd() value changes at everytime?

if ($hour>=7 and $hour<18) { # Assign randomly to Biff or Cliff if (rand() > 0.5) {$Owner->LoadById('70656');} if (rand() < 0.5) {$Owner->LoadById('72586');}}

Polymorphism? What’s that?

if(obj instanceof MMSNode) { MMSNode node = (MMSNode)obj; attribs = node.getAttribs();}else if(obj instanceof MMSPhysComp) { MMSPhysComp physComp = (MMSPhysComp)obj; attribs = physComp.getAttribs();}else if(obj instanceof MMSLogComp) { MMSLogComp logComp = (MMSLogComp)obj; attribs = logComp.getAttribs();}else if (obj instanceof MMSPhysLink) { MMSPhysLink physLink = (MMSPhysLink)obj; attribs = physLink.getAttribs();}

Pop-ups

More pop-ups

Yet more pop-ups

An HTML form

The dialog

FileMatrix

The End