Unique ways to accomplish a task. (Task 2)

99 Bottles Of Beer

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 18777 Views - Last Post: 13 April 2012 - 11:26 AM

#16 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4431
  • View blog
  • Posts: 7,702
  • Joined: 08-June 10

Re: Unique ways to accomplish a task. (Task 2)

Posted 22 July 2010 - 02:12 PM

F#, anyone? I realize it's the VB.NET area, but i thought it would be fun to try it this way.

let bob num = if num = 1 then "bottle of beer" else "bottles of beer"
let otw = "on the wall"
let tp = "Take one down pass it around"
let sNum num = if num = 0 then "No more" else string num
let makeLine2 num = 
    if num <> 0 then
        sprintf "%s, %s %s %s." tp (sNum(num-1)) (bob (num-1)) otw
    else
        "Go to the store buy some more, 99 bottles of beer on the wall."
let makeLine num = 
    sprintf "%s %s %s, %s %s.\n%s" (sNum num) (bob num) otw (sNum num) (bob num) (makeLine2 num)

let rec makeSong num  = 
  match num with
  | 0 -> sprintf "%s\n" (makeLine 0)
  | _ -> sprintf "%s\n\n%s" (makeLine num) (makeSong (num-1))

printf "%s" (makeSong 99)



Edit, come to think...that would stack overflow if provided a negative number. Maybe it should be changed to an if then else instead of a match...

This post has been edited by insertAlias: 22 July 2010 - 02:18 PM

Was This Post Helpful? 0
  • +
  • -

#17 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2240
  • View blog
  • Posts: 9,411
  • Joined: 29-May 08

Re: Unique ways to accomplish a task. (Task 2)

Posted 21 November 2010 - 06:31 PM

Be a while since anyone has add to this.
vb2010 version.
Module Module1
  Sub Main()
    Enumerable.Range(0, 99).Reverse.ToList.ForEach(Sub(B)/> Console.WriteLine("{0}{1}{2}{3}{4}{5}{0}{1}{2}{3}{5}{6}{5}{7}{1}{2}{3}{4}{5}", b, " bottle", If(b <> 1, "s", ""), " of beer", " on the wall", vbNewLine, If(b >= 1, "Take one down, Pass it around", "Go to the store and buy some more"), If(b > 0, b - 1, 99)))
  End Sub
End Module


Can it be shorter still?
Spoiler

Was This Post Helpful? 0
  • +
  • -

#18 thava  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,606
  • Joined: 17-April 07

Re: Unique ways to accomplish a task. (Task 2)

Posted 06 December 2010 - 04:12 AM

really a short one but may be you miss this

View PostAdamSpeight2008, on 15 June 2009 - 12:27 PM, said:

The last verse is.

Quote

No more bottles of beer on the wall, No more bottles of beer.
Go to the store buy some more, 99 bottles of beer on the wall.

Was This Post Helpful? 0
  • +
  • -

#19 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2240
  • View blog
  • Posts: 9,411
  • Joined: 29-May 08

Re: Unique ways to accomplish a task. (Task 2)

Posted 07 December 2010 - 05:48 PM

View Postthava, on 06 December 2010 - 11:12 AM, said:

really a short one but may be you miss this

Damn hoisted by my own petard.
   Enumerable.Range(0, 99).Reverse.ToList.ForEach( 
      Sub(B)/> Console.WriteLine("{0}{1}{2}{3}{4}{5}{0}{1}{2}{3}{5}{6}{5}{7}{1}{2}{3}{4}{5}", 
        If(b > 0, b.ToString, "No more"), " bottle", 
        If(b <> 1, "s", ""), " of beer", " on the wall", vbCrLf, 
        If(b >= 1, "Take one down, Pass it around", "Go to the store and buy some more"), 
        If(b > 0, b - 1, 99) 
        ) 
    ) 



This post has been edited by AdamSpeight2008: 07 December 2010 - 05:50 PM

Was This Post Helpful? 1
  • +
  • -

#20 thava  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,606
  • Joined: 17-April 07

Re: Unique ways to accomplish a task. (Task 2)

Posted 09 December 2010 - 06:30 AM

now this is more readable than the previous one
Was This Post Helpful? 0
  • +
  • -

#21 RetardedGenius  Icon User is offline

  • >>──(Knee)──►
  • member icon

Reputation: 126
  • View blog
  • Posts: 555
  • Joined: 30-October 10

Re: Unique ways to accomplish a task. (Task 2)

