Scroll to see replies
•
Loads the deck information from an XML file using deserialisation
•
Displays the instructions
•
Displays the result based on the user selection.
Public Module Module1
#Region "Private members"
Private m_deckService As DeckService
#End Region
Sub Main()
m_deckService = New DeckService()
displayInstructions()
readInput()
End Sub
#Region "Private methods"
Private Sub readInput()
Dim userInput As String = Console.ReadLine()
parseInput(userInput)
End Sub
Private Sub displayInstructions()
Console.WriteLine("Enter 1 to find the cheapest garden deck")
Console.WriteLine("Enter 2 to display the names of garden decks over a certain length")
Console.WriteLine("Enter 3 to display the names of garden decks over a certain length")
Console.WriteLine("Enter 2 to display the number of garden decks that are available over a certain area")
Console.WriteLine("Enter 4 to quit")
End Sub
Private Sub displayerror()
Console.WriteLine("Invalid input")
End Sub
Private Function inputIsValid(ByVal input As String)
Dim selection As Integer
Return Integer.TryParse(input, selection)
End Function
Private Sub invalidInput()
displayerror()
End Sub
Private Sub parseInput(ByVal userInput As String)
If (inputIsValid(userInput)) Then
Select Case Integer.Parse(userInput)
Case 1
showCheapest()
End Select
Else
invalidInput()
End If
readInput()
End Sub
Private Sub showCheapest()
Dim cheapestDeck = m_deckService.getCheapestDeck()
Console.WriteLine(String.Format("Cheapest deck is:{0}", cheapestDeck.Name))
End Sub
#End Region
End Module
Public Class Deck
Public Property Name As String
Public Property Length As Double
Public Property Width As Double
Public Property Cost As Double
End Class
Public Class DeckService
Private m_Decks As List(Of Deck)
Public Function getCheapestDeck() As Deck
Dim cheapestDeck As Deck = m_Decks.Select( _
Function(x) x).OrderBy(Function(x) x.Cost).First()
Return cheapestDeck
End Function
Public Function getDecksBetween() As List(Of Deck)
End Function
Public Function getDecksAvailableOver(ByVal area As Double) As List(Of Deck)
End Function
''' <summary>
''' Loads the decks from the config
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Private Function loadDecksFromConfig()
Dim conf As New Config()
setDecks(conf.loadDecks())
End Function
Public Function setDecks(ByVal decks As List(Of Deck))
m_Decks = decks
End Function
Public Sub New()
loadDecksFromConfig()
End Sub
End Class
Imports System.Xml.Serialization
Imports DeckAPp
'Handles serialization/deserialisation(reading/writing) of the decks to the config file
Public Class Config
Dim decks As New List(Of Deck)()
Dim x As New XmlSerializer(decks.GetType)
Public Function loadDecks() As List(Of Deck)
'Deserialize text file to a new object.
Dim objStreamReader As New IO.StreamReader("DeckConfig.xml")
decks = x.Deserialize(objStreamReader)
objStreamReader.Close()
Return decks
End Function
Public Sub serializeDecks()
Dim objStreamWriter As New IO.StreamWriter("DeckConfig.xml")
Dim decks = getDecks()
x.Serialize(objStreamWriter, decks)
objStreamWriter.Close()
End Sub
Private Function getDecks() As List(Of Deck)
Dim decks As New List(Of Deck)
decks.Add(New Deck With {
.Name = "TestDeck",
.Cost = 50,
.Length = 50,
.Width = 150
})
decks.Add(New Deck With {
.Name = "TestDeck2",
.Cost = 50,
.Length = 50,
.Width = 150
})
Return decks
End Function
End Class
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfDeck xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Deck>
<Name>Themon</Name>
<Length>3.2</Length>
<Width>2.3</Width>
<Cost>450</Cost>
</Deck>
<Deck>
<Name>Larree</Name>
<Length>3.4</Length>
<Width>3</Width>
<Cost>700</Cost>
</Deck>
<Deck>
<Name>Medree</Name>
<Length>3</Length>
<Width>3</Width>
<Cost>300</Cost>
</Deck>
</ArrayOfDeck>
Public Function getDecksAvailableOver(length as long ) As List(Of Deck)
Dim decksOver As List<Of Deck> =m_Decks.Select( _
Function(x) x.length>length).toList()
Return decksOver
End Function
private sub findDecksOfLength()
dim length as long = readLength()
dim decksOverLength As List<Of Deck>= m_deckService.getDecksAvailableOver(length)
displayDecksOver(decksOverLength)
end sub
private function readLength() as long
Dim userInput As String = Console.ReadLine() //todo input validation
return userInput;
end sub
private function displayDecksOver(decks as List<Of Deck>,length as long)
Console.WriteLine(String.Format("Decks over:{0}", length))
for each deck in decks
Console.WriteLine(decks.Name)
next deck
end function
Select Case Integer.Parse(userInput)
Case 1
showCheapest()
Case 2
findDecksOfLength()
End Select
•
Why are you creating getters and setters when the .NET framework has Properties. It's easier to declare a property with getters and setters.
•
Second of all, why are you using a Select Case statement for one case?
•
Your naming conventions is just.. This is not Java. Capitalize your methods name. Using the camel case naming convention.
•
Also for your string interpolation. Console.WriteLine(String.Format("Cheapest deck is: {0}, cheapestDeck.Name)) Can be shortened down to. Console.WriteLine() has an overload method to do the same thing. Console.WriteLine("Cheapest deck is: {0}, cheapestDeck.Name)
•
You can see that OP is new to programming by the screenshot you posted. So posting something like this is going to be completely over their head. Like your use of Linq queries.
•
Why serialize with XML when using JSON would be a faster and easier alternative. Such resources like JSON.NET provide a class to serialize and deserialize JSON data in a tick.
•
Lastly some of the logic of your code is just ughh and so inconsistent. Like for example, you have you call 1 sub routine which calls another sub routine which calls another sub routine just to display an error message. Why not just write it in the first instance?
•
Why are you creating getters and setters when the .NET framework has Properties. It's easier to declare a property with getters and setters.
•
Second of all, why are you using a Select Case statement for one case?
•
Your naming conventions is just.. This is not Java. Capitalize your methods name. Using the camel case naming convention.
•
Also for your string interpolation. Console.WriteLine(String.Format("Cheapest deck is: {0}, cheapestDeck.Name)) Can be shortened down to. Console.WriteLine() has an overload method to do the same thing. Console.WriteLine("Cheapest deck is: {0}, cheapestDeck.Name)
•
You can see that OP is new to programming by the screenshot you posted. So posting something like this is going to be completely over their head. Like your use of Linq queries.
•
Why serialize with XML when using JSON would be a faster and easier alternative. Such resources like JSON.NET provide a class to serialize and deserialize JSON data in a tick.
•
Lastly some of the logic of your code is just ughh and so inconsistent. Like for example, you have you call 1 sub routine which calls another sub routine which calls another sub routine just to display an error message. Why not just write it in the first instance?
•
XML is easily read and edited using a text editor JSON is not and theirs no real performance benefit to be gained by using JSON in this case.
•
I don't see where I have getters and setters. I have some methods prefixed with get but they perform logic so would not be suitable as properties.
•
The select case statement was intended to be added to by the OP as they implemented the rest of the functionality.
•
Lots of methods doing one thing was intentional - it doesn't call 4 routines to display an error message. Sure get rid of invalidInput that one was redundant but I'd keep the rest.
Last reply 1 month ago
Free website hosting providers with no strings attached?Posted 3 months ago
iDEA silver citizen badge three. Milkshake shack coding problem.Last reply 4 months ago
How do you practice programming java for beginners?Last reply 8 months ago
hows the employment market for tech work in the UK?Last reply 1 month ago
Free website hosting providers with no strings attached?Posted 3 months ago
iDEA silver citizen badge three. Milkshake shack coding problem.Last reply 4 months ago
How do you practice programming java for beginners?Last reply 8 months ago
hows the employment market for tech work in the UK?