RebornJcush
RebornJcush
CC#
Created by RebornJcush on 1/13/2024 in #help
Adding a Database
not specified.. they dont really teach us more so "heres your assignments and a 15 min 10 year old powerpoint goodluck" & ill check it out. thanks!
5 replies
CC#
Created by RebornJcush on 1/13/2024 in #help
Adding a Database
This is the instructions for the assignment: Your task for this week is to implement the classes and functionality needed to support the data storage you've defined. and these are what i declared in my mock up a few weeks back Tables, fields/data types: -Workouts WorkoutID Int (primary key) UserID int Day varchar(50) Exercise varchar(50) Weight int foreign key (userID) references Users(UserID) – allows a relationship between the 2 tables for the the association of workouts to a specific user -Users UserID Int (Primary key) UserName varchar(50) I tried adding in some code into program class for the database but like i said, i dont understand it at all
5 replies
CC#
Created by RebornJcush on 1/13/2024 in #help
Adding a Database
using System;
using System.Data.SqlClient;
class Program
{
static void Main()
{
// Connection string for your database
string connectionString = $"Data Source={orkout};Version=3;";

// Create a SqlConnection using the connection string
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();

// Insert user
int userId;
using (SqlCommand insertUserCommand = new SqlCommand("INSERT INTO Users (UserName) VALUES (@User); SELECT SCOPE_IDENTITY();", connection))
{
insertUserCommand.Parameters.AddWithValue("@User", "Justyn Cushing");

// Execute the command and get the UserId
userId = Convert.ToInt32(insertUserCommand.ExecuteScalar());
}

// Insert workouts
using (SqlCommand insertWorkoutCommand = new SqlCommand("INSERT INTO Workouts (UserName, Day, Exercise, Weight) VALUES (@User, @Day, @Exercise, @Weight);", connection))
{
insertWorkoutCommand.Parameters.AddWithValue("@User", userId);
insertWorkoutCommand.Parameters.AddWithValue("@Day", "Monday");
insertWorkoutCommand.Parameters.AddWithValue("@Exercise", "Squats");
insertWorkoutCommand.Parameters.AddWithValue("@Weight", 50);

// Execute the command for each workout
insertWorkoutCommand.ExecuteNonQuery();

// Repeat for other workouts...
}
}
WeeklyWorkoutTracker weeklyTracker = new WeeklyWorkoutTracker();

// Create a user
User user = new User("Justyn Cushing");

// Add workouts for the user
weeklyTracker.AddWorkout(user, "Monday", "Squats", 50);
weeklyTracker.AddWorkout(user, "Wednesday", "Bench Press", 40);
weeklyTracker.AddWorkout(user, "Friday", "Deadlifts", 60);

// Display the weekly workouts for the user
weeklyTracker.DisplayWeeklyWorkouts();
}
}
public class WeeklyWorkoutTracker
{
private List<Workout> weeklyWorkouts;

public WeeklyWorkoutTracker()
{
weeklyWorkouts = new List<Workout>();
}

public void AddWorkout(User user, string day, string exercise, double weight)
{
Workout workout = new Workout
{
User = user,
Day = day,
Exercise = exercise,
Weight = weight
};

weeklyWorkouts.Add(workout);
}

public void DisplayWeeklyWorkouts()
{
foreach (var workout in weeklyWorkouts)
{
Console.WriteLine(workout);
}
}
}

public class Workout
{
public string? Day { get; set; }
public string? Exercise { get; set; }
public double Weight { get; set; }
public User? User { get; set; }

public override string ToString()
{
return $"On {Day}, {User?.UserName} performed {Exercise} with a weight of {Weight} lbs.\n";
}
}

