PDA

View Full Version : سوال: پر کردن یک Array دوبعدی با آبجکت یک کلاس در حالی که داده ها از یک فایل خوانده می شوند



inf1000
یک شنبه 23 مهر 1391, 15:01 عصر
دوستان عزیز سلام

من برنامه ای نوشته ام که در ابتدای اون به یک مشکل برخوردم و اون هم اینه که در اولین متد که خواندن از فایل تکست هست مشکلی وجود داره و من نمی دونم کجاست.
داده ها رو تک تک از فایل باید بخونه و بعد از یک کلاس آبجکت بسازه و در یک آرایه ذخیره کنه اما نمی کنه. نمی دونم مشکل پر شدن آرایه ار کجاست که پر نمی شه. ممنون می شم اگر کمکی بکنید. در زیر کل کد رو می تونین ببینین. :لبخندساده:




import java.util.*;
import java.io.*;


public class Oblig3{

public static void main(String[] args){
Apartment ap = new Apartment();
ap.readfromfile();
ap.menu();
System.out.println("\nThank you for visiting us");
System.out.println("\n * End of the program *");
}
}

class Flat{
Student s;
boolean tf;
int rent;
}


class Student{
String name;
int balance;

Student(String name , int balance){
this.name = name;
this.balance = balance;
}
}

