자바 간단 한 마 트 프로그램 구현

7887 단어 자바슈퍼마켓
본 논문 의 사례 는 자바 가 작은 슈퍼마켓 프로그램 을 실현 하 는 구체 적 인 코드 를 공유 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.
인물 가방
1.고객 류

package person;

public class Customer {
 public String name;
 public double money;
 public boolean isDrivingCar;
}
슈퍼마켓
1.작은 슈퍼마켓 류

package supermarket;

public class LittleSuperMarket {
 public String superMarketName;
 public String address;
 public int parkingCount;
 public double incomingSum;
 public Merchandise[] merchandises;
 public int[] merchandisesold;
}
2.상품 류

package supermarket;

public class Merchandise {
 public String name;
 public String id;
 public int count;
 public double soldPrice;
 public double purchasePrice;
}
주 프로그램

import person.Customer;
import supermarket.LittleSuperMarket;
import supermarket.Merchandise;

import java.util.Scanner;

public class Run {
 public static void main(String[] args) {

  //            
  LittleSuperMarket littleSuperMarket = new LittleSuperMarket();

  //       
  littleSuperMarket.address = "    999 ";
  littleSuperMarket.superMarketName = "    ";
  littleSuperMarket.parkingCount = 200;

  //       new     200   ,   Merchandise
  littleSuperMarket.merchandises = new Merchandise[200];
  //                      
  littleSuperMarket.merchandisesold = new int[littleSuperMarket.merchandises.length];


  //  
  for (int i = 0; i < littleSuperMarket.merchandises.length; i++) {
   //     merchandise     m,     
   Merchandise m = new Merchandise();
   m.count = 200;
   m.id = "ID" + i;
   m.name = "  " + i;
   m.purchasePrice = 200 * Math.random();
   m.soldPrice = (1 + Math.random()) * m.purchasePrice;

   //                merchandise  ,    merchandise   
   littleSuperMarket.merchandises[i] = m;

  }
  System.out.println("     !! !");


  boolean open = true;
  Scanner scanner = new Scanner(System.in);


  while (open) {
   System.out.println("    :" + littleSuperMarket.superMarketName);
   System.out.println("    :" + littleSuperMarket.address);
   System.out.println("      :" + littleSuperMarket.parkingCount);
   System.out.println("       :" + littleSuperMarket.incomingSum);
   System.out.println("    :" + littleSuperMarket.merchandises.length + " ");


   //        
   Customer customer = new Customer();
   customer.name = "  :" + ((int) (Math.random() * 500));
   customer.isDrivingCar = Math.random() > 0.4;
   customer.money = (1 + Math.random()) * 10000;


   //         
   if (customer.isDrivingCar) {
    System.out.println("    " + customer.name + "        !");
    //           >0,    ,        (     ,      )
    if (littleSuperMarket.parkingCount > 0) {
     littleSuperMarket.parkingCount--;
     System.out.println("    " + customer.name + "      " + littleSuperMarket.superMarketName + ",    " + littleSuperMarket.parkingCount + ",    " + customer.money);
    } else {
     System.out.println("   ,     ,      ");
     continue;
    }
    //   ,    
   } else {
    System.out.println("    " + customer.name + "      " + littleSuperMarket.superMarketName + "    " + customer.money);
   }


   //                      
   double totalCast = 0;
   int numToBuy;


   //    
   while (true) {


    System.out.println("    " + littleSuperMarket.merchandises.length + "   ,    。       :");


    int InDex = scanner.nextInt();
    //             
    if (InDex <= 0) {
     break;
    }
    if (InDex > littleSuperMarket.merchandises.length - 1) {
     System.out.println("   ,         。   0 " + (littleSuperMarket.merchandises.length - 1) + "     ");
     continue;
    }


    System.out.println("          :" + littleSuperMarket.merchandises[InDex].name + "\t   :" + littleSuperMarket.merchandises[InDex].soldPrice);
    System.out.println("       ?");

    numToBuy = scanner.nextInt();
    //             
    if (numToBuy <= 0) {
     System.out.println("      ,      ");
     continue;
    }
    if (numToBuy > littleSuperMarket.merchandises[InDex].count) {
     System.out.println("         ,      ");
     continue;
    }
    /*          ,      ,                  。                     ,      if               。
                           ,        ,    。               
     */

    //    ,     ,             
    totalCast += littleSuperMarket.merchandises[InDex].soldPrice * numToBuy;
    littleSuperMarket.merchandises[InDex].count -= numToBuy;


    //         ,                    
    if (customer.money < totalCast) {

     System.out.println("   ,              ,      ");
     //      ,                          
     //          ,      ,         ,        ,        ,     ,             ,       
     littleSuperMarket.merchandises[InDex].count += numToBuy;
     //              
     //          ,                           ,          ,           
     totalCast = 0;

    } else {
     //    ,        ,           
     littleSuperMarket.merchandisesold[InDex] += numToBuy;
     customer.money -= totalCast;

     //     ,    ,    
     if (customer.isDrivingCar) {
      littleSuperMarket.parkingCount++;
     }
    }
   }

   littleSuperMarket.incomingSum += totalCast;
   //      ,           totalCast   littleSuperMarket.incomingSum ,      
   //littleSuperMarket.incomingSum totalIncoming    ,       
   //littleSuperMarket.incomingSum += totalCast;

   System.out.println("  " + customer.name + "       " + totalCast + ",   " + customer.money);
   System.out.println("         ?");
   open = scanner.nextBoolean();
  }

  System.out.println("     !");
  System.out.println("        " + littleSuperMarket.incomingSum + "      :");


  //          
  double totalNetIncoming = 0;
  double totalIncoming = 0;


  for (int i = 0; i < littleSuperMarket.merchandisesold.length; i++) {
   //         0   
   if (littleSuperMarket.merchandisesold[i] > 0) {
    //          ,      
    double incoming = littleSuperMarket.merchandises[i].soldPrice * littleSuperMarket.merchandisesold[i];
    double netIncoming = (littleSuperMarket.merchandises[i].soldPrice - littleSuperMarket.merchandises[i].purchasePrice) * littleSuperMarket.merchandisesold[i];
    totalIncoming += incoming;
    totalNetIncoming += netIncoming;
    System.out.println(littleSuperMarket.merchandises[i].name + "   " + littleSuperMarket.merchandisesold[i] + " 。    " + incoming + ",    " + netIncoming);

   }
  }
  System.out.println("       :" + totalIncoming + ";       :" + totalNetIncoming);
 }
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기