main
syneffort 2 years ago
parent d4dc58c609
commit 428733668d
  1. 20
      MySolution/InventoryManagement/Components/InventoryContext.cs
  2. 122
      MySolution/InventoryManagement/Components/InventoryManager.cs
  3. 19
      MySolution/InventoryManagement/Components/Product.cs
  4. 23
      MySolution/InventoryManagement/InventoryManagement.csproj
  5. 52
      MySolution/InventoryManagement/Migrations/20231013074328_InitialCreate.Designer.cs
  6. 41
      MySolution/InventoryManagement/Migrations/20231013074328_InitialCreate.cs
  7. 49
      MySolution/InventoryManagement/Migrations/InventoryContextModelSnapshot.cs
  8. 89
      MySolution/InventoryManagement/Program.cs
  9. 6
      MySolution/MySolution.sln

@ -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<Product> 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);
}
}
}

@ -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("****************************************");
}
}
}
}

@ -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; }
}
}

@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateRuntimeConfigurationFiles>True</GenerateRuntimeConfigurationFiles>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.12">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.12" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.12">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>

@ -0,0 +1,52 @@
// <auto-generated />
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
{
/// <inheritdoc />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(450)");
b.Property<int>("Quantity")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("Name")
.IsUnique();
b.ToTable("Products");
});
#pragma warning restore 612, 618
}
}
}

@ -0,0 +1,41 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace InventoryManagement.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Products",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Name = table.Column<string>(type: "nvarchar(450)", nullable: false),
Quantity = table.Column<int>(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);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Products");
}
}
}

@ -0,0 +1,49 @@
// <auto-generated />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(450)");
b.Property<int>("Quantity")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("Name")
.IsUnique();
b.ToTable("Products");
});
#pragma warning restore 612, 618
}
}
}

@ -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;
}
}
}
}
}
}

@ -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

Loading…
Cancel
Save