The Student Room Group

Higher computing 2014/15 coursework penny's decking

Is there anyone that can help with the code I'm struggling a bit got some of it but not all ? ImageUploadedByStudent Room1417689712.686018.jpg


Posted from TSR Mobile

Scroll to see replies

Original post by laurennnm
...


Hi Laurennnm,

If you've already got some code, can you show us so we can take a look and make some suggestions? We don't even know what language you're meant to be coding in!

Granted, I'm of no use to you (I'm a DB guy, not a programmer), but people round here are pretty good! :smile:
Reply 2
I'll take a picture tomorrow in school and get back to you xx


Posted from TSR Mobile
Reply 3
ImageUploadedByStudent Room1417781412.701636.jpgImageUploadedByStudent Room1417781433.918030.jpgImageUploadedByStudent Room1417781449.341913.jpgImageUploadedByStudent Room1417781475.068611.jpgImageUploadedByStudent Room1417781491.254516.jpg

There's what I've got its visual basics


Posted from TSR Mobile
Reply 4
Heres some code for you. I was going to do the entire thing but I've had enough :P Anyway it should be good guidance. It's been coded as a vb.net console app.

Loads the deck information from an XML file using deserialisation

Displays the instructions

Displays the result based on the user selection.



Only the "show cheapest deck" option works at the moment but the structure is there for you to fill in the rest.

This module is the application entry point:
It will display the instructions, accept the user input then based on their selection call the appropiate method in the DeckService object to fetch and display the result



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


The Deck class stores information on a single Deck

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

The DeckService class contains the business logic.
I've done getCheapestDeck() for you using LINQ to find the cheapest deck.
You will need to code getDecksBetween, getDecksAvailableOver and the rest.


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


The config class loads the Decks in from the XML file.

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


Here is the XML file containing the deck information you can easily edit and insert new decks using a text editor. Make sure it is named DeckConfig.xml and stored in the same folder as the .exe.

<?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>



Happy to answer any questions, in this thread.
(edited 9 years ago)
Reply 5
I have never understood why people post screenshots of code (let alone photographs).
Reply 6
What programming language is it ?


Posted from TSR Mobile
Reply 7
laurennnm, if you can show the design/user interface i can create the code required
Reply 8
It was in vb.net if you needed vb6 I can't help you there I'm afraid that language is before my time :wink:
(edited 9 years ago)
Reply 9
Can anyone show me this in Live Code? im also struggling :/
Reply 10
Original post by Pwegoh
Can anyone show me this in Live Code? im also struggling :/

Ok lets extend the code to complete option two "Get the names of decks over a certain length"

Lets create the getDecksAvailableOver function in the DeckService class. I'll use LINQ again if you don't know what it is google it as its awesome.

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


Ok so that will give us the decks over a certain length. But we still need to request the length from the user when they select option 2 and then display the decks.
So lets add the findDecksOfLength method to Module1 this method will read the input in and display the result.
It will get the user input by calling readLength() and display the decks over the desired length by calling displayDecksOver()


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


Finally we need to add a new case to the select statement so that when they select option 2 the new code gets called


Select Case Integer.Parse(userInput)
Case 1
showCheapest()
Case 2
findDecksOfLength()
End Select

Be warned the above is from the top of my head into the reply box so don't be surprised if there's typos.
(edited 9 years ago)
Reply 11
my teacher is making us do part 2 first so having a look at coding task is good. im doing it in python as i dont it for national 5 and it will be alot easier
Original post by laurennnm
Is there anyone that can help with the code I'm struggling a bit got some of it but not all ? ImageUploadedByStudent Room1417689712.686018.jpg


Posted from TSR Mobile


Hi, I am doing the exact same project and coding it in Visual Basic. Please Please Please I am really stuck. Is it okay if you can send me the finished code. I will change everything in it and make it look different. Please I need your help badly PLEASE!!!
Reply 13
Original post by AmyCampbell1997
Hi, I am doing the exact same project and coding it in Visual Basic. Please Please Please I am really stuck. Is it okay if you can send me the finished code. I will change everything in it and make it look different. Please I need your help badly PLEASE!!!


Heh I posted 90% of the answer above but seems people don't understand it enough to run it. Not even bothered to ask any questions they just want to print out the answer.

You wont get anywhere if you don't even try to understand the subject your being taught. I wouldn't suggest blindly printing out the code either because they will question it and you won't be able to answer.
(edited 9 years ago)
Reply 14
Original post by AmyCampbell1997
Is it okay if you can send me the finished code. I will change everything in it and make it look different.


Wow.
Original post by planto
wow.


please like i really need it, friday is the dead line and i havent even done any coding!
Please guys help. If not at all then atleast teach me to do a menu where the user gets to choose the options by numbers.
Reply 17
Original post by INTit
SNIP* Happy to answer any questions, in this thread.


Nice contribution, but I have to disagree with many things you have done. I have some few constructive criticisms for you.


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?

(edited 9 years ago)
Reply 18
Original post by AmyCampbell1997
Please guys help. If not at all then atleast teach me to do a menu where the user gets to choose the options by numbers.


Have you googled this before asking for help?
Reply 19
Original post by Async
Nice contribution, but I have to disagree with many things you have done. I have some few constructive criticisms for you.


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?



Thanks for the input some of it I disagree with though.

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.

(edited 9 years ago)

Quick Reply

Latest