In: Computer Science
This problem is about java program and please show the detail comment and code in each class. Thank you!
Create four classes with the following UML diagrams:
(The "-" means private and the testBankAccount()
testMobilePhone() testChocolate() testStudent() all static
+-----------------------------+
| BankAccount |
+-----------------------------+
| - money: int |
+-----------------------------+
| + BankAccount(int money) |
| + getMoney(): int |
| + setMoney(int money): void |
| + testBankAccount(): void |
+-----------------------------+
+------------------------------------------------+
| MobilePhone |
+------------------------------------------------+
| - number: int |
| - account: BankAccount |
+------------------------------------------------+
| + MobilePhone(int number, BankAccount account) |
| + getNumber(): int |
| + payMoney(int amount): boolean |
| + testMobilePhone(): void |
+------------------------------------------------+
A mobile phone has a phone number and is connected to a bank
account. The owner of the mobile phone can use the mobile phone to
pay money: if amount is not negative and if the
bank account connected to the mobile phone has enough money in it
then the money in the bank account is decreased by
amount and the payMoney method
must return true, otherwise nothing changes for
the bank account and the method must return
false.
+-----------------------------+
| Chocolate |
+-----------------------------+
| - weight: double |
+-----------------------------+
| + Chocolate(double weight) |
| + getWeight(): double |
| + buy(int money): void |
| + eat(double amount): void |
| + testChocolate(): void |
+-----------------------------+
If the constructor of the Chocolate class is given
a negative weight as argument then the weight must be changed
to
0.0 Kg.
When buying chocolate, the weight of chocolate
increases, with the price of chocolate being RMB 100 per 1.5 Kg. It
is
not possible to buy a negative amount of chocolate, so in that case
the buy method must print a message
"Cannot
buy negative amount of chocolate" and nothing else
happens.
It is not possible to eat more chocolate than there is chocolate,
so in that case the eat method must print a message
"Cannot eat nonexistent chocolate, only XXX Kg
available" (where XXX is replaced with
the current weight of chocolate) and nothing else happens.
+-----------------------------------+
| Student |
+-----------------------------------+
| - name: String |
| - phone: MobilePhone |
| - chocolate: Chocolate |
+-----------------------------------+
| + Student(String name, int money) |
| + getName(): String |
| + getChocolateWeight(): double |
| + hungry(int money): void |
| + testStudent(): void |
+-----------------------------------+
When a student is created, the student has 0.0 Kg of chocolate and
a mobile phone which is connected to a bank
account with money in it. Use your student ID
number as the phone number for the mobile phone.
When the student is hungry, the student first tries to use the
mobile phone to pay the money amount. If the
payment is
successful then the student buys the chocolate corresponding to the
same money amount of the payment and then the
student eats half of the weight of chocolate that has just been
bought (not half of the total weight of chocolate). If the
payment is not successful then the hungry method
must print a message "Student is still hungry!"
and
nothing else happens.
Each class has a static test method that contains tests for all the
constructors and all the methods of the class. For each
class, test the simple methods first and the more complicated
methods next. For each constructor and each method,
make sure that you test every possible case.
Add to your software a Start class with a
main method that calls the test method of each of
the four classes, to test
everything in your software.
public class BankAccount {
private int money;
public BankAccount(int money) {
this.money=money;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money=money;
}
public static void testBankAccount() {
BankAccount account=new
BankAccount(200);
System.out.println("Current
amount::"+account.getMoney());
account.setMoney(400);
System.out.println("Current
amount::"+account.getMoney());
}
/*
* public static void main(String[] args) {
BankAccount.testBankAccount(); }
*/
}
*************************************************************************************************************************
public class MobilePhone {
/*
* | - number: int | | - account: BankAccount |
* +------------------------------------------------+ |
+ MobilePhone(int
* number, BankAccount account) | | + getNumber(): int
| | + payMoney(int
* amount): boolean | | + testMobilePhone(): void
|
*
* A mobile phone has a phone number and is connected
to a bank account. The owner of the mobile phone can use the mobile
phone
* to pay money: if amount is not negative and if the
bank account connected to the mobile phone has enough money in it
then the
* money in the bank account is decreased by amount and
the payMoney method must return true, otherwise nothing changes for
the bank
* account and the method must return false.
*/
private int number;
private BankAccount account;
public MobilePhone(int number, BankAccount account)
{
this.number=number;
this.account=account;
}
public int getNumber() {
return number;
}
public boolean paymoney(int amount) {
if(amount>0) {
int
newAmount=account.getMoney()-amount;
if(
newAmount>=0 && account.getMoney()>=newAmount)
{
account.setMoney(newAmount);
return true;
}else {
System.out.println("Insufficient amount in
account.");
}
}else {
System.out.println("please enter positive numbers in
amount");
}
return false;
}
public static void testMobilePhone() {
BankAccount account =new
BankAccount(300);
MobilePhone phone=new
MobilePhone(123456, account);
System.out.println(phone.paymoney(500));
System.out.println(phone.paymoney(200));
System.out.println("balance
::"+account.getMoney());
System.out.println(phone.paymoney(100));
System.out.println("balance
::"+account.getMoney());
}
/*
* public static void main(String[] args) {
MobilePhone.testMobilePhone();
*
* }
*/
}
*******************************************************************************************************************************
public class Chocolate {
/*
* | Chocolate | +-----------------------------+ | -
weight: double |
* +-----------------------------+ | + Chocolate(double
weight) | | +
* getWeight(): double | | + buy(int money): void | | +
eat(double amount): void
* | | + testChocolate(): void |
+-----------------------------
* + If the
* constructor of the Chocolate class is given a
negative weight as argument
* then the weight must be changed to 0.0 Kg.
* When buying chocolate, the weight
* of chocolate increases, with the price of chocolate
being 100 per 1.5 Kg.
* It is not possible to buy a negative amount of
chocolate, so in that case the
* buy method must print a message "Cannot buy negative
amount of chocolate" and
* nothing else happens.
*
* It is not possible to eat more chocolate than there
is
* chocolate, so in that case the eat method must print
a message
* "Cannot eat nonexistent chocolate, only XXX Kg
available" (where XXX is
* replaced with the current weight of chocolate) and
nothing else happens.
*/
private double weight;
public Chocolate(double weight) {
if( weight<(float)0.0) {
weight=0.0;
}
this.weight=weight;
}
public double getWeight() {
return weight;
}
public void buy(int money) {
double
standardPricePerkiloGram=(double) 1.5/ (double)100;
if(money>0) {
double
calculatedWeightOfChocolates=standardPricePerkiloGram*money;
//System.out.println("calculatedWeightOfChocolates::"+calculatedWeightOfChocolates);
if(calculatedWeightOfChocolates>weight) {
System.out.println("Cannot buy negative amount of
chocolate");
}
weight=weight-calculatedWeightOfChocolates;
//System.out.println("weight
now::"+weight);
}else {
System.out.println("please enter correct amount.");
}
}
public void eat(double amount) {
double
standardPricePerkiloGram=(double) 1.5/ (double)100;
double
presentWeightOfChocolates=weight;
double
calculatedWeightOfChocolates=standardPricePerkiloGram*amount;
if(calculatedWeightOfChocolates>presentWeightOfChocolates)
{
System.out.println("Cannot eat nonexistent chocolate, only
"+presentWeightOfChocolates+" Kg available");
}
}
public static void testChocolate() {
Chocolate chocolate=new
Chocolate(3.0);
chocolate.buy(200);
chocolate.eat(100);
}
/*
* public static void main(String[] args) {
Chocolate.testChocolate(); }
*/
}
*******************************************************************************************************************
public class Student {
/*
When a student is created, the student has 0.0 Kg of chocolate and
a mobile phone which is connected to a bank
account with money in it. Use your student ID number as the phone
number for the mobile phone.
Each class has a static test method that contains tests for all
the constructors and all the methods of the class. For each
class, test the simple methods first and the more complicated
methods next. For each constructor and each method,
make sure that you test every possible case.
Add to your software a Start class with a main method that calls
the test method of each of the four classes, to test
everything in your software.
*/
private String name;
private MobilePhone phone;
private Chocolate chocolate;
public Student(String name, int money) {
int studentID=12345;
this.name=name;
this.chocolate=new
Chocolate(0.0);
BankAccount account=new
BankAccount(money);
this.phone=new
MobilePhone(studentID, account);
}
public String getName() {
return name;
}
public double getChocolateWeight() {
return chocolate.getWeight();
}
/*
* When the student is hungry, the student first tries
to use the mobile phone
* to pay the money amount. If the payment is
successful then the student buys
* the chocolate corresponding to the same money amount
of the payment and then
* the student eats half of the weight of chocolate
that has just been bought
* (not half of the total weight of chocolate). If the
payment is not successful
* then the hungry method must print a message "Student
is still hungry!" and
* nothing else happens.
*/
public void hungry(int money) {
if(phone.paymoney(money)) {
//System.out.println("payment done");
chocolate.buy(money);
//System.out.println("chocolate bought");
double
weightofChcoclateBought=chocolate.getWeight();
//System.out.println("weightofChcoclateBought::"+weightofChcoclateBought);
double
halfOfweightofChcoclateBought=weightofChcoclateBought/2;
chocolate=new
Chocolate(halfOfweightofChcoclateBought);
}else{
System.out.println("Student is still hungry!");
}
}
public static void testStudent() {
Student student=new
Student("testStudent", 100);
System.out.println("default
chocolate weight::"+student.getChocolateWeight());
student.chocolate=new
Chocolate(3.0);
System.out.println("chocolate
weight::"+student.getChocolateWeight());
student.hungry(100);
System.out.println("chocolate
weight::"+student.getChocolateWeight());//should be half of the
chocolates bought
}
/*
* public static void main(String[] args) {
Student.testStudent(); }
*/
}
***************************************************************************************************************************
public class Start {
public static void main(String[] args) {
Student.testStudent();
Chocolate.testChocolate();
MobilePhone.testMobilePhone();
BankAccount.testBankAccount();
}
}
Here is the code for your question. the code is in accordance with every line specified in the question and is self-explanatory.
Just copy-paste the classes to any IDE of your preference and you are good to go. I have kept the Sysout in comments for you to understand it better.you can remove it later once you have gone through it.