Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ha
{
/// <summary>
/// 类的访问修饰符省略默认是(internal)
/// </summary>
class Program
{
static void Main(string[] args)
{
//// 通过new实例化创建对象,在实例化的时候可以给对象的属性赋值
////Student stu1 = new Student("张三", 18, '男');
//Student stu1 = new Student { Name = "张三", Age = 18, Gender = '男' };
//stu1.play("王者荣耀");
//Console.WriteLine($"姓名:{stu1.Name},年龄:{stu1.Age},性别:{stu1.Gender}");
//// 没初始化,字符串默认是空,数字默认是0
//Student stu2 = new Student();
//// stu2.Name = "李四";
////stu2.Age = 22;
//stu2.Gender = '女';
//Console.WriteLine($"姓名:{stu2.Name},年龄:{stu2.Age},性别:{stu2.Gender}");
ManagerForm mf = new ManagerForm();
mf.Start();
}
}
}
Student.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Ha
{
/// <summary>
/// 类的访问修饰符省略默认是(internal)
/// </summary>
class Student
{
/// <summary>
/// 构造函数(名称和类名相同)
/// 如果不手动添加无参构造方法默认也有一个
/// 让用户手动添加一个构造方法时,原来的默认构造方法会销毁
/// </summary>
// 方法重载
public Student()
{
}
public Student(string name, int age, char gender)
{
// 关键词 this 指向当前对象的引用
this.Name = name;
this.Age = age;
this.Gender = gender;
}
// 类的成员不写是private
#region 静态特征
/// <summary>
/// 姓名
/// </summary>
public string Name;
/// <summary>
/// 年龄
/// </summary>
public int Age;
/// <summary>
/// 性别
/// </summary>
public char Gender;
#endregion
// public 公共的
// internal 项目内部访问
// internal protected 项目内部或子类可以访问
// protected 受保护的
// private 私有的
#region 动态特征
public void SetAge(int age)
{
if (age >= 0 && age <= 150)
{
Age = age;
}
else
{
throw new ArgumentOutOfRangeException("年龄必须在0到150之间");
}
}
public void study()
{
Console.WriteLine($"{Name}正在学习");
}
public void play(string game)
{
Console.WriteLine($"{Name}正在玩{game}");
}
#endregion
}
}
ManagerForm.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ha
{
public class ManagerForm
{
/// <summary>
/// 功能业务的入口
/// </summary>
/// <returns></returns>
// 模拟数据库数据
public Product[] GetData()
{
// 超出作用范围,GC垃圾回收
//GC.Collect(); // 手动触发垃圾回收
Product[] products = new Product[]
{
new Product { Name = "杯子", Price = 29.9, Love = 100, Position = "A1" },
new Product { Name = "热水器", Price = 399.9, Love = 200, Position = "B2" },
new Product { Name = "电饭煲", Price = 199.9, Love = 150, Position = "C3" }
};
// products[0] = new Product { Name = "杯子", Price = 29.9, Love = 100, Position = "A1" };
//string[] productNames = { "杯子", "热水器", "电饭煲" };
//double[] productPrices = { 29.9, 399.9, 199.9 };
return products;
}
public void Start()
{
Console.WriteLine("库存清单");
Product[] products = GetData();
foreach (Product p in products)
{
Console.WriteLine("产品名称:{0}", p.Name);
}
ShowMenu();
}
//private Product[] products; // 不建议,不一定一直用,但会一直占用内存
// 显示菜单
public void ShowMenu()
{
do
{
Console.WriteLine("请选择:1、获取物品位置 2、获取满意度最高的商品 3、退出");
int menuId = int.Parse(Console.ReadLine());
// Covert.ToINT32 效率低一点
switch (menuId)
{
case 1:
// 获取物品
showProduct();
break;
case 2:
// 获取满意度最高的商品
GetTopOne();
break;
case 3:
Environment.Exit(0); // 控制结束整个程序,控制台使用
// return; // 结束当前方法
break;
}
} while (true);
}
/// <summary>
/// 获取满意度最高的商品
/// </summary>
public void GetTopOne()
{
Product[] products = GetData();
for (int i = 0; i < products.Length - 1; i ++)
{
for (int j = 0; j < products.Length - 1 - i; j++)
{
if (products[j].Love < products[j + 1].Love)
{
Product temp = products[j];
products[j] = products[j + 1];
products[j + 1] = temp;
}
}
}
Console.WriteLine("名称:{0},位置:{1},人气:{2},价格:{3}",
products[0].Name, products[0].Position, products[0].Love, products[0].Price);
}
public void showProduct()
{
// 提示用户输入
Console.WriteLine("请输入产品名称"); // 学数据库后用编号去找
string productName = Console.ReadLine(); // 获取信息
Product[] products = GetData();
foreach (Product p in products)
{
if (p.Name.Equals(productName)) // 判断是否和用户输入一致
{
Console.WriteLine("名称:{0},位置:{1},人气:{2},价格:{3}",
p.Name, p.Position, p.Love,p.Price);
break; // 既然找到了商品,是不是应该终止?
}
}
}
}
}
Product.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Ha
{
public class Product
{
// 名称
public string Name;
// 价格
public double Price;
// 满意度
public int Love;
// 存放位置
public string Position;
}
}
类的写法和超市管理系统。
浙公网安备 33010602011771号