RebornJcush
RebornJcush
CC#
Created by RebornJcush on 1/13/2024 in #help
Adding a Database
Hey everyone, I'm working on an assignment and I've got my code written but I do not understand how to implement a database into it. I had 1 example from my course on this area and it wasn't enough, if anyone could help I'd greatly appreciate it! ```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
public class InterfacePA
{
public static void Main(string[] args)
{
Console.WriteLine("\nName, Week 2 Interface PA\n");


//create instances of car and cargotruck
Car car1 = new Car("Ford", "Festiva", true, "Miami, FL");
Car car2 = new Car("Honda", "Accord", false, "Atlanta, GA");
CargoTruck ct1 = new CargoTruck("Volvo", "VNL 860", false, "150 cubic ft", "Richmond, VA");
CargoTruck ct2 = new CargoTruck("Ford", "Transit", true, "123 cubic ft", "Virginia Beach, VA");

//create list and add instances for list print
List<IVehicle> vehicles = new List<IVehicle> { car1, car2, ct1, ct2 };
Console.WriteLine("Vehicles printed from List");
foreach (IVehicle vehicle in vehicles)
{
PrintVehicle(vehicle, true);
Console.WriteLine();
}


Console.WriteLine("Vehicles printed directly");
PrintVehicle(car1, true);
Console.WriteLine();
PrintVehicle(car2, false);
Console.WriteLine();
PrintVehicle(ct1, false);
Console.WriteLine();
PrintVehicle(ct2, true);
Console.WriteLine();
}
private static void PrintVehicle(IVehicle vehicle, bool running)
{
Console.Write(vehicle);
vehicle.Drive(running, vehicle.GetDestination());
}
}
public class InterfacePA
{
public static void Main(string[] args)
{
Console.WriteLine("\nName, Week 2 Interface PA\n");


//create instances of car and cargotruck
Car car1 = new Car("Ford", "Festiva", true, "Miami, FL");
Car car2 = new Car("Honda", "Accord", false, "Atlanta, GA");
CargoTruck ct1 = new CargoTruck("Volvo", "VNL 860", false, "150 cubic ft", "Richmond, VA");
CargoTruck ct2 = new CargoTruck("Ford", "Transit", true, "123 cubic ft", "Virginia Beach, VA");

//create list and add instances for list print
List<IVehicle> vehicles = new List<IVehicle> { car1, car2, ct1, ct2 };
Console.WriteLine("Vehicles printed from List");
foreach (IVehicle vehicle in vehicles)
{
PrintVehicle(vehicle, true);
Console.WriteLine();
}


Console.WriteLine("Vehicles printed directly");
PrintVehicle(car1, true);
Console.WriteLine();
PrintVehicle(car2, false);
Console.WriteLine();
PrintVehicle(ct1, false);
Console.WriteLine();
PrintVehicle(ct2, true);
Console.WriteLine();
}
private static void PrintVehicle(IVehicle vehicle, bool running)
{
Console.Write(vehicle);
vehicle.Drive(running, vehicle.GetDestination());
}
}
my output is printing like this: Justyn Cushing, Week 2 Interface PA Vehicles printed from List Make: Ford Model: Festiva The car is running and heading to Miami, FL Make: Honda Model: Accord The car is running and heading to Atlanta, GA Make: Volvo Model: VNL 860 The truck is running and hauling 150 cubic ft of cargo to Richmond, VA Make: Ford Model: Transit The truck is running and hauling 123 cubic ft of cargo to Virginia Beach, VA Vehicles printed directly Make: Ford Model: Festiva The car is running and heading to Miami, FL Make: Honda Model: Accord The car is not running and heading to Atlanta, GA Make: Volvo Model: VNL 860 The truck is not running and hauling 150 cubic ft of cargo to Richmond, VA Make: Ford Model: Transit The truck is running and hauling 123 cubic ft of cargo to Virginia Beach, VA The directly output is correct however, the list will not put anything other than all true or all false. I can upload other classes if needed.
44 replies