Posted 15 April 2011 - 07:01 AM

View Postnmgod, on 21 July 2010 - 02:09 PM, said:

Nothing like completely destroying the reason for having a programming language. ^^

Just looking at this makes me laugh :clap:


        Console.WriteLine("99 bottles of beer on the wall, 99 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 98 bottle of beer on the wall.")
        Console.WriteLine("98 bottles of beer on the wall, 98 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 97 bottle of beer on the wall.")
        Console.WriteLine("97 bottles of beer on the wall, 97 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 96 bottle of beer on the wall.")
        Console.WriteLine("96 bottles of beer on the wall, 96 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 95 bottle of beer on the wall.")
        Console.WriteLine("95 bottles of beer on the wall, 95 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 94 bottle of beer on the wall.")
        Console.WriteLine("94 bottles of beer on the wall, 94 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 93 bottle of beer on the wall.")
        Console.WriteLine("93 bottles of beer on the wall, 93 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 92 bottle of beer on the wall.")
        Console.WriteLine("92 bottles of beer on the wall, 92 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 91 bottle of beer on the wall.")
        Console.WriteLine("91 bottles of beer on the wall, 91 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 90 bottle of beer on the wall.")
        Console.WriteLine("90 bottles of beer on the wall, 90 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 89 bottle of beer on the wall.")
        Console.WriteLine("89 bottles of beer on the wall, 89 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 88 bottle of beer on the wall.")
        Console.WriteLine("88 bottles of beer on the wall, 88 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 87 bottle of beer on the wall.")
        Console.WriteLine("87 bottles of beer on the wall, 87 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 86 bottle of beer on the wall.")
        Console.WriteLine("86 bottles of beer on the wall, 86 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 85 bottle of beer on the wall.")
        Console.WriteLine("85 bottles of beer on the wall, 85 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 84 bottle of beer on the wall.")
        Console.WriteLine("84 bottles of beer on the wall, 84 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 83 bottle of beer on the wall.")
        Console.WriteLine("83 bottles of beer on the wall, 83 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 82 bottle of beer on the wall.")
        Console.WriteLine("82 bottles of beer on the wall, 82 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 81 bottle of beer on the wall.")
        Console.WriteLine("81 bottles of beer on the wall, 81 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 80 bottle of beer on the wall.")
        Console.WriteLine("80 bottles of beer on the wall, 80 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 79 bottle of beer on the wall.")
        Console.WriteLine("79 bottles of beer on the wall, 79 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 78 bottle of beer on the wall.")
        Console.WriteLine("78 bottles of beer on the wall, 78 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 77 bottle of beer on the wall.")
        Console.WriteLine("77 bottles of beer on the wall, 77 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 76 bottle of beer on the wall.")
        Console.WriteLine("76 bottles of beer on the wall, 76 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 75 bottle of beer on the wall.")
        Console.WriteLine("75 bottles of beer on the wall, 75 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 74 bottle of beer on the wall.")
        Console.WriteLine("74 bottles of beer on the wall, 74 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 73 bottle of beer on the wall.")
        Console.WriteLine("73 bottles of beer on the wall, 73 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 72 bottle of beer on the wall.")
        Console.WriteLine("72 bottles of beer on the wall, 72 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 71 bottle of beer on the wall.")
        Console.WriteLine("71 bottles of beer on the wall, 71 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 70 bottle of beer on the wall.")
        Console.WriteLine("70 bottles of beer on the wall, 70 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 69 bottle of beer on the wall.")
        Console.WriteLine("69 bottles of beer on the wall, 69 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 68 bottle of beer on the wall.")
        Console.WriteLine("68 bottles of beer on the wall, 68 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 67 bottle of beer on the wall.")
        Console.WriteLine("67 bottles of beer on the wall, 67 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 66 bottle of beer on the wall.")
        Console.WriteLine("66 bottles of beer on the wall, 66 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 65 bottle of beer on the wall.")
        Console.WriteLine("65 bottles of beer on the wall, 65 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 64 bottle of beer on the wall.")
        Console.WriteLine("64 bottles of beer on the wall, 64 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 63 bottle of beer on the wall.")
        Console.WriteLine("63 bottles of beer on the wall, 63 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 62 bottle of beer on the wall.")
        Console.WriteLine("62 bottles of beer on the wall, 62 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 61 bottle of beer on the wall.")
        Console.WriteLine("61 bottles of beer on the wall, 61 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 60 bottle of beer on the wall.")
        Console.WriteLine("60 bottles of beer on the wall, 60 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 59 bottle of beer on the wall.")
        Console.WriteLine("59 bottles of beer on the wall, 59 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 58 bottle of beer on the wall.")
        Console.WriteLine("58 bottles of beer on the wall, 58 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 57 bottle of beer on the wall.")
        Console.WriteLine("57 bottles of beer on the wall, 57 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 56 bottle of beer on the wall.")
        Console.WriteLine("56 bottles of beer on the wall, 56 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 55 bottle of beer on the wall.")
        Console.WriteLine("55 bottles of beer on the wall, 55 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 54 bottle of beer on the wall.")
        Console.WriteLine("54 bottles of beer on the wall, 54 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 53 bottle of beer on the wall.")
        Console.WriteLine("53 bottles of beer on the wall, 53 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 52 bottle of beer on the wall.")
        Console.WriteLine("52 bottles of beer on the wall, 52 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 51 bottle of beer on the wall.")
        Console.WriteLine("51 bottles of beer on the wall, 51 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 50 bottle of beer on the wall.")
        Console.WriteLine("50 bottles of beer on the wall, 50 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 49 bottle of beer on the wall.")
        Console.WriteLine("49 bottles of beer on the wall, 49 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 48 bottle of beer on the wall.")
        Console.WriteLine("48 bottles of beer on the wall, 48 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 47 bottle of beer on the wall.")
        Console.WriteLine("47 bottles of beer on the wall, 47 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 46 bottle of beer on the wall.")
        Console.WriteLine("46 bottles of beer on the wall, 46 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 45 bottle of beer on the wall.")
        Console.WriteLine("45 bottles of beer on the wall, 45 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 44 bottle of beer on the wall.")
        Console.WriteLine("44 bottles of beer on the wall, 44 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 43 bottle of beer on the wall.")
        Console.WriteLine("43 bottles of beer on the wall, 43 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 42 bottle of beer on the wall.")
        Console.WriteLine("42 bottles of beer on the wall, 42 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 41 bottle of beer on the wall.")
        Console.WriteLine("41 bottles of beer on the wall, 41 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 40 bottle of beer on the wall.")
        Console.WriteLine("40 bottles of beer on the wall, 40 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 39 bottle of beer on the wall.")
        Console.WriteLine("39 bottles of beer on the wall, 39 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 38 bottle of beer on the wall.")
        Console.WriteLine("38 bottles of beer on the wall, 38 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 37 bottle of beer on the wall.")
        Console.WriteLine("37 bottles of beer on the wall, 37 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 36 bottle of beer on the wall.")
        Console.WriteLine("36 bottles of beer on the wall, 36 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 35 bottle of beer on the wall.")
        Console.WriteLine("35 bottles of beer on the wall, 35 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 34 bottle of beer on the wall.")
        Console.WriteLine("34 bottles of beer on the wall, 34 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 33 bottle of beer on the wall.")
        Console.WriteLine("33 bottles of beer on the wall, 33 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 32 bottle of beer on the wall.")
        Console.WriteLine("32 bottles of beer on the wall, 32 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 31 bottle of beer on the wall.")
        Console.WriteLine("31 bottles of beer on the wall, 31 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 30 bottle of beer on the wall.")
        Console.WriteLine("30 bottles of beer on the wall, 30 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 29 bottle of beer on the wall.")
        Console.WriteLine("29 bottles of beer on the wall, 29 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 28 bottle of beer on the wall.")
        Console.WriteLine("28 bottles of beer on the wall, 28 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 27 bottle of beer on the wall.")
        Console.WriteLine("27 bottles of beer on the wall, 27 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 26 bottle of beer on the wall.")
        Console.WriteLine("26 bottles of beer on the wall, 26 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 25 bottle of beer on the wall.")
        Console.WriteLine("25 bottles of beer on the wall, 25 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 24 bottle of beer on the wall.")
        Console.WriteLine("24 bottles of beer on the wall, 24 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 23 bottle of beer on the wall.")
        Console.WriteLine("23 bottles of beer on the wall, 23 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 22 bottle of beer on the wall.")
        Console.WriteLine("22 bottles of beer on the wall, 22 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 21 bottle of beer on the wall.")
        Console.WriteLine("21 bottles of beer on the wall, 21 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 20 bottle of beer on the wall.")
        Console.WriteLine("20 bottles of beer on the wall, 20 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 19 bottle of beer on the wall.")
        Console.WriteLine("19 bottles of beer on the wall, 19 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 18 bottle of beer on the wall.")
        Console.WriteLine("18 bottles of beer on the wall, 18 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 17 bottle of beer on the wall.")
        Console.WriteLine("17 bottles of beer on the wall, 17 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 16 bottle of beer on the wall.")
        Console.WriteLine("16 bottles of beer on the wall, 16 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 15 bottle of beer on the wall.")
        Console.WriteLine("15 bottles of beer on the wall, 15 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 14 bottle of beer on the wall.")
        Console.WriteLine("14 bottles of beer on the wall, 14 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 13 bottle of beer on the wall.")
        Console.WriteLine("13 bottles of beer on the wall, 13 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 12 bottle of beer on the wall.")
        Console.WriteLine("12 bottles of beer on the wall, 12 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 11 bottle of beer on the wall.")
        Console.WriteLine("11 bottles of beer on the wall, 11 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 10 bottle of beer on the wall.")
        Console.WriteLine("10 bottles of beer on the wall, 10 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 9 bottle of beer on the wall.")
        Console.WriteLine("9 bottles of beer on the wall, 9 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 8 bottle of beer on the wall.")
        Console.WriteLine("8 bottles of beer on the wall, 8 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 7 bottle of beer on the wall.")
        Console.WriteLine("7 bottles of beer on the wall, 7 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 6 bottle of beer on the wall.")
        Console.WriteLine("6 bottles of beer on the wall, 6 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 5 bottle of beer on the wall.")
        Console.WriteLine("5 bottles of beer on the wall, 5 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 4 bottle of beer on the wall.")
        Console.WriteLine("4 bottles of beer on the wall, 4 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 3 bottle of beer on the wall.")
        Console.WriteLine("3 bottles of beer on the wall, 3 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 2 bottle of beer on the wall.")
        Console.WriteLine("2 bottles of beer on the wall, 2 bottles of beer.")
        Console.WriteLine("Take one down pass it around, 1 bottle of beer on the wall.")
        Console.WriteLine("1 bottle of beer on the wall, 1 bottle of beer.")
        Console.WriteLine("Take one down pass it around, No more bottles of beer on the wall.")
        Console.WriteLine("Go to the store buy some more, 99 bottles of beer on the wall.")


