using System;
using System.Text.RegularExpressions;
namespace calendar
{
class Program
{
static void Main()
{
int year;
int day;
string[] month = new string[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
int[] days = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
Console.Write("Enter the year for which you wish to generate the calendar >> ");
int.TryParse(Console.ReadLine(), out year); // Validate //
Console.Write("Enter the day of the week that January first is on >> ");
int.TryParse(Console.ReadLine(), out day); // Validate //
while (day > 31 || day < 1) // Reprompt for value if date is out of range //
{
Console.WriteLine("Enter a valid date >> ");
Console.Write("Enter the day of the week that January first is on >> ");
int.TryParse(Console.ReadLine(), out day); // Validate //
}
switch (LeapYear(year)) // Switch statement checks if Leap Year is true //
{
case true:
days[1] += 1;
Console.WriteLine("Calendar for year - {0}", year);
for (int i = 0; i < month.Length; i++)
{
Console.WriteLine("\n" + month[i]);
day = DisplayCalender(days[i], day);
Console.Write("\n");
}
break;
}
}
public static int DisplayCalender(int days, int start) //Display Function//
{
int startDay = start;
Console.WriteLine("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
for (int i = 0; i < start; i++)
Console.Write("\t");
for (int i = 1; i <= days; i++)
{
if (startDay > 6)
{
startDay = 0;
Console.WriteLine();
}
Console.Write(i + "\t");
startDay++;
}
return startDay;
}
public static Boolean LeapYear(int year)
{
if ((year % 400 == 0) || ((year % 4 == 0) && !(year % 100 == 0))) // Checks each OR AND statements and return true or false //
{
return true;
}
else
return false;
}
}
}
I'm imagining the problem you're describing is having a month that starts on Sunday is making the calendar skip an entire line. Like the image:
That is happening because your method public static int DisplayCalender(int days, int start) is receiving the parameter int start with a value of 7.
That makes write tabs on the whole week on your first for and then skip the line on the second for.
To solve the issue you can simply reassign startDay with zero when it is 7 and check on you tab loop for startDay instead of start:
public static int DisplayCalender(int days, int start) //Display Function//
{
int startDay = start == 7 ? 0 : start;
Console.WriteLine("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
for (int i = 0; i < startDay; i++)
Console.Write("\t");
for (int i = 1; i <= days; i++)
{
if (startDay > 6)
{
startDay = 0;
Console.WriteLine();
}
Console.Write(i + "\t");
startDay++;
}
return startDay;
}
This will give you the expected result:
What can you improve from here?
Making all that from scratch probably made you learn a lot about loop and flow. If you have some time check on DateTime.
There you have methods to find leap years, day of the week, and Month. That would help you simplify your code a lot.
Welcome to StackOverflow!
First I must say you are overcomplicating this very much, there is a perfect date library out of the box that you could have used.
For example:
DateTime now = DateTime.Now;
bool isLeapYear = DateTime.IsLeapYear(now.Year);
int daysInCurrentMonth = DateTime.DaysInMonth(now.Year, now.Month);
Instead of this:
Console.WriteLine("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
for (int i = 0; i < start; i++)
Console.Write("\t");
Do this:
Console.WriteLine("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
if (start < 7)
{
for (int i = 0; i < start; i++)
{
Console.Write("\t");
}
}
And instead of this:
if (startDay > 6)
{
startDay = 0;
Console.WriteLine();
}
do this:
if (startDay > 6)
{
startDay = 0;
if (i!=1)
{
Console.WriteLine();
}
}
So DisplayCalender method should look like this:
public static int DisplayCalender(int days, int start) //Display Function//
{
int startDay = start;
Console.WriteLine("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
if (start < 7)
{
for (int i = 0; i < start; i++)
{
Console.Write("\t");
}
}
for (int i = 1; i <= days; i++)
{
if (startDay > 6)
{
startDay = 0;
if (i != 1 )
{
Console.WriteLine();
}
}
Console.Write(i + "\t");
startDay++;
}
return startDay;
}
Related
I created a program to perform bubble sort on random numbers and I am trying to include the time it takes to go through each sorting operations in milliseconds. I tried a few ways but my program does not even acknowledge the code for it. Does anybody see what I am doing wrong?
class BubbleSort
{
static void Main(string[] args)
{
int[] a = { 50000, 250000, 25000, 750000, 500000, 100, 100000,
1000000, 1000 };
int t;
Console.WriteLine("Array is: ");
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i]);
}
DateTime start = DateTime.Now;
DateTime end;
for (int j = 0; j <= a.Length - 2; j++)
{
for (int i = 0; i <= a.Length - 2; i++)
{
if (a[i] > a[i + 1])
{
t = a[i + 1];
a[i + 1] = a[i];
a[i]=t;
}
}
}
end = DateTime.Now;
TimeSpan ts = end.Subtract(start);
Console.WriteLine("The Sorted Array: ");
foreach (int Array in a)
Console.Write(Array + " ");
Console.Read();
Console.WriteLine("Duration = {0} ms", ts.TotalMilliseconds);
}
Here's the prompt: Write a program
that prints a calendar for one month. Input
consists of an integer specifying the first
day of the month (1 = Sunday) and an
integer specifying how many days are
in a month.
Here is a sample output of what it should look like:
First day of the month 3
Number of days in the month 31
Sunday Monday Tuesday Wednesday Thursday Friday Saturday
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
I've been working on it and here is what I have so far.
namespace Program_202t
{
class Program
{
static void Main(string[] args)
{
Console.Title = "Program 202t";
Console.Write("Enter the first day of the month: ");
int year = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the number of days in a month: ");
int month = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\nSunday Monday Tuesday Wedsnesday Thursday Friday Saturday");
if (year == 1)
{
Console.SetCursorPosition(0, 4);
for (int i = 1; i <= 10; i++)
{
Console.Write(i + " ");
if (((i) % 7) > 0)
{
}
else
{
Console.Write("\n");
}
}
Console.Write("\b");
for (int b = 11; b <= month; b++)
{
if (((b) % 7) > 0)
{
Console.Write(b + " ");
}
else
{
Console.Write(b + "\n");
}
}
}
if (year == 2)
{
//Console.SetCursorPosition(10, 4);
for (int i = 1; i <= month; i++)
{
if (i == 1)
{
Console.Write(" " + i + " ");
}
else if (i > 1 && i < 11 && i != 6)
{
Console.Write(i + " ");
}
Console.Write("\b");
if (i == 6)
{
Console.Write(i + "\n");
}
if (i > 11 && i != 14)
{
Console.Write(i + " ");
}
if (i == 14)
{
Console.Write(i + "\n");
}
/*if (((i) % 6) > 0)
{
}
else
{
Console.Write("\n");
}
}
Console.Write("\b");
//FIX!!!
for (int b = 11; b <= month; b++)
{
if (((b) % 13) > 0 && b >13)
{
Console.Write(b + " ");
}
if (((b) % 20) > 0 && b <= 13)
{
Console.Write(b + " ");
}
else
{
Console.Write("\n");
}
}*/
}
if (year == 3)
{
Console.SetCursorPosition(20, 4);
for (int i = 1; i <= 10; i++)
{
Console.Write(i + " ");
if (((i) % 5) > 0)
{
}
else
{
Console.Write("\n");
}
}
Console.Write("\b");
for (int b = 11; b <= month; b++)
{
if (((b) % 7) > 0)
{
Console.Write(b + " ");
}
else
{
Console.Write("\n");
}
}
}
if (year == 4)
{
for (int i = 1; i <= 10; i++)
{
Console.Write(i + " ");
if (((i) % 7) > 0)
{
}
else
{
Console.Write("\n");
}
}
Console.Write("\b");
for (int b = 11; b <= month; b++)
{
if (((b) % 7) > 0)
{
Console.Write(b + " ");
}
else
{
Console.Write(b + "\n");
}
}
}
if (year == 5)
{
for (int i = 1; i <= 10; i++)
{
Console.Write(i + " ");
if (((i) % 7) > 0)
{
}
else
{
Console.Write("\n");
}
}
Console.Write("\b");
for (int b = 11; b <= month; b++)
{
if (((b) % 7) > 0)
{
Console.Write(b + " ");
}
else
{
Console.Write(b + "\n");
}
}
}
if (year == 6)
{
for (int i = 1; i <= 10; i++)
{
Console.Write(i + " ");
if (((i) % 7) > 0)
{
}
else
{
Console.Write("\n");
}
}
Console.Write("\b");
for (int b = 11; b <= month; b++)
{
if (((b) % 7) > 0)
{
Console.Write(b + " ");
}
else
{
Console.Write(b + "\n");
}
}
}
if (year == 7)
{
for (int i = 1; i <= 10; i++)
{
Console.Write(i + " ");
if (((i) % 7) > 0)
{
}
else
{
Console.Write("\n");
}
}
Console.Write("\b");
for (int b = 11; b <= month; b++)
{
if (((b) % 7) > 0)
{
Console.Write(b + " ");
}
else
{
Console.Write(b + "\n");
}
}
}
Console.ReadLine();
}
}
}
Do any of you know what I am doing wrong for the years that aren't 1? One works fine, but the rest have various problems.
Thanks for the help!
Will this do?
class Program
{
static void Main(string[] args)
{
Console.Title = "Program 202t";
Console.Write("Enter the first day of the month: ");
int startingDay = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the number of days in a month: ");
int daysInMonth = Convert.ToInt32(Console.ReadLine());
List<string> daysOfTheWeek = new List<string>() {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
foreach (string day in daysOfTheWeek)
{
Console.Write($"{day,10}");
}
List<string> days = new List<string>();
for (int i = 0; i < startingDay; i++)
{
days.Add($"{"",10}");
}
for (int i = 1; i < daysInMonth+1; i++)
{
days.Add($"{i,10}");
}
for (int i = 0; i < days.Count; i++)
{
if (i%7!=0) {Console.Write(days[i]);}
else {Console.WriteLine(days[i]);}
}
}
Alrighty, i did a quick run through of the code and I see where you're issues are so I'll give you some tips on where to look.
For one, your code has logical issues revolving around the required formatting of the output. There are simpler ways to output the correct formatting. If you look at the .NET String Reference then look at formatting, you may find an easier way to do it.
Next, you have an issue with misaligned block endings so some of your code is never getting executed. I recommend you breakpoint your code at the first if statement and step through the code to understand the execution in runtime.
Lastly, I recommend clarifying your variable names a little bit. If the first input represent that day of the week, then perhaps use a variable name that represents it (for example dayOfWeek, but month doesn't do it for me). Also the number of days in the month could use a better variable name.
Please do ask questions if you need some more guidance. I'd be happy to help.
Ok, I'm attempting to make a simple program that reads in number of pizzas sold for a given day and then have the user input the type of pizza sold for that day (with this I need to use the Split() with the user input).
I'm having issues populating the columns for my jagged array.
Now, I can get what I have to work for only 1 pizza sold, but anything beyond that it is not taking in my user input for the type of pizzas sold for that day (the column values). It's not reading in the user input as separate items so once I input, it goes to the next line like it's waiting for data instead of moving on. (Since I'm testing this for one day, it would end the program once the user input was read in).
I'm not quite sure where my issue is with my loops putting in my column values, but I'm figuring it has something with reading in the user input and placing that in the column of the jagged array. Any help would be great.
static void Main(string[] args)
{
Greeting();
string[][] pizza = new string[7][];
GetPizzas(pizza);
}
static void Greeting()
{
Write("Welcome to Z's Pizza Report!");
}
static void GetPizzas(string[][] array)
{
int numOfPizza;
string day;
for (int r = 0; r < array.Length; r++)
{
if (r == 0)
{
day = "Monday";
Write("How many total pizzas were there for {0}? ", day);
numOfPizza = int.Parse(ReadLine());
while (numOfPizza < 0)
{
Write("Number cannot be negative. Try Again: ");
numOfPizza = int.Parse(ReadLine());
}
array[r] = new string[numOfPizza];
Write("Enter all the pizzas for {0}, seperated by spaces: ", day);
for (int c = 0; c < array[r].Length; c++)
{
string total = ReadLine();
array[c] = total.Split(' ');
while (array[r] != array[c])
{
Write("Input does not match number needed. Try Again: ");
total = ReadLine();
array[c] = total.Split(' ');
}
}
}
else if (r == 1)
{
day = "Tuesday";
}
else if (r == 2)
{
day = "Wednesday";
}
else if (r == 3)
{
day = "Thursday";
}
else if (r == 4)
{
day = "Friday";
}
else if (r == 5)
{
day = "Saturday";
}
else
{
day = "Sunday";
}
}
}
The code seems overly complicated to me, but does something like this help? Oh, notice that I'm using the built-in DayOfWeek enum to parse the friendly name for the day. The difference between this and yours is that Sunday is day 0.
This line of code casts the integer to the enum, and then gets the string value:
var dayOfWeek = ((DayOfWeek)i).ToString();
I also added a helper function to get an integer from the user. It takes in a prompt string, an error string, an optional min value and an optional max value, then it prompts the user for an integer and won't return until they enter a valid value:
static int GetIntFromUser(string prompt, string error,
int minValue = int.MinValue, int maxValue = int.MaxValue)
{
int result;
if (!string.IsNullOrEmpty(prompt)) Console.Write(prompt);
while (!int.TryParse(Console.ReadLine(), out result)
|| result < minValue || result > maxValue)
{
if (!string.IsNullOrEmpty(error)) Console.Write(error);
}
return result;
}
The rest of the code looks like:
static void Main(string[] args)
{
var pizzasSold = new string[7][];
GetPizzas(pizzasSold);
for (int i = 0; i < pizzasSold.Length; i++)
{
var dayOfWeek = ((DayOfWeek)i).ToString();
Console.WriteLine("You sold {0} pizzas on {1}: {2}",
pizzasSold[i].Length, dayOfWeek, string.Join(", ", pizzasSold[i]));
}
Console.Write("\nDone!\nPress any key to exit...");
Console.ReadKey();
}
static void GetPizzas(string[][] array)
{
for (int r = 0; r < array.Length; r++)
{
var dayOfWeek = ((DayOfWeek)r).ToString();
var numPizzasSold =
GetIntFromUser($"How many total pizzas were there for {dayOfWeek}? ",
"Number cannot be negative. Try Again: ", 0);
Console.Write($"Enter all the pizzas for {dayOfWeek}, seperated by spaces: ");
var pizzasSold = Console.ReadLine().Split(new[] { ' ' },
StringSplitOptions.RemoveEmptyEntries);
while (pizzasSold.Length != numPizzasSold)
{
Console.Write($"Input does not match number needed. Try Again: ");
pizzasSold = Console.ReadLine().Split(new[] { ' ' },
StringSplitOptions.RemoveEmptyEntries);
}
array[r] = pizzasSold;
}
}
using System;
namespace ConsoleApp12
{
class Program
{
static void Main(string[] args)
{
int[][] n = new int[3][];
int i;
n[0] = new int[4];
n[1] = new int[3];
n[2] = new int[2];
// n[1] = new int[] { 1, 2, 3 };
// Console.WriteLine("enter the rollno");
for (i = 0; i < 4; i++)
{
n[0][i] = Convert.ToInt32(Console.ReadLine());
}
for (i = 0; i < 3; i++)
{
n[1][i] = Convert.ToInt32(Console.ReadLine());
}
for (i = 0; i < 2; i++)
{
n[2][i] = Convert.ToInt32(Console.ReadLine());
}
// for (i = 0; i < 3; i++)
// {
// n[i][j] = Convert.ToInt32(Console.ReadLine());
//}
for (i = 0; i <4; i++)
{
Console.Write(n[0][i] + " ");
}
Console.WriteLine();
for (i = 0; i < 3; i++)
{
Console.Write(n[1][i] + " ");
}
}
}
}
since im new to C#, even though i have coded in C before, i still have a question on how I go about to execute the part where i ask the user to enter a set of inputs after a program has already been run.
The following program prints a calendar with the specified number of months, but i need help writing another line of code that would ask the user to enter the month, year and number of months to be printed, after the user has already inputed the values once. Do i have to do some type of looping in my main function or do i have to do it on the method above my main function ?
static void GenMonth(int month, int year)
{
int daycode, ndim;
PrintHeader(month, year);
ndim=GetNDIM(month,year);
int day=1;
daycode = GetDayCode(month, day, year);
int a,i;
for(a=1;a<=daycode;a++)
{
Console.Write(" ");
}
for (i = 1; i <= GetNDIM(month, year); i++)
{
Console.Write("{0,4}", i);
if (((i + daycode) % 7) == 0)
Console.Write("\n");
}
daycode = GetDayCode(month, day, year);
if (daycode == 6)
{
Console.Write("\n");
}
}
static void Main(string[] args)
{
Console.WriteLine("please enter m,y,n: \n");
string input = Console.ReadLine();
string[] split = input.Split(' ');
int month = Int32.Parse(split[0]);
int year = Int32.Parse(split[1]);
int numberOfMonths = Int32.Parse(split[2]);
int i=0;
for (i = 0; i < numberOfMonths; i++)
{
GenMonth(month, year);
month++;
Console.Write("\n \n");
}
if (month > 12)
{
month = 1;
year++;
}
Console.ReadKey();
}
You'll probably get a few ways to do this - here's one possibility. Just loop continuously, then break out of the loop (and the program will terminate) when you detect a value of 0 for the month.
static void Main(string[] args)
{
int month = -1;
while (true)
{
Console.WriteLine("please enter m,y,n: \n");
string input = Console.ReadLine();
string[] split = input.Split(' ');
month = Int32.Parse(split[0]);
if (month == 0)
break;
int year = Int32.Parse(split[1]);
int numberOfMonths = Int32.Parse(split[2]);
...
...
}
}
Try this out:
static void Main(string[] args)
{
while (AskForDate())
{}
}
private static bool AskForDate()
{
Console.WriteLine("please enter m,y,n: \n");
string input = Console.ReadLine();
string[] split = input.Split(' ');
int month = Int32.Parse(split[0]);
int year = Int32.Parse(split[1]);
int numberOfMonths = Int32.Parse(split[2]);
int i = 0;
for (i = 0; i < numberOfMonths; i++)
{
GenMonth(month, year);
month++;
Console.Write("\n \n");
}
if (month > 12)
{
month = 1;
year++;
}
Console.Out.WriteLine("Again? [Y/n]");
var key = Console.ReadKey();
return key.Key != ConsoleKey.N;
}
for(;;)
{
Console.WriteLine("Enter: Month Year NumberOfMonths\nPress enter to stop.");
string line = Console.ReadLine();
if (line == "")
break;
string[] terms = line.Split();
int
month = int.Parse(terms[0]),
year = int.Parse(terms[1]),
numberOfMonths = int.Parse(terms[2]);
for (int i = 0; i < numberOfMonths; i++)
{
GenMonth(month, year);
if (month == 12)
{
month = 1;
year++;
}
else
month++;
}
}
Console.Write("\nPress any key...");
Console.ReadKey();
Just use a while clause:
static void Main(string[] args)
{
Console.WriteLine("please enter m,y,n: \n");
string input = Console.ReadLine();
string[] split = input.Split(' ');
int month = Int32.Parse(split[0]);
while (month != 0)
{
int year = Int32.Parse(split[1]);
int numberOfMonths = Int32.Parse(split[2]);
int i=0;
for (i = 0; i < numberOfMonths; i++)
{
GenMonth(month, year);
month++;
Console.Write("\n \n");
}
if (month > 12)
{
month = 1;
year++;
}
Console.ReadKey();
Console.WriteLine("please enter m,y,n: \n");
input = Console.ReadLine();
split = input.Split(' ');
month = Int32.Parse(split[0]);
}
}
Let me know if that's not what you meant.
I have a Java application I'm trying to convert to C#. I have solved a fair bit of the program, but I have this clear method that troubles me:
private void checkCourts()
{
if (splMonth.getSelectedValue() != null && splDate.getSelectedValue() != null)
{
courtModel.clear();
Calendar booking = new GregorianCalendar();
int year = Calendar.getInstance().get(Calendar.YEAR);
int month = new Scanner(splMonth.getSelectedValue().toString()).nextInt() - 1;
int date = new Scanner(splDate.getSelectedValue().toString()).nextInt();
int time = Integer.parseInt(cmbxTime.getSelectedItem().toString());
int currentMonth = Calendar.getInstance().get(Calendar.MONTH);
int currentDate = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
int currentTime = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
booking.set(year, month, date, time, 0, 0);
if (month > currentMonth || (month == currentMonth && date > currentDate) || (month == currentMonth && date == currentDate && time > currentTime))
{
try
{
ArrayList<Reservation> rs = BookingManager.getInstance().getReservations();
Reservation r = new Reservation(booking);
ArrayList<String> courtNames = BookingManager.getInstance().getCourtsName();
for (int i = 0; i < rs.size(); i++)
{
r.getReservationTime().clear(Calendar.MILLISECOND);
rs.get(i).getReservationTime().clear(Calendar.MILLISECOND);
}
if (!rs.contains(r))
{
for (String c : courtNames)
{
courtModel.addElement(c);
}
}
else
{
for (String c : courtNames)
{
courtModel.addElement(c);
}
for (int i = 0; i < rs.size(); i++)
{
if (r.getReservationTime().getTime().equals(rs.get(i).getReservationTime().getTime()))
{
String courtName = BookingManager.getInstance().getNameById(rs.get(i).getCourtId());
courtModel.removeElement(courtName);
}
}
}
splCourt.setModel(courtModel);
}
catch (Exception e)
{
System.out.println("ERROR - " + e.getMessage());
}
}
else
{
JOptionPane.showMessageDialog(null, "Den valgte dato er ikke tilgængelig for booking.", "Advarsel", JOptionPane.INFORMATION_MESSAGE);
}
}
}
Well, the top for loop is the real issue, I think. I would like to remove the reservation times that already have been booked.
This is my first for loop try-out:
private void checkCourts()
{
DateTime current = DateTime.Now;
int year = Int32.Parse(DateTimePicker.Value.ToString("yyyy"));
int currentYear = current.Year;
int month = Int32.Parse(DateTimePicker.Value.ToString("MM"));
int currentMonth = current.Month;
int day = Int32.Parse(DateTimePicker.Value.ToString("dd"));
int currentDay = current.Day;
int time = (int)cmbxTime.SelectedItem;
int currentTime = current.TimeOfDay.Hours;
string date1 = year.ToString() + "," + month.ToString() + "," + day.ToString();
DateTime thisdate = DateTime.Parse(date1);
thisdate = thisdate.AddHours(time);
List<Reservation> rs = BookingManager.getInstance().getReservations();
Reservation r = new Reservation(thisdate);
List<string> courtNames = BookingManager.getInstance().getCourtsName();
if (month > currentMonth || (month == currentMonth && day > currentDay) ||
(month == currentMonth && day == currentDay && time > currentTime) && year >= currentYear)
{
try
{
for (int i = 0; i < rs.Count; i++)
{
r.ReservationTime = r.ReservationTime.AddTicks(-r.ReservationTime.Ticks % 10000000);
rs[i].ReservationTime = rs[i].ReservationTime.AddTicks(-rs[i].ReservationTime.Ticks % 10000000);
}
if (!rs.Contains(r))
{
foreach (string c in courtNames)
{
lboxCourts.Items.Add(c);
}
}
else
{
foreach (string c in courtNames)
{
lboxCourts.Items.Add(c);
}
for (int i = 0; i < rs.Count; i++)
{
if (r.ReservationTime.Equals(rs[i].ReservationTime))
{
String courtName = BookingManager.getInstance().getNameById(rs[i].CourtId);
lboxCourts.Items.Remove(courtName);
MessageBox.Show("is equal");
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
else
{
MessageBox.Show("Den valgte dato er ikke gyldig! - vær opmærksom på at hvis du vælger dags dato, at tidspunktet ikke kan være tidligere end nuværende tidspunkt!");
}
}
Hope you can clear my sight.. I have simply lost focus. I know of what I have see online - that datetimepicker is not that easy to edit. But then I would just edit the already booked item - to something like "already booked".
According to the docs, your Java code .clear(Calendar.MILLISECOND) is simply removing any milliseconds from the value. It's not doing anything with your application logic to remove the actual reservation times. It doesn't appear to involve any kind of DateTimePicker either.
Assuming in c# that the ReservationTime is a DateTime property, and r.ReservationTime is a different property than rs[i].ReservationTime, then you would need to do the following:
for (int i = 0; i < rs.Count; i++)
{
r.ReservationTime = r.ReservationTime.AddTicks(-r.ReservationTime.Ticks % 10000000);
rs[i].ReservationTime = rs[i].ReservationTime.AddTicks(-rs[i].ReservationTime.Ticks % 10000000);
}
There are a couple of points to note:
Java's Calendar class has resolution to the millisecond, so removing milliseconds would be truncating the value to the second.
DateTime in .Net has resolution to the tick. I tick is 100 nanoseconds, so there are 10,000,000 ticks in a second.
Therefore, you can't just clear the milliseconds, you have to clear by computing the remainder of ticks smaller than one second. Those are then subtracted from the original value, getting you the same result.
DateTime in .Net is immutable, so you can't just change one property. You have to compute a new value, and then assign it back to the original variable.
It ended up like this:
for (int i = 0; i < rs.Count; i++)
{
r.ReservationTime = r.ReservationTime;
rs[i].ReservationTime = DateTime.Parse(rs[i].ReservationTime.ToString());
if (thisdate.CompareTo(rs[i].ReservationTime) != 0)
{
foreach (string c in courtNames)
{
lboxCourts.Items.Add(c);
}
}
else
{
lboxCourts.Items.Clear();
foreach (string c in courtNames)
{
lboxCourts.Items.Add(c);
}
for (int j = 0; j < rs.Count; j++)
{
if (r.ReservationTime.Equals(rs[j].ReservationTime))
{
string courtName = BookingManager.getInstance().getNameById(rs[j].CourtId);
lboxCourts.Items.Remove(courtName);
}
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
else
{
MessageBox.Show("Den valgte dato er ikke gyldig! - vær opmærksom på at hvis du vælger dags dato, at tidspunktet ikke kan være tidligere end nuværende tidspunkt!");
}
}
Thank you for your help... now my method is removing the already booked courts ;-)
Kindest regards
Rasmus