Hi, I need help with an Assignment that requires me to continue(inheritance) on an existing assignment that I previously did.
When I run the program and choose option 4 or 5 nothing happens, it skips the rest of the loop and starts again.
#This is the main code that uses all the methods.
import java.util.Random;
import java.util.Scanner;
class pdaSIM{
public static void main(String args[]){
pdaCMD pda = new pdaCMD();
Random rand = new Random();
Scanner in = new Scanner(System.in);
double credit = 0;
String battery_stat="[#][#][#][][]";
String emailData = "John1987@gmail.com*This a test email.";
credit = 2+rand.nextInt(10);
//use power procedudure
pda.power = true;
//start of main code
//loops until power is false
while (pda.power == true){
//split emailData string
emailData = pda.splitEmail(emailData);
//displays layout
pda.showCredit(credit);
System.out.print("\t");
pda.batteryStatus(battery_stat);
//displays menu
pda.menu();
//checks option chosen
switch (pda.menuCh) {
case 1: credit = pda.Call(credit); break;
case 2: credit = pda.topup(credit); break;
case 3: battery_stat = pda.recharge(battery_stat); break;
case 4: emailData = pda.readEmail(emailData); break;
case 5: emailData = pda.writeEmail(emailData); break;
case 6: //switch pda off
pda.power(); break;
default: System.out.print("Invalid choice.\n"); break;
}
System.out.print("\n");
}
}
}
#I think that the problem is in this class.
import java.util.Scanner;
public class pdaCMD extends mobileCMD{
Scanner in = new Scanner(System.in);
String temp[];
public String splitEmail(String emailData){
temp = emailData.split("*");
return emailData;
}
public String readEmail(String emailData){
for (int i=0; i<temp.length; i++){
if (temp[i].endsWith(".com")){
System.out.print("\nFrom: " +temp[i]+ "\n");
System.out.print("\t" +temp[i+1]+ "\n");
} else {
i++;
}
}
return emailData;
}
public String writeEmail(String emailData){
emailData = emailData + "*";
System.out.print(": ");
emailData = in.nextLine();
return emailData;
}
public void menu(){
System.out.print("\n\n1. Call\n2. Topup\n3. Recharge\n4. Read Email\n5. Write email\n6. Power\t");
menuCh = in.nextInt();
}
}
#This class extends 'pdaCMD'
import java.util.Scanner;
public class mobileCMD{
Scanner in = new Scanner(System.in);
boolean power = false;
int menuCh;
public void power(){
while (power == false){
System.out.print("\tPower on.\n");
power = true;
break;
}
while (power == true){
System.out.print("\n\tPower off.");
power = false;
break;
}
}
public double Call(double credit){
if (credit <= 0){
System.out.print("Please topup credit.\n");
}
if (credit > 0){
credit = credit-0.5;
}
return credit;
}
public void showCredit(double credit){
System.out.print(credit+ "euros");
}
public double topup(double credit){
credit = credit+5;
return credit;
}
public void batteryStatus(String battery_stat){
System.out.print(battery_stat);
}
public String recharge(String battery_stat){
if (battery_stat == "[#][#][#][#][#]"){
System.out.print("Battery charged.\nPlease unplug charger to save energy.\n");
}
battery_stat = "[#][#][#][#][#]";
return battery_stat;
}
public void menu(){
System.out.print("\n\n1. Call\n2. Topup\n3. Recharge\n4. Power\t");
menuCh = in.nextInt();
}
}
Please help.(: