Ok, I have this project In which I have to Write a Java program that can simulate a truck delivery, using the Java console as the exclusive input and output device. That is, each input to the TruckItem class, either unload or load an item (meaning creation of an object of TruckItem and marking the field "delivered" TRUE). After each such input (iteration), you should output to the Java console all TruckItems by displaying items already delivered followed by items yet to be delivered.
You should create two classes, TruckItem and Delivery. Your program can run by Delivery class.

I've been working on this for a while and I am close to ripping my hair out
I think that I am close but I can't get the console to loop the user input message.
Here's what I got so far.

package Project1;
import java.util.ArrayList; // to enable ArrayList

public class TruckItem1 {
    //Instance variables
    String name;
    double price;
    boolean delivered;
    TruckItem truck;
    int itemLeftForDelivery = 0;
    private ArrayList<TruckItem1> aTruck = new ArrayList<TruckItem1>();

        //Constructor
        public TruckItem1(String nm, double pc){
        name = nm;
        price = pc;
        delivered = false;
        }
        // Accessor Methods
        public String getName(){return name;}
        public double getPrice(){return price;}
        public boolean setDelivery(){return true;}


}



package Project1;
import java.util.Scanner;  //Scanner for user input
import java.util.ArrayList;  // to utilize and ArrayList
import java.io.*;  // To catch IOExceptions

public class Delivery1 {
    public static void main(String args[]) throws IOException{
        try{
Scanner s = new Scanner(System.in);  // Create a new scanner for user input
ArrayList<TruckItem1> aTruck = new ArrayList<TruckItem1>();  // Create new TruckItem1 Array 
        int options;
        int itemCount = 0;
        int max_items = 5; //maximum items allowed on the truck
        int unLoader;


do {
System.out.println("Choose an Options (1):Load  (2)Unload:  (3): Get Truck Status (4): Exit"); // Display options to user options = s.nextInt();

switch(options){/* Case 1(Load), if user selects option 1, the prompt will ask to enter item name and price upon 
* Receiving the information a TruckItem1 will be loaded into the array
* and the itemCount will be index++
*/
case 1:  
if(options == 1 && aTruck.size() <= max_items){
        String nm;
        double pr;
        System.out.println("Enter Item Name: ");
        nm = s.next();
        System.out.println("Enter Price");
        pr = s.nextDouble();
        TruckItem1 truck = new TruckItem1(nm, pr);
        itemCount++;
    } else
            { if(aTruck.size() == max_items){
        System.out.println("*** Truck is full 5 out of 5 Items Loaded ***");
                            }
                    }
    }break;
   /*  Case 2(unload), if user selects option 2, the prompt will display items currently in the truck
    *   The display will show index + 1 and the item name and price
    *   the user will then be allowed to select the item they wish to remove from the truck 
   */
     case 2:
     if(options == 2){
    System.out.println("*** Items On Truck ***");
    for(int index = 0; index < aTruck.size(); index++){
    System.out.println("("+(index +1)+")" + "/t" + aTruck.get(index).name 
    + aTruck.get(index).price );
                  }
    System.out.println(" Choose and item to Unload ");
    unLoader = s.nextInt();
    aTruck.remove(unLoader);
    }break;
    *
    * Case 3(Get Truck Status), will display items on truck(name, price)
    * then display how many items are left to be delivered
        */
case 3:
      if(options == 3){
    System.out.println("*** Items to be Delivered ***");
    for(int index = 0; index < aTruck.size(); index++){
    System.out.println("("+(index +1)+")" + "/t" + aTruck.get(index).name 
    aTruck.get(index).price );
    System.out.println(aTruck.size() + " Items are left to be delivered ");
    }
    }break;
} while (options != 4);  // The program should end if the user select option 4 as an option.

}catch(NumberFormatException ne)
  System.out.println(ne.getMessage() + " is not a numeric value.");            ne.printStackTrace();
    }

  }
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Please edit your post and wrap your code in code tags. Use the [code] icon about the input box.

Edited 5 Years Ago by NormR1: n/a

package Project1;
import java.util.ArrayList; // to enable ArrayList
 
public class TruckItem1 {
	//Instance variables
	String name;
	double price;
	boolean delivered;
	TruckItem truck;
	int itemLeftForDelivery = 0;
	private ArrayList<TruckItem1> aTruck = new ArrayList<TruckItem1>();
	
		//Constructor
		public TruckItem1(String nm, double pc){
		name = nm;
		price = pc;
		delivered = false;
		}
		// Accessor Methods
		public String getName(){return name;}
		public double getPrice(){return price;}
		public boolean setDelivery(){return true;}
		
		
}
package Project1;
import java.util.Scanner;  //Scanner for user input
import java.util.ArrayList;  // to utilize and ArrayList
import java.io.*;  // To catch IOExceptions

public class Delivery1 {
	public static void main(String args[]) throws IOException{
		try{
	
Scanner s = new Scanner(System.in);
// Create a new scanner for user input
ArrayList<TruckItem1> aTruck = new ArrayList<TruckItem1>();  
// Create new TruckItem1 Array 
		
int options;
int itemCount = 0;
int max_items = 5; //maximum items allowed on the truck
int unLoader;
	

do {

System.out.println("Choose an Options (1):Load  (2)Unload:  (3): Get Truck Status (4): Exit"); // Display options to user
options = s.nextInt();
			
switch(options){
/* Case 1(Load), if user selects option 1, the prompt will ask to enter item name and         *price upon 
* Receiving the information a TruckItem1 will be loaded into the array
* and the itemCount will be index++
*/
case 1:  
	if(options == 1 && aTruck.size() <= max_items){
	 String nm;
	 double pr;
	System.out.println("Enter Item Name: ");
	nm = s.next();
	System.out.println("Enter Price");
	pr = s.nextDouble();
	TruckItem1 truck = new TruckItem1(nm, pr);
	itemCount++;
	} else
	{ if(aTruck.size() == max_items){
	System.out.println("*** Truck is full 5 out of 5 Items Loaded ***");
							}
	}break;
		    
/*  Case 2(unload), if user selects option 2, the prompt will display items currently in the truck
*The display will show index + 1 and the item name and price
*the user will then be allowed to select the item they wish to remove from the truck	*/
	case 2:
	if(options == 2){
	System.out.println("*** Items On Truck ***");
	for(int index = 0; index < aTruck.size(); index++){
	System.out.println("("+(index +1)+")" + "/t" + aTruck.get(index).name 
	+ aTruck.get(index).price );
	 }
	System.out.println(" Choose and item to Unload ");
	unLoader = s.nextInt();
	aTruck.remove(unLoader);
					
	}break;
/*
* Case 3(Get Truck Status), will display items on truck(name, price)
* then display how many items are left to be delivered
*/
     case 3:
	if(options == 3){
	System.out.println("*** Items to be Delivered ***");
	for(int index = 0; index < aTruck.size(); index++){
	System.out.println("("+(index +1)+")" + "/t" + aTruck.get(index).name 
		+ aTruck.get(index).price );
	System.out.println(aTruck.size() + " Items are left to be delivered ");
	 }
	}break;
	  
} while (options != 4);  // The program should end if the user select option 4 as an option.
		
}catch(NumberFormatException ne){
     System.out.println(ne.getMessage() + " is not a numeric value.");
      ne.printStackTrace();
	}

  }
}

My issue is that I want the user to select an input. So I decided to use a switch statement but I also want the message to display after each case is complete allowing the user to select another option. I don't know if what I'm doing is valid but I am getting an error on the case statements after the first one.

Edited 5 Years Ago by ninjaConfused: n/a

I am getting an error on the case statements after the first one.

Please post the full text of the error message.

This article has been dead for over six months. Start a new discussion instead.