论坛首页 入门技术论坛

简易超市收款系统 <三>

浏览 1673 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (1)
作者 正文
   发表时间:2009-02-02  

新增功能
  可以允许用户客户支付欧元或人民币 系统将自动转兑换为美元


以下是代码

本代码包含两个文件

一个文件是
CashRegister.java

/**

 * ***********CopyRight**************

 *-------Powered by QianXunNet-----

 *-----Version 1.3   2009-01-23-----

 *-----   Design BY  NiChao    -----

 *^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

 */
package chap04;

public class CopyOfCashRegister {
                   //申明常量
    public static final double quarter_value=0.25;  //25美分的货币折合成美元的价值
    public static final double half_value=0.5;           //50美分
    public static final double min_value=0.01;           //1美分
           //汇率
    public static final double rmb_to_dor=0.21;          //人民币换美元
    public static final double eur_to_dor=1.13;          //欧元换美元
   
    private double purchase;          //应收金额
    private double payment;          //实际收取
    private double change;           //找零
        //分类找零
    private int change_dollors;
    private int change_quarter;
    private int change_half;
    private int change_min;
    private  int times=0;               //用来计数 货物输入次数
   
   
    //类的方法
    public int getTimes() {
        return times;
    }
     //逐次结算总金额
    public void recordParchase(double money){
        this.purchase=this.purchase+money;
        this.times++;
    }
           //将其他花币兑换成美元    initmoney 原始钱数    kind 兑换种类
        public double exchange(double initMoney, int kind){
   
             if(1 == kind)   //人民币兑换美元
             {
                 return  initMoney*rmb_to_dor;
             }
             else if(2 == kind)
             {
                 return initMoney*eur_to_dor;
             }
             else return initMoney;             //美元换自己
           
        }
    //接受客户付款  有两种付款方式
    //直接付出全部金额   ( 方法重载)
         public void recordPayment(double payMent){
             this.payment=payMent;
             
         }
         public void recordPayment(double dollers,double quarter ,double half ,double min ){
        //分别输入客户给的各种货币 美元 四分之美元 半美元 以及1美分的个数
       
        //计算客户支付的总的钱数 payment
        this.payment=this.payment+dollers+quarter*quarter_value+half*half_value+min*min_value;
    }
    //找零方法
    public void  giveChange(){
        this.change=this.payment-this.purchase;
        //现将找零乘上100
        int change_temp =(int) Math.round(this.change*100);
            //美元数
        this.change_dollors=change_temp/100;
            //1/2美元数
        this.change_half=(change_temp-this.change_dollors*100)/50;
              //1/4美元数
        this.change_quarter = (change_temp-this.change_dollors*100-this.change_half*50)/25;
              //没分数
        this.change_min = (change_temp-this.change_dollors*100-this.change_half*50-this.change_quarter*25);
    }
    public int getChange_dollors() {
        return change_dollors;
    }
    public void setChange_dollors(int change_dollors) {
        this.change_dollors = change_dollors;
    }
    public int getChange_quarter() {
        return change_quarter;
    }
    public void setChange_quarter(int change_quarter) {
        this.change_quarter = change_quarter;
    }
    public int getChange_half() {
        return change_half;
    }
    public void setChange_half(int change_half) {
        this.change_half = change_half;
    }
    public int getChange_min() {
        return change_min;
    }
    public void setChange_min(int change_min) {
        this.change_min = change_min;
    }
    public double getChange() {
        return change;
    }
    public void setChange(double change) {
        this.change = change;
    }


    }




还有一个文件是
CashRegisterTester.java

/**

 * ***********CopyRight**************

 *-------Powered by QianXunNet-----

 *-----Version 1.3   2009-01-23-----

 *-----   Design BY  NiChao    -----

 *^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

 */
package chap04;
import java.io.*;
import java.util.*;

import chap04.CopyOfCashRegister;
public class CopyOfCashRegisterTester {

   
    public static void main(String[] args) {
        // TODO Auto-generated method stub
               System.out.println("----------------简易超市收款系统----------------");
               System.out.println("-------------感谢您的使用 祝您工作顺利------------");
               System.out.println("输入0开始工作");
        Scanner cin=new Scanner(System.in);
        while(cin.nextInt()==0){
              System.out.print("请输入客户购物的总金额……");
              CopyOfCashRegister case1 = new CopyOfCashRegister();
              int input =1;
              while(input == 1)
              {
              double p =cin.nextDouble();               //输入客户支付的金额
              case1.recordParchase(p);
             
              System.out.print("input continue? 0:结束 1:继续");
              input=cin.nextInt();
              }
             
              //做出选择  纯美元支付还是美元美分共同支付
              System.out.println("总计输入"+case1.getTimes()+"次");
              System.out.println("1.多种货币支付  2.单一货币支付");
             
              int a=cin.nextInt();
             while(a!=1&&a!=2 )
             {System.out.print("选择有误请重新选择……");System.out.println("1.多种货币支付  2.单一货币支付");a=cin.nextInt();}
             
                  if(1 == a){
                      System.out.println("请输入各种货币的数量");
                      System.out.println("美元数: ");
                      double d=cin.nextDouble();
                      System.out.println("25美分数量: ");
                      double q=cin.nextDouble();
                      System.out.println("50美分数量: ");
                      double h=cin.nextDouble();
                      System.out.println("1美分数量: ");
                      double m=cin.nextDouble();
                      case1.recordPayment(d,q,h,m);                    
                  }
                  else if( 2== a){
                      System.out.println("客户选择了单一一种美元: ");
                      System.out.println("请选择你支付的货币种类 1.人民币 2.欧元 3.美元 ");
                      int kind1=cin.nextInt();
                      System.out.println("请输入客户支付的钱数");
                      double d1=cin.nextDouble();
                      double d2=case1.exchange(d1, kind1);
                      case1.recordPayment(d2);
                      System.out.printf("客户支付的钱折合成美元是 %5.2f 美元",d2);
                  }
                 
             
             
              //  找零模块
             
             case1.giveChange();
 
              System.out.println("找零如下:");
              System.out.printf("总计找零 %5.2f",case1.getChange());
              System.out.println("支付如下 ");
              System.out.println(case1.getChange_dollors()+"个美元");
              System.out.println(case1.getChange_half()+"个1/2美元");
              System.out.println(case1.getChange_quarter()+"个1/4美元");
              System.out.println(case1.getChange_min()+"个美分");

             
        }
              
    }
    }




运行实例如下


----------------简易超市收款系统----------------
-------------感谢您的使用 祝您工作顺利------------
输入0开始工作
0
请输入客户购物的总金额……45
input continue? 0:结束 1:继续1
23.34
input continue? 0:结束 1:继续0
总计输入2次
1.多种货币支付  2.单一货币支付
2
客户选择了单一一种美元:
请选择你支付的货币种类 1.人民币 2.欧元 3.美元
2
请输入客户支付的钱数
64.88
客户支付的钱折合成美元是 73.31 美元找零如下:
总计找零  4.97支付如下
4个美元
1个1/2美元
1个1/4美元
22个美分
-------------------------------------------------------------- 

       
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics