How do I Solve this method? - java

I have 3 Classes:Account,Customer and Main.the Main Class has the main method:
these Classes are the some parts of a programm.
public class Account {
private static ArrayList<Account> allAccounts=new ArrayList<>();
private Bank bank;
private int id;
private int money;
private int remainingDuration;
private int interest;
private Customer customer;
public Account(Bank bank, Customer customer,int id, int money,int duration,int interest) {
this.bank = bank;
this.customer=customer;
this.id=id;
this.money = money;
this.remainingDuration=duration;
this.interest = interest;
allAccounts.add(this);
}
public int getId() {
return id;
}
public Bank getBank() {
return bank;
}
}
public class Customer {
private static ArrayList<Customer> allCustomers=new ArrayList<>();
private String name;
private double moneyInSafe;
private ArrayList<Account> allActiveAccounts;
private int totalNumberOfAccountsCreated;
private int negativeScore;
public Customer(String name, double moneyInSafe) {
this.name=name;
this.moneyInSafe=moneyInSafe;
totalNumberOfAccountsCreated=0;
allCustomers.add(this);
}
public static Customer getCustomerByName(String name){
for (Customer customer:allCustomers){
if(customer.getName().equals(name)){
return customer;
}
}
return null;
}
public String getName() {
return name;
}
public void createNewAccount(Bank bank,int money,int duration,int interest){
totalNumberOfAccountsCreated++;
allActiveAccounts.add(new Account(bank,this, totalNumberOfAccountsCreated, money, duration, interest));
}
public double getMoneyInSafe() {
return moneyInSafe;
}
public void setMoneyInSafe(double moneyInSafe) {
this.moneyInSafe = moneyInSafe;
}
public boolean hasActiveAccountBank(Bank bank){
}
private Account getAccountWithId(int id){
for(Account account:allActiveAccounts){
if(account.getId()==id){
return account;
}
}
return null;
}
}
public class Bank {
private static ArrayList<Bank> allBanks=new ArrayList<>();
private String name;
public Bank(String name) {
this.name = name;
allBanks.add(this);
}
public static Bank getBankWithName(String name){
for (Bank bank:allBanks){
if(bank.getName().equals(name)){
return bank;
}
}
return null;
}
public static boolean isThereBankWithName(String name){
return allBanks.contains(getBankWithName(name));
}
public static int getAccountInterestFromName (String name){
if(name.equals("KOOTAH")){
return 10;
}else if(name.equals("BOLAN")){
return 30;
}else{
return 50;
}
}
public String getName() {
return name;
}
}
So my question is How do I Define the hasActiveAccountBank method in Customer Class to Check Is there any Account with this Account id or not in Main Class.
the part of the Main Class has a matcher that returns Customer's name and the id so they are given.Here is the part:
if (!getCustomerByName(matcher.group(1)).hasActiveAccountBank()) {
System.out.println("Chizi zadi?!");
}
So How do i Write in the hasActiveAccountBank() argument?

Related

UML with subclasses

