一、简介:
当当网是一个简单的购物控制台项目,其中有管理员(商家)与普通用户。
管理员的功能有登录、添加/查看/修改/删除商品、查看/删除用户、查看/删除(取消)订单。
普通用户的功能有注册、登录、查看商品并确定是否购买下单、查看/修改/删除订单。
通过这些功能能够达到的管理员添加商品后,用户能查看商品并进行购买下单,之后
用户还可根据情况修改订单或删除订单,管理员在用户下完单后查看用户订单。
二、项目分析:
C#是一个面向对象的语言,遵循万物皆对象的原则,需要定义的有管理员类、用户类、商品类、订单类,
根据项目功能可设计两个接口(管理员与用户)来声明各自的功能,然后定义它们的是实现类来实现其中的功能
使用MVC设计模式将以上类与接口进行分层
M(model 模型):定义接口与其实现类、分装操作类
dao(定义接口):管理员接口、用户接口
Impl(接口实现类): 管理实现接口类、用户实现接口类
pojp(分装操作类):管理员类、用户类、商品类、订单类
V (view 视图):定义界面显示类
C (controller 控制器):定义测试类
三、相关代码
解决方案资源管理器:
Model模块 :
管理员封装类:
用户封装类:
商品类:
订单类:
管理员接口:
用户接口:
管理员接口实现类:
using _10_14_当当网.Model.pojo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10_14_当当网.Model.dao.impl
{
class AdminDaoImpl : IAdminDao
{
public static List<Admin> list = new List<Admin>();//创建用户列表对象
public static List<Product> list1 = new List<Product>();//创建商品列表对象
public Admin Login(string name, string password)//登入方法
{
Admin a = null;
foreach (var admin in list)
{
if (admin.Name.Equals(name) && admin.Password.Equals(password))
{
a = new Admin();
a.Name = admin.Name;
break;
}
}
return a;
}
public void Init() //初始化方法
{
Admin a1 = new Admin();
a1.Id = 101;
a1.Name = "张三";
a1.Password = "123";
Admin a2 = new Admin(102, "李四", "456");
list.Add(a1);
list.Add(a2);
}
public Product AddProduct(int id,string name,int price,int num,string des)//添加商品
{
Product p = new Product(id,name,price,num,des);
list1.Add(p);
return p;
}
public List<Product> SelectProduct()//查看商品
{
return list1;
}
public Product RecomposeProduct(int id, string name, int price, int num, string des)//修改商品
{
Product p = SelectById(id);
list1.Remove(p);
Product p1 = new Product(id, name, price, num, des);
list1.Add(p1);
return p1;
}
public Product DeleteProduct(int id)//删除商品
{
Product p = SelectById(id);
list1.Remove(p);
return p;
}
public List<Users> SelectUsers()//查看用户
{
return UsersDaoImpl.ulist;
}
public Users DeleteUsers(int id)//删除用户
{
Users u = SelectById1(id);
UsersDaoImpl.ulist.Remove(u);
return u;
}
public List<Orders> SelectOrders() //查看订单
{
return UsersDaoImpl .olist;
}
public Orders DeleteOrders(int id)//删除订单
{
Orders ord = SelectById2 (id);
UsersDaoImpl.olist.Remove(ord);
return ord;
}
public Product SelectById(int id)//根据商品编号找商品
{
Product p = null;
foreach (var pro in list1)
{
if (pro.Id==id)
{
p = pro;
break;
}
}
return p;
}
public Users SelectById1(int id)//根据用户编号找用户
{
Users u = null;
foreach (var pro in UsersDaoImpl.ulist)
{
if (pro.Id == id)
{
u = pro;
break;
}
}
return u;
}
public Orders SelectById2(int id)//根据订单编号找订单
{
Orders ord = null;
foreach (var pro in UsersDaoImpl.olist)
{
if (pro.Id == id)
{
ord = pro;
break;
}
}
return ord;
}
}
}
用户接口实现类:
using _10_14_当当网.Model.pojo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10_14_当当网.Model.dao.impl
{
class UsersDaoImpl : IUsersDao
{
public static List<Users> ulist = new List<Users>();//创建用户列表对象
public static List<Orders> olist = new List<Orders>();//创建订单列表对象
public Users Register(int id,string name, string password)//注册
{
Users user = null;
if (name .Length !=0&&password .Length !=0)
{
for (int i = 0; i < ulist.Count; i++)
{
while (ulist[i].Name.Equals(name))
{
Console.WriteLine("用户名已存在");
Console.Write("重新输入用户名:");
name = Console.ReadLine();
}
}
user = new Users();
user.Id = id;
user.Name = name;
user.Password = password;
ulist.Add(user);
return user;
}
else
{
Console.WriteLine("用户名、密码不能为空!");
}
return user;
}
public Users Login(string name, string password)//登入
{
Users user1 = null;
for (int i = 0; i < ulist.Count ; i++)
{
if (ulist[i].Name.Equals(name )&& ulist[i].Password.Equals (password ))
{
// user1 = new Users();
user1 = ulist[i] ;
break;
}
}
return user1;
}
public List<Product > InitProduct() //初始化商品
{
Product p1 = new Product(1001, "三国演义", 200, 30, "中国四大名著之一,作者:罗贯中");
Product p2 = new Product(1002, "西游记", 200, 30, "中国四大名著之一,作者:吴承恩");
Product p3 = new Product(1003, "水浒传", 200, 30, "中国四大名著之一,作者:施耐庵");
Product p4 = new Product(1004, "红楼梦", 200, 30, "中国四大名著之一,作者:曹雪芹");
AdminDaoImpl.list1.Add(p1);
AdminDaoImpl.list1.Add(p2);
AdminDaoImpl.list1.Add(p3);
AdminDaoImpl.list1.Add(p4);
return AdminDaoImpl.list1;
}
public List<Product>SelectProduct()//查看商品
{
return AdminDaoImpl .list1;
}
int oid = 0;//订单编号
public Orders Buy(int id, int count, int sum, Users u, Product p)//购买商品
{
oid++;
Orders ord = new Orders(id,count,sum,u,p);
ord.Id = oid;
olist.Add(ord);
return ord;
}
public List<Orders> SelectOrders()//根据用户的ID查看订单
{
return olist;
}
public Product SelectById(int id)
{
Product p = null;
foreach (var pro in AdminDaoImpl.list1)
{
if (pro.Id == id)
{
p = pro;
break;
}
}
return p;
}
public Users SelectById1(int id)
{
Users u = null;
foreach (var pro in ulist)
{
if (pro.Id == id)
{
u = pro;
break;
}
}
return u;
}
public Orders SelectById2(int id)
{
Orders ord = null;
foreach (var pro in olist )
{
if (pro.Id == id)
{
ord = pro;
break;
}
}
return ord;
}
//参数:id:订单编号; pid:商品编号; count:修改后的商品数量; u:用户; p:商品
public Orders UpdataOrders(int id,int pid, int count,Users u,Product p)//修改订单
{
Product p1 = SelectById(pid);
p.Id = pid;
p.Name = p1.Name;
p.Num = p1.Num - count;
p.Price = p1.Price;
p.Desc = p1.Desc;
Orders ord = SelectById2(id);
Orders ord1 = new Orders();
ord1.Product = p;
ord1.Id = id;
ord1.Count = count;
ord1.Sum = ord.Product .Price *count;
ord1.Users = u;
olist.Remove(ord);
olist.Add(ord1);
return ord1;
}
public Orders DeleteOrders(int id )
{
Orders ord = SelectById2(id);
olist.Remove(ord);
return ord;
}
}
}
View模块:
管理员界面:
用户界面:
Controller模块:
管理员测试类:
using _10_14_当当网.Model.dao.impl;
using _10_14_当当网.Model.pojo;
using _10_14_当当网.view;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10_14_当当网.Controller
{
class AdminController
{
AdminOperation ao = new AdminOperation();
AdminDaoImpl adi = new AdminDaoImpl();
public void LoginInput() {
ao.Welcome();
adi.Init();
Console.WriteLine("请输入账号:");
string name = Console.ReadLine();
Console.WriteLine("请输入密码:");
string password = Console.ReadLine();
Admin a =adi.Login (name,password );
if (a!=null)
{
Console.WriteLine("登录成功");
OperationInput();
}
else
{
Console.WriteLine("登录失败");
LoginInput();
}
}
private void OperationInput()
{
ao.Welcome1();
Console.Write("请输入你的选择:");
int a = int.Parse(Console.ReadLine ());
switch (a)
{
case 1://添加商品
InsertProduct();
break;
case 2://查看商品
SelectProduct();
break;
case 3://修改商品
SelectByPid();
break;
case 4://删除商品
DeleteProduct();
break;
case 5://查看用户
SelectUsers();
break;
case 6://删除用户
DeleteUsers();
break;
case 7://查看订单
SelectOrders();
break;
case 8://删除订单
DeleteOrders();
break;
case 9://回到首页
new Test().Welcome();
break;
case 0://返回上一级
LoginInput();
break;
default:
Console.WriteLine("请输入正确的数字!");
break;
}
}
int pid = 1000;//商品编码
public void InsertProduct() //添加商品
{
pid++;
Console.Write("请输入商品名称:");
string name = Console.ReadLine();
Console.Write("请输入商品单价:");
int price = int.Parse(Console.ReadLine ());
Console.Write("请输入商品库存:");
int num= int.Parse(Console.ReadLine());
Console.Write("请输入商品描述:");
string des = Console.ReadLine();
Product p= adi.AddProduct(pid,name,price,num,des);
if (p!=null)
{
Console.WriteLine("添加商品成功!");
}
else
{
Console.WriteLine("添加商品失败!");
}
OperationInput();
}
public void SelectProduct() //查看商品
{
List<Product> list = adi.SelectProduct();
if (list.Count >0)
{
Console.WriteLine("商品编号" + "\t" + "商品名称" + "\t" + "商品单价" + "\t" + "商品数量" + "\t" + "商品描述");
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(list[i].Id + "\t\t" + list[i].Name + "\t\t" +
list[i].Price + "\t\t" + list[i].Num + "\t\t" + list[i].Desc);
}
}
else
{
Console.WriteLine("库存中没有该商品了!");
}
OperationInput();
}
int a;
public void SelectByPid() //查找商品编号
{
Console.Write("请输入要修改的商品编号:");
a = int.Parse(Console.ReadLine ());
Product p = adi.SelectById(a);
if (p!=null)
{
Console.WriteLine("商品编号" + "\t" + "商品名称" + "\t" + "商品数量" + "\t" + "商品单价" + "\t" + "商品描述");
Console.WriteLine(p.Id + "\t\t" + p.Name + "\t\t" + p.Num + "\t\t" + p.Price + "\t\t" + p.Desc);
UpdataProdct();//找到要修改的商品编号后,修改商品
}
else
{
Console.WriteLine("库存中没有该商品了!");
}
}
private void UpdataProdct()//修改商品
{
Console.WriteLine("是否修改商品:(Y/N)");
string b = Console.ReadLine();
if (b.Equals ("Y") ||b.Equals ("y"))
{
Console.Write("请输入商品编号:");
a = int.Parse(Console.ReadLine ());
Console.Write("请输入商品名称:");
string name = Console.ReadLine();
Console.Write("请输入商品单价:");
int price = int.Parse(Console.ReadLine());
Console.Write("请输入商品库存:");
int num = int.Parse(Console.ReadLine());
Console.Write("请输入商品描述:");
string des = Console.ReadLine();
Product p = adi.RecomposeProduct(a, name, price, num, des);
if (p != null)
{
Console.WriteLine("修改商品成功!");
}
else
{
Console.WriteLine("添加商品失败!");
}
}
OperationInput();
}
private void DeleteProduct()//删除商品
{
Console.Write("请输入商品编号:");
a = int.Parse(Console.ReadLine());
adi.DeleteProduct(a);
SelectProduct();
OperationInput();
}
public void SelectUsers()// 查看用户
{
List<Users> list2 = adi.SelectUsers();
if (list2 .Count >0)
{
Console.WriteLine("用户编号" + "\t" + "用户名称" + "\t" + "用户密码" );
for (int i = 0; i < list2.Count; i++)
{
Console.WriteLine(list2[i].Id + "\t\t" + list2[i].Name + "\t\t" +list2[i].Password );
}
}
else
{
Console.WriteLine("该用户不存在!");
}
OperationInput();
}
public void DeleteUsers()// 删除用户
{
Console.Write("请输入用户编号:");
a = int.Parse(Console.ReadLine());
adi.DeleteUsers(a);
SelectUsers ();
OperationInput();
}
public void SelectOrders()// 查看订单
{
List<Orders> olist = adi.SelectOrders();
if (olist.Count > 0)
{
Console.WriteLine("订单号" + "\t\t" + "商品编号" + "\t" + "用户名" + "\t\t" + "商品名称" + "\t\t" + "购买数量" + "\t\t" + "总计");
foreach (var item in olist)
{
Console.WriteLine(item.Id + "\t \t" + item.Product.Id + "\t \t" + item.Users.Name + "\t \t" + item.Product.Name + "\t \t"
+ item.Count + "\t \t" + item.Sum);
}
}
OperationInput();
}
public void DeleteOrders()// 删除订单
{
Console.Write("请输入用户编号:");
a = int.Parse(Console.ReadLine());
adi.DeleteOrders(a);
OperationInput();
}
}
}
用户测试类:
using _10_14_当当网.Model.dao;
using _10_14_当当网.Model.dao.impl;
using _10_14_当当网.Model.pojo;
using _10_14_当当网.view;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10_14_当当网.Controller
{
class UsersController
{
UsersDaoImpl udi = new UsersDaoImpl();
UsersOperation uo = new UsersOperation();
AdminDaoImpl adi = new AdminDaoImpl();
Users u = null;
//List<Product> list;
public void LoginInput() {
uo.Welcome();
Console.Write("请输入您的操作:");
int a = int.Parse(Console.ReadLine());
switch (a)
{
case 1://注册
Register();
break;
case 2://登录
Login();
break;
case 0://退出
new Test().Welcome();//返回首页
break;
}
}
private void Login()
{
Console.Write("请输入用户名:");
string name1 = Console.ReadLine();
Console.Write("请输入密码:");
string password1 = Console.ReadLine();
u = udi.Login(name1, password1);
if (u != null)
{
Console.WriteLine("登录成功");
ShoppingInput();//商品操作
}
else
{
Console.WriteLine("登录失败");
LoginInput();
}
}
int id = 0;
private void Register()
{
id++;
Console.Write("请输入用户名:");
string name = Console.ReadLine();
Console.Write("请输入密码:");
string password = Console.ReadLine();
Users u1 = udi.Register(id, name, password);
if (u1 != null)
{
Console.WriteLine("注册成功");
}
else
{
Console.WriteLine("注册失败");
}
LoginInput();
}
private void ShoppingInput()
{
uo.Welcome1();
Console.Write("请输入您的操作:");
int b = int.Parse(Console.ReadLine());
switch (b)
{
case 1://查看商品并下单
SelectProduct();
break;
case 2://查看订单
SelectOrders();
break;
case 3://修改订单
UpdataOrders();
break;
case 4://删除订单
DeleteOrders();
break;
case 0://返回上一级
LoginInput();
break;
}
}
private void SelectProduct()//查看商品并判断是否购买下单
{
List<Product > plist= AdminDaoImpl .list1;
if (plist.Count > 0)
{
Console.WriteLine("商品编号" + "\t" + "商品名称" + "\t" + "商品单价" + "\t" + "商品数量" + "\t" + "商品描述");
for (int i = 0; i < plist.Count; i++)
{
Console.WriteLine(plist[i].Id + "\t\t" + plist[i].Name + "\t\t" + plist[i].Price + "\t\t" +
plist[i].Num + "\t\t" + plist[i].Desc);
}
Console.WriteLine("是否购买商品:(Y/N)");
string b = Console.ReadLine();
if (b.Equals("Y") || b.Equals("y"))
{
Console.Write("请输入购买的商品编号:");
int id = int.Parse(Console.ReadLine());
Console.Write("请输入要购买商品的数量:");
int count = int.Parse(Console.ReadLine());
Product p = adi.SelectById(id);
int sum = p.Price * count;
p.Num -= count;//库存量减count
Orders ord = udi.Buy(id, count, sum, u, p);
if (ord != null)
{
Console.Write("请输入电话号码:");
string tel = Console.ReadLine();
Console.Write("请输入收件地址:");
string address = Console.ReadLine();
u.Tal = tel;
u.Address = address;
Console.WriteLine("下单成功!");
}
else
{
Console.WriteLine("下单失败!");
}
}
}
else
{
Console.WriteLine("暂无商品!");
}
ShoppingInput();
}
public void SelectOrders() //查看订单
{
List<Orders> olist = udi.SelectOrders( );
if (olist .Count >0)
{
Console.WriteLine("订单号"+"\t\t"+"商品编号" + "\t" + "用户名" + "\t\t" + "商品名称" + "\t\t" + "购买数量" + "\t\t" + "总计");
foreach (var item in olist )
{
Console.WriteLine( item .Id +"\t \t"+item .Product .Id +"\t \t"+item .Users .Name +"\t \t"+item .Product .Name +"\t \t"
+item.Count +"\t \t"+item .Sum );
}
Console.WriteLine("收件人电话"+"\t"+"收件地址");
Console.WriteLine(u.Tal +"\t\t"+u.Address);
}
else
{
Console.WriteLine("暂无订单!");
}
ShoppingInput();
}
public void UpdataOrders() //修改订单
{
Console.WriteLine("是否修改订单:(Y/N)");
string b = Console.ReadLine();
if (b.Equals("Y")||b.Equals ("y"))
{
Console.Write("请输入订单编号:");
int a = int.Parse(Console.ReadLine());
Console.Write("请输入商品编号:");
int pid = int.Parse(Console.ReadLine());
Console.Write("请输入修改商品的数量:");
int count = int.Parse(Console.ReadLine());
Console.WriteLine("请输入修改的电话");
string tel = Console.ReadLine();
Console.WriteLine("请输入修改的地址");
string address = Console.ReadLine();
Product p = new Product() ;
u= new Users(u.Id ,u.Name ,tel,address );
Orders ord = udi.UpdataOrders(a,pid,count,u,p);
if (ord!=null)
{
Console.WriteLine("修改成功!");
Console.WriteLine("订单号" + "\t\t" + "商品编号" + "\t" + "用户名" + "\t\t" + "商品名称" + "\t\t" + "购买数量" + "\t\t" + "总计");
foreach (var item in UsersDaoImpl.olist)
{
Console.WriteLine(item.Id + "\t \t" + item.Product.Id + "\t \t" + item.Users.Name + "\t \t" + item.Product.Name + "\t \t"
+ item.Count + "\t \t" + item.Sum);
}
Console.WriteLine("收件人电话" + "\t" + "收件地址");
Console.WriteLine(u.Tal + "\t\t" + u.Address);
}
else
{
Console.WriteLine("修改失败!");
}
}
ShoppingInput();
}
public void DeleteOrders()//删除订单
{
Console.Write("请输入订单编号:");
int a = int.Parse(Console.ReadLine());
udi.DeleteOrders(a);
ShoppingInput();
}
}
}
总测试类: