Ms. Blanchett owns 5 apartment buildings. Each building contains 10 apartments that she rent out for RM 500 per month each. The program should output 12 payment coupons for each of the 10 apartments in each of the 5 buildings. Each coupon should contain the building number (1 through 5), the apartment number (1 through 10), the month (1 through 12), and the amount of rent due.
import java.util.*;
public class MyClass {
public static void main(String args[]) {
int building = 5;
int apartment = 10;
int costForMonth = 500;
int month = 12;
ArrayList<Building> buildings = new ArrayList<Building>();
ArrayList<Ticket> tickets = new ArrayList<Ticket>();
for(int i = 1; i<building+1; i++){
Building build = new Building(i);
for(int a = 1; a<apartment+1; a++){
Apartment apart = new Apartment(a);
apart.setCoastForMonth(costForMonth);
build.addApartment(apart);
for(int m = 1; m<month+1; m++){
tickets.add(new Ticket(build.getNumber(), apart.getNumber(), m, apart.getCostForMonth()));
}
}
}
for(Ticket t: tickets){
t.show();
}
}
private static class Building {
private List<Apartment> apartments;
private int number;
public Building(int number){
this.number = number;
apartments = new ArrayList<Apartment>();
}
public void addApartment(Apartment apartment){
apartments.add(apartment);
}
public int getNumber(){
return number;
}
}
private static class Apartment {
private int costForMonth = 0;
private int number;
public Apartment(int number){
this.number = number;
}
public void setCoastForMonth(int costForMonth){
this.costForMonth = costForMonth;
}
public int getNumber(){
return number;
}
public int getCostForMonth(){
return costForMonth;
}
}
private static class Ticket{
private int numberOfBuilding;
private int numberOfApartment;
private int month;
private int amountOfRent;
public Ticket(int numberOfBuilding, int numberOfApartment, int month, int amountOfRent){
this.numberOfBuilding = numberOfBuilding;
this.numberOfApartment = numberOfApartment;
this.month = month;
this.amountOfRent = amountOfRent;
}
public void show(){
System.out.printf("building number: %d - apartment number: %d - the month: %d - amount of rent: %d \n", numberOfBuilding, numberOfApartment, month, amountOfRent);
}
}
}
Comments
Leave a comment