diff --git a/MySolution/InventoryManagement/Components/InventoryContext.cs b/MySolution/InventoryManagement/Components/InventoryContext.cs new file mode 100644 index 0000000..1cd06d9 --- /dev/null +++ b/MySolution/InventoryManagement/Components/InventoryContext.cs @@ -0,0 +1,20 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace InventoryManagement.Components +{ + internal class InventoryContext : DbContext + { + public DbSet Products { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseSqlServer(@"Data Source=peacecloud.synology.me,21433;Initial Catalog=Study;User ID=study;Password=Study1234;Encrypt=false"); + base.OnConfiguring(optionsBuilder); + } + } +} diff --git a/MySolution/InventoryManagement/Components/InventoryManager.cs b/MySolution/InventoryManagement/Components/InventoryManager.cs new file mode 100644 index 0000000..0b5b43d --- /dev/null +++ b/MySolution/InventoryManagement/Components/InventoryManager.cs @@ -0,0 +1,122 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace InventoryManagement.Components +{ + internal class InventoryManager + { + public void AddProduct(Product product) + { + using (var context = new InventoryContext()) + { + if (context.Products.Any(p => p.Name == product.Name)) + { + Console.WriteLine("Product with the same name already exists."); + return; + } + + context.Products.Add(product); + context.SaveChanges(); + Console.WriteLine($"{product.Name} added."); + } + } + + public void BuyProduct(string name, int quantity) + { + using (var context = new InventoryContext()) + { + var product = context.Products.FirstOrDefault(p => p.Name == name); + if (product == null) + { + Console.WriteLine($"Cannot find the product named {name}"); + return; + } + + product.Quantity += quantity; + context.SaveChanges(); + Console.WriteLine($"{product.Name}'s quantity: {product.Quantity}"); + } + } + + public void BuyProduct(int id, int quantity) + { + using (var context = new InventoryContext()) + { + var product = context.Products.Find(id); + if (product == null) + { + Console.WriteLine("Product not found."); + return; + } + + product.Quantity += quantity; + context.SaveChanges(); + Console.WriteLine($"{product.Name}'s quantity: {product.Quantity}"); + } + } + + public void SellProduct(string name, int quantity) + { + using (var context = new InventoryContext()) + { + var product = context.Products.FirstOrDefault(p => p.Name == name); + if (product == null) + { + Console.WriteLine($"Cannot find the product named {name}"); + return; + } + + if (product.Quantity >= quantity) + { + product.Quantity -= quantity; + context.SaveChanges(); + Console.WriteLine($"{quantity} units of {product.Name} sold."); + } + else + { + Console.WriteLine($"Insufficient quantity in stock (Current: {product.Quantity})"); + } + } + } + + public void SellProduct(int id, int quantity) + { + using (var context = new InventoryContext()) + { + var product = context.Products.Find(id); + if (product == null) + { + Console.WriteLine("Product not found."); + return; + } + + if (product.Quantity >= quantity) + { + product.Quantity -= quantity; + context.SaveChanges(); + Console.WriteLine($"{quantity} units of {product.Name} sold."); + } + else + { + Console.WriteLine($"Insufficient quantity in stock (Current: {product.Quantity})"); + } + } + } + + public void ShowInventory() + { + using (var context = new InventoryContext()) + { + Console.WriteLine("********** Current Inventory **********"); + foreach (var product in context.Products) + { + Console.WriteLine($"ID: {product.Id}, Name: {product.Name}, Quantity: {product.Quantity}"); + } + Console.WriteLine("****************************************"); + } + } + } +} diff --git a/MySolution/InventoryManagement/Components/Product.cs b/MySolution/InventoryManagement/Components/Product.cs new file mode 100644 index 0000000..bedf71f --- /dev/null +++ b/MySolution/InventoryManagement/Components/Product.cs @@ -0,0 +1,19 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace InventoryManagement.Components +{ + [PrimaryKey(nameof(Id))] + [Index(nameof(Name), IsUnique = true)] + internal class Product + { + public int Id { get; } + public string Name { get; set; } + public int Quantity { get; set; } + } +} diff --git a/MySolution/InventoryManagement/InventoryManagement.csproj b/MySolution/InventoryManagement/InventoryManagement.csproj new file mode 100644 index 0000000..c883837 --- /dev/null +++ b/MySolution/InventoryManagement/InventoryManagement.csproj @@ -0,0 +1,23 @@ + + + + Exe + net6.0 + enable + enable + True + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + diff --git a/MySolution/InventoryManagement/Migrations/20231013074328_InitialCreate.Designer.cs b/MySolution/InventoryManagement/Migrations/20231013074328_InitialCreate.Designer.cs new file mode 100644 index 0000000..f0cea5d --- /dev/null +++ b/MySolution/InventoryManagement/Migrations/20231013074328_InitialCreate.Designer.cs @@ -0,0 +1,52 @@ +// +using InventoryManagement.Components; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace InventoryManagement.Migrations +{ + [DbContext(typeof(InventoryContext))] + [Migration("20231013074328_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.12") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("InventoryManagement.Components.Product", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(450)"); + + b.Property("Quantity") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.ToTable("Products"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MySolution/InventoryManagement/Migrations/20231013074328_InitialCreate.cs b/MySolution/InventoryManagement/Migrations/20231013074328_InitialCreate.cs new file mode 100644 index 0000000..b00e905 --- /dev/null +++ b/MySolution/InventoryManagement/Migrations/20231013074328_InitialCreate.cs @@ -0,0 +1,41 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace InventoryManagement.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Products", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Name = table.Column(type: "nvarchar(450)", nullable: false), + Quantity = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Products", x => x.Id); + }); + + migrationBuilder.CreateIndex( + name: "IX_Products_Name", + table: "Products", + column: "Name", + unique: true); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Products"); + } + } +} diff --git a/MySolution/InventoryManagement/Migrations/InventoryContextModelSnapshot.cs b/MySolution/InventoryManagement/Migrations/InventoryContextModelSnapshot.cs new file mode 100644 index 0000000..63f8285 --- /dev/null +++ b/MySolution/InventoryManagement/Migrations/InventoryContextModelSnapshot.cs @@ -0,0 +1,49 @@ +// +using InventoryManagement.Components; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace InventoryManagement.Migrations +{ + [DbContext(typeof(InventoryContext))] + partial class InventoryContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.12") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("InventoryManagement.Components.Product", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(450)"); + + b.Property("Quantity") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.ToTable("Products"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MySolution/InventoryManagement/Program.cs b/MySolution/InventoryManagement/Program.cs new file mode 100644 index 0000000..89cb6b1 --- /dev/null +++ b/MySolution/InventoryManagement/Program.cs @@ -0,0 +1,89 @@ +using InventoryManagement.Components; + +namespace InventoryManagement +{ + internal class Program + { + static void Main(string[] args) + { + using (var context = new InventoryContext()) + { + context.Database.EnsureCreated(); + } + + var InventoryManager = new InventoryManager(); + while (true) + { + Console.WriteLine(); + Console.WriteLine("Pease Enter the command."); + Console.WriteLine("(show: Show current Inventory, add: Add new product, buy: Buy product, sell: Sell product)"); + Console.Write("CMD> "); + string cmd = Console.ReadLine().ToLower(); + switch (cmd) + { + case "show": + InventoryManager.ShowInventory(); + break; + case "add": + { + Console.Write("\tName: "); + string name = Console.ReadLine(); + Console.Write("\tDefault quantity: "); + string qt = Console.ReadLine(); + int quantity = 0; + if (!int.TryParse(qt, out quantity)) + { + Console.WriteLine("Invalid quanitity."); + break; + } + Product product = new Product() { Name = name, Quantity = quantity }; + InventoryManager.AddProduct(product); + break; + } + + case "buy": + { + Console.Write("\tId or Name: "); + string name = Console.ReadLine(); + int id = 0; + bool idAvailable = int.TryParse(name, out id); + Console.Write("\tQuantity: "); + string qt = Console.ReadLine(); + int quantity = 0; + if (!int.TryParse(qt, out quantity)) + { + Console.WriteLine("Invalid quanitity."); + break; + } + if (idAvailable) + InventoryManager.BuyProduct(id, quantity); + else + InventoryManager.BuyProduct(name, quantity); + break; + } + case "sell": + { + Console.Write("\tId or Name: "); + string name = Console.ReadLine(); + int id = 0; + bool idAvailable = int.TryParse(name, out id); + Console.Write("\tQuantity: "); + string qt = Console.ReadLine(); + int quantity = 0; + if (!int.TryParse(qt, out quantity)) + { + Console.WriteLine("Invalid quanitity."); + break; + } + if (idAvailable) + InventoryManager.SellProduct(id, quantity); + else + InventoryManager.SellProduct(name, quantity); + break; + } + } + + } + } + } +} \ No newline at end of file diff --git a/MySolution/MySolution.sln b/MySolution/MySolution.sln index 14ea030..ac0102c 100644 --- a/MySolution/MySolution.sln +++ b/MySolution/MySolution.sln @@ -17,6 +17,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SObject", "SObject\SObject. EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpStreamingApp", "HttpStreamingApp\HttpStreamingApp.csproj", "{3D549B90-D7ED-4771-8595-9147BBF221C6}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InventoryManagement", "InventoryManagement\InventoryManagement.csproj", "{CDF5F490-9F8C-4CB2-AA5D-2460CBD43CC1}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -51,6 +53,10 @@ Global {3D549B90-D7ED-4771-8595-9147BBF221C6}.Debug|Any CPU.Build.0 = Debug|Any CPU {3D549B90-D7ED-4771-8595-9147BBF221C6}.Release|Any CPU.ActiveCfg = Release|Any CPU {3D549B90-D7ED-4771-8595-9147BBF221C6}.Release|Any CPU.Build.0 = Release|Any CPU + {CDF5F490-9F8C-4CB2-AA5D-2460CBD43CC1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CDF5F490-9F8C-4CB2-AA5D-2460CBD43CC1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CDF5F490-9F8C-4CB2-AA5D-2460CBD43CC1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CDF5F490-9F8C-4CB2-AA5D-2460CBD43CC1}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE