Scroll to see replies
•
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.
Imports Newtonsoft.Json
Public Class DeckHelper
Private Shared ReadOnly R As New Random
Public Shared Function GetDeckByArea(deckList As List(Of GardenDeck), area As Integer) As IEnumerable(Of GardenDeck)
Return From d In deckList Where d.Area >= area
End Function
Public Shared Function GetDeckByLength(deckList As List(Of GardenDeck), length As Integer) As IEnumerable(Of GardenDeck)
Return From d In deckList Where d.Length >= length
End Function
Public Shared Function GetCheapestDeck(deckList As List(Of GardenDeck)) As GardenDeck
Return deckList.OrderBy(Function(d) d.Price).FirstOrDefault()
End Function
Public Shared Function LoadDeck(location As String) As GardenDeck()
Dim txt As String = IO.File.ReadAllText(location)
Return JsonConvert.DeserializeObject(Of GardenDeck())(txt)
End Function
Public Shared Sub SaveDeck(deckList As List(Of GardenDeck), location As String)
Dim txt As String = JsonConvert.SerializeObject(deckList)
IO.File.WriteAllText(location, txt)
End Sub
Public Shared Function GenerateDeck(amount As Integer) As GardenDeck()
Dim decks(amount) As GardenDeck
For i = 0 To amount
decks(i) = New GardenDeck With {.Name = "Deck: " & i, .Length = R.Next(1, 500), .Width = R.Next(1, 500), .Price = R.Next(1, 500)}
Next
Return decks
End Function
End Class
Public Class GardenDeck
Public Property Name As String
Public Property Length As Integer
Public Property Width As Integer
Public Property Price As Double
Public ReadOnly Property Area As Double
Get
Return Width * Length
End Get
End Property
End Class
Module Module1
Private MyDeckList As New List(Of GardenDeck)
Sub Main()
Start()
End Sub
Private Sub Start()
Dim choice As Integer
Do
Console.Clear()
DisplayMenu()
choice = GetUserChoice()
Select Case choice
Case 1
Dim deck As GardenDeck = DeckHelper.GetCheapestDeck(MyDeckList)
Console.WriteLine("The cheapest desk is {0} which costs ${1}", deck.Name, deck.Price)
Case 2
Dim length As Integer = 50
For Each deck In DeckHelper.GetDeckByLength(MyDeckList, 10)
Console.WriteLine("{0} is greater than {1}cm", deck.Name, length)
Next
Case 3
Dim area As Integer = 27
For Each deck In DeckHelper.GetDeckByArea(MyDeckList, area)
Console.WriteLine("{0} is greater than {1}cm", deck.Name, area)
Next
Case 4
MyDeckList.AddRange(DeckHelper.GenerateDeck(20))
Case 5
DeckHelper.SaveDeck(MyDeckList, "deck.txt")
Case 6
MyDeckList.AddRange(DeckHelper.LoadDeck("deck.txt"))
Case 7
MyDeckList = New List(Of GardenDeck)
Case 8
Environment.Exit(0)
End Select
Console.ReadLine()
Loop Until choice = 8
End Sub
Private Function GetUserChoice() As Integer
Dim choice As Integer
Do
Console.WriteLine()
Console.WriteLine("Enter your choice")
Loop Until Integer.TryParse(Console.ReadLine(), choice)
Return choice
End Function
Private Sub DisplayMenu()
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 garden decks that are available over a certain area")
Console.WriteLine("Enter 4 to generate a deck")
Console.WriteLine("Enter 5 to save the current deck")
Console.WriteLine("Enter 6 to load the current deck")
Console.WriteLine("Enter 7 to wipe current deck")
Console.WriteLine("Enter 8 to quit")
Console.WriteLine()
End Sub
End Module
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
public class DeckStore
{
private readonly string _location;
public DeckStore(string location)
{
if (location == null) throw new ArgumentNullException("location");
_location = location;
}
public ICollection<Deck> Load()
{
...
}
public void Save(ICollection<Deck> decks)
{
...
}
}
public class DeckStore
{
private readonly string _location;
public DeckStore(string location)
{
if (location == null) throw new ArgumentNullException("location");
_location = location;
}
public ICollection<Deck> Load()
{
...
}
public void Save(ICollection<Deck> decks)
{
...
}
}
Imports Newtonsoft.Json
Public Class DeckProvider
Private Shared ReadOnly R As New Random
Private _DeckList As List(Of GardenDeck)
Sub New()
_DeckList = New List(Of GardenDeck)
End Sub
Public ReadOnly Property Count As Integer
Get
Return _DeckList.Count
End Get
End Property
Public Function GetDeckByArea(area As Integer) As IEnumerable(Of GardenDeck)
Return From d In _DeckList Where d.Area >= area
End Function
Public Function GetDeckByLength(length As Integer) As IEnumerable(Of GardenDeck)
Return From d In _DeckList Where d.Length >= length
End Function
Public Function GetCheapestDeck() As GardenDeck
If _DeckList.Count <= 0 Then
Throw New Exception("Deck is empty")
Else
Return _DeckList.OrderBy(Function(d) d.Price).FirstOrDefault()
End If
End Function
Public Sub ImportDeck(location As String)
Dim txt As String = IO.File.ReadAllText(location)
_DeckList.AddRange(JsonConvert.DeserializeObject(Of GardenDeck())(txt))
End Sub
Public Sub SaveDeck(location As String)
Dim txt As String = JsonConvert.SerializeObject(_DeckList)
IO.File.WriteAllText(location, txt)
End Sub
Public Sub GenerateDecks(amount As Integer)
For i = 0 To amount
_DeckList.Add(New GardenDeck With {.Name = "Deck: " & i, .Length = R.Next(1, 50), .Width = R.Next(1, 50), .Price = R.Next(1, 100)})
Next
End Sub
Public Sub Add(deck As GardenDeck)
_DeckList.Add(deck)
End Sub
Public Sub RemoveAt(i As Integer)
_DeckList.RemoveAt(i)
End Sub
Public Sub ClearDeck()
_DeckList = New List(Of GardenDeck)
End Sub
Public Function GetAllDecks() As List(Of GardenDeck)
If _DeckList.Count = 0 Then
Throw New Exception("Deck list is empty")
Else
Return _DeckList
End If
End Function
End Class
Public Class GardenDeck
Public Property Name As String
Public Property Length As Integer
Public Property Width As Integer
Public Property Price As Double
Public ReadOnly Property Area As Double
Get
Return Width * Length
End Get
End Property
End Class
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?