Object oriented comprehensive exercise (supermarket cashier system)

osc_ r2jpynxi 2021-01-21 08:04:39
object oriented comprehensive exercise supermarket


Comprehensive practical : Supermarket cash register system

Analyze system requirements
The goods provided by the supermarket : laptop 、 mobile phone 、 The soy sauce 、 Banana
There should be... In the supermarket : Price 、 Number 、 Number
So you can abstract out a parent class (ProductFather), Price included (Price)、 Number (Count)、 Number (ID)


If there's not enough to sell , I need to go to the warehouse to get the goods
What the warehouse needs : Storing goods 、 Take delivery of goods 、 Stock purchase

Supermarkets need a cash register function when they sell things

Abstract the parent class of a commodity

class ProductFather
{
public decimal Price
{
get;
set;
}
public double Count
{
get;
set;
}
public string ID
{
get;
set;
}
public ProductFather(string id, decimal price, double count)
{
this.ID = id;
this.Price = price;
this.Count = count;
}
}

Create commodity classes separately, inherit from parent class

class ShouJi:ProductFather
{
public ShouJi(string id, decimal price, double count) : base(id, price, count)
{
}
}

Create a warehouse class

class CangKu
{
//1. Storing goods
//1.1 list It's like a warehouse ,List<ProductFather> Add a collection of data , It's like putting shelves in the warehouse , So we can categorize them
List<List<ProductFather>> list = new List<List<ProductFather>>();



 /// <summary>
/// I want users to show the goods 
/// </summary>
public void ShowPros()
{
foreach (var item in list)
{
Console.WriteLine(" The warehouse has :" + item[0].Name + "," + "\t" + " Yes " + item.Count + " individual ," + "\t" + " Every " + item[0].Price + " element .");
}
}
/// <summary>
/// When creating a warehouse object , Add shelves to the warehouse 
/// </summary>
//list[0] Save the computer list[1] Save your cell phone list[2] Save soy sauce list[3] Save bananas , At this time if more , It can be done through a loop 
public CangKu()
{
list.Add(new List<ProductFather>());
list.Add(new List<ProductFather>());
list.Add(new List<ProductFather>());
list.Add(new List<ProductFather>());
}
/// <summary>
/// Stock purchase 
/// </summary>
/// <param name="strType"> The type of goods </param>
/// <param name="count"> The quantity of the goods </param>
public void GetPros(string strType,int count)
{
for (int i = 0; i < count; i++)
{
switch (strType)
{
case "NoteBook": list[0].Add(new NoteBook(Guid.NewGuid().ToString(), 5000, " laptop "));
break;
case "ShouJi":list[1].Add(new ShouJi(Guid.NewGuid().ToString(), 3000, " mobile phone "));
break;
case "JiangYou":list[2].Add(new JiangYou(Guid.NewGuid().ToString(), 10, " The soy sauce "));
break;
case "Banana":list[3].Add(new Banana(Guid.NewGuid().ToString(), 20, " Banana "));
break;
}
}
}
/// <summary>
/// Pick up 
/// </summary>
/// <param name="strType"> The type of goods </param>
/// <param name="count"> The quantity of the goods </param>
// When the goods are taken out , Where do you need to put it ? And it's possible to take a lot of them, so go back ProductFather[]
public ProductFather[] QuPros(string strType, int count)
{
ProductFather[] pros = new ProductFather[count];
for (int i = 0; i < count; i++)
{
switch (strType)
{
case "NoteBook":
if (list[0].Count == 0)
{
Console.WriteLine(" There is no computer !");
}
else
{
pros[i] = list[0][0];
list[0].RemoveAt(0);
}
break;
case "ShouJi":
if (list[1].Count == 0)
{
Console.WriteLine(" There's no cell phone !");
}
else
{
pros[i] = list[1][0];
list[1].RemoveAt(0);
}
break;
case "JiangYou":
if (list[2].Count == 0)
{
Console.WriteLine(" No soy sauce !");
}
else
{
pros[i] = list[2][0];
list[2].RemoveAt(0);
}
break;
case "Banana":
if (list[3].Count == 0)
{
Console.WriteLine(" No soy sauce !");
}
else
{
pros[i] = list[3][0];
list[3].RemoveAt(0);
}
break;
}
}
return pros;
}
}

Supermarket cash register system continued
Create a supermarket class

