ざっくりオブジェクト指向プログラミング解説:サンプルコード #4実践編①
こちらは別サイトの動画の『ざっくりオブジェクト指向プログラミング解説』で使用したサンプルコードを公開する記事になっています。
動画をご覧になっていない方には、ほぼ意味不明の記事ですのでその点ご了承ください。
1.1 Main ー第2章ソースコードー
オブジェクト指向要素のないMainクラス。
import Basket.BasicInformation;
import Basket.PurchasedGoods;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class Main {
public final static String HR = "===============================";
public final static String HR2 = " ----------------------------- ";
public static void main(String[] args) {
//レシート印字用のデータ生成
BasicInformation basicInformation = getBasicInformation();
List<PurchasedGoods> goodsList = getGoodsList();
//ここからがレシート処理
//header
System.out.println();
System.out.println(lineFormatCenter(basicInformation.getStoreName()));
System.out.println(basicInformation.getBranchName());
System.out.println();
System.out.println(HR);
//contents
String printStr = null;
BigDecimal sum = new BigDecimal("0");
for(PurchasedGoods goods : goodsList){
sum = sum.add(goods.getPrice());
printStr = lineFormatGoodsRow(goods.getGoodsName(), goods.getPrice());
System.out.println(printStr);
}
System.out.println(HR2);
printStr = lineFormatGoodsRow("合計", sum);
System.out.println(printStr);
System.out.println(HR2);
basicInformation.getPaymentAmount().intValue();
printStr = lineFormatGoodsRow("お預り", basicInformation.getPaymentAmount());
System.out.println(printStr);
BigDecimal change = basicInformation.getPaymentAmount().subtract(sum);
printStr = lineFormatGoodsRow("お釣り", change);
System.out.println(printStr);
//footer
System.out.println(HR);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh時mm分ss秒");
System.out.println(sdf.format(basicInformation.getPurchaseTime()));
}
//テスト用のBasicInformationを生成する
private static BasicInformation getBasicInformation(){
Calendar c = Calendar.getInstance();
c.set(2019,1,10,12,34,50);
return new BasicInformation("イタコ食堂","恐山支店",c.getTime(),"2000");
}
//テスト用のPurchasedGoodsリストを生成する
private static List<PurchasedGoods> getGoodsList(){
ArrayList<PurchasedGoods> goodsList = new ArrayList<PurchasedGoods>();
goodsList.add(new PurchasedGoods("あじの盛り塩焼き","500"));
goodsList.add(new PurchasedGoods("水の子サラダ","250"));
goodsList.add(new PurchasedGoods("お神酒","300"));
return goodsList;
}
private static String lineFormatGoodsRow(String gName, BigDecimal gPrice){
final int gNameLen = 20;
final int gPriceLen = 6;
int diffStrLen = 0;
for(char c : gName.toCharArray()){
if(String.valueOf(c).getBytes().length > 1){
//全角の場合
diffStrLen++;
}
}
return String.format(" %-"+ (gNameLen - diffStrLen) +"s %" + gPriceLen + "d円 ",gName,gPrice.intValue());
}
private static String lineFormatCenter(String orgStr){
final int strLen = 31;
int diffStrLen = 0;
for(char c : orgStr.toCharArray()){
if(String.valueOf(c).getBytes().length > 1){
//全角の場合
diffStrLen++;
}
}
int preSpCnt = (strLen - orgStr.length() - diffStrLen) / 2;
//String displayStr = IntStream.range(0, preSpCnt).mapToObj(i -> " ").collect(Collectors.joining("", "", orgStr));
StringBuffer displayStr = new StringBuffer();
for(int i = 0 ; i < preSpCnt; i++) {
displayStr.append(" ");
}
displayStr.append(orgStr);
return String.format("%-"+ (strLen - diffStrLen) +"s",displayStr);
}
}
1.2 BasicInformation ー第2章ソースコードー
店舗の基礎的な情報を保持するクラス。
package Basket;
import java.math.BigDecimal;
import java.util.Date;
public class BasicInformation {
private String storeName = null;
private String branchName = null;
private Date purchaseTime = null;
private BigDecimal paymentAmount = null;
public BasicInformation(){
}
public BasicInformation(String storeName, String branchName,
Date purchaseTime, String paymentAmount){
setStoreName(storeName);
setBranchName(branchName);
setPurchaseTime(purchaseTime);
setPaymentAmount(paymentAmount);
}
public String getStoreName() {
return storeName;
}
public void setStoreName(String storeName) {
this.storeName = storeName;
}
public String getBranchName() {
return branchName;
}
public void setBranchName(String branchName) {
this.branchName = branchName;
}
public Date getPurchaseTime() {
return purchaseTime;
}
public void setPurchaseTime(Date purchaseTime) {
this.purchaseTime = purchaseTime;
}
public BigDecimal getPaymentAmount() {
return paymentAmount;
}
public void setPaymentAmount(BigDecimal paymentAmount) {
this.paymentAmount = paymentAmount;
}
public void setPaymentAmount(String paymentAmount) {
this.paymentAmount = new BigDecimal(paymentAmount);
}
}
1.3 PurchasedGoods ー第2章ソースコードー
購入商品の情報を保持するクラス。
package Basket;
import java.math.BigDecimal;
public class PurchasedGoods {
private String goodsName = null;
private BigDecimal price = null;
public PurchasedGoods(String goodsName, String price){
this.goodsName = goodsName;
this.price = new BigDecimal(price);
}
public String getGoodsName() {
return goodsName;
}
public BigDecimal getPrice() {
return price;
}
}
2.1 Main ー第3章ソースコードー
役割分担後のMain。
import Basket.BasicInformation;
import Basket.PurchasedGoods;
import receipt.nooop.ReceiptController;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class Main {
public static void main(String[] args) {
BasicInformation basicInformation = getBasicInformation();
List<PurchasedGoods> goodsList = getGoodsList();
ReceiptController receiptController = new ReceiptController();
receiptController.execute(basicInformation,goodsList);
}
//テスト用のBasicInformationを生成する
private static BasicInformation getBasicInformation(){
Calendar c = Calendar.getInstance();
c.set(2019,1,10,12,34,50);
return new BasicInformation("イタコ食堂","恐山支店",c.getTime(),"2000");
}
//テスト用のPurchasedGoodsリストを生成する
private static List<PurchasedGoods> getGoodsList(){
ArrayList<PurchasedGoods> goodsList = new ArrayList<PurchasedGoods>();
goodsList.add(new PurchasedGoods("あじの盛り塩焼き","500"));
goodsList.add(new PurchasedGoods("水の子サラダ","250"));
goodsList.add(new PurchasedGoods("お神酒","300"));
return goodsList;
}
}
2.2 ReceiptController ー第3章ソースコードー
役割分担で追加したクラス。
package receipt.nooop;
import Basket.BasicInformation;
import Basket.PurchasedGoods;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;
public class ReceiptController {
public final static String HR = "===============================";
public final static String HR2 = " ----------------------------- ";
public void execute(BasicInformation basicInformation, List<PurchasedGoods> purchasedGoodsList){
//header
createHeader(basicInformation);
//contents
createContents(basicInformation, purchasedGoodsList);
//footer
createFooter(basicInformation);
}
private void createHeader(BasicInformation basicInformation) {
//header
System.out.println();
System.out.println(lineFormatCenter(basicInformation.getStoreName()));
System.out.println(basicInformation.getBranchName());
System.out.println();
System.out.println(HR);
}
private void createContents(BasicInformation basicInformation, List<PurchasedGoods> purchasedGoodsList) {
//contents
String printStr = null;
BigDecimal sum = new BigDecimal("0");
for(PurchasedGoods goods : purchasedGoodsList){
sum = sum.add(goods.getPrice());
printStr = lineFormatGoodsRow(goods.getGoodsName(), goods.getPrice());
System.out.println(printStr);
}
System.out.println(HR2);
printStr = lineFormatGoodsRow("合計", sum);
System.out.println(printStr);
System.out.println(HR2);
basicInformation.getPaymentAmount().intValue();
printStr = lineFormatGoodsRow("お預り", basicInformation.getPaymentAmount());
System.out.println(printStr);
BigDecimal change = basicInformation.getPaymentAmount().subtract(sum);
printStr = lineFormatGoodsRow("お釣り", change);
System.out.println(printStr);
}
private void createFooter(BasicInformation basicInformation) {
//footer
System.out.println(HR);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh時mm分ss秒");
System.out.println(sdf.format(basicInformation.getPurchaseTime()));
}
private String lineFormatGoodsRow(String gName, BigDecimal gPrice){
final int gNameLen = 20;
final int gPriceLen = 6;
int diffStrLen = 0;
for(char c : gName.toCharArray()){
if(String.valueOf(c).getBytes().length > 1){
//全角の場合
diffStrLen++;
}
}
return String.format(" %-"+ (gNameLen - diffStrLen) +"s %" + gPriceLen + "d円 ",gName,gPrice.intValue());
}
private String lineFormatCenter(String orgStr){
final int strLen = 31;
int diffStrLen = 0;
for(char c : orgStr.toCharArray()){
if(String.valueOf(c).getBytes().length > 1){
//全角の場合
diffStrLen++;
}
}
int preSpCnt = (strLen - orgStr.length() - diffStrLen) / 2;
//String displayStr = IntStream.range(0, preSpCnt).mapToObj(i -> " ").collect(Collectors.joining("", "", orgStr));
StringBuffer displayStr = new StringBuffer();
for(int i = 0 ; i < preSpCnt; i++) {
displayStr.append(" ");
}
displayStr.append(orgStr);
return String.format("%-"+ (strLen - diffStrLen) +"s",displayStr);
}
}
2.3 BasicInformation ー第3章ソースコードー
変更無いため省略。
「1.2 BasicInformation ー第2章ソースコードー」を参照のこと。
2.4 PurchasedGoods ー第3章ソースコードー
変更無いため省略。
「1.3 PurchasedGoods ー第2章ソースコードー」を参照のこと。
この記事が気に入ったらサポートをしてみませんか?