Please tell me you didn't type that all out by hand! :death:
Was This Post Helpful? -1
  • +
  • -

#22 Ice.Harvester  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 16-June 11

Re: Unique ways to accomplish a task. (Task 2)

Posted 18 June 2011 - 01:14 AM

a) I'm at work and can't access .NET to write it properly
B) Don't usually use console
c) Meh, it's just for a laugh right?

a(0) = " bottles of beer on the wall," : b(0) = " bottles of beer." :c(0) = "Take one down pass it around,"
a(1) = " bottle of beer on the wall," : b(1) = " bottle of beer." :c(1) = "No more bottles of beer on the wall."
d = "Go to the store and buy some more"

For I = 99 to 3 Step -1 
Console.WriteLine(str(I) + a(0) + str(I) + b(0)) : Console.WriteLine(c(0) + str(I-1) + a(0))
Console.WriteLine()
Next I

Console.WriteLine("2" + a(0) + "2" + b(0)) : Console.WriteLine(c(0) + "1" + a(1))
Console.WriteLine()

Console.WriteLine("1" + a(1) + "1" + b(1)) : Console.WriteLine(c(0) + c(1))
Console.WriteLine()

Console.WriteLine(d + "99" + a(0))
Console.WriteLine()


Was This Post Helpful? 1
  • +
  • -

