Добрый день! Недавно мне прислали тестовое задание на должность автотестера джун +. Что-то накодил, отправил работодателю. Пока ответа нет, но интересно Ваше мнение. Вот собственно задание:
Implement a solution to the following problem in Java. We are looking for clean, well-factored, OO code.
You do not need to provide any form of persistence in this program. Your project should contain some way of running automated tests to prove it works, whether you use jUnit or some other way is up to you.
The program should be an API. You can opt to put a user interface on it or not, but we will only be looking at the API portion.Here are the requirements:
Consider a grocery market where items have prices per unit but also volume prices. For example, doughnuts may be $1.25 each or 3 for $3 dollars.
Implement a point-of-sale scanning API that accepts an arbitrary ordering of products (similar to what would happen when actually at a checkout line) then returns the correct total price for an entire shopping cart based on the per unit prices or the volume prices as applicable.Here are the products listed by code and the prices to use (there is no sales tax):
Product Code Price
A $1.25 each or 3 for $3.00
B $4.25
C $1.00 or $5 for a six pack
D $0.75
The interface at the top level PointOfSaleTerminal service object should look something like this. You are free to design/implement the rest of the code however you wish, including how you specify the prices in the system:
PointOfSaleTerminal terminal = new PointOfSaleTerminal(); terminal.setPricing(…); terminal.scan(“A”);
terminal.scan(“C”); … etc.
BigDecimal result = terminal.calculateTotal();
Here are the minimal inputs you should use for your test cases. These test cases must be shown to work in your program: Scan these items in this order: ABCDABA; Verify the total price is $13.25. Scan these items in this order: CCCCCCC; Verify the total price is $6.00. Scan these items in this order: ABCD; Verify the total price is $7.25
И вот мое решение:
public class PointOfSaleTerminal {
public String productList(String s) {
char[] productList = s.toCharArray();
// считаем количество а, b, с и d
int numberA = 0;
int numberB = 0;
int numberC = 0;
int numberD = 0;
for (int x = 0; x < productList.length; x++) {
if (productList[x] == 'A')
numberA++;
if (productList[x] == 'B')
numberB++;
if (productList[x] == 'C')
numberC++;
if (productList[x] == 'D')
numberD++;
// создаем исключения для несуществующего товара
if (productList[x] != 'A') {
if (productList[x] != 'B') {
if (productList[x] != 'C') {
if (productList[x] != 'D') {
System.out.println("Данного товара нет в списке");
}
}
}
}
}
// Остаток от деления количества а на 3
int restA = numberA % 3;
// Остаток от деления количества c на 6
int restC = numberC % 6;
// cчитаем стоимость всех товаров по наименованиям
double costA = ((numberA - restA) / 3) * 3 + (restA) * 1.25;
double costB = numberB * 4.25;
double costC = ((numberC - restC) / 6) * 5 + restC * 1;
double costD = numberD * 0.75;
// получаем общий результат
double TotalResult = costA + costB + costC + costD;
String str;
str = Double.toString(TotalResult);
return str;
}
}
import org.junit.*;
public class Testing {
@Test
public void Test() {
PointOfSaleTerminal terminal = new PointOfSaleTerminal();
String list = terminal.productList("ABCDABA");
Assert.assertEquals(list, "13.25");
}
@Test
public void Test1() {
PointOfSaleTerminal terminal = new PointOfSaleTerminal();
String list = terminal.productList("CCCCCCC");
Assert.assertEquals(list, "6.0");
}
@Test
public void Test2() {
PointOfSaleTerminal terminal = new PointOfSaleTerminal();
String list = terminal.productList("ABCD");
Assert.assertEquals(list, "7.25");
}
}
Мне понятно, что если бы я шел на дева, то пришлось бы создавать кучу объектов с замысловатой логикой, но как для тестировщика вроде и этого должно хватить. Надеюсь на Ваши советы по улучшению кода.