namespace Supermarket cash register system
{
class SuperMarket
{
//// When the user enters the supermarket , There should be something in the warehouse , But after finishing the warehouse class, there is still no stock , Create warehouse object and give warehouse goods through constructor 
CangKu ck = new CangKu();
/// <summary>
/// When you create a supermarket object , Import goods to the shelves in the warehouse 
/// </summary>
public SuperMarket()
{
ck.GetPros("ShouJi", 1000);
ck.GetPros("NoteBook", 1000);
ck.GetPros("JiangYou", 1000);
ck.GetPros("banana", 1000);
}
/// <summary>
/// The process of interacting with customers ( Ask the customer what to buy )
/// </summary>
public void AskBuying()
{
Console.WriteLine(" welcome , What can I do for you ");
Console.WriteLine(" We have laptops here (NoteBook)、 mobile phone (ShouJi)、 The soy sauce (JiangYou)、 Banana (Banana)");
string strType = Console.ReadLine();
Console.WriteLine(" How much do you need ?");
int count = Convert.ToInt32(Console.ReadLine());
// Go to the warehouse to pick up the goods 
ProductFather[] pros = ck.QuPros(strType, count);
// After picking up the goods , Calculate the price , Write a method , Calculate the total price 
double realMoney = GetMoney(pros);
Console.WriteLine(" What you buy , The total price is {0} element ", realMoney);
// Discount method 
Console.WriteLine(" What you buy , The total price is {0} element ", realMoney);
Console.WriteLine(" Please choose your discount method 1-- No discount 2-- 10% off 3-- hit 85 fold 4-- buy 300 send 50 5-- buy 500 send 100");
string input = Console.ReadLine();
}
/// <summary>
/// Calculate the total price according to the goods bought by users 
/// </summary>
/// <param name="pros"> Goods purchased by users </param>
/// <returns></returns>
public double GetMoney(ProductFather[] pros)
{
// State a total price 
double realMoney = 0;
for (int i = 0; i < pros.Length; i++)
{
// It means to add the price of each goods to the total price 
realMoney += pros[i].Price;
}
return realMoney;
}
}

}

Discount method of class creation
Because the discount is not fixed , There are no objects that can be implemented directly , So you can use an abstract class to implement
Create the parent of the discount class

namespace Supermarket cash register system
{
/// <summary>
/// The parent of discount 
/// </summary>
abstract class CalFather
{
/// <summary>
/// Calculate how much to pay after the discount 
/// </summary>
/// <param name="realMoney"> Money payable before discount </param>
/// <returns> Return the discounted money </returns>
public abstract double GetTotalMoney(double realMoney);
}
}

With discount parent class , Learn from your needs , There are three discount models 1. No discount 2. According to the discount 3. Free as you buy . First, create a non discount class

namespace Supermarket cash register system
{
/// <summary>
/// No discount , How much is how much 
/// </summary>
class CalNormal : CalFather
{
public override double GetTotalMoney(double realMoney)
{
return realMoney;
}
}
}

Then create a discount class according to the discount rate

namespace Supermarket cash register system
{
/// <summary>
/// Discount at the discount rate 
/// </summary>
class CalRate : CalFather
{
/// <summary>
/// Discount rate , Because the discount rate is different every day , So you need an attribute to calculate the discount rate 
/// </summary>
public double Rate
{
get;
set;
}
// When creating an object, pass in the discount rate 
public CalRate(double reta)
{
this.Rate = reta;
}
public override double GetTotalMoney(double realMoney)
{
return realMoney * this.Rate;
}
}
}

Finally, create a free as you buy class

namespace Supermarket cash register system
{
/// <summary>
/// buy M element send N element 
/// </summary>
class CalMN : CalFather
{
// Free as you buy 
public double M // The money to buy 
{
get;
set;
}
public double N // Money sent 
{
get;
set;
}
public CalMN(double m, double n) // Assign values to properties through constructors 
{
this.M = m;
this.N = n;
}
/// <summary>
/// Judge how much to buy (M) How much is it (N)
/// </summary>
/// <param name="realMoney"> How much did it cost </param>
/// <returns> How much should I pay when I return the money </returns>
public override double GetTotalMoney(double realMoney)
{
if (realMoney >= this.M)
{
// There is a problem at this point , If the user buys 1000 Well ? You have to send two if you want to 100 element .
//return realMoney - this.N;
// How much discount depends on how many M, So I got a few M, because realMoney yes double type , So we need to turn it into int Integer type 
return realMoney - (int)(realMoney/this.M)*this.N;
}
else
{
return realMoney;
}
}
}
}

So far, the supermarket discount steps are completed

At this time, we actually don't know which discount mode users will choose , This is where the simple factory design pattern is used , Returns an object based on user input .
Because of uncertainty about the user's choice , So it's not sure which subclass to return , But there's no mistake in returning the parent class .
Get the discount mode selected by the user , To give the price

