Skip to content

Instantly share code, notes, and snippets.

@galador
Last active August 29, 2015 13:57
Show Gist options
  • Save galador/9818327 to your computer and use it in GitHub Desktop.
Save galador/9818327 to your computer and use it in GitHub Desktop.
' Some modified code for GV
Imports System.Runtime.CompilerServices
Imports System.Reflection
Imports System.Text
Class RegisterItem
Public Property Name As String
Public Property Price As Decimal
Public Property Quantity As Integer
Public ReadOnly Property Total As Decimal
Get
Return Price * Quantity
End Get
End Property
End Class
Public Enum RegisterCommand
AddItem
ClearItem
UndoLastAction
CheckOut
EndProgram
Unknown
End Enum
Public Module Extensions
<Extension()>
Public Function AsCommand(input As Char) As RegisterCommand
Select Case input.ToString.ToUpper
Case "A"
Return RegisterCommand.AddItem
Case "C"
Return RegisterCommand.ClearItem
Case "U"
Return RegisterCommand.UndoLastAction
Case "X"
Return RegisterCommand.CheckOut
Case "Q"
Return RegisterCommand.EndProgram
Case Else
Return RegisterCommand.Unknown
End Select
End Function
<Extension()>
Public Function AsDescription(input As RegisterCommand) As String
Select Case input
Case RegisterCommand.AddItem
Return "Add this item"
Case RegisterCommand.ClearItem
Return "Clear all of this item"
Case RegisterCommand.UndoLastAction
Return "Remove last added set of items"
Case RegisterCommand.CheckOut
Return "Check out customer (finish transaction)"
Case RegisterCommand.EndProgram
Return "End this program"
Case Else
Return "Unknown"
End Select
End Function
<Extension()>
Public Function AsCommandLetter(input As RegisterCommand) As String
Select Case input
Case RegisterCommand.AddItem
Return "A"
Case RegisterCommand.ClearItem
Return "C"
Case RegisterCommand.UndoLastAction
Return "U"
Case RegisterCommand.CheckOut
Return "X"
Case RegisterCommand.EndProgram
Return "Q"
Case Else
Return "Unknown"
End Select
End Function
End Module
Module PosMain
' finance-related constants
'Const SalesTax As Single = 0.06F
'Response constants
Const YesResponse = "Y"c
Const NoResponse = "N"c
' miscellaneous constants
Const OrganizationName = "StarDucks Coffee Shoppe"
Const PadLength As Integer = 30
Private _currentItem As Integer = 1
Private _lastQuantity As Integer = 0
Private ReadOnly RegisterData As Dictionary(Of Integer, RegisterItem) = New Dictionary(Of Integer, RegisterItem)
Public Sub Main()
AddItems()
' set up console session
Console.TreatControlCAsInput = True
Console.Title = OrganizationName
' main program loop
Do
UpdateScreen()
Dim userInput = GetCommand()
If ProcessInput(userInput) Then
'ProcessInput = True means to exit
Return
End If
Loop
End Sub
Private Sub AddItems()
Dim item = New RegisterItem
item.Name = "Small coffee"
item.Price = CDec(4.95)
RegisterData(1) = item
item = New RegisterItem
item.Name = "Medium coffee"
item.Price = CDec(6.87)
RegisterData(2) = item
item = New RegisterItem
item.Name = "Large coffee"
item.Price = CDec(8.52)
RegisterData(3) = item
item = New RegisterItem
item.Name = "Bagel"
item.Price = CDec(2.99)
RegisterData(4) = item
item = New RegisterItem
item.Name = "Cream"
item.Price = CDec(0.71)
RegisterData(5) = item
item = New RegisterItem
item.Name = "Sugar"
item.Price = CDec(0.5)
RegisterData(6) = item
item = New RegisterItem
item.Name = "Cup Lid"
item.Price = CDec(0.99)
RegisterData(7) = item
End Sub
Private Sub UpdateScreen()
' updates what's shown on the screen
' also skips lines between things for readability
' clear the screen
Console.Clear()
Console.Write(OrganizationName)
Console.Write(" POS version ")
Console.Write(Assembly.GetExecutingAssembly().GetName().Version.ToString())
Console.WriteLine()
Console.WriteLine()
For Each i As RegisterCommand In System.Enum.GetValues(GetType(RegisterCommand))
If i = RegisterCommand.Unknown Then
Continue For
End If
' set the text foreground color to (bright) white for showing command and item keys so they stand out
Console.ForegroundColor = ConsoleColor.White
Console.Write(i.AsCommandLetter())
Console.ResetColor()
Console.Write(" = ")
Console.Write(i.AsDescription())
Console.WriteLine()
Next
Console.WriteLine()
For Each item In RegisterData.Values
Console.Write(item.Name.PadRight(PadLength, "."c))
Console.Write(item.Total.ToString("C"))
Console.WriteLine()
Console.WriteLine()
Next
Dim tot As Decimal = (From i As RegisterItem In RegisterData.Values Select i.Total).Sum()
Console.Write("Total".PadRight(PadLength, "."c))
Console.Write(tot.ToString("C"))
Console.WriteLine()
Console.WriteLine()
End Sub
Private Function GetCommand() As RegisterCommand
' gets command input
Console.Write("Please press a letter key indicating what you want to do: ")
Do
' this next line actually gets input (by waiting for a key to be pressed)
Dim firstInput = Console.ReadKey(True).KeyChar
Dim command = firstInput.AsCommand
If command <> RegisterCommand.Unknown Then
Console.Write(firstInput)
Console.WriteLine()
Console.WriteLine()
Return command
End If
Loop
End Function
Private Sub GetItem(ByVal prompt As String)
For Each item In RegisterData
Dim key = item.Key
Dim val = item.Value
Console.ForegroundColor = ConsoleColor.White
Console.Write(key.ToString.PadRight(4))
Console.ResetColor()
Console.Write(val.Name)
Console.WriteLine()
Console.WriteLine()
Next
Dim tempInt = 0
Do
Console.Write(prompt)
Dim tempInput As New StringBuilder
Do
Dim info = Console.ReadKey()
If info.Key = ConsoleKey.Enter Then
Exit Do
End If
tempInput.Append(info.KeyChar)
Loop
If IsNumeric(tempInput.ToString) Then
tempInt = CInt(tempInput.ToString)
End If
Console.WriteLine()
Loop Until tempInt >= 1 AndAlso tempInt <= RegisterData.Count
_currentItem = tempInt
End Sub
Private Function ProcessInput(ByVal command As RegisterCommand) As Boolean
' processes input
' user wants to add an item
If command = RegisterCommand.AddItem Then
GetItem("Please enter the number of the item to add and press Enter: ")
Dim tempInt As Integer
Do
Console.Write("Please enter the amount of the current item to add and press Enter: ")
Dim tempInput as new stringbuilder
Do
Dim info = Console.ReadKey()
If info.Key = ConsoleKey.Enter Then
Exit Do
End If
tempInput.Append(info.KeyChar)
Loop
If IsNumeric(tempInput.ToString) Then
tempInt = CInt(tempInput.ToString)
Exit Do
End If
Console.WriteLine()
Loop
Console.WriteLine()
RegisterData(_currentItem).Quantity += tempInt
End If
' user wants to clear all of an item type
If command = RegisterCommand.ClearItem Then
GetItem("Please enter the number of the item to clear and press Enter: ")
RegisterData(_currentItem).Quantity = 0
End If
' user wants to remove the last item added
If command = RegisterCommand.UndoLastAction Then
Console.Write("Undoing previous item alteration...")
RegisterData(_currentItem).Quantity = _lastQuantity
Console.Write("Press any key to continue")
Console.ReadKey()
Console.WriteLine()
Console.WriteLine()
End If
' user wants to finish with this customer
If command = RegisterCommand.CheckOut Then
Dim checkSure As ConsoleKeyInfo
Console.WriteLine("If you continue all order data will be cleared. This cannot be undone.")
Console.Write("Are you sure you want to clear all items (Y/N)? ")
Do
checkSure = Console.ReadKey(True)
Loop Until UCase(checkSure.KeyChar) = YesResponse Or UCase(checkSure.KeyChar) = NoResponse
Console.Write(checkSure.KeyChar)
Console.WriteLine()
Console.WriteLine()
If UCase(checkSure.KeyChar) = YesResponse Then
Console.Write("Registering transaction..." + ControlChars.NewLine)
RegisterTransaction()
Console.Write("Printing receipt..." + ControlChars.NewLine)
PrintReceipt()
Console.Write("Clearing data..." + ControlChars.NewLine)
RegisterData.Clear()
AddItems()
Console.WriteLine()
_lastQuantity = 0
Console.Write("Please the Enter Key to continue")
Console.ReadLine()
Console.WriteLine()
Console.WriteLine()
End If
End If
' user wants to end the program
If command = RegisterCommand.EndProgram Then
Dim checkSure As ConsoleKeyInfo
Console.Write("Are you sure you want to end the program (Y/N)? ")
Do
checkSure = Console.ReadKey()
Loop Until UCase(checkSure.KeyChar) = YesResponse Or UCase(checkSure.KeyChar) = NoResponse
Console.WriteLine()
Console.WriteLine()
If UCase(checkSure.KeyChar) = YesResponse Then
Console.Write("Disconnecting...")
Console.WriteLine()
Console.WriteLine()
LogOff()
Console.Write("Please press the Enter Key to continue")
Console.ReadLine()
Return True
End If
End If
' this function is done, end it but stay in the main program loop
Return False
End Function
Private Sub RegisterTransaction()
' to do: make this actually register a transaction
Console.WriteLine()
End Sub
Private Sub PrintReceipt()
' to do: make this actually print a receipt
Console.WriteLine()
End Sub
Private Sub LogOff()
' to do: make this actually log off, disconnect, get data structures and files sorted,
' whatever needs to be done before the program ends
Console.WriteLine()
End Sub
End Module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment