⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cashier.java

📁 the applet is used for Grocery Store Simulation,When you execute the main() method, the output shoul
💻 JAVA
字号:

/**
 * Cashier: point of sale objects through which fruits are sold;
 *          stores total cash collected from sales
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
import java.awt.*;
public class Cashier
{
   private static double totalSales = 0;  //recod the total number that the store sales
   private PriceAndStockManager mgr;
   private int totalstore;         
   private double havemoney;         //record the custom give the money
   private static int totalpeople=0; //record the custom's number
   private double changemoney;   //record the changemoney
   
   public Cashier( PriceAndStockManager mgr )
   {
       this.mgr = mgr;
       
   }
   class Exception1 extends Exception //define a Exception 
   {
   	public Exception1()
   	{
   		System.out.println("Errors!");
   	}
   	
   }
   class Exception2 extends Exception //define a Exception
   {
   	public Exception2()
   	{
   		System.out.println("Errors!");
   	}
   	
    }
    class Exception3 extends Exception //define a Exception
   {
   	public Exception3()
   	{
   		System.out.println("Errors!");
   	}
   	
   }
   
   public void buyApples( int qty )
   {
   try{ 
   	    if(qty<0)   // the number of the custom want is wrong
   	    throw new Exception1();
   	    else if(havemoney<qty*mgr.getPrice("apples")) //the custom don't have enough money
   	    throw new Exception2();
   	    else if(qty>mgr.getStock("apples")) //the store don't have enough number
   	    throw new Exception3(); 
   	    totalpeople++;  //add the number of the custom
   	    totalSales=totalSales+qty*mgr.getPrice("apples"); //the totalsales add
   	    changemoney=havemoney-qty*mgr.getPrice("apples"); //record the changemoney
   	    mgr.buy("apples",qty);    //the number of the apples reduce
   	    }catch(Exception1 e){
   	              System.out.println("the number you want is wrong!");
   	    }catch(Exception2 e){
         	      System.out.println("your  money is not enough!");
        }catch(Exception3 e){
         	      System.out.println("the store don't have enough! ");
        }
        try 
        {
          throw new Exception1();
        	
        }catch(Exception1 e){}
      	    
   }
   
   public void buyOranges( int qty )
   {
  try{ 
   	    if(qty<0)              // the number of the custom want is wrong
   	    throw new Exception1();
   	    else if(havemoney<qty*mgr.getPrice("oranges")) //the custom don't have enough money
   	    throw new Exception2();
   	    else if(qty>mgr.getStock("oranges")) //the store don't have enough number
   	    throw new Exception3(); 
   	    totalpeople++;        //add the number of the custom
   	    totalSales=totalSales+qty*mgr.getPrice("oranges");//the totalsales add
   	    changemoney=havemoney-qty*mgr.getPrice("orange"); //record the changemoney 
   	    mgr.buy("oranges",qty); //the number of the apples reduce
   	    }catch(Exception1 e){
   	              System.out.println("the number you want is wrong!");
   	    }catch(Exception2 e){
         	      System.out.println("your  money is not enough!");
        }catch(Exception3 e){
         	      System.out.println("the store don't have enough! ");
        }	
   }
   
   public void buyPomelos( int qty )
   {
   
   	try{ 
   	    if(qty<0)                  // the number of the custom want is wrong
   	    throw new Exception1();
   	    else if(havemoney<qty*mgr.getPrice("pomelos")) //the custom don't have enough money
   	    throw new Exception2();
   	    else if(qty>mgr.getStock("pomelos")) //the store don't have enough number
   	    throw new Exception3(); 
   	    totalpeople++;  //add the number of the custom
   	    totalSales=totalSales+qty*mgr.getPrice("pomelos");//the totalsales add
   	    changemoney=havemoney-qty*mgr.getPrice("pomelos");//record the changemoney
   	    mgr.buy("pomelos",qty);//the number of the apples reduce
   	    }catch(Exception1 e){
   	              System.out.println("the number you want is wrong!");
   	    }catch(Exception2 e){
         	      System.out.println("your  money is not enough!");
        }catch(Exception3 e){
         	      System.out.println("the store don't have enough! ");
        }	
             
    
   }
   
   public double getTotalSales() //get the total of sales
   {
       return totalSales;
   }
   public void startPurchase( double money ) 
   {
   	havemoney=money;
   }
   public void buy( String productName, int qty )
   {
   	if(productName=="apples")   //the custom purchase apples
   	buyApples(qty);
   	else if(productName=="oranges")//the custom purchase oranges
   	buyOranges(qty);
   	else if(productName=="pomelos")//the custom purchase pomelos
   	buyPomelos(qty);
   }
   public void endPurchase()
   {//print the money that the custom give and the store giveback to custom
   System.out.println("the money that the custom give is: "+havemoney);
   System.out.println("the money that the store give the custom is: "+changemoney);
   }
   public void printSalesSummary()
   {//print the total number of sales and custom
   	System.out.println("the total money that is: "+totalSales);
   	System.out.println("the total customs that is: "+totalpeople);
   	
   }
    
   
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -