一、语言和环境
A、实现语言
Java
B、环境要求
MyEclipse 10
二、功能要求
开发阳光旅行网app中用户下单功能,可实现用户添加订单并完善出行人信息、修改订单,删除订单以及打印行程信息等功能。
具体功能要求如下:
(1)显示系统主菜单
包括添加出行订单、查看我的出行订单、修改订单信息、删除出行人、查看行程、退出系统6项功能,其他功能本项目不做要求,当用户选择功能编号后可进入执行相应功能。
你可能还想了解的:
首先声明一个共同的抽象类:
package com.pojo;
/**
* 老人、成年人、小孩的抽象类
*
* @author Think
*
*/
public abstract class Person {
private String name;
private int age;
public Person() {
super();
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public abstract double calMoney();
public abstract void show();
}
成年人类:
package com.pojo;
/**
* 成年人类
*
* @author Think
*
*/
public class Adult extends Person {
// 调用方法直接返回金额
@Override
public double calMoney() {
// TODO Auto-generated method stub
return 2000;
}
@Override
public void show() {
System.out.println(this.getName() + "\t" + this.getAge() + "\t\t\t2000");
}
}
小孩类:
package com.pojo;
/**
* 小孩类
*
* @author Think
*
*/
public class Child extends Person {
public boolean isOk;
@Override
public double calMoney() {
return 30;
}
@Override
public void show() {
if (isOk) {
System.out.println(this.getName() + "\t" + this.getAge() + "\t是\t\t30");
} else {
System.out.println(this.getName() + "\t" + this.getAge() + "\t否\t\t0");
}
}
}
老人类:
package com.pojo;
/**
* 老人类
*
* @author Think
*
*/
public class Old extends Person {
@Override
public double calMoney() {
return 1000;
}
public void setAge(int age) {
if (age > 65) {
super.setAge(age);
} else {
System.out.println("对不起,老人订单的年龄需为65岁以上!");
}
}
@Override
public void show() {
System.out.println(this.getName() + "\t" + this.getAge() + "\t\t\t1000");
}
}
出行类订单,助手控制类,用于协助管理类控制订单
package com.pojo;
import java.util.Date;
import java.util.Map;
/**
* 出行订单类,助手控制类
*
* @author Administrator
*
*/
public class Order {
private Date date;// 出行日期
private String phone;// 出行联系人电话
private Map<String, Person> personMap;// 出行人信息
public void show() {
System.out.println("出行日期:" + date.toString());
System.out.println("出行联系人电话:" + phone);
System.out.println("详细信息:");
System.out.println("姓名\t年龄\t儿童是否占床\t金额");
for (Person p : personMap.values()) {
p.show();
}
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Map<String, Person> getPersonMap() {
return personMap;
}
public void setPersonMap(Map<String, Person> personMap) {
this.personMap = personMap;
}
}
主体总控制类:
package com.pojo;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
/**
* 管理类
*
* @author Think
*
*/
public class Manager {
// 定义一个出行订单集合。
static List<Order> orders = new ArrayList<Order>();
public Scanner input;
/* 初始化一个订单 */
// 使其在一开始的时候查询的时候有数据
static {
Order order = new Order();
order.setDate(new Date());
order.setPhone("13424455555");
Map<String, Person> personMap = new HashMap<String, Person>();
personMap.put("老人", new Old());
personMap.put("小孩", new Child());
personMap.put("成年人", new Adult());
order.setPersonMap(personMap);
orders.add(order);
}
private String deletename;
// 增加出行订单
public void add() {
// 创建泛型结合保存数据
Map<String, Person> personMap = new HashMap<String, Person>();
// 创建对象
Order order = new Order();
System.out.println("请输入出行日期(如:2017-05-01)");
String dataString = input.next();
// 将字符串转换成时间
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
try {
Date da = fmt.parse(dataString);
order.setDate(da);
System.out.println("请输入联系人手机号码:");
String phone = input.next();
// 将数据同步更改
order.setPhone(phone);
if (phone.length() != 11) {
try {
throw new Exception("手机号码必须为11位");
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("********添加行人信息*******");
// 循环开始
String answer = null;// 循环控制
double money = 0;
do {
System.out.println("1.成人2.儿童(1.2米以下)3.老人(65岁以下)请选择类别:");
int sort = input.nextInt();
// 选择输入订单信息
switch (sort) {
case 1:
// 成年人
Person person = new Adult();
money += person.calMoney();
System.out.println("请输入姓名:");
person.setName(input.next());
System.out.println("请输入年龄:");
person.setAge(input.nextInt());
System.out.println("姓名:" + person.getName() + ",年龄:" + person.getAge() + "本次出行金额" + 2000.0 + "元。");
// 将信息导入泛型集合中
personMap.put(person.getName(), person);
order.setPersonMap(personMap);
break;
case 2:
// 儿童
// 创建小孩类
Child person1 = new Child();
System.out.println("请输入姓名:");
person1.setName(input.next());
System.out.println("请输入年龄:");
person1.setAge(input.nextInt());
System.out.println("是否占床:1.占床2.不占床");
int bed = input.nextInt();
person1.isOk = true;
if (bed == 1) {
money += person1.calMoney();
System.out.println("姓名:" + person1.getName() + ",年龄:" + person1.getAge() + ",本次出行金额"
+ person1.calMoney() + "元。");
} else {
person1.isOk = false;
System.out.println(
"姓名:" + person1.getName() + ",年龄:" + person1.getAge() + ",不占床位不收钱,本次出行金额" + 0 + "元。");
}
// 将信息导入泛型集合中
personMap.put(person1.getName(), person1);
order.setPersonMap(personMap);
break;
case 3:
// 老人
person = new Old();// 老年
System.out.println("请输入姓名:");
person.setName(input.next());
System.out.println("请输入年龄:");
int age = input.nextInt();
person.setAge(age);
// 如果年龄符合要求才收钱!
if (age > 65) {
money += person.calMoney();
System.out.println("姓名:" + person.getName() + ",年龄:" + person.getAge() + ",65岁以上老人半价,本次出行金额"
+ person.calMoney() + "元。");
// 将信息导入泛型集合中
personMap.put(person.getName(), person);
}
break;// 第二个case的
}
System.out.println("是否继续添加(Y/N)?");
answer = input.next();
// 如果等于y就继续循环
} while ("y".equals(answer));// 循环点
System.out.println("**************订单信息**************");
System.out.println("出行日期:" + order.getDate());
System.out.println("联系人手机号码:" + order.getPhone());
System.out.println("订单总金额:" + money + "元。");
} catch (ParseException e) {
e.printStackTrace();
}
orders.add(order);
// 返回主菜单
menu();
}
// 显看出行订单
public void show() {
// 查看我的出行订单
for (Order order : orders) {
order.show();
}
// 再返回主菜单
menu();
}
// 修改出行订单信息
public void update() {
System.out.println("请输入订单的出行日期:");
String date1 = input.next();
// 将接收到的字符串转换成日期Date的格式
SimpleDateFormat fmt1 = new SimpleDateFormat("yyyy-M-d");
Date date = null;
try {
date = fmt1.parse(date1);
} catch (ParseException e1) {
e1.printStackTrace();
}
System.out.println("请输入订单的联系人电话号码:");
String phone = input.next();
boolean bool = false;
// 循环判断输入的日期和手机号码是否匹配
for (Order order : orders) {
if (order.getDate().equals(date) && order.getPhone().contentEquals(phone)) {
bool = true;
break;
}
}
// 判断是否存在,输入是否合法
if (!bool) {
System.out.println("没有找到该订单!");
} else {
System.out.println("请输入新的订单的出行日期:");
String dateString = input.next();
System.out.println("请输入新的订单的联系人电话号码:");
String phoneString = input.next();
try {
if (phoneString.length() != 11) {
throw new Exception("手机号必须为11位");
}
} catch (Exception e) {
e.printStackTrace();
}
// 修改信息
for (Order order : orders) {
if (order.getDate().equals(date) && order.getDate().equals(phone)) {
// 进行日期的转换
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-M-d");
Date date2;
try {
date2 = fmt.parse(dateString);
order.setDate(date2);
} catch (ParseException e1) {
e1.printStackTrace();
}
order.setPhone(phoneString);
}
}
}
// 之后返回主菜单
menu();
}
// 删除出行订单
public void del() {
System.out.println("请输入要删除的出行人姓名:");
deletename = input.next();
// 通过泛型的键值对循环查找再删除,若是没有找到该名字
boolean bool = false;
for (Order order : orders) {
for (String key : order.getPersonMap().keySet()) {
if (key.equals(deletename)) {
order.getPersonMap().remove(key);
bool = true;
break;
}
}
}
if (bool) {
System.out.println("删除成功!");
} else {
System.out.println("对不起,订单中不包含此游客信息!");
}
// 返回主菜单
menu();
}
// 查看行程
public void journey() {
// 查看行程
// 通过文档流读取文件信息并输出
InputStream fis = null;
try {
fis = new FileInputStream("E:\\2.txt");
// 实现读取操作
// 用数组保存读取到的信息
byte[] words = new byte[1024];
int len;// 存储读入数组的长度
while ((len = fis.read(words)) != -1) {
System.out.println(new String(words, 0, len));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭流
try {
if (fis != null) {
fis.close();
// System.out.println("数据流关闭成功!");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 返回主菜单
menu();
}
// 显示菜单功能
public void menu() {
input = new Scanner(System.in);
System.out.println("************欢迎光临阳光旅游网*************");
System.out.println("1.添加出行订单2.查看我的出行订单3.修改订单信息4.删除出行人5.查看行程6.退出系统" + "请选择(1~6):");
int number = input.nextInt();
switch (number) {
case 1:
// 添加出行订单
add();
break;
case 2:
// 查看我的出行订单
show();
break;
case 3:
// 修改出行订单信息
update();
break;
case 4:
// 删除出行人
del();
break;
case 5:
// 查看行程
journey();
break;
case 6:
// 退出系统
System.out.println("欢迎下次再来!");
break;
}
}
}
再加上最后的一个测试类:
package com.pojo;
/**
* 测试类
*
* @author Think
*
*/
public class TestManager {
public static void main(String[] args) {
// 创建Manager的实列对象并调用菜单的方法
Manager man = new Manager();
man.menu();
}
}