← Vissza
A feladat forráskódja:
package nezoter;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
class Theatre {
private boolean free;
private int priceCat;
public Theatre(int priceCat) {
this.priceCat = priceCat;
this.free = true;
}
public boolean isFree() {
return free;
}
public int getPriceCat() {
return priceCat;
}
public void setFree(boolean free) {
this.free = free;
}
public int getPrice() {
switch (priceCat) {
case 1:
return 5000;
case 2:
return 4000;
case 3:
return 3000;
case 4:
return 2000;
case 5:
return 1500;
}
return -1;
}
}
/**
*
* @author comtur
*/
public class Nezoter {
private static Theatre[][] db;
public static void main(String[] args) {
task1();
task2();
task34();
task5();
task6();
task7();
}
private static void task1() {
taskNum(1);
System.out.println("(Állományok beolvasása és eltárolása)");
db = new Theatre[15][20];
try (BufferedReader br = new BufferedReader(new FileReader("kategoria.txt"))) {
String line = null;
int rowCount = 0;
int cat = 0;
while ((line = br.readLine()) != null) {
for (int i = 0; i < line.length(); i++) {
cat = Integer.parseInt(line.substring(i, i + 1));
Theatre theatre = new Theatre(cat);
db[rowCount][i] = theatre;
}
rowCount++;
}
} catch (IOException ex) {
Logger.getLogger(Nezoter.class.getName()).log(Level.SEVERE, null, ex);
System.err.println("Hiba!" + ex.getMessage());
}
try (BufferedReader br = new BufferedReader(new FileReader("foglaltsag.txt"))) {
String line = null;
int rowCount = 0;
char xo;
while ((line = br.readLine()) != null) {
for (int i = 0; i < line.length(); i++) {
xo = line.charAt(i);
if (xo == 'x') {
db[rowCount][i].setFree(false);
}
}
rowCount++;
}
} catch (IOException ex) {
Logger.getLogger(Nezoter.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\n----------");
}
private static void task2() {
taskNum(2);
Scanner sc = new Scanner(System.in);
System.out.print("Adja meg egy sor és egy szék számát, szóközzel elválasztva: ");
String[] rowChair = sc.nextLine().split(" ");
if (db[Integer.parseInt(rowChair[0]) - 1][Integer.parseInt(rowChair[1]) - 1].isFree()) {
System.out.println("A megadott hely szabad.");
} else {
System.out.println("A megadott hely foglalt.");
}
}
private static void task34() {
taskNum(3);
int sold = 0;
int sold1 = 0;
int sold2 = 0;
int sold3 = 0;
int sold4 = 0;
int sold5 = 0;
for (Theatre[] theatres : db) {
for (Theatre theatre : theatres) {
if (!theatre.isFree()) {
sold++;
switch (theatre.getPriceCat()) {
case 1:
sold1++;
break;
case 2:
sold2++;
break;
case 3:
sold3++;
break;
case 4:
sold4++;
break;
case 5:
sold5++;
break;
}
}
}
}
System.out.printf("Az előadásra eddig %d jegyet adtak el, ez a nézőtér %.0f%%-a.%n",
sold, sold * 100.0 / (db.length * db[0].length));
taskNum(4);
int max = Math.max(sold1, Math.max(sold2, Math.max(sold3, Math.max(sold4, sold5))));
if (sold1 == max) {
System.out.println("A legtöbb jegyet a(z) 1. árkategóriában értékesítették.");
}
if (sold2 == max) {
System.out.println("A legtöbb jegyet a(z) 2. árkategóriában értékesítették.");
}
if (sold3 == max) {
System.out.println("A legtöbb jegyet a(z) 3. árkategóriában értékesítették.");
}
if (sold4 == max) {
System.out.println("A legtöbb jegyet a(z) 4. árkategóriában értékesítették.");
}
if (sold5 == max) {
System.out.println("A legtöbb jegyet a(z) 5. árkategóriában értékesítették.");
}
}
private static void task5() {
taskNum(5);
int sumIncome = 0;
for (Theatre[] theatres : db) {
for (Theatre theatre : theatres) {
if (!theatre.isFree()) {
sumIncome += theatre.getPrice();
}
}
}
System.out.printf("A színház bevétele a pillanatnyilag eladott jegyek alapján %d Ft lenne.%n",
sumIncome);
}
private static void task6() {
taskNum(6);
int alone = 0;
for (int i = 0; i < db.length; i++) {
for (int j = 0; j < db[0].length; j++) {
if (db[i][j].isFree()) {
if (j == 0) {
if (!db[i][j + 1].isFree()) {
alone++;
}
} else if (j == db[0].length - 1) {
if (!db[i][j - 1].isFree()) {
alone++;
}
} else {
if ((!db[i][j + 1].isFree()) && (!db[i][j - 1].isFree())) {
alone++;
}
}
}
}
}
System.out.printf("A nézőtéren %d egyedülálló üres hely van.%n", alone);
}
private static void task7() {
taskNum(7);
System.out.println("(\"szabad.txt\" állomány elkészítése)");
try (PrintWriter pw = new PrintWriter("szabad.txt")) {
StringBuilder sb = null;
for (int i = 0; i < db.length; i++) {
sb = new StringBuilder();
for (int j = 0; j < db[i].length; j++) {
if (db[i][j].isFree()) {
sb.append(db[i][j].getPriceCat());
} else {
sb.append("x");
}
}
pw.println(sb);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Nezoter.class.getName()).log(Level.SEVERE, null, ex);
System.err.println("Hiba! " + ex.getMessage());
}
}
}