Jump to content

And i'm back this time its c# i'll get right into it.

 

i am creating a program to bookings for a squash court for college, it has 2 courts.

 

This is the class i'm using for court 1 and only done Monday so far.

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace BookingSquash{    class court1    {        public class monday : IEquatable<monday>        {            public static List<monday> mon = new List<monday>();            public string time { get; set; }            public string booked { get; set; }            public int client { get; set; }            public override string ToString()            {                return "time: " + time;            }            public override bool Equals(object obj)            {                if (obj == null) return false;                monday objAsPart = obj as monday;                if (objAsPart == null) return false;                else return Equals(objAsPart);            }            public override int GetHashCode()            {                return client;            }            public bool Equals(monday other)            {                if (other == null) return false;                return (this.time.Equals(other.time));            }            // Should also override == and != operators.            public void createLists()            {                //add times to list                mon.Add(new monday() { time = "0900", booked = "no", client = 0 });                mon.Add(new monday() { time = "1000", booked = "no", client = 0 });                mon.Add(new monday() { time = "1100", booked = "no", client = 0 });                mon.Add(new monday() { time = "1200", booked = "no", client = 0 });                mon.Add(new monday() { time = "1300", booked = "no", client = 0 });                mon.Add(new monday() { time = "1400", booked = "no", client = 0 });                mon.Add(new monday() { time = "1500", booked = "no", client = 0 });                mon.Add(new monday() { time = "1600", booked = "no", client = 0 });                mon.Add(new monday() { time = "1700", booked = "no", client = 0 });            }            public void display()            {                foreach (monday value in mon)                {                    if(value.booked=="yes")                    {                        Console.WriteLine(value.time + " is booked by ID : " + value.client);                    }                    else                    {                        Console.WriteLine(value);                    }                                    }            }            /// <summary>            /// adds a new booking to the time slot            /// need to create checking if they have a booking that day            /// and that it is less than weekly limt            /// </summary>            /// holds the time to be booked            /// <param name="t"></param>            /// holds the clients ID            /// <param name="id"></param>            public void addBooking()            {                string t;                int ID=0;                Console.WriteLine("what time");                t = Console.ReadLine();                Console.WriteLine("Enter client ID :");                try                {                    ID = int.Parse(Console.ReadLine());                    //cilent ID for total books                        //clicent to check books that day                    foreach(monday value in mon)                    {                        if (value.time == t & value.booked == "no")                        {                            value.booked = "yes";                            value.client = ID;                        }                        else                        {                            Console.WriteLine("Time already booked by ID:" + value.client);                        }                     }                }                catch                {                    Console.WriteLine("invaild ID");                }                            }                   }//end monday class}}

Right now this is working just as i want allowing the user to pick a  day and time and then ally an ID to it. What i would like help with is saving this list to a file so that i can actually save the data across runs.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
https://linustechtips.com/topic/377919-c-saving-list-to-file/
Share on other sites

Link to post
Share on other sites

And i'm back this time its c# i'll get right into it.

 

i am creating a program to bookings for a squash court for college, it has 2 courts.

 

This is the class i'm using for court 1 and only done Monday so far.

..

Right now this is working just as i want allowing the user to pick a  day and time and then ally an ID to it. What i would like help with is saving this list to a file so that i can actually save the data across runs.

 

cant post the exact code now since im at work but try looking streamwriter or file.writeline like this example :

https://msdn.microsoft.com/en-us/en-en/en-en/library/aa287548(v=vs.71).aspx 

 

google should help you with the rest

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
https://linustechtips.com/topic/377919-c-saving-list-to-file/#findComment-5107303
Share on other sites

Link to post
Share on other sites

Literally copied from this thread:

 

I'd suggest you use LINQ to XML; take a look at System.Xml.Linq Namespace, specifically XDocument and XElement. It's easy, declarative and you can have explicit control over your own serialization.

 

Is this not the same kind of thing?

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
https://linustechtips.com/topic/377919-c-saving-list-to-file/#findComment-5107993
Share on other sites

Link to post
Share on other sites

You should put a bit more thought into the design of your program. Are the two squash courts really different from each other? No, there should be a single Court object that you have two instances of. 

You also shouldn't put your logic for day bookings inside the Court class, that's a logically separate class called Booking. A booking has a date, time and the court that is booked. Are you going to copy and paste that Monday code 14 times, once for every day of the week and then again for court 2? That's a lot of effort and literally fourteen times the code you could write otherwise. You should have a generic Day class that can be any day of the week.

 

What happens when the bookings change to half hour intervals or the courts stay open another hour longer? Are you going to go update all the code 14 times over? Program design is important and this is a bad design.

 

Edit: Other notes

 

Why does your toString just print the time? It would be useful to see the client and the other booking details when it's printed

 

Why are you using a string for booked? A booking in your model has either two states, yes or no. This is better modelled by a boolean that has either true or false.

 

Similarly, your time should be using the DateTime class rather than a string time. Since time is a public field on a booking I could set time to "peter" and everything would break because you weren't expecting it. If you were using DateTime you could only put valid DateTime values into it.

 

The implementation of GetHashCode is awful but it's unlikely you'll be actually using a hashset and proper hashcodes take a bit of learning to understand so this is more forgiveable.

if (value.time == t & value.booked == "no")

The above should be &&. Single & is a bit-wise operator and is not suitable for boolean logic.

 

Your exception handling is non-existent. You should only catch the specific exceptions you can handle, which in the case of your only try-catch block would likely be some sort of integer parse exception. You should also print out the exception details even if only to a debug output and try to provide more information than just "invalid id".

 

You shouldn't separate your variable declaration and initialisation and you should be more aware of the scope of the variables.

 

Variable naming could also do with some work, a single letter variable name is not acceptable nor is "value" inside your loops. It doesn't tell you anything about the variable's purpose or usage.

Link to comment
https://linustechtips.com/topic/377919-c-saving-list-to-file/#findComment-5112328
Share on other sites

Link to post
Share on other sites

You should put a bit more thought into the design of your program. Are the two squash courts really different from each other? No, there should be a single Court object that you have two instances of. 

You also shouldn't put your logic for day bookings inside the Court class, that's a logically separate class called Booking. A booking has a date, time and the court that is booked. Are you going to copy and paste that Monday code 14 times, once for every day of the week and then again for court 2? That's a lot of effort and literally fourteen times the code you could write otherwise. You should have a generic Day class that can be any day of the week.

 

What happens when the bookings change to half hour intervals or the courts stay open another hour longer? Are you going to go update all the code 14 times over? Program design is important and this is a bad design.

 

 

Edit: Other notes

 

Why does your toString just print the time? It would be useful to see the client and the other booking details when it's printed

 

Why are you using a string for booked? A booking in your model has either two states, yes or no. This is better modelled by a boolean that has either true or false.

 

Similarly, your time should be using the DateTime class rather than a string time. Since time is a public field on a booking I could set time to "peter" and everything would break because you weren't expecting it. If you were using DateTime you could only put valid DateTime values into it.

 

The implementation of GetHashCode is awful but it's unlikely you'll be actually using a hashset and proper hashcodes take a bit of learning to understand so this is more forgiveable.

if (value.time == t & value.booked == "no")

The above should be &&. Single & is a bit-wise operator and is not suitable for boolean logic.

 

Your exception handling is non-existent. You should only catch the specific exceptions you can handle, which in the case of your only try-catch block would likely be some sort of integer parse exception. You should also print out the exception details even if only to a debug output and try to provide more information than just "invalid id".

 

You shouldn't separate your variable declaration and initialisation and you should be more aware of the scope of the variables.

 

Variable naming could also do with some work, a single letter variable name is not acceptable nor is "value" inside your loops. It doesn't tell you anything about the variable's purpose or usage.

 

 

Yeah i know my code isn't great, i have 3 weeks to code this whole thing and all the paper work that goes with it. I will look more over this after my college year is up right now i'm just focusing on getting it to work. I just ran with the first thing i came up with.

 

i will go back and look at it. 

 

I'm not sure how i'm gonna link everything together this class was meant to be taught over the year but issues with strikes meant to I got given a 3 months to do the work i was meant to do in 9.

 

 

so create court class, day class and bookings?

 

court > name

day > list of times?

bookings > client, court, day and time?

 

 

I'm struggling to see how i can group these together. 

 

 

 

EDIT::::

 

created booking class

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Squash{    class booking    {        public string client, day;        public int time;     }}

Then created an array 

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Squash{        class Program    {        static void Main(string[] args)        {           booking[] bk = new booking[500];           bk[0].client = "f";        }            }    }

but i'm getting null referance exception every time i try and add to the array.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
https://linustechtips.com/topic/377919-c-saving-list-to-file/#findComment-5135673
Share on other sites

Link to post
Share on other sites

...but i'm getting null referance exception every time i try and add to the array.

I can't give you much because I'm away from home and on a mobile but I can explain your issue there.

You have a class (booking) that's great (by the way capitalise the 'B') and you are creating an array of them. What you are not doing is initializing your instances of Booking in your array. See, by default they will be null - hence when you try to access the property you get that exception.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
https://linustechtips.com/topic/377919-c-saving-list-to-file/#findComment-5137891
Share on other sites

Link to post
Share on other sites

I can't give you much because I'm away from home and on a mobile but I can explain your issue there.

You have a class (booking) that's great (by the way capitalise the 'B') and you are creating an array of them. What you are not doing is initializing your instances of Booking in your array. See, by default they will be null - hence when you try to access the property you get that exception.

 

something like 

 

foreach in array

bk[0] = new booking();

 

 

?

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
https://linustechtips.com/topic/377919-c-saving-list-to-file/#findComment-5138952
Share on other sites

Link to post
Share on other sites

something like 

 

foreach in array

bk[0] = new booking();

 

 

?

 

You're basically there. Ok really quick example then:

    class Program    {        static void Main(string[] args)        {            var arrayOfSomethings = new Something[100];            for (var i = 0; i < arrayOfSomethings.Length; i++)            {                arrayOfSomethings[i] = new Something { SomeProperty = string.Format("Something #{0}", i) };            }            Array.ForEach(arrayOfSomethings, something => Console.WriteLine(something.SomeProperty));            Console.ReadKey();        }    }
    class Something    {        public string SomeProperty { get; set; }    }

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
https://linustechtips.com/topic/377919-c-saving-list-to-file/#findComment-5139678
Share on other sites

Link to post
Share on other sites

 

You're basically there. Ok really quick example then:

    class Program    {        static void Main(string[] args)        {            var arrayOfSomethings = new Something[100];            for (var i = 0; i < arrayOfSomethings.Length; i++)            {                arrayOfSomethings[i] = new Something { SomeProperty = string.Format("Something #{0}", i) };            }            Array.ForEach(arrayOfSomethings, something => Console.WriteLine(something.SomeProperty));            Console.ReadKey();        }    }
    class Something    {        public string SomeProperty { get; set; }    }

Thanks :) will give this a try tomorrow hopefully i can get this up and running quicky next task is to save all the arrays to a file to keep the data. That'll be fun ^_^

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
https://linustechtips.com/topic/377919-c-saving-list-to-file/#findComment-5139821
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×