자바 간단 한 마 트 프로그램 구현
인물 가방
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);
}
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.