← Vissza
A feladat forráskódja:
package otszaz;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
class Buyer {
private List<Item> products;
public Buyer() {
this.products = new ArrayList<>();
}
public void addItem(Item item) {
String itemName = item.getName();
boolean contains = false;
for (Item product : products) {
if (product.getName().equals(itemName)) {
int price = product.getPrice();
if (price == 500) {
product.setPrice(950);
} else if (price > 500) {
product.setPrice(price + 400);
}
product.setPiece(product.getPiece() + 1);
contains = true;
}
}
if (!contains) {
products.add(item);
}
}
public List<Item> getProducts() {
return products;
}
public boolean containsString(String str) {
for (Item product : products) {
if (product.getName().equals(str)) {
return true;
}
}
return false;
}
public void writeInfo() {
for (Item product : products) {
System.out.printf("%d %s%n", product.getPiece(), product.getName());
}
}
public int getPrice() {
int price = 0;
for (Item product : products) {
price += product.getPrice();
}
return price;
}
}
class Item {
private String name;
private int piece;
private int price;
public Item(String name) {
this.name = name;
this.piece = 1;
this.price = 500;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPiece() {
return piece;
}
public void setPiece(int piece) {
this.piece = piece;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
/**
*
* @author comtur
*/
public class Otszaz {
private static List<Buyer> buyerList;
private static int buyerNo;
private static String productName;
private static int buyPiece;
public static void main(String[] args) {
task1();
task2();
task3();
task4();
task5();
task6();
task7();
task8();
}
private static void task1() {
taskNum(1);
System.out.println("(Adatok beolvasása)");
buyerList = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader("penztar.txt"))) {
Buyer buyer = new Buyer();
String line = null;
while ((line = br.readLine()) != null) {
if (line.equals("F")) {
buyerList.add(buyer);
buyer = new Buyer();
} else {
buyer.addItem(new Item(line));
}
}
} catch (IOException ex) {
Logger.getLogger(Otszaz.class.getName()).log(Level.SEVERE, null, ex);
System.err.println("Hiba! " + ex.getMessage());
}
}
private static void taskNum(int i) {
System.out.println("\n" + i + ". feladat");
System.out.println("----------");
}
private static void task2() {
taskNum(2);
System.out.println("A fizetések száma: " + buyerList.size());
}
private static void task3() {
taskNum(3);
System.out.println("Az első vásárló " + buyerList.get(0).getProducts().size() + " db árucikket vásárolt.");
}
private static void task4() {
taskNum(4);
Scanner sc = new Scanner(System.in);
System.out.print("Adja meg egy vásárlás sorszámát! ");
buyerNo = sc.nextInt();
sc.nextLine();
System.out.print("Adja meg egy árucikk nevét! ");
productName = sc.nextLine();
System.out.print("Adja meg a vásárolt darabszámot! ");
buyPiece = sc.nextInt();
sc.nextLine();
}
private static void task5() {
taskNum(5);
int first = 0;
int last = 0;
int count = 0;
for (int i = 0; i < buyerList.size(); i++) {
if (buyerList.get(i).containsString(productName)) {
if (first == 0) {
first = i;
}
last = i;
count++;
}
}
System.out.println("Az első vásárlás sorszáma: " + (first + 1));
System.out.println("Az utolsó vásárlás sorszáma: " + (last + 1));
System.out.println(count + " vásárlás során vettek belőle.");
}
private static void task6() {
taskNum(6);
int fizetendo = ertek(buyPiece);
System.out.println(buyPiece + " darab vételekor fizetendő: " + fizetendo);
}
private static int ertek(int buyPiece) {
switch (buyPiece) {
case 0:
return 0;
case 1:
return 500;
case 2:
return 950;
default:
return 950 + (buyPiece - 2) * 400;
}
}
private static void task7() {
taskNum(7);
buyerList.get(buyerNo - 1).writeInfo();
}
private static void task8() {
taskNum(8);
System.out.println("(Kimeneti fájl készítése)");
try (PrintWriter pw = new PrintWriter("osszeg.txt")) {
for (int i = 0; i < buyerList.size(); i++) {
pw.println((i + 1) + ": " + buyerList.get(i).getPrice());
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Otszaz.class.getName()).log(Level.SEVERE, null, ex);
System.err.println("Hiba! " + ex.getMessage());
}
}
}