Question:
Vector and menu help in java programming?
anonymou
2013-05-12 14:50:04 UTC
I'm trying to write a java program of a car hiring system. I need to store the information in a Vector and I have to have menu that displays A. Hire a car B. Modify C. Display all D. Save E. Quit
I have it that I can hire a car but no matter what I try I can't get a menu or a vector to work could someone please help
Code:
import java.util.Scanner;
import java.util.Vector;
class carTest{
public static void main(String [] args){
Scanner in = new Scanner(System.in);
String make;
int year;
String reg;
Double cost;
int days;
Vector data = new Vector ();
Car[] c = new Car[1];

for(int j = 0; j < c.length; j++)
{
//get input for the 3 car objects stored in the array
System.out.println("Enter make");
make = in.nextLine();

System.out.println("Enter year");
year = in.nextInt();

System.out.println("Enter reg");
reg = in.nextLine();

System.out.println("Enter days");
days = in.nextInt();
//create three new car objects. referenced by each element of the aray
c[j] = new Car(make, reg, year, days);

//compute the carhire cost
c[j].computeCost(days);
}

for(int i = 0; i < c.length; i ++)
{
c[i].display();
System.out.println();
}
}
}//end class

Code2:
//Car Hire example program
class Car
{
// attributes
public String make;
public int year;
public String reg;
public int numDays;
private double cost;

// methods
public Car()
{
cost = 0.0;
}

// the constructor
public Car(String m, String r, int y, int days)
{
make = m;
year = y;
reg = r;
numDays = days;
cost = 0.0;

}

//methods to read the attributes
public String getMake()
{
return make;
}
public int getyear()
{
return year;
}

public String getReg()
{
return reg;
}

public int getNumDays()
{
return numDays;
}

public double getCost()
{
return cost;
}

//method to compute the total car hire cost
public double computeCost(int days)
{
if (days > 10){
System.out.println("Error");
}
else if (year >= 5){
cost = (days * 10);
}
else if (days >= 6){
cost = (days * 15) - ((days * 15)*0.04);
}
else {
cost = days * 15;
}

return cost;
}

public void display(){
System.out.println("\n\nMake: " + make);
System.out.println("\nyear: " + year);
System.out.println("\nreg: " + reg);
System.out.println("\nNum Days: " + numDays);
System.out.println("\nCost: " + cost);

}
}//end class
Three answers:
2013-05-13 11:27:02 UTC
in.nextLine ();

should be

in.next ();
cauley
2017-01-05 18:47:22 UTC
you may no longer embed your approaches like that, additionally you do not have the wonderful parameters for you approaches - take a glance at right here: case a million: equipment.out.print("enter the radius of the circle: "); radius = keyboard.nextDouble(); circleArea(radius); ruin; then, outdoors of you significant technique placed: public static void circleArea(double radius) { double section = (3.14 * radius * radius); equipment.out.println("the element of the circle is " + section); }
2016-12-15 12:04:11 UTC
you may no longer embed your procedures like that, additionally you do not have the terrific suited parameters for you procedures - take a seem at here: case a million: device.out.print("enter the radius of the circle: "); radius = keyboard.nextDouble(); circleArea(radius); injury; then, exterior of you substantial approach positioned: public static void circleArea(double radius) { double area = (3.14 * radius * radius); device.out.println("the area of the circle is " + area); }


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...