Wednesday, 15 May 2013

java - Check to see if future objects are equal -



java - Check to see if future objects are equal -

so question this: can equate 2 objects if 1 of object may constructed later? eg: in code below have film creation class , that's , good. have no issues that( here context) real question in film store class. film store class initializes 3 film objects: batman, serenity , clueless want write method called rentmovie(string title) when called check see if title string of parameter matches title string of 1 of movies. know 1 way be:

if(title.equals("batman")) //the title beingness 1 of initialized movies { movie1.rentmovie(); //rentmovie() exists in class film , actual method calculations }

however take multiple if statements , every time wanted build new film in moviestore class have add together new line of code. there way create "global" (without public field) don't have define specific film title string rentmovie(string title) equal to?

here film class:

public class film { private string title; private int cost; private int instock; private int totalrentals; public movie(string title, int cost) { this.title = title; this.cost = cost; instock = 5+(int)(math.random()*((15-5)+1)); //random value in interval [5-15] totalrentals = 0; } public void rentmovie(string title)throws exception { if(this.title.equals(title) && instock>0) { totalrentals++; instock--; } else if(!this.title.equals(title)) { throw new exception("false. film rented not match film given"); } } public int getcost() { homecoming cost; } public int gettotalrentals() { homecoming totalrentals; } public void printdetails() { system.out.println("----------------"); system.out.println("title:\t\t"+title); system.out.println("cost:\t\t"+cost); system.out.println("in stock:\t"+instock); system.out.println("rented:\t\t"+totalrentals); system.out.println("----------------"); } }

here film store class: (i've left if statement incomplete because @ loss on here)

public class moviestore { private film movie1; private film movie2; private film movie3; public moviestore() { movie1 = new movie("batman",3); movie2 = new movie("clueless",5); movie3 = new movie("serenity",4); } public void rentmovie(string title) { if(title.equals()) { //not sure set here.rentmovie(title); // there bit of redundacy check here because of film class rentmovie() method //but need check film titles equality here first way i'm sure rentmovie() //from film class run. system.out.println("yes"); //used check if doing working } } public void printdetails() { movie1.printdetails(); movie2.printdetails(); movie3.printdetails(); int total = (movie1.getcost()*movie1.gettotalrentals())+(movie2.getcost()*movie2.gettotalrentals())+(movie3.getcost()*movie3.gettotalrentals()); system.out.println(" total revenues rentals were: " + "$"+total); } }

currently film object looked using film name, lookup based solution should work,

example: create hashmap key lower_case(movie-name) , value film object, used lookup/find whether film available.

let moviemap hashmap

something following:

movie film = moviemap.get(title.tolowercase()); if (movie == null) { // print error message } else { system.out.println("yes"); }

java string object equality

No comments:

Post a Comment