public class Student {
private String name;
private long id;
private String grade;
private int[] test;
private int NUM_TESTS;
public Student(){
name="Un";
id=0;
grade="Un";
test=new int[0];
NUM_TESTS=5;
}
public Student(String x, long z) {
name=x;
id=z;
}
public void setName(String n) {
name=n;
}
public void setID(long i) {
id=i;
}
public void setGrade(String g) {
grade=g;
}
/*public void setTestScore(int t,int s) {
test=t;
test=s;
}
public int getTestScore(int) {
return test;
}*/
public int getNumTests() {
return NUM_TESTS;
}
public String getName() {
return name;
}
public long getID() {
return id;
}
public String getGrade() {
return grade;
}
public String toString() {
return getTestScore()+getNumTests()+getName()+getID()+getGrade();
}
/*public void calculateResult() {
int sum=0;
for (int t:test)sum+=t;
double average= 1.0t*sum/5;*/
}
}
Here is my code I have spaced out the places where I am having the issues. I am writing a Student subclass with subclasses undergrad and postgrad.
Here is the UML
I don't understand how to correctly implement testScore if it is not one of the variables? Nevermind the calculate result I'll fix that myself. I am also unsure if my constructors are accurate. All the students do five exams that's a constant
setTestScore(int t, int s)... I do recommend to use carefully chosen names (identifiers). For example if you just rename the parameters to: setTestScore(int testNumber, int score) you can be more familiar what should you inplement.
test = new int[0];isn't what you want. You want test = new int[NUM_TESTS]
Try to reconsider method setTestScore(int testNumber, int score)
first parameter is actually the index in the array of test and the second is the value.
So, your method should be something like this:
public void setTestScore(int testNumber, int score) {
test[testNumber] = score;
}
I just gave you some guidance for your own implementation...
First of all, It seems that Student class should be abstract. because each student is UnderGraduate or PostGraduate.
Secondly, you should extend the child classes from Student class.
I hope the below code be helpful:
abstract class Student {
private String name;
private long id;
private String grade;
private int[] test;
private final int NUM_TESTS = 5;
public Student(){
name = "UN";
id = 0;
grade = "UN";
test = new int[NUM_TESTS];
}
public Student(String name, long id){
this.name = name;
this.id = id;
}
#Override
public String toString() {
//TODO: write your desire toString method
return getNUM_TESTS()+getName()+getId()+getGrade();
}
abstract void claculateResult();
public int getTestScore(int testNumber){
if(testNumber >= NUM_TESTS)
return 0;
return test[testNumber];
}
public void setTestScore(int testNumber, int score){
if(testNumber >= NUM_TESTS)
return;
test[testNumber] = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public int[] getTest() {
return test;
}
public void setTest(int[] test) {
this.test = test;
}
public int getNUM_TESTS() {
return NUM_TESTS;
}
}
and the UnderGraduate class would be:
public class UnderGraduate extends Student{
public UnderGraduate(){
}
public UnderGraduate(String name, long id){
super();
}
#Override
void claculateResult() {
//TODO: DO whatever you want
}
}
remember that the PostGraduate class is same as UnderGraduate.

how to search recipe based on important ingredients ? java android

I want to search recipe which match with shopping list items and it will suggest recipe to user based on most important ingredients like in Banana MilkShake {milk and banana} could make banana milkshake alone {sugar and vanilla} are optional,if I add items on shopping list like {banana,milk} it will search recipe on the basis of these ingredients rather {banana,milk,sugar and vanilla}
for example:
Banana Milk Shake Ingredients:
**Ingredient Name
1.banana (important)
2.milk (important)
3.vanilla (optional/not important)
4.sugar (optional/not important)
**Shopping List**
banana
milk
egg
rice
We could also make Banana Milkshake without sugar and vanilla so important ingredients will be milk and banana.
public class Ingredient implements Serializable{
private String name;
private String quantity;
public Ingredient(){
}
public Ingredient(String name,String quantity){
this.name=name;
this.quantity=quantity;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Override
public String toString() {
return name;
}
public String getQuantity() {
return quantity;
}
public void setQuantity(String quantity) {
this.quantity = quantity;
}
}
and my working code:
public Set<Recipe> searchByIngredient(List<ShoppingList> shop, List<Recipe> list) {
int matchedItems = 0;
Set<Recipe> result = new HashSet<Recipe>();
for (Recipe rn : list) {
boolean recipeMatched = false;
for (ShoppingList r : shop) {
for (int i = 0; i < rn.getIngre().size(); i++) {
if (rn.getIng(i).contains(r.getName())) {
matchedItems++;
int temp = matchedItems;
if(temp >= 2){
result.add(rn);
recipeMatched = true;
}
}
if (recipeMatched)
break;
}
if (recipeMatched)
break;
}
matchedItems = 0;
}
return result;
}
public class Recipe implements Serializable{
private String instructions;
private String recName;
private String image;
private List<Ingredient> ing = new ArrayList<Ingredient>();
public Recipe(){
}
public Recipe(String item,String quan){
this.ing.add(new Ingredient(item, quan));
}
public List<Ingredient> getIngre(){
return ing;
}
public String getIng(int i){
return ing.get(i).toString();
}
public void setIngredients(List<Ingredient> item){
this.ing = item;
}
public String getRecName() {
return recName;
}
public void setRecName(String recName) {
this.recName = recName;
}
// #Override
public String toString() {
return recName;
}
public String getInstructions() {
return instructions;
}
public void setInstructions(String instructions) {
this.instructions = instructions;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
Based on your comments. You could add another variable to your Ingredient class. Like this:
public class Ingredient implements Serializable{
private String name;
private String quantity;
private boolean isImportant;
public Ingredient(){
}
public Ingredient(String name,String quantity){
this.name=name;
this.quantity=quantity;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Override
public String toString() {
return name;
}
public String getQuantity() {
return quantity;
}
public void setQuantity(String quantity) {
this.quantity = quantity;
}
public boolean getIsImportant() {
return isImportant;
}
public void setIsImportant(boolean isImportant) {
this.isImportant= isImportant;
}
}
Then you could check in the recipe if all the important ingredients are present or not.

Passing objects as parameter to method JAVA

I was given the main coding to do the sub class .
but I was stuck in these coding as shown at the below :
z.setName(z.obj1);
z.setID(z.obj2);
********** It should be the way to insert input. **************
========== The subClass I shown at the below was written by myself ==========
The main coding given as below :
public static void main(String[] args) {
StudReg z = new StudReg();
z.setName(z.obj1);
z.setID(z.obj2);
System.out.println(z.getName());
System.out.println(z.getID());
System.out.println(z.getJava());
System.out.println(z.getDatabase());
StuComputing obj3,obj4;
obj4 = new StuComputing();
obj4.setStudReg(z);
System.out.println(obj4.GPA().getGPA());
}
The subClass (StudReg) I've done as below :
public class StudReg {
//Data Member
String Name;
String ID;
double Java,Database;
//Constructor
public StudReg(){};
public StudReg(String a,String b){
Name = a;
ID = b;
};
//Name
public void setName(String n){
Name = n;
}
public String getName() {
return Name;
}
//Id
public void setID (String i){
ID = i;
}
public String getID (){
return ID;
}
//Java
public void setJava (double j){
Java = j;
}
public double getJava (){
return Java;
}
//Database
public void setDatabase (double d){
Database = d;
}
public double getDatabase (){
return Database;
}
//FUNCTION
public StudReg (StudReg gg){
double aa,bb;
//refer to data from MAIN
aa = this.getJava();
bb = this.getDatabase();
}
Another SubClass - StuComputing:
public class StuComputing {
//DATA MEMBER
public StudReg ss;
double GPA;
//CONSTRUCTOR
public StuComputing (){};
public StuComputing (double a1){
GPA = a1;
};
//StudReg
public void setStudReg (StudReg st){
ss = st;
}
public StudReg getStudReg(){
return ss;
}
//GPA
public void setGPA(double g){
GPA = g;
}
public double getGPA(){
return GPA;
}
Beside answer my problem, can you all show a simple example ?
So, I can understand it easily ><
Thanks
I have still no idea what you really want to do... try this
class StudReg {
String obj1 ;
String obj2 ;
double java ;
double database ;
String name ;
String id ;
public double getJava() {
return java;
}
public double getDatabase() {
return database;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getID() {
return id;
}
public void setID(String id) {
this.id = id;
}
public String getObj1() {
return obj1;
}
public void setObj1(String obj1) {
this.obj1 = obj1;
}
public String getObj2() {
return obj2;
}
public void setObj2(String obj2) {
this.obj2 = obj2;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
class StuComputing {
StudReg studReg;
GPA gpa;
public StuComputing() {
gpa = new GPA();
}
public StudReg getStudReg() {
return studReg;
}
public void setStudReg(StudReg sr) {
this.studReg = sr;
}
public GPA GPA() {
return gpa;
}
}
public class GPA {
double gpa;
public GPA() {
}
public double getGPA(){
return gpa;
}
}
You must have forgotten something. there is no "obj1" or "obj2" attribute in your class StudReg.
So how can we understand your problem? I think you need to ask your question again in other wording.

Creating an object and calling it

this is my current code to store rooms(it compiles fine) but in the UML there is a variable called addEquipment and there is also another class called Equipment to be defined. I'm having trouble wrapping my head around what I'm supposed to do with this. Am I supposed to create and call an object called Equipment? what goes in addEquipment?
public class Room {
//begin variable listing
private String name;
private int id;
private int capacity;
private String equipmentList;
//begins get methods for variables
public String getName(){
return name;
}
public int getID(){
return id;
}
public int getCapacity(){
return capacity;
}
public String getEquipmentList(){
return equipmentList;
}
// Set the variables
public void setName(String aName){
name=aName;
}
public void setID(int anID){
id=anID;
}
public void setCapacity(int aCapacity){
capacity=aCapacity;
}
public void setEquipmentList(String anEquipmentList){
equipmentList=anEquipmentList;
}
public String addEquipment(String newEquipment, String currentEquipment){
}
//Create room object
public Room(int capacity, String equipmentList) {
setCapacity(capacity);
setEquipmentList(equipmentList);
}
//Convert variables to string version of room
public String toString(){
return "Room "+name+", capacity: "+capacity+", equipment: "+getEquipmentList();
}
}
You can create a new class Equipment and modify your attribute equipmentList to be a List:
public class Equipment {
private String name;
public Equipment(String name) {
this.name = name;
}
}
public class Room {
//begin variable listing
private String name;
private int id;
private int capacity;
private List<Equipment> equipmentList = new ArrayList<Equipment>();
//begins get methods for variables
public String getName(){
return name;
}
public int getID(){
return id;
}
public int getCapacity(){
return capacity;
}
public List<Equipment> getEquipmentList(){
return equipmentList;
}
// Set the variables
public void setName(String aName){
name=aName;
}
public void setID(int anID){
id=anID;
}
public void setCapacity(int aCapacity){
capacity=aCapacity;
}
public void setEquipmentList(List<Equipment> anEquipmentList){
equipmentList=anEquipmentList;
}
public String addEquipment(String newEquipment, String currentEquipment){
Equipment oneEquipment = new Equipment(newEquipment);
equipmentList.add(oneEquipment);
}
//Create room object
public Room() {
setCapacity(capacity);
setEquipmentList(equipmentList);
}
//Convert variables to string version of room
public String toString(){
String capacity=String.valueOf(getCapacity());
String room = "Room "+name+", capacity: "+capacity+", equipment: "+getEquipmentList();
return room;
}
}
In the method addEquipment, you can create a new Equipment and add it to equipmentList, like code above.
An Equipment class could be anything. Lets assume the "Equipment"-class has a String called "name" as it's attribute
public class Equipment {
String name;
public Equipment( String name) {
this.name = name;
}
public String getName() {
return this.name
}
}
When you extend your Room class by the requested "addEquipment" method, you can do something like this.
public class Room {
... // Your code
private int equipmentIndex = 0;
private Equipment[] equipment = new Equipment[10]; // hold 10 Equipment objects
public void addEquipment( Equipment eq ) {
if ( equipmentIndex < 10 ) {
equipment[ equipmentIndex ] = eq;
equipmentIndex++;
System.out.println("Added new equipment: " + eq.getName());
} else {
System.out.println("The equipment " + eq.getName() + " was not added (array is full)");
}
}
}
Now when you call
room.addEquipment( new Equipment("Chair") );
on your previously initialized object of the Room-class, you will get
"Added new equipment: Chair"
Hope this helps a bit.
PS: The code is untestet (maybe there hides a syntax error somewhere)

Gson, parsing json innerclass list, javabean

Well so I'm trying to parse a bit of JSon. I succeeded to parse:
Member.json:
{"member":{"id":585897,"name":"PhPeter","profileIconId":691,"age":99,"email":"peter#adress.com "}}
but what if I need to parse:
{"Members":[{"id":585897,"name":"PhPeter","profileIconId":691,"age":99,"email":‌​‌​"peter#adress.com"},{"id":645231,"name":"Bill","profileIconId":123,"age":56,"em‌​ai‌​l":"bill#adress.com"}]}
Ofcourse I searched the web, I think, I need to use "List<>" here private List<memberProfile> member;but how do I "get" this from my main class??
I used this to parse the first string:
memeberClass.java
public class memberClass {
private memberProfile member;
public memberProfile getMember() {
return member;
}
public class memberProfile{
int id;
String name;
int profileIconId;
int age;
String email;
//Getter
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getProfileIconId() {
return profileIconId;
}
public int getAge() {
return age;
}
public String getEmail() {
return email;
}
}
}
memberToJava.java
public class memberToJava {
public static void main(String[] args) {
Gson gson = new Gson();
try {
BufferedReader br = new BufferedReader(new FileReader("...Member.json"));
//convert the json string back to object
memberClass memberObj = gson.fromJson(br, memberClass.class);
System.out.println("Id: " + memberObj.getMember().getId());
System.out.println("Namw: " + memberObj.getMember().getName());
System.out.println("ProfileIconId: " + memberObj.getMember().getProfileIconId());
System.out.println("Age: " + memberObj.getMember().getAge());
System.out.println("Email: " + memberObj.getMember().getEmail());
} catch (IOException e) {
e.printStackTrace();
}
}
}
see below code
MemberClass.java
import java.util.List;
public class MemberClass {
private List<MemberProfile> member;
public List<MemberProfile> getMember() {
return member;
}
public void setMember(List<MemberProfile> member) {
this.member = member;
}
public class MemberProfile {
int id;
String name;
int profileIconId;
int age;
String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getProfileIconId() {
return profileIconId;
}
public void setProfileIconId(int profileIconId) {
this.profileIconId = profileIconId;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
}
Main Class
import com.google.gson.Gson;
public class MemTest {
public static void main(String[] args) {
String json = "{'member':[{'id':585897,'name':'PhPeter','profileIconId':691,'age':99,'email':‌​‌​'peter#adress.com'},{'id':645231,'name':'Bill','profileIconId':123,'age':56,'em‌​ai‌​l':'bill#adress.com'}]}";
MemberClass memberClass = new Gson().fromJson(json, MemberClass.class);
System.out.println(new Gson().toJson(memberClass));
}
}
Output
{"member":[{"id":585897,"name":"PhPeter","profileIconId":691,"age":99,"email":"‌​‌​\u0027peter#adress.com\u0027"},{"id":645231,"name":"Bill","profileIconId":123,"age":56}]}
Hi I made some changes to your application and it seems to work now ! You where quite close alls you need is a wrapper for the array.
public class memberWrapper {
private List<memberClass> Members;
public List<memberClass> getMembers() {
return Members;
}
public void setMembers(List<memberClass> members) {
this.Members = members;
}
}
Then I changed youir original class a little:
public class memberClass {
int id;
String name;
int profileIconId;
int age;
String email;
//Getter
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getProfileIconId() {
return profileIconId;
}
public int getAge() {
return age;
}
public String getEmail() {
return email;
}
}
and then in the main:
BufferedReader br = new BufferedReader(new FileReader("stuff.json"));
//convert the json string back to object
memberWrapper memberObj = gson.fromJson(br, memberWrapper.class);
System.out.println("Id: " + memberObj.getMembers().get(0).getId());
It should work now the important thing when dealing with JSOn is to just make sure the key matches the name of your variables.

Categories

Resources