Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm having trouble with my Java code. When I run it in the CMD compiler no problems pop up but it seems like its not

I'm having trouble with my Java code. When I run it in the CMD compiler no problems pop up but it seems like its not grabbing the data from the rentalDB.txt document that I need it to. Please help!

public class RentalDueTest { public static void main(String[] args){ DBHandler dbHandler = new DBHandler(); dbHandler.setupDB("C:\\Program Files\\Java\\Notepad++\\Labs\\Lab2\ entalDB.txt");

//for testing ApartmentRental appt = new ApartmentRental(); appt.outputCurrentRent(dbHandler.getRentalProperties()); SingleFamilyRental sing = new SingleFamilyRental(); sing.outputCurrentRent(dbHandler.getRentalProperties()); } }

-----------------------------------------------------------------------------------------------------------------------------------------------------------

import java.util.ArrayList;

public class SingleFamilyRental extends RentalProperty implements Payment{ public SingleFamilyRental() {} public SingleFamilyRental(String rentalID, String numberRooms) { super.rentalType = "S"; super.rentalID = rentalID; super.numberRooms = numberRooms; }

public void outputCurrentRent(ArrayList rentalProperties) { // prints out a formatted list of home IDs and their associated rents

System.out.print("Single Family Rental Summary: House ID Number Rent Amount Due ================ =============== "); for (int i=0 ; i RentalProperty property = (RentalProperty) rentalProperties.get(i);

String outputFormatString; String rent = "...";

if (property.getRentalType().equals("S")) { if (property.getNumberRooms().equals("1")) { rent = " $936.00"; } else if (property.getNumberRooms().equals("2")) { rent = "$1248.00"; } else if (property.getNumberRooms().equals("3")) { rent = "$1456.00"; } outputFormatString = " " + property.getRentalID() + " " + rent; System.out.println(outputFormatString); } } System.out.println(" "); } }

------------------------------------------------------------------------------------------------------------------------------------------

import java.util.ArrayList;

public class ApartmentRental extends RentalProperty implements Payment{ public ApartmentRental(){} public ApartmentRental (String rentalID, String numberRooms) { super.rentalType = "A"; super.rentalID = rentalID; super.numberRooms = numberRooms; }

public void outputCurrentRent(ArrayList rentalProperties) { //prints a formatted list of Apartment IDs and their associated rent

System.out.print("Apartment Rental Summary: Apartment ID No. Rent Amount Due ================ =============== "); for (int i=0 ; i RentalProperty property = (RentalProperty) rentalProperties.get(i);

String outputFormatString; String rent = " ......."; if (property.getRentalType().equals("A")) { if (property.getNumberRooms().equals("1")) { rent = " $648.00"; } else if (property.getNumberRooms().equals("2")) { rent = " $972.00"; } outputFormatString = " " + property.getRentalID() + " " + rent; System.out.println(outputFormatString); } } System.out.println(" "); } }

-------------------------------------------------------------------------------------------------------------------------------------------

import java.io.File; import java.util.ArrayList; import java.util.Scanner;

public class DBHandler {

private File file; private ArrayList rentalProperties = new ArrayList<>();

public void setupDB(String fileString) { //This will take a .txt database and parse it into a ArrayList file = new File(fileString); try { Scanner fileScan = new Scanner("C:\\Program Files\\Java\\Notepad++\\Labs\\Lab2\ entalDB.txt"); while (fileScan.hasNextLine()) { String fileLine = fileScan.nextLine(); String[] prop = fileLine.split(" "); String propertyType = prop[0]; if (propertyType.equals("A")) { ApartmentRental a = new ApartmentRental(prop[1], prop[2]); rentalProperties.add(a); } else if(propertyType.equals("S")) { SingleFamilyRental s = new SingleFamilyRental(prop[1], prop[2]); rentalProperties.add(s); } } } catch (Exception ex) { ex.printStackTrace(); } }

public ArrayList getRentalProperties() { return rentalProperties; } }

---------------------------------------------------------------------------------------------------------------------------------------

import java.util.ArrayList; interface Payment { void outputCurrentRent(ArrayList rentalProperties); }

----------------------------------------------------------------------------------------------------------------------------------------

public class RentalProperty { String rentalType; String rentalID; String numberRooms;

public RentalProperty() { }

public void outputCurrentRent() { System.out.println("ERROR: Can not output current rent"); }

public String getRentalType() { return rentalType; }

public String getRentalID() { return rentalID; }

public String getNumberRooms() { return numberRooms; } }

------------------------------------------------------------------------------------------------------------------------------------------

1 S SABQ138 3 1456.00 2 A AABQ205 2 972.00 3 S SABQ127 1 936.00 4 S SABQ126 2 1248.00 5 A AABQ302 2 918.00 6 A AABQ201 1 648.00

Above is the data in the rentalDB.txt document.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Databases Technologies And Applications

Authors: Zongmin Ma

1st Edition

1599041219, 978-1599041216

More Books

Students also viewed these Databases questions

Question

iv. Tradition s, Customs, and F aith ii. In Medi a

Answered: 1 week ago