首页 > IT职业 > IT生活 > 个人发卡网源码(基于PHP的最新版发卡网源码系统)
在本文中,您将逐步学习如何用PHP开发个人发卡网源码。这发卡网源码是一个简单的基于文本的程序,这意味着您将使用控制台与自动售货机交互并输入文本。
在本教程中,您将看到一个简单程序的完整开发过程。在这里了解更多关于软件开发过程的信息。
详情个人发卡网源码演示及内核源码资料可参看:ask.dcloud.net.cn/article/38806
第一步是定义需求或定义个人发卡网源码程序应该如何工作。我们将创建用例和UML图来定义和阐明这些需求。
接下来,我们将看到如何按照面向对象的编程方法创建一个框架实现。创建一个框架实现意味着创建一些类和接口,但是我们还没有编写所有的代码。这一步将有助于组织代码。
最后,我们将完成实现。在写代码的时候,我建议遵循下面文章中分享的干净代码规则:编写PHP干净代码的4个简单技巧。
1、定义个人发卡网源码的需求
在这个阶段,我们将使用用例和UML图来细化自动售货机java程序的工作方式。给自动售货机编程是一个相当模糊的声明。因此,过程的这一步是必要的,因为它帮助定义程序将如何更具体地工作。
让我们从创建一个用例开始:
个人发卡网显示欢迎信息所有的产品和价格
个人发卡网要求用户选择一个产品
个人发卡网要求用户输入硬币。接受的硬币有5,10,20,50,100分
个人发卡网计算插入的总钱数
计算变化量
计算要返回的最小硬币数量
向用户显示更改的金额和硬币
在上面,您可以看到这个问题中的一些实体,并突出显示潜在的操作。使用这些实体和操作,我们可以创建一个流程图,帮助我们可视化实体将如何通信:
2、创建一个框架实现
现在,我们对应用程序应该如何工作有了更好的理解。以下是课程:
主要
VendingMachineInterface
VendingMachineController
计算器
硬币和CoinBundle
产品
Main类将启动程序并协调用户和VendingMachineInterface之间的通信。
import java.util.Scanner;
public class Main{
public static void main(String[]args){
Scanner scanner=new Scanner(System.in);
//TODO To complete in step3
VendingMachineInterface machineInterface=null;
machineInterface.displayProducts();
String selectedProduct=scanner.nextLine();
machineInterface.selectProduct(Integer.parseInt(selectedProduct));
machineInterface.displayEnterCoinsMessage();
String userEnteredCoins=scanner.nextLine();
int[]enteredCoins={};//TODO Complete in step 3
machineInterface.enterCoins(enteredCoins);
machineInterface.displayChangeMessage();
}
}
VendingMachineInterface是定义如何与用户通信的类。在本例中,我们将通过控制台使用文本进行通信;因此,该类将显示文本消息并接收用户文本输入,如所选产品或硬币。
public interface VendingMachineInterface{
void displayProducts();
void selectProduct(int product);
void displayEnterCoinsMessage();
void enterCoins(int...coins);
void displayChangeMessage();
}
另一个重要的类是计算器。calculator类将执行操作来计算插入的总金额和最小硬币数,以返回更改金额。
public interface Calculator{
int calculateTotal(CoinBundle enteredCoins);
CoinBundle calculateChange(int enteredByUserMoney);
}
接下来,我们将创建一个VendingController类,用于协调接口和计算器之间的通信。这个类帮助将接口与程序的其余部分分离开来。接口不需要知道如何计算变化;它只需要知道在哪里请求它。
public class VendingMachineRequest{
public Product product;
public CoinBundle enteredCoins;
public VendingMachineRequest(int selectedProduct,int...enteredCoins){
//TODO Complete in step 3
this.product=null;
this.enteredCoins=new CoinBundle(enteredCoins);
}
}
硬币类将代表机器接受的所有硬币:5美分、10美分、20美分、50美分和100美分。
public enum Coin{
FIVE_CENTS(5),TEN_CENTS(10),TWENTY_CENTS(20),FIFTY_CENTS(50),HUNDRED_CENTS(100);
private int value
Coin(int value){
this.value=value;
}
public int getValue(){
return this.value;
}
}
接下来,因为我们将接收一堆硬币并返回一堆硬币,所以我们可以表示一个类,而不是传递代表硬币数量的数字数组。此更改将简化类之间的通信。我们将这个类称为CoinBundle。
public class CoinBundle{
public int number5CentsCoins;
public int number10CentsCoins;
public int number20CentsCoins;
public int number50CentsCoins;
public int number100CentsCoins;
public CoinBundle(int...enteredCoins){
this.number5CentsCoins=enteredCoins[0];
this.number10CentsCoins=enteredCoins[1];
this.number20CentsCoins=enteredCoins[2];
this.number50CentsCoins=enteredCoins[3];
this.number100CentsCoins=enteredCoins[4];
}
}
最后是Product类,它将定义自动售货机提供的产品。在这个例子中,我们的自动售货机出售Twix巧克力棒、可乐、水和三明治。这个类有两条信息:购买产品的选择号和价格。
public enum Product{
TWIX(1,100),COKE(2,50),WATER(3,30),SANDWICH(4,150),EMPTY(0,0);
private int selectionNumber;
private int price;
Product(int selectionNumber,int price){
this.selectionNumber=selectionNumber;
this.price=price;
}
public int getSelectionNumber(){
return selectionNumber;
}
public int getPrice(){
return this.price;
}
}
到目前为止,我们有八个类来表示程序中的所有实体,并且清楚地定义了每个类的职责。现在要做的就是填补空缺;换句话说,用实际的代码实现接口。
3、完成实施
完成实现是我们软件开发过程中的最后一步。到目前为止,我们已经清楚地了解了程序的外观以及代码如何在不同的类之间分布。
由于前面定义了一些接口,现在我们将实现这些接口。我们将用具体的类和方法覆盖这些接口,这些类和方法向程序指示如何执行这些接口操作。
3.1、实现VendingMachineInterface
让我们从实现VendingMachineInterface开始。VendingMachineInterface是与用户通信的类。因为它是一个基于文本的接口,所以我将其称为TextVendingMachineInterface。因此,我们需要写出五种方法:
displayProducts():此方法在控制台中显示一条消息,欢迎用户并显示可用的产品。
selectProduct(int产品):接收用户选择的产品。
displayEnterCoinsMessage():显示请求用户输入货币的消息的方法。
enterCoins(int…硬币):接收用户输入的钱币。
displayChangeMessage():它显示一条消息,让用户知道他的零钱和硬币的数量。
public class TextVendingMachineInterface implements VendingMachineInterface{
private VendingMachineController controller=new SimpleVendingMachineController();
private int selectedProduct;
private CoinBundle change;
Override
public void displayProducts(){
System.out.println("*********************************************");
System.out.println("WELCOME TO THE VENDING MACHINE");
System.out.println("*********************************************");
System.out.println("Products available:");
System.out.println("");
for(Product product:Product.values()){
if(!Product.EMPTY.equals(product)){
System.out.println(""+product.getSelectionNumber()+""+product.name()+"-Price:"+product.getPrice()+"");
}
}
System.out.println("");
System.out.println("Please select your product:");
}
Override
public void selectProduct(int product){
this.selectedProduct=product;
}
Override
public void displayEnterCoinsMessage(){
System.out.println("Please enter coins as follows:");
System.out.println("num of 5 cents coins,num of 10 cents coins,num of 20 cents coins,num of 50 cents coins,num of 100 cents coins");
System.out.println("");
System.out.println("Example:If you would like to enter 2 ten cents coins:0,2,0,0,0");
System.out.println("Plese enter coins:");
}
Override
public void enterCoins(int...coins){
VendingMachineRequest request=new VendingMachineRequest(selectedProduct,coins);
change=controller.calculateChange(request);
}
Override
public void displayChangeMessage(){
System.out.println("");
System.out.println("Your change is:"+change.getTotal()+"cents splitted as follows:");
System.out.println("100 cents coins:"+change.number100CentsCoins);
System.out.println("50 cents coins:"+change.number50CentsCoins);
System.out.println("20 cents coins:"+change.number20CentsCoins);
System.out.println("10 cents coins:"+change.number10CentsCoins);
System.out.println("5 cents coins:"+change.number5CentsCoins);
}
}
另外,将这个方法添加到CoinBundle中,以便上面的代码工作:
public int getTotal(){
int total=0;
total=total+this.number5CentsCoins*Coin.FIVE_CENTS.getValue();
total=total+this.number10CentsCoins*Coin.TEN_CENTS.getValue();
total=total+this.number20CentsCoins*Coin.TWENTY_CENTS.getValue();
total=total+this.number50CentsCoins*Coin.FIFTY_CENTS.getValue();
total=total+this.number100CentsCoins*Coin.HUNDRED_CENTS.getValue();
return total;
}
此外,您还需要更新VendingMachineRequest类并修改构造函数,如下所示:
public VendingMachineRequest(int selectedProduct,int...enteredCoins){
this.product=Product.valueOf(selectedProduct);
this.enteredCoins=new CoinBundle(enteredCoins);
}
并向Product类添加.valueof()方法。该方法将用户插入的数字映射为可用的产品。
public static Product valueOf(int numberSelection){
for(Product product:Product.values()){
if(numberSelection==product.getSelectionNumber()){
return product;
}
}
return EMPTY;
}
3.2、实现VendingMachineController
接下来,我们将通过创建一个名为SimpleVendingMachineController的类来实现VendingMachineController接口。这个类只是协调前面的接口和计算器之间的通信。
public class SimpleVendingMachineController implements VendingMachineController{
private Calculator calculator=new SimpleCalculator();
Override
public CoinBundle calculateChange(VendingMachineRequest request){
int total=calculator.calculateTotal(request.enteredCoins);
int totalChange=total-request.product.getPrice();
return calculator.calculateChange(totalChange);
}
}
很好,您已经完成了个人发卡网源码。您可以通过执行Main类来测试它。
如果您想要创建一个可执行文件,以便与您的朋友共享您的新程序,请查看这篇文章,我在其中解释了如何打包PHP程序。
结论
总之,本教程将介绍如何使用面向对象的编程方法创建个人发卡网源码。我们从定义需求开始,创建程序的框架实现,最后完成个人发卡网源码PHP程序。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69934491/viewspace-2760532/ ,如需转载,请注明出处,否则将追究法律责任。