Работает, но не принимает. А это значит, что я с высокой вероятностью сделал где-то что-то некорректно.
Прошу прощения за пространность кода. Размахнулся как умею.
Помогите, пожалуйста.
package com.javarush.task.task18.task1827;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
public class Item {
private int quantity;
private double price10;
private String productName;
private int Id;
public Item(String productName, double price, int quantity) {
this.price10 = price*1.00;
this.productName = ( productName.toCharArray().length > 30 ) ? productName.substring(0, 30) : productName;
this.quantity = quantity;
}
public int getQuantity() {
return this.quantity;
}
public double getPrice() {
return this.price10;
}
public String getProductName() {
return this.productName;
}
public void setId(int Id) {
this.Id = Id;
}
public int getId() {
return this.Id;
}
public void record(String fileName) throws Exception {
FileOutputStream fileOutputStream = new FileOutputStream(fileName, true);
BufferedOutputStream bufferOut = new BufferedOutputStream(fileOutputStream, 2);
byte[] IdAsBytes = Integer.toString(this.getId()).getBytes();
byte[] ProductNameAsByte = this.getProductName().getBytes();
byte[] PriceAsByte = String.format("%-8.2f", this.getPrice()).replace(',','.').getBytes();
byte[] QuantityAsByte = Integer.toString(this.getQuantity()).getBytes();
bufferOut.write("\r\n".getBytes());
if (IdAsBytes.length > 8) bufferOut.write(requestArray(IdAsBytes, 8));
else {
bufferOut.write(IdAsBytes);
bufferOut.write(addSpaceToValue(8 - IdAsBytes.length));
}
if (ProductNameAsByte.length > 60) bufferOut.write(requestArray(ProductNameAsByte, 60));
else {
bufferOut.write(ProductNameAsByte);
bufferOut.write(addSpaceToValue(30 - this.getProductName().toCharArray().length));
}
if (PriceAsByte.length > 8) bufferOut.write(requestArray(PriceAsByte, 8));
else {
bufferOut.write(PriceAsByte);
bufferOut.write(addSpaceToValue(8 - PriceAsByte.length));
}
if (QuantityAsByte.length > 4) bufferOut.write(requestArray(QuantityAsByte, 8));
else {
bufferOut.write(QuantityAsByte);
bufferOut.write(addSpaceToValue(4 - QuantityAsByte.length));
}
bufferOut.close();
fileOutputStream.close();
}
public byte[] addSpaceToValue(int spaceNumber) {
byte[] array = new byte[spaceNumber];
for (int i = 0; i < array.length; i++) {
array[i] = 32;
}
return array;
}
public byte[] requestArray(byte[] array, int rightSize) {
byte[] arr = new byte[rightSize];
for (int i = 0; i < arr.length; i++) {
arr[i] = array[i];
}
return arr;
}
}