public class User
{
public string UserName { get; set; }

public User(string userName)
{
UserName = userName;
}
}
using System;
using System.Data.SqlClient;
class Program
{
static void Main()
{
// Connection string for your database
string connectionString = $"Data Source={orkout};Version=3;";

// Create a SqlConnection using the connection string
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();

// Insert user
int userId;
using (SqlCommand insertUserCommand = new SqlCommand("INSERT INTO Users (UserName) VALUES (@User); SELECT SCOPE_IDENTITY();", connection))
{
insertUserCommand.Parameters.AddWithValue("@User", "Justyn Cushing");

// Execute the command and get the UserId
userId = Convert.ToInt32(insertUserCommand.ExecuteScalar());
}

// Insert workouts
using (SqlCommand insertWorkoutCommand = new SqlCommand("INSERT INTO Workouts (UserName, Day, Exercise, Weight) VALUES (@User, @Day, @Exercise, @Weight);", connection))
{
insertWorkoutCommand.Parameters.AddWithValue("@User", userId);
insertWorkoutCommand.Parameters.AddWithValue("@Day", "Monday");
insertWorkoutCommand.Parameters.AddWithValue("@Exercise", "Squats");
insertWorkoutCommand.Parameters.AddWithValue("@Weight", 50);

// Execute the command for each workout
insertWorkoutCommand.ExecuteNonQuery();

// Repeat for other workouts...
}
}
WeeklyWorkoutTracker weeklyTracker = new WeeklyWorkoutTracker();

// Create a user
User user = new User("Justyn Cushing");

// Add workouts for the user
weeklyTracker.AddWorkout(user, "Monday", "Squats", 50);
weeklyTracker.AddWorkout(user, "Wednesday", "Bench Press", 40);
weeklyTracker.AddWorkout(user, "Friday", "Deadlifts", 60);

// Display the weekly workouts for the user
weeklyTracker.DisplayWeeklyWorkouts();
}
}
public class WeeklyWorkoutTracker
{
private List<Workout> weeklyWorkouts;

public WeeklyWorkoutTracker()
{
weeklyWorkouts = new List<Workout>();
}

public void AddWorkout(User user, string day, string exercise, double weight)
{
Workout workout = new Workout
{
User = user,
Day = day,
Exercise = exercise,
Weight = weight
};

weeklyWorkouts.Add(workout);
}

public void DisplayWeeklyWorkouts()
{
foreach (var workout in weeklyWorkouts)
{
Console.WriteLine(workout);
}
}
}

public class Workout
{
public string? Day { get; set; }
public string? Exercise { get; set; }
public double Weight { get; set; }
public User? User { get; set; }

public override string ToString()
{
return $"On {Day}, {User?.UserName} performed {Exercise} with a weight of {Weight} lbs.\n";
}
}

public class User
{
public string UserName { get; set; }

public User(string userName)
{
UserName = userName;
}
}
5 replies
CC#
Created by RebornJcush on 12/19/2023 in #help
Why is my list output not using the method to check true/false
thanks again! Ive been stuck on that for like 3 hours lol....
44 replies
CC#
Created by RebornJcush on 12/19/2023 in #help
Why is my list output not using the method to check true/false
lets put it this way.. i graduate in less than a year and i have no idea what im doing other than basic loops and making tiny programs that will never be used in the workforce 😄
44 replies
CC#
Created by RebornJcush on 12/19/2023 in #help
Why is my list output not using the method to check true/false
none of them are.. be happy you havent seen some of my other classes .. and yeah get set is a lot simpler
44 replies
CC#
Created by RebornJcush on 12/19/2023 in #help
Why is my list output not using the method to check true/false
oh wow it really was such a simple fix thank you.. idk why our guided assignment has us do it the way i had it and it works then when i reference that for this one it doesnt work that way
44 replies
CC#
Created by RebornJcush on 12/19/2023 in #help
Why is my list output not using the method to check true/false
yeah i seem to hear that alot.. the joys of school.. so when i remove the parameters my writeline for running? and destination error because they dont exist in the context
44 replies
CC#
Created by RebornJcush on 12/19/2023 in #help
Why is my list output not using the method to check true/false
yeah i know its sloppy and can be simplified but this is how the teacher wants it done lol
44 replies
CC#
Created by RebornJcush on 12/19/2023 in #help
Why is my list output not using the method to check true/false
so it should just be public void Drive(string destination)?
44 replies
CC#
Created by RebornJcush on 12/19/2023 in #help
Why is my list output not using the method to check true/false
yeah chat gpt had me redo the whole thing because it royally messed it up
44 replies
CC#
Created by RebornJcush on 12/19/2023 in #help
Why is my list output not using the method to check true/false
44 replies
CC#
Created by RebornJcush on 12/19/2023 in #help
Why is my list output not using the method to check true/false
yeah one sec
44 replies
CC#
Created by RebornJcush on 12/19/2023 in #help
Why is my list output not using the method to check true/false
so i tried not having true but then it tells me im missing a paramtere for that bool and fails to run.. i added true after trying chat gpt
44 replies
CC#
Created by RebornJcush on 12/19/2023 in #help
Why is my list output not using the method to check true/false
yes its
public void Drive(bool running, string destination)
{
Console.WriteLine("The car" + (running ? " is" : " is not") + " running and heading to " + destination);
}
public void Drive(bool running, string destination)
{
Console.WriteLine("The car" + (running ? " is" : " is not") + " running and heading to " + destination);
}
44 replies
CC#
Created by RebornJcush on 12/19/2023 in #help
Why is my list output not using the method to check true/false
trying to add the other classes
44 replies
CC#
Created by RebornJcush on 12/19/2023 in #help
Why is my list output not using the method to check true/false
my bool for true / false has a check to change it to is or is not so that it doesnt say true running or false running but that method only seems to be working in the direct
44 replies