#23 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 69
  • View blog
  • Posts: 314
  • Joined: 12-January 11

Re: Unique ways to accomplish a task. (Task 2)

Posted 21 June 2011 - 08:52 AM

I haven't seen anyone use a stringbuilder yet, so here you go!

Imports System.Text

Module beerMod

    Sub Main()

        Dim numberOfBeers As Integer
        buyBeer(numberOfBeers)
        haveAGoodTime(numberOfBeers)

    End Sub

    Private Sub haveAGoodTime(ByVal beer As Integer)

        Dim song As New List(Of StringBuilder)

        While beer >= 0
            Dim verse As New StringBuilder
            With verse
                If beer > 1 Then
                    .Append(beer).Append(" bottles of beer on the wall, ").Append(beer).AppendLine(" bottles of beer.")
                    If beer = 2 Then
                        .Append("Take one down pass it around, ").Append(beer - 1).AppendLine(" bottle of beer on the wall.")
                    Else
                        .Append("Take one down pass it around, ").Append(beer - 1).AppendLine(" bottles of beer on the wall.")
                    End If
                ElseIf beer = 1 Then
                    .Append(beer).Append(" bottle of beer on the wall, ").Append(beer).AppendLine(" bottle of beer.")
                    .Append("Take one down pass it around, ").AppendLine("no more bottles of beer on the wall.")
                Else
                    .AppendLine("No more bottles of beer on the wall, no moe bottles of beer.")
                    .AppendLine("Go to the store buy some more, 99 bottles of beer on the wall.")
                End If
                .AppendLine()
            End With
            song.Add(verse)
            beer -= 1
        End While
        writeSong(song)
    End Sub

    Private Sub buyBeer(ByRef b As Integer)
        b += 99
    End Sub

    Private Sub writeSong(ByVal s As List(Of StringBuilder))
        For Each v In s
            Console.Write(v)
        Next
    End Sub
