|
 |
05-10-2007, 05:23 PM
|
#1
|
|
FLAC
Join Date: Apr 2005
Location: Queens, New York
Posts: 1,385
|
Need help with a loop
Ok so i am writing a little program for school. The program is 99% done all i want to do left is add a loop so it will keep going back to the "selection menu" It is coded in java. I was wondering because i cant get a loop to seem to work. WOuld anyone beable to help put a loop so it keeps going back the the menu after the selection? ALso i need to add an exit button well here is the program:
Code:
import java.io.*;
public class PetTest {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("------------------------------------------------");
System.out.println("Please Choose From the Following:\n 1. Dogs " +
"2. Cats 3. Fish 4. Birds 5. VIEW ALL ");
System.out.println("------------------------------------------------");
String line = br.readLine().trim();
int selection = Integer.parseInt(line);
String s = "";
switch(selection) {
case 1:
s = "dog";
break;
case 2:
s = "cat";
break;
case 3:
s = "bird";
break;
case 4:
s = "fish";
break;
case 5:
s = "View All";
break;
default:
s = "unexected selection";
}
System.out.println("selection = " + s);
Dog dog = null ;
Cat cat = null;
Fish fish = null;
Bird bird = null;
if(s.equalsIgnoreCase("dog") || s.equalsIgnoreCase("View All"))
{
dog = new Dog(4, 12, 50.00, "Terrier", Dog.DRY);
System.out.println("\ndog = " + dog);
}
if(s.equalsIgnoreCase("cat") || s.equalsIgnoreCase("View All")){
cat = new Cat(5, 5, 25.00, "Persian", Cat.CLAWED);
System.out.println("\ncat = " + cat);}
if(s.equalsIgnoreCase("bird")|| s.equalsIgnoreCase("View All")){
bird = new Bird(3, 2, 5.00, "Canary", "USA");
System.out.println("\nbird = " + bird);}
if(s.equalsIgnoreCase("fish")|| s.equalsIgnoreCase("View All")){
fish = new Fish(1, 1, 10.00, "Angel", Fish.SALT);
System.out.println("\nfish = " + fish);
}
br.close();
} catch(IOException e) {
System.out.println("Read error: " + e.getMessage());
}
}
}
class Pet {
int age;
double weight;
double price;
protected Pet(int age, double weight, double price) {
this.age = age;
this.weight = weight;
this.price = price;
}
public String toString() {
//String name = getClass().getName();
return ", \nage:" + age +
", \nweight:" + weight +
", \nprice:" + price;
}
}
class Dog extends Pet {
String breed;
String foodType;
final static String CANNED = "canned food";
final static String DRY = "dry food";
public Dog(int age, double weight, double price,
String breed, String foodType) {
super(age, weight, price);
this.breed = breed;
this.foodType = foodType;
}
public String toString() {
return "\n\nDog \nbreed:" + breed +
", \nfoodType:" + foodType + super.toString() + "";
}
}
class Cat extends Pet {
String breed;
String clawStatus;
final static String CLAWED = "has claws";
final static String DECLAWED = "de-clawed";
public Cat(int age, double weight, double price,
String breed, String clawStatus) {
super(age, weight, price);
this.breed = breed;
this.clawStatus = clawStatus;
}
public String toString() {
return "\n\nCat \nbreed:" + breed +
", \nclawStatus:" + clawStatus +
super.toString() + "";
}
}
class Bird extends Pet {
String type;
String nationalOrigin;
public Bird(int age, double weight, double price,
String type, String origin) {
super(age, weight, price);
this.type = type;
nationalOrigin = origin;
}
public String toString() {
return "\n\nBird \ntype:" + type +
", \nnationalOrigin:" + nationalOrigin +
super.toString() + "";
}
}
class Fish extends Pet {
String type;
String waterType;
final static String FRESH = "freah water";
final static String SALT = "salt water";
public Fish(int age, double weight, double price,
String type, String habitat) {
super(age, weight, price);
this.type = type;
waterType = habitat;
}
public String toString() {
return "\n\nFish \ntype:" + type +
", \nwaterType:" + waterType +
super.toString() + "";
}
}
__________________
2002 Mitsubishi Galant
Progress: 90% [-▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓- ->
Carputer Specs:
Via M10K
512mb Ram
60GB HDD
Souund Blaster Audigy2 NX
OPUS ITX PC Case
███
|
|
|
|
|
|
Advertisement
|
Sponsored links
|
05-10-2007, 05:32 PM
|
#2
|
|
Fusion Brain Creator
Join Date: Mar 2006
Location: Colorado, but Canadian!
Posts: 8,862
|
edit: nevermind, at first glance I thought polymorphic, but I see now it isnt... I hate polymorphic substitution.
Ok well just put it in a while loop, with the break statement being a switch.
Code:
import java.io.*;
public class PetTest {
public static void main(String[] args) {
while(true) {
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("------------------------------------------------");
System.out.println("Please Choose From the Following:\n 1. Dogs " +
"2. Cats 3. Fish 4. Birds 5. VIEW ALL ");
System.out.println("------------------------------------------------");
String line = br.readLine().trim();
int selection = Integer.parseInt(line);
String s = "";
switch(selection) {
case 1:
s = "dog";
break;
case 2:
s = "cat";
break;
case 3:
s = "bird";
break;
case 4:
s = "fish";
break;
case 5:
s = "View All";
break;
case 6:
s = "EXIT";
break;
default:
s = "unexected selection";
}
System.out.println("selection = " + s);
Dog dog = null ;
Cat cat = null;
Fish fish = null;
Bird bird = null;
if(s.equalsIgnoreCase("exit"))
{
break;
}
if(s.equalsIgnoreCase("dog") || s.equalsIgnoreCase("View All"))
{
dog = new Dog(4, 12, 50.00, "Terrier", Dog.DRY);
System.out.println("\ndog = " + dog);
}
if(s.equalsIgnoreCase("cat") || s.equalsIgnoreCase("View All")){
cat = new Cat(5, 5, 25.00, "Persian", Cat.CLAWED);
System.out.println("\ncat = " + cat);}
if(s.equalsIgnoreCase("bird")|| s.equalsIgnoreCase("View All")){
bird = new Bird(3, 2, 5.00, "Canary", "USA");
System.out.println("\nbird = " + bird);}
if(s.equalsIgnoreCase("fish")|| s.equalsIgnoreCase("View All")){
fish = new Fish(1, 1, 10.00, "Angel", Fish.SALT);
System.out.println("\nfish = " + fish);
}
}
br.close();
} catch(IOException e) {
System.out.println("Read error: " + e.getMessage());
}
}
}
class Pet {
int age;
double weight;
double price;
protected Pet(int age, double weight, double price) {
this.age = age;
this.weight = weight;
this.price = price;
}
public String toString() {
//String name = getClass().getName();
return ", \nage:" + age +
", \nweight:" + weight +
", \nprice:" + price;
}
}
class Dog extends Pet {
String breed;
String foodType;
final static String CANNED = "canned food";
final static String DRY = "dry food";
public Dog(int age, double weight, double price,
String breed, String foodType) {
super(age, weight, price);
this.breed = breed;
this.foodType = foodType;
}
public String toString() {
return "\n\nDog \nbreed:" + breed +
", \nfoodType:" + foodType + super.toString() + "";
}
}
class Cat extends Pet {
String breed;
String clawStatus;
final static String CLAWED = "has claws";
final static String DECLAWED = "de-clawed";
public Cat(int age, double weight, double price,
String breed, String clawStatus) {
super(age, weight, price);
this.breed = breed;
this.clawStatus = clawStatus;
}
public String toString() {
return "\n\nCat \nbreed:" + breed +
", \nclawStatus:" + clawStatus +
super.toString() + "";
}
}
class Bird extends Pet {
String type;
String nationalOrigin;
public Bird(int age, double weight, double price,
String type, String origin) {
super(age, weight, price);
this.type = type;
nationalOrigin = origin;
}
public String toString() {
return "\n\nBird \ntype:" + type +
", \nnationalOrigin:" + nationalOrigin +
super.toString() + "";
}
}
class Fish extends Pet {
String type;
String waterType;
final static String FRESH = "freah water";
final static String SALT = "salt water";
public Fish(int age, double weight, double price,
String type, String habitat) {
super(age, weight, price);
this.type = type;
waterType = habitat;
}
public String toString() {
return "\n\nFish \ntype:" + type +
", \nwaterType:" + waterType +
super.toString() + "";
}
}
Last edited by 2k1Toaster; 05-10-2007 at 05:35 PM.
|
|
|
05-10-2007, 05:43 PM
|
#3
|
|
Super Moderator. If my typing sucks it's probably because I'm driving....
Join Date: Oct 2004
Location: NY
Posts: 6,102
|
gotta love the badger
|
|
|
|
Sponsored links
|
|
Advertisement
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:56 PM.
| |