class Apartment{
String name;
int balance, m, y, totalearn, totalmonth, normalrent, topfloorrent, floor, room;
char ch;
Scanner df = new Scanner(System.in);
Flat[][] flat = new Flat[3][6];

void readfromfile(){
try{
Scanner datafile = new Scanner(new File("hybeldata.txt"));
datafile.useDelimiter(";|\\n");
m = datafile.nextInt();
y = datafile.nextInt();
totalearn = datafile.nextInt();
totalmonth = datafile.nextInt();
normalrent = datafile.nextInt();
topfloorrent = datafile.nextInt();
while(datafile.hasNext()){
datafile.nextLine();
floor = datafile.nextInt();
System.out.println(floor);
ch = datafile.next().charAt(0);
System.out.println(ch);
balance = datafile.nextInt();
System.out.println(balance);
name = datafile.next();
System.out.println(name);
room = char2num(ch);
flat[floor-1][room].s = new Student(name,balance);
}

datafile.close();
}
catch(Exception e){}
}

void menu(){
int num = 0;
while (num < 8){

System.out.println("\n\n");
System.out.println("* Welcome to the Gulbrand Grås Student Housing *");
System.out.println("\t\n Please choose your favorite option:");
System.out.println("\n 1. Dispaly overview of all flats");
System.out.println(" 2. Registeration");
System.out.println(" 3. Payment");
System.out.println(" 4. Voluntary emigiration");
System.out.println(" 5. Month running tenancy");
System.out.println(" 6. Throw out the tenant");
System.out.println(" 7. Increase rent");
System.out.println(" 8. Exit\n");
num = chooseoption();

switch(num){
case 1: overview();
break;
case 2: registration();
break;
case 3: payment();
break;
case 4: voluntaryemigration();
break;
case 5: monthruntenancy();
break;
case 6: throwout();
break;
case 7: increasrent();
break;
default: writetofile();
break;
}
}
}

int chooseoption(){
System.out.print("Option No.: ");
int c = df.nextInt();
return c;
}

void overview(){

System.out.println("\n\n");
System.out.println(" Flat Tenant Balance");
System.out.println(" ---- ------------------- ----------");
for(int i = 0; i < flat.length; i++){
for(int j = 0; j< flat[i].length; j++){
System.out.print(" " + i + num2char(j) + " " + flat[i][j] + " " + flat[i][j].s.balance + "\n");
}
}
System.out.println("Month/Year: " + m + "/" + y + "\tRunning time: " + totalmonth + "\nTotal income: " + totalearn);
}

void registration(){
int freeroom = 0;
for(int i = 0; i < flat.length; i++){
for(int j = 0; j < flat[i].length; j++){
if(flat[i][j].s.name.equals("Vacant Room")){
freeroom += 1;
System.out.println("Vacant room(s) at floor No. " + i + " is(are) " + num2char(j) + ".");
}
}
}

if(freeroom == 0){
System.out.println("OBS! There is no more vacant flat!\nThank you for choosing us :)");
return;
}
else{

System.out.print("\nWhich floor do prefere? (please see the map and enter the floor number) ");
int ff = df.nextInt();
System.out.print("\nPlease choose your room: ");
char ch = df.next(".").charAt(0);
if(flat[ff][char2num(ch)].s.name.equals("Vacant Room")){
System.out.print("\nPlease enter your name: ");
String name = df.next();
System.out.print("\nPlease choose your room: ");
flat[ff][char2num(ch)].s.name = name;
if(ff == 3){
flat[ff][char2num(ch)].tf = true;
totalearn = totalearn + flat[ff][char2num(ch)].rent;
flat[ff][char2num(ch)].s.balance = flat[ff][char2num(ch)].s.balance - flat[ff][char2num(ch)].rent;
}
else{
flat[ff][char2num(ch)].tf = true;
totalearn = totalearn + flat[ff][char2num(ch)].rent;
flat[ff][char2num(ch)].s.balance = flat[ff][char2num(ch)].s.balance - flat[ff][char2num(ch)].rent;
}
System.out.println("\nRomm number " + ff + ch + " is rented to " + name + ".");
System.out.println("Your deposite is 15000 NOK, please pay it now.");
}
else System.out.println("This room has already a tenant!");
}
}

void payment(){
boolean count = true;
while (count){
System.out.print("\nPlease enter your floor number: ");
int f = df.nextInt();
System.out.print("\nPlease enter your room character: ");
char c = df.next(".").charAt(0);
int ch = char2num(c);
System.out.print("\nPlease enter your name: ");
String n = df.next();
if(flat[f][ch].s.name.equals(n)){
System.out.print("\nPlease enter the amount which you want to pay: ");
int pay = df.nextInt();
flat[f][ch].s.balance = flat[f][ch].s.balance + pay;
System.out.println("\nYour total balance is " + flat[f][ch].s.balance + ".");
count = false;
}
else{
System.out.println("Room number and tenant name doesn't match or exist!");
System.out.println("Which of the following options:\n1. Try again\n2. Main menu ");
int y = df.nextInt();
if(y == 2) count = false;
}
}
}

void voluntaryemigration(){

boolean l = true;
while (l){
System.out.print("\nPlease enter your floor number: ");
int f = df.nextInt();
System.out.print("\nPlease enter your room character: ");
char c = df.next(".").charAt(0);
int ch = char2num(c);
System.out.print("\nPlease enter your name: ");
String n = df.next();
if(flat[f][ch].s.name.equals(n)){
flat[f][ch].s.name = "Vacant Room";
if(f == 3){
flat[f][ch].tf = true;
totalearn = totalearn + flat[f][ch].rent;
int returnedmoney = flat[f][ch].s.balance - flat[f][ch].rent;
flat[f][ch].s.balance = 0;
System.out.println("Tenant " + n + " has sucssesfuly removed from the system and the amount of " + returnedmoney + " NOK has payed back.");
}
else{
flat[f][ch].tf = false;
totalearn = totalearn + flat[f][ch].rent;
int returnedmoney = flat[f][ch].s.balance - flat[f][ch].rent;
flat[f][ch].s.balance = 0;
System.out.println("Tenant " + n + " has sucssesfuly removed from the system and the amount of " + returnedmoney + " NOK has payed back.");
}
l = false;
}
else{
System.out.println("Room number and tenant name doesn't match or exist!");
System.out.println("Which of the following options:\n1. Try again\n2. Main menu ");
int y = df.nextInt();
if(y == 2) l = false;
}
}
}

void monthruntenancy(){

int monthlyearn = 0;
int monthlytax = (1200*18)+(1700*3);
int numofstudent = 0;
String name;
if(m == 12){
System.out.print("Date is " + m + "/" + y + ".\nDo you want to run for the 1/" + (y+1) + "? (Y/N) ");
m = 1;
y++;
char ans = df.next(".").charAt(0);
if(ans == 'Y' || ans == 'y'){
for(int i=0; i<flat.length; i++){
for(int j=0; j<flat[i].length; j++){
name = flat[i][j].s.name;
if(!name.equals("Vacant Room")){
if(i == 3){
flat[i][j].s.balance -= flat[i][j].rent;
monthlyearn += flat[i][j].rent;
totalmonth++;
numofstudent++;
}
else{
flat[i][j].s.balance -= flat[i][j].rent;
monthlyearn += flat[i][j].rent;
totalmonth++;
numofstudent++;
}
}
}
}
}
}
else{
System.out.print("Date is " + m + "/" + y + ".\nDo you want to run for the " + (m+1) + "/" + y + "? (Y/N)");
char ans = df.next(".").charAt(0);
if(ans == 'Y' || ans == 'y'){
for(int i=0; i<flat.length; i++){
for(int j=0; j<flat[i].length; j++){
if(!flat[i][j].s.name.equals("Vacant Room")){
if(i == 3){
flat[i][j].s.balance -= flat[i][j].rent;
monthlyearn += flat[i][j].rent;
totalmonth++;
numofstudent++;
}
else{
flat[i][j].s.balance -= flat[i][j].rent;
monthlyearn += flat[i][j].rent;
totalmonth++;
numofstudent++;
}
}
}
}
totalearn += monthlyearn - monthlytax;
System.out.println("The tola month running tenancy are " + totalmonth + " and there are " + numofstudent + " students in the apartment.");
System.out.println("Monthly Report:");
System.out.println("Month: " + m + "\tYear: " + y + "\tNormal rent: " + flat[2][1].rent + "\tTop floor rent: " + flat[3][1]);
System.out.println("Total income: " + totalearn + "\tCurrent month income: " + monthlyearn + "\tExpenses: " + monthlytax + "\tAverage income: " + (totalearn/totalmonth));
}
}
}

void increasrent(){

System.out.println("The current rents are:\nNormal rent: " + flat[1][1].rent + "\nTop floor rent: " + flat[3][1].rent);
System.out.println("Do you want to increase the rents?(Y/N)");
char an = df.next(".").charAt(0);
if(an == 'Y' || an == 'y'){
System.out.print("Which rent do you want to increase(N:normal, T:topfloor)?(N/T)");
char d = df.next(".").charAt(0);
if(d == 'N' || d == 'n'){
System.out.print("\nPlease enter your amount for the normal rent: ");
int r1 = df.nextInt();
for(int i=0; i < flat.length; i++){
for(int j=0; j < flat[i].length; j++){
if (i < 3){
flat[i][j].rent = r1;
}
}
}
}
else{
System.out.print("\nPlease enter your amount for the top floor rent: ");
int r2 = df.nextInt();
for(int i=0; i < flat.length; i++){
for(int j=0; j < flat[i].length; j++){
if (i == 3){
flat[i][j].rent = r2;
}
}
}

}
System.out.println("\nYour new rents are:\nNormal rent: " + flat[1][1].rent + "\nTop floor rent: " + flat[3][1].rent);
}
}

void throwout(){
int geb = 3000;
for(int i = 0; i < flat.length; i++){
for(int j = 0; j < flat[i].length; j++){
if(!flat[i][j].s.name.equals("Vacant Room")){
if(flat[i][j].s.balance < - flat[i][j].rent){
totalearn = totalearn - flat[i][j].s.balance + (geb/2);
char a = num2char(j);
tocallHole(i,a,(geb/2));
System.out.println("Dear " + flat[i][j] + "! You haven't paied your rent for more than two months and your balance is " + flat[i][j].s.balance + ". Therefore we have to say that you don't have the room any more and it has been vacant for new student.");
}
}
}
}
}



char num2char(int floor){
char room = (char) ('A' + floor);
return room;
}


int char2num(char roomnum){
int floor = (int) (roomnum - 'A');
return floor;
}


boolean Topfloor(){
boolean r = true;
for(int i = 0; i < flat.length; i++){
for(int j = 0; j < flat[i].length; j++){
if(i == 3) {
flat[i][j].rent = 7000;
}
else{
flat[i][j].rent = 6000;
r = false;
}
}
}
return r;
}

void tocallHole(int floor, char room, int demand){
FileWriter pw = null;
try{
pw = new FileWriter("Torpodo.txt",true);
pw.write(floor);
pw.write(";");
pw.write(room);
pw.write(";");
pw.write(demand);
pw.write("\n");
pw.close();
}
catch(Exception e){}
}


void writetofile(){
FileWriter pw = null;
try{
pw = new FileWriter("Apartment.txt");
pw.write(m);
pw.write(";");
pw.write(y);
pw.write(";");
pw.write(totalearn);
pw.write(";");
pw.write(totalmonth);
pw.write(";");
pw.write(flat[1][1].rent);
pw.write(";");
pw.write(flat[3][1].rent);
pw.write("\n");
for(int i=0; i< flat.length; i++){
for(int j = 0; j<flat[i].length; j++){
pw.write(i);
pw.write(";");
pw.write(num2char(j));
pw.write(";");
pw.write(flat[i][j].s.name);
pw.write(";");
pw.write(flat[i][j].s.balance);
pw.write("\n");
}
}
pw.close();
}
catch(Exception e){}
}
}

همایون افشاری
دوشنبه 08 آبان 1391, 18:50 عصر
باید حاصل اجرای برنامه رو هم بزارید تا ببینیم مشکل کجاست.

spiderman200700
دوشنبه 08 آبان 1391, 19:58 عصر
ساختار و محتویات فایل رو هم باید دید تا بشه نظر داد