End Module


Was This Post Helpful? 0
  • +
  • -

#24 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 69
  • View blog
  • Posts: 314
  • Joined: 12-January 11

Re: Unique ways to accomplish a task. (Task 2)

Posted 13 April 2012 - 11:26 AM

Hmm...how about this way!!!
Module BottlesOfBeerSong
    Sub Main()
        Dim myBeer As New Bottles(99, liquid.beer)

        'While True
            Do Until myBeer.Empty = True
                GetCount(myBeer)
                ConsumeBeer(myBeer)
            Loop
            
            GetCount(myBeer)
            GetMoreBeer(myBeer)
        'End While
    End Sub

    Private Sub GetCount(ByRef b As Bottles)
        Console.WriteLine(b.Count & Space(1) & b.GetType.Namespace & " of " & [Enum].GetName(GetType(liquid), liquid.beer) & " on the " & [Enum].GetName(GetType(locations), locations.wall))
        Console.WriteLine(b.Count & Space(1) & b.GetType.Namespace & " of " & [Enum].GetName(GetType(liquid), liquid.beer) & ".")
    End Sub

    Private Sub ConsumeBeer(ByRef b As Bottles)
        Console.WriteLine(b(b.Count - 1).Consume())
        b.TossEmpties()
        Console.WriteLine(b.Count & Space(1) & b.GetType.Namespace & " of " & [Enum].GetName(GetType(liquid), liquid.beer) & " on the " & [Enum].GetName(GetType(locations), locations.wall))
    End Sub

    Private Sub GetMoreBeer(ByRef b As Bottles)
        Console.WriteLine("Go to the store and get some more.")
        b = New Bottles(99, liquid.beer)
        Console.WriteLine(b.Count & Space(1) & b.GetType.Namespace & " of " & [Enum].GetName(GetType(liquid), liquid.beer) & " on the " & [Enum].GetName(GetType(locations), locations.wall))
    End Sub
End Module

Public Enum liquid
    beer
End Enum

Public Enum locations
    wall
    floor
End Enum

Public Class Bottles
    Inherits List(Of Bottle)
    Private _empty As Boolean
    Public ReadOnly Property Empty As Boolean
        Get
            Return _empty
        End Get
    End Property
    Public Sub New(ByVal num As Integer, ByVal liq As liquid)
        For i As Integer = 0 to num-1
            Add(New Bottle(liq, locations.wall))
        Next
    End Sub

    Public Sub Consume(ByRef index As Integer, ByVal loc As locations)
        If Not Empty then Me(index).Consume
    End Sub

    Public Sub TossEmpties()
        Dim indices As New List(Of bottle)

        For Each b In Me
            If b.Empty = True then indices.Add(B)/>
        Next

        For Each i In indices
            Me.Remove(i)
        Next

        If Me.Count = 0 then _empty = True
    End Sub
End Class

Public Class Bottle
    Public Property Contents As liquid
    Public Readonly Property Location As locations
        Get
            Return _Loc
        End Get
    End Property
    Private _loc As locations
    Public ReadOnly Property Empty As Boolean
        Get
            Return _empty
        End Get
    End Property
    Private _empty As Boolean = False

    Public Sub New(ByVal l As liquid, ByVal loc As locations)
        Contents = l
        _loc = loc
    End Sub

    Public Function Consume() As String
        _empty = True
        _loc = locations.floor
        Consume = ("Take one down and pass it around.")
    End Function
End Class


Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2