class SuperMarket
{
// When the user enters the supermarket , There should be something in the warehouse , But after finishing the warehouse class, there is still no stock , Create warehouse object and give warehouse goods through constructor 
CangKu ck = new CangKu();
/// <summary>
/// When you create a supermarket object , Import goods to the shelves in the warehouse 
/// </summary>
public SuperMarket()
{
ck.GetPros("ShouJi", 1000);
ck.GetPros("NoteBook", 1000);
ck.GetPros("JiangYou", 1000);
ck.GetPros("banana", 1000);
}
/// <summary>
/// The process of interacting with customers ( Ask the customer what to buy )
/// </summary>
public void AskBuying()
{
Console.WriteLine(" welcome , What can I do for you ");
Console.WriteLine(" We have laptops here (NoteBook)、 mobile phone (ShouJi)、 The soy sauce (JiangYou)、 Banana (Banana)");
string strType = Console.ReadLine();
Console.WriteLine(" How much do you need ?");
int count = Convert.ToInt32(Console.ReadLine());
// Go to the warehouse to pick up the goods 
ProductFather[] pros = ck.QuPros(strType, count);
// After picking up the goods , Calculate the price , Write a method , Calculate the total price 
double realMoney = GetMoney(pros);
// Discount method 
Console.WriteLine(" What you buy , The total price is {0} element ", realMoney);
Console.WriteLine(" Please choose your discount method 1-- No discount 2-- 10% off 3-- hit 85 fold 4-- buy 300 send 50 5-- buy 500 send 100");
string input = Console.ReadLine();
// Through the simple factory design pattern, we can get a discount object according to the user's input 
CalFather cal = GetCal(input);
double totalMoney = cal.GetTotalMoney(realMoney);
Console.WriteLine(" After the discount , You have to deal with {0} element ", totalMoney);
}
/// <summary>
/// Returns a discount object according to the discount method selected by the user 
/// </summary>
/// <param name="input"> User choice </param>
/// <returns> Returns the object of the parent class , But it contains subclass objects </returns>
public CalFather GetCal(string input)
{
CalFather cal = null;
switch (input)
{
case "1": cal = new CalNormal();
break;
case "2": cal = new CalRate(0.9);
break;
case "3": cal = new CalRate(0.85);
break;
case "4": cal = new CalMN(300, 50);
break;
case "5": cal = new CalMN(500, 100);
break;
}
return cal;
}
/// <summary>
/// Calculate the total price according to the goods bought by users 
/// </summary>
/// <param name="pros"> Goods purchased by users </param>
/// <returns></returns>
public double GetMoney(ProductFather[] pros)
{
// State a total price 
double realMoney = 0;
for (int i = 0; i < pros.Length; i++)
{
// It means to add the price of each goods to the total price 
realMoney += pros[i].Price;
}
return realMoney;
}
}

When the program runs , We want to see something in this supermarket , How much , That is to show the goods , The method of displaying the goods has been written in the warehouse class , It's supposed to be in Main Function to create the warehouse object , There is a problem at this time , When creating objects, there are only shelves in the warehouse , And no goods . Goods are only available when the supermarket object is created
At the bottom of the supermarket class, call the function of displaying goods in the warehouse class , So you don't have to go to Main Function

 /// <summary>
/// Call the warehouse to display the goods , So you don't have to go Main Function to create the warehouse object 
/// </summary>
public void ShowPros()
{
ck.ShowPros();
}

Finish here , Our program is basically complete , stay Main Function and call

class Program
{
static void Main(string[] args)
{
// Create supermarket objects 
SuperMarket sm = new SuperMarket();
//1. Show the goods 
sm.ShowPros();
//2. Interact with users 
sm.AskBuying();
Console.ReadKey();
}

Finally, after the supermarket finished shopping , Will print a small ticket

public void AskBuying()
{
Console.WriteLine(" welcome , What can I do for you ");
Console.WriteLine(" We have laptops here (NoteBook)、 mobile phone (ShouJi)、 The soy sauce (JiangYou)、 Banana (Banana)");
string strType = Console.ReadLine();
Console.WriteLine(" How much do you need ?");
int count = Convert.ToInt32(Console.ReadLine());
// Go to the warehouse to pick up the goods 
ProductFather[] pros = ck.QuPros(strType, count);
// After picking up the goods , Calculate the price , Write a method , Calculate the total price 
double realMoney = GetMoney(pros);
// Discount method 
Console.WriteLine(" What you buy , The total price is {0} element ", realMoney);
Console.WriteLine(" Please choose your discount method 1-- No discount 2-- 10% off 3-- hit 85 fold 4-- buy 300 send 50 5-- buy 500 send 100");
string input = Console.ReadLine();
// Through the simple factory design pattern, we can get a discount object according to the user's input 
CalFather cal = GetCal(input);
double totalMoney = cal.GetTotalMoney(realMoney);
Console.WriteLine(" After the discount , You have to deal with {0} element ", totalMoney);
Console.WriteLine(" Here is your shopping information ");
foreach (var item in pros)
{
Console.WriteLine(" goods :" + item.Name + "," + "\t" + " The unit price of goods " + item.Price + "," + "\t" + " Cargo number :" + item.ID);
}
Console.WriteLine(" After the discount , You have to deal with {0} element ", totalMoney);
}

So far, the practice of supermarket cashier system is completed !!

版权声明
本文为[osc_ r2jpynxi]所创,转载请带上原文链接,感谢
https://javamana.com/2021/01/20210121080308658g.html

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云