ASU CSE 110 Assignment #4 Due date Time: Friday, March 3rd, 2017 at 5:30pm What
ID: 3862366 • Letter: A
Question
ASU CSE 110 Assignment #4 Due date Time: Friday, March 3rd, 2017 at 5:30pm What this Assignment Is About Given a UML diagram, learn to design a class. Team how to define constnictor, accessor, mutator and toString( methods, etc. Learu how to create au object and wall an in ruethod. Coding Guidelines for All Labs/Assignments You will be graded on this Give identifiers semantic meaning and make them easy to read (example numSndents, grossPay, etc). Keep identifiers lo a reasonably sbort length. Use upper case for constants. Use title case (first letter is u case) for classes. Use lower case with uppercase word separators for all other identifiers variables, methods. objects). Use tabs or spaces to indent code within blocks (code surrounded by braces). This includes classes, methods, and code associated with its. switches and loops. Be consistent with the number of spaces or tabs that you use to ndent Use white space to make your progra more readable Use comments properly before or after the ending brace of classes, methods, and blocks to identify to which block it belongs Assignment description 1. Step According to the follo ing UML diagrau, desigu a Flight class, save the souce vode as Flight java Flight irLL ept City String ur icily: uring price: double t light int, String String double) get Flight Num int +get Dept City String getArricity String +get price donible elFlighuNuur (inL) void. etnept.City tring) void +selArricily SLring void raise Price (double) void +reduct Fri Kdouble) voidExplanation / Answer
// Flight.java
public class Flight {
private int flightNum;
private String deptCity;
private String arriCity;
private double price;
public Flight(int flightNum, String deptCity, String arriCity, double price) {
this.flightNum = flightNum;
this.deptCity = deptCity;
this.arriCity = arriCity;
this.price = price;
}
public int getFlightNum() {
return flightNum;
}
public String getDeptCity() {
return deptCity;
}
public String getArriCity() {
return arriCity;
}
public double getPrice() {
return price;
}
public void setFlightNum(int flightNum) {
this.flightNum = flightNum;
}
public void setDeptCity(String deptCity) {
this.deptCity = deptCity;
}
public void setArriCity(String arriCity) {
this.arriCity = arriCity;
}
public void raisePrice(double rate) {
price += price * rate;
}
public void reducePrice(double rate) {
price -= price * rate;
}
@Override
public String toString(){
DecimalFormat decimalFormat = new DecimalFormat("###.##");
StringBuilder builder = new StringBuilder();
builder.append(" ").append("Flight No:").append(" ").append(String.valueOf(flightNum));
builder.append(" ").append("Departure:").append(" ").append(deptCity);
builder.append(" ").append("Arrival:").append(" ").append(arriCity);
builder.append(" ").append("Price:").append(" ").append(" ").append("$").append(decimalFormat.format(price));
builder.append(" ");
return builder.toString();
}
}
----------------------------------------------------------
// Assignment4.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
public class Assignment4 {
private static Flight flightOne;
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
flightOne = getFlightInfo();
displayMenu();
}
public static void displayMenu() throws IOException {
DecimalFormat decimalFormat = new DecimalFormat("###.##");
System.out.println("Chose and action");
System.out.println("----------------------");
System.out.println("D. Change departure city");
System.out.println("A. Change arrival city");
System.out.println("I. Increase the price");
System.out.println("R. Reduce the price");
System.out.println("S. Show flight info");
System.out.println("P. Display the menu");
System.out.println("Q. Exit");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char ch;
while ((ch = br.readLine().charAt(0)) != 'Q' && ch != 'q') {
switch (ch) {
case 'D':
case 'd':
System.out.println("Enter new departure city");
String deptCity = br.readLine();
String oldDeptCity = flightOne.getDeptCity();
flightOne.setDeptCity(deptCity);
System.out.println("Departure city changed from " + oldDeptCity + " to " + deptCity);
break;
case 'A':
case 'a':
System.out.println("Enter new arrival city");
String arriCity = br.readLine();
String oldArriCity = flightOne.getArriCity();
flightOne.setArriCity(arriCity);
System.out.println("Arrival city changed from " + oldArriCity + " to " + arriCity);
break;
case 'I':
case 'i':
System.out.println("Enter new price increase rate");
double rate = Double.parseDouble(br.readLine());
flightOne.raisePrice(rate);
System.out.println("Price was increased by " + rate * 100 + "%. The new price is %" + decimalFormat.format(flightOne.getPrice()));
break;
case 'R':
case 'r':
System.out.println("Enter new price decrease rate");
double decreaseRate = Double.parseDouble(br.readLine());
flightOne.reducePrice(decreaseRate);
System.out.println("Price was decreased by " + decreaseRate * 100 + "%. The new price is %" + decimalFormat.format(flightOne.getPrice()));
break;
case 'S':
case 's':
System.out.println(flightOne.toString());
break;
case 'P':
case 'p':
displayMenu();
return;
default:
System.out.println("unknown command");
}
}
}
public static Flight getFlightInfo() throws IOException {
int flightNum;
String deptCity, arriCity;
double price;
System.out.println("Enter flight info : Flight No. Departure Arrival Price");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
flightNum = Integer.parseInt(br.readLine());
deptCity = br.readLine();
arriCity = br.readLine();
price = Double.parseDouble(br.readLine());
return new Flight(flightNum, deptCity, arriCity, price);
} catch (NumberFormatException e) {
System.out.println("You enter wrong information");
return getFlightInfo();
}
}
}