第一次使用面向对象的思路写了一个简单的小程序
目录
domain包
Car类
User类
service包
RentCarService类
utils包
Unityutils类
view包
RentCarView类
RentCar_App类
程序的包名 和 包的作用:
程序名:rentcar
1.用户信息的包:domain
2.用户对象的操作包:service
3.程序公用的一些工具:utils
4.程序的界面包:view
汽车租赁系统的APP类
domain包
Car类
package rentcar.domain;
public class Car {
private String carType;
private int carPrice;
//空构造器
public Car() {
}
public String getCarType() {
return carType;
}
public void setCarType(String carType) {
this.carType = carType;
}
public int getCarPrice() {
return carPrice;
}
public void setCarPrice(int carPrice) {
this.carPrice = carPrice;
}
}
User类
package rentcar.domain;
public class User {
private String userAccount;//用户账号
private String usePassWord;//用户密码
private String nowDate;//租赁的日期
private int rentDuration;//租赁的时长
private int firstMoney;//预付款
private int lastMoney;//尾款
private Car car;//Car 对象
public User(String account, String passWord) {
this.userAccount = account;
this.usePassWord = passWord;
}
public User(){
}
//get 和 set 方法
public String getUserAccount() {
return userAccount;
}
public void setUserAccount(String userAccount) {
this.userAccount = userAccount;
}
public String getUsePassWord() {
return usePassWord;
}
public void setUsePassWord(String usePassWord) {
this.usePassWord = usePassWord;
}
public String getNowDate() {
return nowDate;
}
public void setNowDate(String nowDate) {
this.nowDate = nowDate;
}
public int getRentDuration() {
return rentDuration;
}
public void setRentDuration(int rentDuration) {
this.rentDuration = rentDuration;
}
public int getFirstMoney() {
return firstMoney;
}
public void setFirstMoney(int firstMoney) {
this.firstMoney = firstMoney;
}
public int getLastMoney() {
return lastMoney;
}
public void setLastMoney(int lastMoney) {
this.lastMoney = lastMoney;
}
public void changeUserCar(){
car = null;
}
//创建一个 user对象是 需要初始化Car对象
public Car setUserCar(){
car = new Car();
return car;
}
public Car getCar(){
return car;
}
}
service包
RentCarService类
package rentcar.service;
import rentcar.domain.Car;
import rentcar.domain.User;
public class RentCarService {
//在这里进行车辆的操作 车辆个数有限
private static int bk = 5;
private static int bm = 10;
private static int bkly = 15;
private static int jb = 5;
private static int jl = 10;
private User[] users;//创建了用户的数组
private int index ;//默认值为0
//构造器 为数组创建对象
public RentCarService(int size){
users = new User[size];
}
public RentCarService(){
}
public static int getBk() {
return bk;
}
public static void setBk(int bk) {
RentCarService.bk = bk;
}
public static int getBm() {
return bm;
}
public static void setBm(int bm) {
RentCarService.bm = bm;
}
public static int getBkly() {
return bkly;
}
public static void setBkly(int bkly) {
RentCarService.bkly = bkly;
}
public static int getJb() {
return jb;
}
public static void setJb(int jb) {
RentCarService.jb = jb;
}
public static int getJl() {
return jl;
}
public static void setJl(int jl) {
RentCarService.jl = jl;
}
//因为数组是私有的 需要有个方法返回
public User[] getUser(){
return users;
}
public boolean addNewUser(User newUser){
if(users.length == 0){
System.out.println("系统错误");
return false;
}
if(index == users.length){
System.out.println("内存已满");
return false;
}else{
users[index++] = newUser;
}
return true;
}
//判断账号 是否已经在对象中存在 需要对对象进行遍历 所以写在操作类中 如果合理就返回true 否则false
public boolean accountReasonable(String account){
if(users.length == 0){
//如果 对象数组的长度 是0 就不能创建 返回
System.out.println("系统错误");
return false;
}
for(int i = 0 ; i < users.length ; i++){
if(users[i] == null){
return true;
}else{
if((users[i].getUserAccount()).equals(account)){
return false;
}
}
}
return true;
}
//根据存在的账号 返回账号在对象数组的索引位置
public int getIndex(String account){
int index;
if(users[0] == null){
System.out.println("暂无信息");
return -1;
}
for ( index = 0; index < users.length; index++) {
if(users[index].getUserAccount().equals(account)){
break;
}
}
return index;
}
//对象需要支付预金 通过索引 支付一半
public int FirstMoney(Car car, int rentTime){
return (car.getCarPrice()*rentTime)/2;
}
//获取对象应该支付的尾款
public int getLastMoney(User user){
return user.getLastMoney();
}
//对象支付尾款 这时候需要将用户的所有除了账户 密码 外的所有清零
public boolean clearLastMoney(User user,int lastMoney){
if(lastMoney == user.getLastMoney()){
user.setLastMoney(0);//尾款 0
user.setFirstMoney(0);//预款 0
user.setNowDate(null);
user.setRentDuration(0);
user.changeUserCar();
return true;//返回消除成功
}
return false;
}
//列出所有用户的信息
public void userInfor(User user){
if(user.getCar().getCarType() == null){
System.out.println("未租赁车辆");
}else{
System.out.println("车辆类型:"+user.getCar().getCarType());
System.out.println("租赁天数:"+user.getRentDuration());
System.out.println("预付款:"+user.getFirstMoney());
System.out.println("尾款:"+user.getLastMoney());
}
}
}
utils包
Unityutils类
package rentcar.utils;
import java.text.SimpleDateFormat;//格式化 日期
import java.util.Calendar;//抽象类 日历
import java.util.Date;//日期类
import java.util.GregorianCalendar;
import java.util.Scanner;//输入类
public class UnityUtils {
private static Date date;
private static Scanner sc = new Scanner(System.in);
private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
private static Calendar ca = Calendar.getInstance();
private static String readBoard(int limit,boolean blankBoard){
String input = "";
do{
//直接回车键
input = sc.nextLine();
if(input == "r"){
if(limit == 0){
//允许输入不输入任何东西
if(blankBoard == true){
return input;
}else{
continue;//否则就要接着输入
}
}
}else{
if(input.length() > limit){
System.out.println("输入内容长度不能大于:"+limit);
System.out.print("重新输入:");
continue;
}
return input;
}
}while (sc.hasNextLine());
return input;
}
//单个字符的输入
public static char readChar(){
return readBoard(1, false).charAt(0);
}
public static String TargetString(int number,char target){
StringBuilder finalString = new StringBuilder();
for (int i = 0; i < number; i++) {
finalString.append(target);
}
return new String(finalString);
}
public static String readLine(String regex,int limit){
String finalString = "";
do{
finalString = readBoard(limit, false);
if( finalString.length() != limit ){
System.out.println("输入长度应为:"+limit);
System.out.print("重新输入:");
}else{
if(finalString.matches(regex) == true){
return finalString;
}else{
System.out.println("输入不合法");
System.out.print("按照要求重新输入:");
}
}
}while (true);
}
public static int readNumber(){
return Integer.parseInt(readLine());
}
//readLine 方法的重载 只需要输入字符串 预先给定一个limit 只会提醒输入的字符串长度超过20
public static String readLine(){
String input = "";
input = readBoard(20, false);
return input;
}
//随机产生 limit长度 的验证码 返回字符串
public static String randomGet(int limit){
char [] arr = new char[limit];
for(int i = 0 ; i < limit ;i++) { //控制其验证码个数
arr[i]= 0==(int)(Math.random()*2)? //随机生成大写或小写字母
(char) ('A'+(int)(Math.random()*26)):
(char) ('a'+(int)(Math.random()*26));
}
String str = new String(arr);
return str;
}
public static boolean judgeSame(String str1,String str2,String str3,String str4){
if(str1.equals(str2) && str3.equals(str4)){
return true;
}
return false;
}
//比较两个字符串 是否相同
public static boolean judgeSame(String str1,String str2){
if(str1.equals(str2)){
return true;
}
return false;
}
//传入一个字符串数组 查看字符串 是否在这个字符串数组中
public static boolean carNameReasonable(String[] strings,String str){
for(String index:strings){
if(str.equals(index)){
return true;//存在
}
}
return false;//不存在
}
//获取现在的日期Date类型
public static Date getDate(){
date = new Date();
return date;
}
//将Date类型转换成String类型
public static String getStringDate(Date date) {
return simpleDateFormat.format(date);
}
public static String getStringDate(Date date,int duration) {
ca.setTime(date);
ca.add(Calendar.DATE, duration);
date = ca.getTime();
String enddate = simpleDateFormat.format(date);//将这个日期直接转换成字符串
return enddate;
}
public static int getCarsMoney(String[] strings,int[] ints,String str){
int i;
for( i = 0 ; i < strings.length ; i++){
if(str.equals(strings[i])){
break;
}
}
return ints[i];
}
}
view包
RentCarView类
package rentcar.view;
import com.demo.phonesystem.utils.Unity;
import rentcar.domain.Car;
import rentcar.domain.User;
import rentcar.service.RentCarService;
import rentcar.utils.UnityUtils;
import java.text.ParseException;
import java.util.Date;
public class RentCarView {
private String[] carsName = {"别克商务","宝马550i","别克林荫大道","金杯","金龙"};
private int[] carsMoney = {600,500,300,800,1500};
char aChar; //接受用户的单个字符进行判断
private boolean flag;//默认值false
private RentCarService service = new RentCarService(10);//假设只能有10个用户 创建操作对象 通过这个对象调用数组 来进行添加
public void sysMenu() throws ParseException {
//标题 ╭───────────╮
//欢迎来到:│CarsRentSys┃
// ╰━━━━━━━━━━━╯
System.out.println("tt╭───────────╮");
System.out.print("欢迎来到:");
System.out.println("│CarsRentSys┃");
System.out.println("tt╰━━━━━━━━━━━╯");
do{
//选项说明
System.out.println(UnityUtils.TargetString(65, '='));
System.out.println("选项(A)");
System.out.println("tA.查询车辆");//输入 本系统所能租赁的车辆种类 金额 和个数
System.out.println("tB.注册账号");
System.out.println("tC.登录租赁");//登录账号之后进行操作 真正的操作
System.out.print("请输入:");
aChar = UnityUtils.readChar(); //输入单个字符 存给key
//通过switch 选择结构 进行选择 进入其他的界面
switch (aChar){
case 'A':
listCarsType();
break;
case 'B':
//注册对象的账号和密码
addNewUser();
break;
case 'C'://登录并且进行下一步操作
signIn();
break;
default:
System.out.println("输入不合法,");
break;
}
}while(!flag);
}
public void listCarsType(){
//列表
System.out.println("tttttt轿车ttttttt客车(金杯、金龙)");
System.out.println("车型tttt别克商务 宝马550i 别克林荫大道ttt<=16座 >16座");
System.out.println("日租费/天tt600 500 300tttt800 1500");
System.out.println("剩余tttt"+service.getBk()+"tt"+service.getBm()+"ttt"+service.getBkly()+
"tttt"+service.getJb()+"tt"+service.getJl()
);
System.out.println(UnityUtils.TargetString(65, '='));
}
//创建新用户的界面
public void addNewUser(){
System.out.print("注册账号(共6位,首位必须是大小写字符,末尾不能是0):");
String account,passWord,randomCode,inputCode;
do{
//先创建账号
account = UnityUtils.readLine("[A-z]\d{4}[1-9]",6);
//账号的输入是否合理 还需要查看 账号是否已经存在
if(service.accountReasonable(account)){
break;
}else{
System.out.println("账号已存在");
System.out.print("重新输入:");
}
}while (!flag);
System.out.print("创建密码(共10位,首位大写,末尾以@结尾):");
do{
passWord = UnityUtils.readLine("[A-Z]\d{8}@", 10);
System.out.print("再次输入密码:");
String againPassWord = UnityUtils.readLine();//readLine 方法的重载
//随机生成验证码
randomCode = UnityUtils.randomGet(5);
System.out.println("验证码:"+randomCode);
System.out.print("输入验证码:");
inputCode = UnityUtils.readLine();
//判断两次密码 验证码 是否相同
if(UnityUtils.judgeSame(passWord, againPassWord, randomCode, inputCode)){
break;
}
}while (!flag);
//一切完成 创建数组中的元素对象
User newUser = new User(account,passWord);
//将这个对象添加到数组中 作为数组的元素
if(service.addNewUser(newUser)){
System.out.println("创建成功");
}
}
//登录账号
public void signIn() throws ParseException {
int sum = 0;
String account;
String passWord;
String randomCode;
String temp;
int index = 0;
do{
System.out.print("输入账号:");
account = UnityUtils.readLine();
System.out.print("输入密码:");
passWord = UnityUtils.readLine();
System.out.print("输入验证码("+(temp = UnityUtils.randomGet(4))+"):");
randomCode = UnityUtils.readLine();
if(!UnityUtils.judgeSame(temp, randomCode)){
System.out.println("验证码错误");
continue;//验证码错误 直接进入下一次循环
}
//找到对象的位置
index = service.getIndex(account);
if(index == -1){
//返回系统
System.out.println("返回主菜单");
sysMenu();
}
if(!(UnityUtils.judgeSame((service.getUser())[index].getUsePassWord(), passWord))|| (service.accountReasonable(account))){
if(sum > 3){
System.out.println("输入超三次,重新进入");
}
sum ++;
System.out.println("账号或密码错误(三次机会),剩余"+(3-sum)+"次");
continue;//剩余次数还没用完 直接进行下一次循环
}else{
System.out.println("登陆成功");
break;
}
}while (!flag);
operateMenu(index);
}
//操作页面 这里需要有个有参的 来对对象操作
public void operateMenu(int index) throws ParseException {
int firstMoney,lastMoney;//预款 和 尾款
int duration;//租赁的时长
int carPrice;//租车总共的价钱
Date date;//日期
User user = service.getUser()[index];
String yourCarChoice;//自己选择的车辆类型
String input = "";//空字符串
System.out.println("tt╭───────────╮");
System.out.print("用户选项:");
System.out.println("│tA~Et┃");
System.out.println("tt╰━━━━━━━━━━━╯");
System.out.println("tA:查看车辆");
System.out.println("tB:租赁汽车");
System.out.println("tC:支付尾款");
System.out.println("tD:查询租赁");
System.out.println("tE:返回主菜单");
do{
//接受用户的单个输入
System.out.print("输入选项(A~E):");
System.out.print("请输入:");
aChar = UnityUtils.readChar();
switch (aChar){
case 'A':
System.out.print("是否查看车辆信息(Yes or No):");
do{
input = UnityUtils.readLine();
if(UnityUtils.judgeSame(input, "Yes")){
listCarsType();
break;
}else if(UnityUtils.judgeSame(input, "No")){
continue;
}else{
System.out.println("输入不合法,重新输入");
}
}while (true);
break;
//B
case 'B':
yourCarChoice = choiceCar();//车辆名字储存
//判断是否租赁
System.out.println("是否租赁");
System.out.print("输入(Yes[租赁] or No[不租赁]):");
do{
input = UnityUtils.readLine();
if(input.equals("Yes")){
Car car = user.setUserCar();//这个index用户对象的 车辆对象
car.setCarType(yourCarChoice);//将车对象名字赋值
//选择租赁的时间
System.out.print("输入租赁时间:");
duration = UnityUtils.readNumber();
user.setRentDuration(duration);
date = UnityUtils.getDate();
System.out.println("租赁起始时间:"+UnityUtils.getStringDate(date)+"t租赁时长:"+duration
+"t截止时间:"+UnityUtils.getStringDate(date, duration));
System.out.println("所选车辆:"+car.getCarType());
car.setCarPrice(UnityUtils.getCarsMoney(carsName, carsMoney, car.getCarType()));//将价格赋予
user.setFirstMoney(service.FirstMoney(car,duration));//用户对象存预款
firstMoney = user.getFirstMoney();
carPrice = car.getCarPrice()*duration;
System.out.println("总款数:"+carPrice);
System.out.println("预付款:"+firstMoney);//输出预款
user.setLastMoney(carPrice - firstMoney);//尾款存入
}else if(input.equals("No")){
break;
}else{
System.out.println("输入有误");
continue;
}
break;
}while (!flag);
break;
//支付尾款
case 'C':
int yourLastMoney;
System.out.println("您要支付的尾款"+service.getLastMoney(user));
if(user.getLastMoney() == 0){
System.out.println("未租赁汽车");
}else{
while(true){
System.out.println("输入尾款");
yourLastMoney = UnityUtils.readNumber();
if(service.clearLastMoney(service.getUser()[index],yourLastMoney )){
System.out.println("尾款支付完毕");
break;
}else if(!service.clearLastMoney(service.getUser()[index],yourLastMoney )){
System.out.println("请一次性输入尾款");
}else{
System.out.println("输入不合法");
}
}
}
break;
case 'D':
//查询用户的信息
service.userInfor(user);
break;
case 'E':
//返回主界面
sysMenu();
break;
default:
System.out.println("输入不合法,输入()");
}
}while(!flag);
}
// 选车界面
public String choiceCar(){
String carName;//车名
System.out.println("tttttt轿车ttttttt客车(金杯、金龙)");
System.out.println("车型tttt别克商务 宝马550i 别克林荫大道ttt<=16座 >16座");
do{
System.out.print("输入车型:");
carName = UnityUtils.readLine();
if(!UnityUtils.carNameReasonable(carsName, carName)){
System.out.println("车型不存在");
continue;
}
//如果没有继续 就是车型符合
break;
}while (!flag);
//需要输出车的价格
System.out.println(carName+"的日租费:"+UnityUtils.getCarsMoney(carsName, carsMoney, carName));
return carName;
}
}
RentCar_App类
package rentcar;
import rentcar.view.RentCarView;
import java.text.ParseException;
public class RentCar_App {
public static void main(String[] args) throws ParseException {
new RentCarView().sysMenu();
}
}
package rentcar.domain;
public class Car {
private String carType;
private int carPrice;
//空构造器
public Car() {
}
public String getCarType() {
return carType;
}
public void setCarType(String carType) {
this.carType = carType;
}
public int getCarPrice() {
return carPrice;
}
public void setCarPrice(int carPrice) {
this.carPrice = carPrice;
}
}
User类
package rentcar.domain;
public class User {
private String userAccount;//用户账号
private String usePassWord;//用户密码
private String nowDate;//租赁的日期
private int rentDuration;//租赁的时长
private int firstMoney;//预付款
private int lastMoney;//尾款
private Car car;//Car 对象
public User(String account, String passWord) {
this.userAccount = account;
this.usePassWord = passWord;
}
public User(){
}
//get 和 set 方法
public String getUserAccount() {
return userAccount;
}
public void setUserAccount(String userAccount) {
this.userAccount = userAccount;
}
public String getUsePassWord() {
return usePassWord;
}
public void setUsePassWord(String usePassWord) {
this.usePassWord = usePassWord;
}
public String getNowDate() {
return nowDate;
}
public void setNowDate(String nowDate) {
this.nowDate = nowDate;
}
public int getRentDuration() {
return rentDuration;
}
public void setRentDuration(int rentDuration) {
this.rentDuration = rentDuration;
}
public int getFirstMoney() {
return firstMoney;
}
public void setFirstMoney(int firstMoney) {
this.firstMoney = firstMoney;
}
public int getLastMoney() {
return lastMoney;
}
public void setLastMoney(int lastMoney) {
this.lastMoney = lastMoney;
}
public void changeUserCar(){
car = null;
}
//创建一个 user对象是 需要初始化Car对象
public Car setUserCar(){
car = new Car();
return car;
}
public Car getCar(){
return car;
}
}
service包
RentCarService类
package rentcar.service;
import rentcar.domain.Car;
import rentcar.domain.User;
public class RentCarService {
//在这里进行车辆的操作 车辆个数有限
private static int bk = 5;
private static int bm = 10;
private static int bkly = 15;
private static int jb = 5;
private static int jl = 10;
private User[] users;//创建了用户的数组
private int index ;//默认值为0
//构造器 为数组创建对象
public RentCarService(int size){
users = new User[size];
}
public RentCarService(){
}
public static int getBk() {
return bk;
}
public static void setBk(int bk) {
RentCarService.bk = bk;
}
public static int getBm() {
return bm;
}
public static void setBm(int bm) {
RentCarService.bm = bm;
}
public static int getBkly() {
return bkly;
}
public static void setBkly(int bkly) {
RentCarService.bkly = bkly;
}
public static int getJb() {
return jb;
}
public static void setJb(int jb) {
RentCarService.jb = jb;
}
public static int getJl() {
return jl;
}
public static void setJl(int jl) {
RentCarService.jl = jl;
}
//因为数组是私有的 需要有个方法返回
public User[] getUser(){
return users;
}
public boolean addNewUser(User newUser){
if(users.length == 0){
System.out.println("系统错误");
return false;
}
if(index == users.length){
System.out.println("内存已满");
return false;
}else{
users[index++] = newUser;
}
return true;
}
//判断账号 是否已经在对象中存在 需要对对象进行遍历 所以写在操作类中 如果合理就返回true 否则false
public boolean accountReasonable(String account){
if(users.length == 0){
//如果 对象数组的长度 是0 就不能创建 返回
System.out.println("系统错误");
return false;
}
for(int i = 0 ; i < users.length ; i++){
if(users[i] == null){
return true;
}else{
if((users[i].getUserAccount()).equals(account)){
return false;
}
}
}
return true;
}
//根据存在的账号 返回账号在对象数组的索引位置
public int getIndex(String account){
int index;
if(users[0] == null){
System.out.println("暂无信息");
return -1;
}
for ( index = 0; index < users.length; index++) {
if(users[index].getUserAccount().equals(account)){
break;
}
}
return index;
}
//对象需要支付预金 通过索引 支付一半
public int FirstMoney(Car car, int rentTime){
return (car.getCarPrice()*rentTime)/2;
}
//获取对象应该支付的尾款
public int getLastMoney(User user){
return user.getLastMoney();
}
//对象支付尾款 这时候需要将用户的所有除了账户 密码 外的所有清零
public boolean clearLastMoney(User user,int lastMoney){
if(lastMoney == user.getLastMoney()){
user.setLastMoney(0);//尾款 0
user.setFirstMoney(0);//预款 0
user.setNowDate(null);
user.setRentDuration(0);
user.changeUserCar();
return true;//返回消除成功
}
return false;
}
//列出所有用户的信息
public void userInfor(User user){
if(user.getCar().getCarType() == null){
System.out.println("未租赁车辆");
}else{
System.out.println("车辆类型:"+user.getCar().getCarType());
System.out.println("租赁天数:"+user.getRentDuration());
System.out.println("预付款:"+user.getFirstMoney());
System.out.println("尾款:"+user.getLastMoney());
}
}
}
utils包
Unityutils类
package rentcar.utils;
import java.text.SimpleDateFormat;//格式化 日期
import java.util.Calendar;//抽象类 日历
import java.util.Date;//日期类
import java.util.GregorianCalendar;
import java.util.Scanner;//输入类
public class UnityUtils {
private static Date date;
private static Scanner sc = new Scanner(System.in);
private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
private static Calendar ca = Calendar.getInstance();
private static String readBoard(int limit,boolean blankBoard){
String input = "";
do{
//直接回车键
input = sc.nextLine();
if(input == "r"){
if(limit == 0){
//允许输入不输入任何东西
if(blankBoard == true){
return input;
}else{
continue;//否则就要接着输入
}
}
}else{
if(input.length() > limit){
System.out.println("输入内容长度不能大于:"+limit);
System.out.print("重新输入:");
continue;
}
return input;
}
}while (sc.hasNextLine());
return input;
}
//单个字符的输入
public static char readChar(){
return readBoard(1, false).charAt(0);
}
public static String TargetString(int number,char target){
StringBuilder finalString = new StringBuilder();
for (int i = 0; i < number; i++) {
finalString.append(target);
}
return new String(finalString);
}
public static String readLine(String regex,int limit){
String finalString = "";
do{
finalString = readBoard(limit, false);
if( finalString.length() != limit ){
System.out.println("输入长度应为:"+limit);
System.out.print("重新输入:");
}else{
if(finalString.matches(regex) == true){
return finalString;
}else{
System.out.println("输入不合法");
System.out.print("按照要求重新输入:");
}
}
}while (true);
}
public static int readNumber(){
return Integer.parseInt(readLine());
}
//readLine 方法的重载 只需要输入字符串 预先给定一个limit 只会提醒输入的字符串长度超过20
public static String readLine(){
String input = "";
input = readBoard(20, false);
return input;
}
//随机产生 limit长度 的验证码 返回字符串
public static String randomGet(int limit){
char [] arr = new char[limit];
for(int i = 0 ; i < limit ;i++) { //控制其验证码个数
arr[i]= 0==(int)(Math.random()*2)? //随机生成大写或小写字母
(char) ('A'+(int)(Math.random()*26)):
(char) ('a'+(int)(Math.random()*26));
}
String str = new String(arr);
return str;
}
public static boolean judgeSame(String str1,String str2,String str3,String str4){
if(str1.equals(str2) && str3.equals(str4)){
return true;
}
return false;
}
//比较两个字符串 是否相同
public static boolean judgeSame(String str1,String str2){
if(str1.equals(str2)){
return true;
}
return false;
}
//传入一个字符串数组 查看字符串 是否在这个字符串数组中
public static boolean carNameReasonable(String[] strings,String str){
for(String index:strings){
if(str.equals(index)){
return true;//存在
}
}
return false;//不存在
}
//获取现在的日期Date类型
public static Date getDate(){
date = new Date();
return date;
}
//将Date类型转换成String类型
public static String getStringDate(Date date) {
return simpleDateFormat.format(date);
}
public static String getStringDate(Date date,int duration) {
ca.setTime(date);
ca.add(Calendar.DATE, duration);
date = ca.getTime();
String enddate = simpleDateFormat.format(date);//将这个日期直接转换成字符串
return enddate;
}
public static int getCarsMoney(String[] strings,int[] ints,String str){
int i;
for( i = 0 ; i < strings.length ; i++){
if(str.equals(strings[i])){
break;
}
}
return ints[i];
}
}
view包
RentCarView类
package rentcar.view;
import com.demo.phonesystem.utils.Unity;
import rentcar.domain.Car;
import rentcar.domain.User;
import rentcar.service.RentCarService;
import rentcar.utils.UnityUtils;
import java.text.ParseException;
import java.util.Date;
public class RentCarView {
private String[] carsName = {"别克商务","宝马550i","别克林荫大道","金杯","金龙"};
private int[] carsMoney = {600,500,300,800,1500};
char aChar; //接受用户的单个字符进行判断
private boolean flag;//默认值false
private RentCarService service = new RentCarService(10);//假设只能有10个用户 创建操作对象 通过这个对象调用数组 来进行添加
public void sysMenu() throws ParseException {
//标题 ╭───────────╮
//欢迎来到:│CarsRentSys┃
// ╰━━━━━━━━━━━╯
System.out.println("tt╭───────────╮");
System.out.print("欢迎来到:");
System.out.println("│CarsRentSys┃");
System.out.println("tt╰━━━━━━━━━━━╯");
do{
//选项说明
System.out.println(UnityUtils.TargetString(65, '='));
System.out.println("选项(A)");
System.out.println("tA.查询车辆");//输入 本系统所能租赁的车辆种类 金额 和个数
System.out.println("tB.注册账号");
System.out.println("tC.登录租赁");//登录账号之后进行操作 真正的操作
System.out.print("请输入:");
aChar = UnityUtils.readChar(); //输入单个字符 存给key
//通过switch 选择结构 进行选择 进入其他的界面
switch (aChar){
case 'A':
listCarsType();
break;
case 'B':
//注册对象的账号和密码
addNewUser();
break;
case 'C'://登录并且进行下一步操作
signIn();
break;
default:
System.out.println("输入不合法,");
break;
}
}while(!flag);
}
public void listCarsType(){
//列表
System.out.println("tttttt轿车ttttttt客车(金杯、金龙)");
System.out.println("车型tttt别克商务 宝马550i 别克林荫大道ttt<=16座 >16座");
System.out.println("日租费/天tt600 500 300tttt800 1500");
System.out.println("剩余tttt"+service.getBk()+"tt"+service.getBm()+"ttt"+service.getBkly()+
"tttt"+service.getJb()+"tt"+service.getJl()
);
System.out.println(UnityUtils.TargetString(65, '='));
}
//创建新用户的界面
public void addNewUser(){
System.out.print("注册账号(共6位,首位必须是大小写字符,末尾不能是0):");
String account,passWord,randomCode,inputCode;
do{
//先创建账号
account = UnityUtils.readLine("[A-z]\d{4}[1-9]",6);
//账号的输入是否合理 还需要查看 账号是否已经存在
if(service.accountReasonable(account)){
break;
}else{
System.out.println("账号已存在");
System.out.print("重新输入:");
}
}while (!flag);
System.out.print("创建密码(共10位,首位大写,末尾以@结尾):");
do{
passWord = UnityUtils.readLine("[A-Z]\d{8}@", 10);
System.out.print("再次输入密码:");
String againPassWord = UnityUtils.readLine();//readLine 方法的重载
//随机生成验证码
randomCode = UnityUtils.randomGet(5);
System.out.println("验证码:"+randomCode);
System.out.print("输入验证码:");
inputCode = UnityUtils.readLine();
//判断两次密码 验证码 是否相同
if(UnityUtils.judgeSame(passWord, againPassWord, randomCode, inputCode)){
break;
}
}while (!flag);
//一切完成 创建数组中的元素对象
User newUser = new User(account,passWord);
//将这个对象添加到数组中 作为数组的元素
if(service.addNewUser(newUser)){
System.out.println("创建成功");
}
}
//登录账号
public void signIn() throws ParseException {
int sum = 0;
String account;
String passWord;
String randomCode;
String temp;
int index = 0;
do{
System.out.print("输入账号:");
account = UnityUtils.readLine();
System.out.print("输入密码:");
passWord = UnityUtils.readLine();
System.out.print("输入验证码("+(temp = UnityUtils.randomGet(4))+"):");
randomCode = UnityUtils.readLine();
if(!UnityUtils.judgeSame(temp, randomCode)){
System.out.println("验证码错误");
continue;//验证码错误 直接进入下一次循环
}
//找到对象的位置
index = service.getIndex(account);
if(index == -1){
//返回系统
System.out.println("返回主菜单");
sysMenu();
}
if(!(UnityUtils.judgeSame((service.getUser())[index].getUsePassWord(), passWord))|| (service.accountReasonable(account))){
if(sum > 3){
System.out.println("输入超三次,重新进入");
}
sum ++;
System.out.println("账号或密码错误(三次机会),剩余"+(3-sum)+"次");
continue;//剩余次数还没用完 直接进行下一次循环
}else{
System.out.println("登陆成功");
break;
}
}while (!flag);
operateMenu(index);
}
//操作页面 这里需要有个有参的 来对对象操作
public void operateMenu(int index) throws ParseException {
int firstMoney,lastMoney;//预款 和 尾款
int duration;//租赁的时长
int carPrice;//租车总共的价钱
Date date;//日期
User user = service.getUser()[index];
String yourCarChoice;//自己选择的车辆类型
String input = "";//空字符串
System.out.println("tt╭───────────╮");
System.out.print("用户选项:");
System.out.println("│tA~Et┃");
System.out.println("tt╰━━━━━━━━━━━╯");
System.out.println("tA:查看车辆");
System.out.println("tB:租赁汽车");
System.out.println("tC:支付尾款");
System.out.println("tD:查询租赁");
System.out.println("tE:返回主菜单");
do{
//接受用户的单个输入
System.out.print("输入选项(A~E):");
System.out.print("请输入:");
aChar = UnityUtils.readChar();
switch (aChar){
case 'A':
System.out.print("是否查看车辆信息(Yes or No):");
do{
input = UnityUtils.readLine();
if(UnityUtils.judgeSame(input, "Yes")){
listCarsType();
break;
}else if(UnityUtils.judgeSame(input, "No")){
continue;
}else{
System.out.println("输入不合法,重新输入");
}
}while (true);
break;
//B
case 'B':
yourCarChoice = choiceCar();//车辆名字储存
//判断是否租赁
System.out.println("是否租赁");
System.out.print("输入(Yes[租赁] or No[不租赁]):");
do{
input = UnityUtils.readLine();
if(input.equals("Yes")){
Car car = user.setUserCar();//这个index用户对象的 车辆对象
car.setCarType(yourCarChoice);//将车对象名字赋值
//选择租赁的时间
System.out.print("输入租赁时间:");
duration = UnityUtils.readNumber();
user.setRentDuration(duration);
date = UnityUtils.getDate();
System.out.println("租赁起始时间:"+UnityUtils.getStringDate(date)+"t租赁时长:"+duration
+"t截止时间:"+UnityUtils.getStringDate(date, duration));
System.out.println("所选车辆:"+car.getCarType());
car.setCarPrice(UnityUtils.getCarsMoney(carsName, carsMoney, car.getCarType()));//将价格赋予
user.setFirstMoney(service.FirstMoney(car,duration));//用户对象存预款
firstMoney = user.getFirstMoney();
carPrice = car.getCarPrice()*duration;
System.out.println("总款数:"+carPrice);
System.out.println("预付款:"+firstMoney);//输出预款
user.setLastMoney(carPrice - firstMoney);//尾款存入
}else if(input.equals("No")){
break;
}else{
System.out.println("输入有误");
continue;
}
break;
}while (!flag);
break;
//支付尾款
case 'C':
int yourLastMoney;
System.out.println("您要支付的尾款"+service.getLastMoney(user));
if(user.getLastMoney() == 0){
System.out.println("未租赁汽车");
}else{
while(true){
System.out.println("输入尾款");
yourLastMoney = UnityUtils.readNumber();
if(service.clearLastMoney(service.getUser()[index],yourLastMoney )){
System.out.println("尾款支付完毕");
break;
}else if(!service.clearLastMoney(service.getUser()[index],yourLastMoney )){
System.out.println("请一次性输入尾款");
}else{
System.out.println("输入不合法");
}
}
}
break;
case 'D':
//查询用户的信息
service.userInfor(user);
break;
case 'E':
//返回主界面
sysMenu();
break;
default:
System.out.println("输入不合法,输入()");
}
}while(!flag);
}
// 选车界面
public String choiceCar(){
String carName;//车名
System.out.println("tttttt轿车ttttttt客车(金杯、金龙)");
System.out.println("车型tttt别克商务 宝马550i 别克林荫大道ttt<=16座 >16座");
do{
System.out.print("输入车型:");
carName = UnityUtils.readLine();
if(!UnityUtils.carNameReasonable(carsName, carName)){
System.out.println("车型不存在");
continue;
}
//如果没有继续 就是车型符合
break;
}while (!flag);
//需要输出车的价格
System.out.println(carName+"的日租费:"+UnityUtils.getCarsMoney(carsName, carsMoney, carName));
return carName;
}
}
RentCar_App类
package rentcar;
import rentcar.view.RentCarView;
import java.text.ParseException;
public class RentCar_App {
public static void main(String[] args) throws ParseException {
new RentCarView().sysMenu();
}
}
RentCarService类
package rentcar.service;
import rentcar.domain.Car;
import rentcar.domain.User;
public class RentCarService {
//在这里进行车辆的操作 车辆个数有限
private static int bk = 5;
private static int bm = 10;
private static int bkly = 15;
private static int jb = 5;
private static int jl = 10;
private User[] users;//创建了用户的数组
private int index ;//默认值为0
//构造器 为数组创建对象
public RentCarService(int size){
users = new User[size];
}
public RentCarService(){
}
public static int getBk() {
return bk;
}
public static void setBk(int bk) {
RentCarService.bk = bk;
}
public static int getBm() {
return bm;
}
public static void setBm(int bm) {
RentCarService.bm = bm;
}
public static int getBkly() {
return bkly;
}
public static void setBkly(int bkly) {
RentCarService.bkly = bkly;
}
public static int getJb() {
return jb;
}
public static void setJb(int jb) {
RentCarService.jb = jb;
}
public static int getJl() {
return jl;
}
public static void setJl(int jl) {
RentCarService.jl = jl;
}
//因为数组是私有的 需要有个方法返回
public User[] getUser(){
return users;
}
public boolean addNewUser(User newUser){
if(users.length == 0){
System.out.println("系统错误");
return false;
}
if(index == users.length){
System.out.println("内存已满");
return false;
}else{
users[index++] = newUser;
}
return true;
}
//判断账号 是否已经在对象中存在 需要对对象进行遍历 所以写在操作类中 如果合理就返回true 否则false
public boolean accountReasonable(String account){
if(users.length == 0){
//如果 对象数组的长度 是0 就不能创建 返回
System.out.println("系统错误");
return false;
}
for(int i = 0 ; i < users.length ; i++){
if(users[i] == null){
return true;
}else{
if((users[i].getUserAccount()).equals(account)){
return false;
}
}
}
return true;
}
//根据存在的账号 返回账号在对象数组的索引位置
public int getIndex(String account){
int index;
if(users[0] == null){
System.out.println("暂无信息");
return -1;
}
for ( index = 0; index < users.length; index++) {
if(users[index].getUserAccount().equals(account)){
break;
}
}
return index;
}
//对象需要支付预金 通过索引 支付一半
public int FirstMoney(Car car, int rentTime){
return (car.getCarPrice()*rentTime)/2;
}
//获取对象应该支付的尾款
public int getLastMoney(User user){
return user.getLastMoney();
}
//对象支付尾款 这时候需要将用户的所有除了账户 密码 外的所有清零
public boolean clearLastMoney(User user,int lastMoney){
if(lastMoney == user.getLastMoney()){
user.setLastMoney(0);//尾款 0
user.setFirstMoney(0);//预款 0
user.setNowDate(null);
user.setRentDuration(0);
user.changeUserCar();
return true;//返回消除成功
}
return false;
}
//列出所有用户的信息
public void userInfor(User user){
if(user.getCar().getCarType() == null){
System.out.println("未租赁车辆");
}else{
System.out.println("车辆类型:"+user.getCar().getCarType());
System.out.println("租赁天数:"+user.getRentDuration());
System.out.println("预付款:"+user.getFirstMoney());
System.out.println("尾款:"+user.getLastMoney());
}
}
}
utils包
Unityutils类
package rentcar.utils;
import java.text.SimpleDateFormat;//格式化 日期
import java.util.Calendar;//抽象类 日历
import java.util.Date;//日期类
import java.util.GregorianCalendar;
import java.util.Scanner;//输入类
public class UnityUtils {
private static Date date;
private static Scanner sc = new Scanner(System.in);
private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
private static Calendar ca = Calendar.getInstance();
private static String readBoard(int limit,boolean blankBoard){
String input = "";
do{
//直接回车键
input = sc.nextLine();
if(input == "r"){
if(limit == 0){
//允许输入不输入任何东西
if(blankBoard == true){
return input;
}else{
continue;//否则就要接着输入
}
}
}else{
if(input.length() > limit){
System.out.println("输入内容长度不能大于:"+limit);
System.out.print("重新输入:");
continue;
}
return input;
}
}while (sc.hasNextLine());
return input;
}
//单个字符的输入
public static char readChar(){
return readBoard(1, false).charAt(0);
}
public static String TargetString(int number,char target){
StringBuilder finalString = new StringBuilder();
for (int i = 0; i < number; i++) {
finalString.append(target);
}
return new String(finalString);
}
public static String readLine(String regex,int limit){
String finalString = "";
do{
finalString = readBoard(limit, false);
if( finalString.length() != limit ){
System.out.println("输入长度应为:"+limit);
System.out.print("重新输入:");
}else{
if(finalString.matches(regex) == true){
return finalString;
}else{
System.out.println("输入不合法");
System.out.print("按照要求重新输入:");
}
}
}while (true);
}
public static int readNumber(){
return Integer.parseInt(readLine());
}
//readLine 方法的重载 只需要输入字符串 预先给定一个limit 只会提醒输入的字符串长度超过20
public static String readLine(){
String input = "";
input = readBoard(20, false);
return input;
}
//随机产生 limit长度 的验证码 返回字符串
public static String randomGet(int limit){
char [] arr = new char[limit];
for(int i = 0 ; i < limit ;i++) { //控制其验证码个数
arr[i]= 0==(int)(Math.random()*2)? //随机生成大写或小写字母
(char) ('A'+(int)(Math.random()*26)):
(char) ('a'+(int)(Math.random()*26));
}
String str = new String(arr);
return str;
}
public static boolean judgeSame(String str1,String str2,String str3,String str4){
if(str1.equals(str2) && str3.equals(str4)){
return true;
}
return false;
}
//比较两个字符串 是否相同
public static boolean judgeSame(String str1,String str2){
if(str1.equals(str2)){
return true;
}
return false;
}
//传入一个字符串数组 查看字符串 是否在这个字符串数组中
public static boolean carNameReasonable(String[] strings,String str){
for(String index:strings){
if(str.equals(index)){
return true;//存在
}
}
return false;//不存在
}
//获取现在的日期Date类型
public static Date getDate(){
date = new Date();
return date;
}
//将Date类型转换成String类型
public static String getStringDate(Date date) {
return simpleDateFormat.format(date);
}
public static String getStringDate(Date date,int duration) {
ca.setTime(date);
ca.add(Calendar.DATE, duration);
date = ca.getTime();
String enddate = simpleDateFormat.format(date);//将这个日期直接转换成字符串
return enddate;
}
public static int getCarsMoney(String[] strings,int[] ints,String str){
int i;
for( i = 0 ; i < strings.length ; i++){
if(str.equals(strings[i])){
break;
}
}
return ints[i];
}
}
view包
RentCarView类
package rentcar.view;
import com.demo.phonesystem.utils.Unity;
import rentcar.domain.Car;
import rentcar.domain.User;
import rentcar.service.RentCarService;
import rentcar.utils.UnityUtils;
import java.text.ParseException;
import java.util.Date;
public class RentCarView {
private String[] carsName = {"别克商务","宝马550i","别克林荫大道","金杯","金龙"};
private int[] carsMoney = {600,500,300,800,1500};
char aChar; //接受用户的单个字符进行判断
private boolean flag;//默认值false
private RentCarService service = new RentCarService(10);//假设只能有10个用户 创建操作对象 通过这个对象调用数组 来进行添加
public void sysMenu() throws ParseException {
//标题 ╭───────────╮
//欢迎来到:│CarsRentSys┃
// ╰━━━━━━━━━━━╯
System.out.println("tt╭───────────╮");
System.out.print("欢迎来到:");
System.out.println("│CarsRentSys┃");
System.out.println("tt╰━━━━━━━━━━━╯");
do{
//选项说明
System.out.println(UnityUtils.TargetString(65, '='));
System.out.println("选项(A)");
System.out.println("tA.查询车辆");//输入 本系统所能租赁的车辆种类 金额 和个数
System.out.println("tB.注册账号");
System.out.println("tC.登录租赁");//登录账号之后进行操作 真正的操作
System.out.print("请输入:");
aChar = UnityUtils.readChar(); //输入单个字符 存给key
//通过switch 选择结构 进行选择 进入其他的界面
switch (aChar){
case 'A':
listCarsType();
break;
case 'B':
//注册对象的账号和密码
addNewUser();
break;
case 'C'://登录并且进行下一步操作
signIn();
break;
default:
System.out.println("输入不合法,");
break;
}
}while(!flag);
}
public void listCarsType(){
//列表
System.out.println("tttttt轿车ttttttt客车(金杯、金龙)");
System.out.println("车型tttt别克商务 宝马550i 别克林荫大道ttt<=16座 >16座");
System.out.println("日租费/天tt600 500 300tttt800 1500");
System.out.println("剩余tttt"+service.getBk()+"tt"+service.getBm()+"ttt"+service.getBkly()+
"tttt"+service.getJb()+"tt"+service.getJl()
);
System.out.println(UnityUtils.TargetString(65, '='));
}
//创建新用户的界面
public void addNewUser(){
System.out.print("注册账号(共6位,首位必须是大小写字符,末尾不能是0):");
String account,passWord,randomCode,inputCode;
do{
//先创建账号
account = UnityUtils.readLine("[A-z]\d{4}[1-9]",6);
//账号的输入是否合理 还需要查看 账号是否已经存在
if(service.accountReasonable(account)){
break;
}else{
System.out.println("账号已存在");
System.out.print("重新输入:");
}
}while (!flag);
System.out.print("创建密码(共10位,首位大写,末尾以@结尾):");
do{
passWord = UnityUtils.readLine("[A-Z]\d{8}@", 10);
System.out.print("再次输入密码:");
String againPassWord = UnityUtils.readLine();//readLine 方法的重载
//随机生成验证码
randomCode = UnityUtils.randomGet(5);
System.out.println("验证码:"+randomCode);
System.out.print("输入验证码:");
inputCode = UnityUtils.readLine();
//判断两次密码 验证码 是否相同
if(UnityUtils.judgeSame(passWord, againPassWord, randomCode, inputCode)){
break;
}
}while (!flag);
//一切完成 创建数组中的元素对象
User newUser = new User(account,passWord);
//将这个对象添加到数组中 作为数组的元素
if(service.addNewUser(newUser)){
System.out.println("创建成功");
}
}
//登录账号
public void signIn() throws ParseException {
int sum = 0;
String account;
String passWord;
String randomCode;
String temp;
int index = 0;
do{
System.out.print("输入账号:");
account = UnityUtils.readLine();
System.out.print("输入密码:");
passWord = UnityUtils.readLine();
System.out.print("输入验证码("+(temp = UnityUtils.randomGet(4))+"):");
randomCode = UnityUtils.readLine();
if(!UnityUtils.judgeSame(temp, randomCode)){
System.out.println("验证码错误");
continue;//验证码错误 直接进入下一次循环
}
//找到对象的位置
index = service.getIndex(account);
if(index == -1){
//返回系统
System.out.println("返回主菜单");
sysMenu();
}
if(!(UnityUtils.judgeSame((service.getUser())[index].getUsePassWord(), passWord))|| (service.accountReasonable(account))){
if(sum > 3){
System.out.println("输入超三次,重新进入");
}
sum ++;
System.out.println("账号或密码错误(三次机会),剩余"+(3-sum)+"次");
continue;//剩余次数还没用完 直接进行下一次循环
}else{
System.out.println("登陆成功");
break;
}
}while (!flag);
operateMenu(index);
}
//操作页面 这里需要有个有参的 来对对象操作
public void operateMenu(int index) throws ParseException {
int firstMoney,lastMoney;//预款 和 尾款
int duration;//租赁的时长
int carPrice;//租车总共的价钱
Date date;//日期
User user = service.getUser()[index];
String yourCarChoice;//自己选择的车辆类型
String input = "";//空字符串
System.out.println("tt╭───────────╮");
System.out.print("用户选项:");
System.out.println("│tA~Et┃");
System.out.println("tt╰━━━━━━━━━━━╯");
System.out.println("tA:查看车辆");
System.out.println("tB:租赁汽车");
System.out.println("tC:支付尾款");
System.out.println("tD:查询租赁");
System.out.println("tE:返回主菜单");
do{
//接受用户的单个输入
System.out.print("输入选项(A~E):");
System.out.print("请输入:");
aChar = UnityUtils.readChar();
switch (aChar){
case 'A':
System.out.print("是否查看车辆信息(Yes or No):");
do{
input = UnityUtils.readLine();
if(UnityUtils.judgeSame(input, "Yes")){
listCarsType();
break;
}else if(UnityUtils.judgeSame(input, "No")){
continue;
}else{
System.out.println("输入不合法,重新输入");
}
}while (true);
break;
//B
case 'B':
yourCarChoice = choiceCar();//车辆名字储存
//判断是否租赁
System.out.println("是否租赁");
System.out.print("输入(Yes[租赁] or No[不租赁]):");
do{
input = UnityUtils.readLine();
if(input.equals("Yes")){
Car car = user.setUserCar();//这个index用户对象的 车辆对象
car.setCarType(yourCarChoice);//将车对象名字赋值
//选择租赁的时间
System.out.print("输入租赁时间:");
duration = UnityUtils.readNumber();
user.setRentDuration(duration);
date = UnityUtils.getDate();
System.out.println("租赁起始时间:"+UnityUtils.getStringDate(date)+"t租赁时长:"+duration
+"t截止时间:"+UnityUtils.getStringDate(date, duration));
System.out.println("所选车辆:"+car.getCarType());
car.setCarPrice(UnityUtils.getCarsMoney(carsName, carsMoney, car.getCarType()));//将价格赋予
user.setFirstMoney(service.FirstMoney(car,duration));//用户对象存预款
firstMoney = user.getFirstMoney();
carPrice = car.getCarPrice()*duration;
System.out.println("总款数:"+carPrice);
System.out.println("预付款:"+firstMoney);//输出预款
user.setLastMoney(carPrice - firstMoney);//尾款存入
}else if(input.equals("No")){
break;
}else{
System.out.println("输入有误");
continue;
}
break;
}while (!flag);
break;
//支付尾款
case 'C':
int yourLastMoney;
System.out.println("您要支付的尾款"+service.getLastMoney(user));
if(user.getLastMoney() == 0){
System.out.println("未租赁汽车");
}else{
while(true){
System.out.println("输入尾款");
yourLastMoney = UnityUtils.readNumber();
if(service.clearLastMoney(service.getUser()[index],yourLastMoney )){
System.out.println("尾款支付完毕");
break;
}else if(!service.clearLastMoney(service.getUser()[index],yourLastMoney )){
System.out.println("请一次性输入尾款");
}else{
System.out.println("输入不合法");
}
}
}
break;
case 'D':
//查询用户的信息
service.userInfor(user);
break;
case 'E':
//返回主界面
sysMenu();
break;
default:
System.out.println("输入不合法,输入()");
}
}while(!flag);
}
// 选车界面
public String choiceCar(){
String carName;//车名
System.out.println("tttttt轿车ttttttt客车(金杯、金龙)");
System.out.println("车型tttt别克商务 宝马550i 别克林荫大道ttt<=16座 >16座");
do{
System.out.print("输入车型:");
carName = UnityUtils.readLine();
if(!UnityUtils.carNameReasonable(carsName, carName)){
System.out.println("车型不存在");
continue;
}
//如果没有继续 就是车型符合
break;
}while (!flag);
//需要输出车的价格
System.out.println(carName+"的日租费:"+UnityUtils.getCarsMoney(carsName, carsMoney, carName));
return carName;
}
}
RentCar_App类
package rentcar;
import rentcar.view.RentCarView;
import java.text.ParseException;
public class RentCar_App {
public static void main(String[] args) throws ParseException {
new RentCarView().sysMenu();
}
}
Unityutils类
package rentcar.utils;
import java.text.SimpleDateFormat;//格式化 日期
import java.util.Calendar;//抽象类 日历
import java.util.Date;//日期类
import java.util.GregorianCalendar;
import java.util.Scanner;//输入类
public class UnityUtils {
private static Date date;
private static Scanner sc = new Scanner(System.in);
private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
private static Calendar ca = Calendar.getInstance();
private static String readBoard(int limit,boolean blankBoard){
String input = "";
do{
//直接回车键
input = sc.nextLine();
if(input == "r"){
if(limit == 0){
//允许输入不输入任何东西
if(blankBoard == true){
return input;
}else{
continue;//否则就要接着输入
}
}
}else{
if(input.length() > limit){
System.out.println("输入内容长度不能大于:"+limit);
System.out.print("重新输入:");
continue;
}
return input;
}
}while (sc.hasNextLine());
return input;
}
//单个字符的输入
public static char readChar(){
return readBoard(1, false).charAt(0);
}
public static String TargetString(int number,char target){
StringBuilder finalString = new StringBuilder();
for (int i = 0; i < number; i++) {
finalString.append(target);
}
return new String(finalString);
}
public static String readLine(String regex,int limit){
String finalString = "";
do{
finalString = readBoard(limit, false);
if( finalString.length() != limit ){
System.out.println("输入长度应为:"+limit);
System.out.print("重新输入:");
}else{
if(finalString.matches(regex) == true){
return finalString;
}else{
System.out.println("输入不合法");
System.out.print("按照要求重新输入:");
}
}
}while (true);
}
public static int readNumber(){
return Integer.parseInt(readLine());
}
//readLine 方法的重载 只需要输入字符串 预先给定一个limit 只会提醒输入的字符串长度超过20
public static String readLine(){
String input = "";
input = readBoard(20, false);
return input;
}
//随机产生 limit长度 的验证码 返回字符串
public static String randomGet(int limit){
char [] arr = new char[limit];
for(int i = 0 ; i < limit ;i++) { //控制其验证码个数
arr[i]= 0==(int)(Math.random()*2)? //随机生成大写或小写字母
(char) ('A'+(int)(Math.random()*26)):
(char) ('a'+(int)(Math.random()*26));
}
String str = new String(arr);
return str;
}
public static boolean judgeSame(String str1,String str2,String str3,String str4){
if(str1.equals(str2) && str3.equals(str4)){
return true;
}
return false;
}
//比较两个字符串 是否相同
public static boolean judgeSame(String str1,String str2){
if(str1.equals(str2)){
return true;
}
return false;
}
//传入一个字符串数组 查看字符串 是否在这个字符串数组中
public static boolean carNameReasonable(String[] strings,String str){
for(String index:strings){
if(str.equals(index)){
return true;//存在
}
}
return false;//不存在
}
//获取现在的日期Date类型
public static Date getDate(){
date = new Date();
return date;
}
//将Date类型转换成String类型
public static String getStringDate(Date date) {
return simpleDateFormat.format(date);
}
public static String getStringDate(Date date,int duration) {
ca.setTime(date);
ca.add(Calendar.DATE, duration);
date = ca.getTime();
String enddate = simpleDateFormat.format(date);//将这个日期直接转换成字符串
return enddate;
}
public static int getCarsMoney(String[] strings,int[] ints,String str){
int i;
for( i = 0 ; i < strings.length ; i++){
if(str.equals(strings[i])){
break;
}
}
return ints[i];
}
}
view包
RentCarView类
package rentcar.view;
import com.demo.phonesystem.utils.Unity;
import rentcar.domain.Car;
import rentcar.domain.User;
import rentcar.service.RentCarService;
import rentcar.utils.UnityUtils;
import java.text.ParseException;
import java.util.Date;
public class RentCarView {
private String[] carsName = {"别克商务","宝马550i","别克林荫大道","金杯","金龙"};
private int[] carsMoney = {600,500,300,800,1500};
char aChar; //接受用户的单个字符进行判断
private boolean flag;//默认值false
private RentCarService service = new RentCarService(10);//假设只能有10个用户 创建操作对象 通过这个对象调用数组 来进行添加
public void sysMenu() throws ParseException {
//标题 ╭───────────╮
//欢迎来到:│CarsRentSys┃
// ╰━━━━━━━━━━━╯
System.out.println("tt╭───────────╮");
System.out.print("欢迎来到:");
System.out.println("│CarsRentSys┃");
System.out.println("tt╰━━━━━━━━━━━╯");
do{
//选项说明
System.out.println(UnityUtils.TargetString(65, '='));
System.out.println("选项(A)");
System.out.println("tA.查询车辆");//输入 本系统所能租赁的车辆种类 金额 和个数
System.out.println("tB.注册账号");
System.out.println("tC.登录租赁");//登录账号之后进行操作 真正的操作
System.out.print("请输入:");
aChar = UnityUtils.readChar(); //输入单个字符 存给key
//通过switch 选择结构 进行选择 进入其他的界面
switch (aChar){
case 'A':
listCarsType();
break;
case 'B':
//注册对象的账号和密码
addNewUser();
break;
case 'C'://登录并且进行下一步操作
signIn();
break;
default:
System.out.println("输入不合法,");
break;
}
}while(!flag);
}
public void listCarsType(){
//列表
System.out.println("tttttt轿车ttttttt客车(金杯、金龙)");
System.out.println("车型tttt别克商务 宝马550i 别克林荫大道ttt<=16座 >16座");
System.out.println("日租费/天tt600 500 300tttt800 1500");
System.out.println("剩余tttt"+service.getBk()+"tt"+service.getBm()+"ttt"+service.getBkly()+
"tttt"+service.getJb()+"tt"+service.getJl()
);
System.out.println(UnityUtils.TargetString(65, '='));
}
//创建新用户的界面
public void addNewUser(){
System.out.print("注册账号(共6位,首位必须是大小写字符,末尾不能是0):");
String account,passWord,randomCode,inputCode;
do{
//先创建账号
account = UnityUtils.readLine("[A-z]\d{4}[1-9]",6);
//账号的输入是否合理 还需要查看 账号是否已经存在
if(service.accountReasonable(account)){
break;
}else{
System.out.println("账号已存在");
System.out.print("重新输入:");
}
}while (!flag);
System.out.print("创建密码(共10位,首位大写,末尾以@结尾):");
do{
passWord = UnityUtils.readLine("[A-Z]\d{8}@", 10);
System.out.print("再次输入密码:");
String againPassWord = UnityUtils.readLine();//readLine 方法的重载
//随机生成验证码
randomCode = UnityUtils.randomGet(5);
System.out.println("验证码:"+randomCode);
System.out.print("输入验证码:");
inputCode = UnityUtils.readLine();
//判断两次密码 验证码 是否相同
if(UnityUtils.judgeSame(passWord, againPassWord, randomCode, inputCode)){
break;
}
}while (!flag);
//一切完成 创建数组中的元素对象
User newUser = new User(account,passWord);
//将这个对象添加到数组中 作为数组的元素
if(service.addNewUser(newUser)){
System.out.println("创建成功");
}
}
//登录账号
public void signIn() throws ParseException {
int sum = 0;
String account;
String passWord;
String randomCode;
String temp;
int index = 0;
do{
System.out.print("输入账号:");
account = UnityUtils.readLine();
System.out.print("输入密码:");
passWord = UnityUtils.readLine();
System.out.print("输入验证码("+(temp = UnityUtils.randomGet(4))+"):");
randomCode = UnityUtils.readLine();
if(!UnityUtils.judgeSame(temp, randomCode)){
System.out.println("验证码错误");
continue;//验证码错误 直接进入下一次循环
}
//找到对象的位置
index = service.getIndex(account);
if(index == -1){
//返回系统
System.out.println("返回主菜单");
sysMenu();
}
if(!(UnityUtils.judgeSame((service.getUser())[index].getUsePassWord(), passWord))|| (service.accountReasonable(account))){
if(sum > 3){
System.out.println("输入超三次,重新进入");
}
sum ++;
System.out.println("账号或密码错误(三次机会),剩余"+(3-sum)+"次");
continue;//剩余次数还没用完 直接进行下一次循环
}else{
System.out.println("登陆成功");
break;
}
}while (!flag);
operateMenu(index);
}
//操作页面 这里需要有个有参的 来对对象操作
public void operateMenu(int index) throws ParseException {
int firstMoney,lastMoney;//预款 和 尾款
int duration;//租赁的时长
int carPrice;//租车总共的价钱
Date date;//日期
User user = service.getUser()[index];
String yourCarChoice;//自己选择的车辆类型
String input = "";//空字符串
System.out.println("tt╭───────────╮");
System.out.print("用户选项:");
System.out.println("│tA~Et┃");
System.out.println("tt╰━━━━━━━━━━━╯");
System.out.println("tA:查看车辆");
System.out.println("tB:租赁汽车");
System.out.println("tC:支付尾款");
System.out.println("tD:查询租赁");
System.out.println("tE:返回主菜单");
do{
//接受用户的单个输入
System.out.print("输入选项(A~E):");
System.out.print("请输入:");
aChar = UnityUtils.readChar();
switch (aChar){
case 'A':
System.out.print("是否查看车辆信息(Yes or No):");
do{
input = UnityUtils.readLine();
if(UnityUtils.judgeSame(input, "Yes")){
listCarsType();
break;
}else if(UnityUtils.judgeSame(input, "No")){
continue;
}else{
System.out.println("输入不合法,重新输入");
}
}while (true);
break;
//B
case 'B':
yourCarChoice = choiceCar();//车辆名字储存
//判断是否租赁
System.out.println("是否租赁");
System.out.print("输入(Yes[租赁] or No[不租赁]):");
do{
input = UnityUtils.readLine();
if(input.equals("Yes")){
Car car = user.setUserCar();//这个index用户对象的 车辆对象
car.setCarType(yourCarChoice);//将车对象名字赋值
//选择租赁的时间
System.out.print("输入租赁时间:");
duration = UnityUtils.readNumber();
user.setRentDuration(duration);
date = UnityUtils.getDate();
System.out.println("租赁起始时间:"+UnityUtils.getStringDate(date)+"t租赁时长:"+duration
+"t截止时间:"+UnityUtils.getStringDate(date, duration));
System.out.println("所选车辆:"+car.getCarType());
car.setCarPrice(UnityUtils.getCarsMoney(carsName, carsMoney, car.getCarType()));//将价格赋予
user.setFirstMoney(service.FirstMoney(car,duration));//用户对象存预款
firstMoney = user.getFirstMoney();
carPrice = car.getCarPrice()*duration;
System.out.println("总款数:"+carPrice);
System.out.println("预付款:"+firstMoney);//输出预款
user.setLastMoney(carPrice - firstMoney);//尾款存入
}else if(input.equals("No")){
break;
}else{
System.out.println("输入有误");
continue;
}
break;
}while (!flag);
break;
//支付尾款
case 'C':
int yourLastMoney;
System.out.println("您要支付的尾款"+service.getLastMoney(user));
if(user.getLastMoney() == 0){
System.out.println("未租赁汽车");
}else{
while(true){
System.out.println("输入尾款");
yourLastMoney = UnityUtils.readNumber();
if(service.clearLastMoney(service.getUser()[index],yourLastMoney )){
System.out.println("尾款支付完毕");
break;
}else if(!service.clearLastMoney(service.getUser()[index],yourLastMoney )){
System.out.println("请一次性输入尾款");
}else{
System.out.println("输入不合法");
}
}
}
break;
case 'D':
//查询用户的信息
service.userInfor(user);
break;
case 'E':
//返回主界面
sysMenu();
break;
default:
System.out.println("输入不合法,输入()");
}
}while(!flag);
}
// 选车界面
public String choiceCar(){
String carName;//车名
System.out.println("tttttt轿车ttttttt客车(金杯、金龙)");
System.out.println("车型tttt别克商务 宝马550i 别克林荫大道ttt<=16座 >16座");
do{
System.out.print("输入车型:");
carName = UnityUtils.readLine();
if(!UnityUtils.carNameReasonable(carsName, carName)){
System.out.println("车型不存在");
continue;
}
//如果没有继续 就是车型符合
break;
}while (!flag);
//需要输出车的价格
System.out.println(carName+"的日租费:"+UnityUtils.getCarsMoney(carsName, carsMoney, carName));
return carName;
}
}
RentCar_App类
package rentcar;
import rentcar.view.RentCarView;
import java.text.ParseException;
public class RentCar_App {
public static void main(String[] args) throws ParseException {
new RentCarView().sysMenu();
}
}
RentCarView类
package rentcar.view;
import com.demo.phonesystem.utils.Unity;
import rentcar.domain.Car;
import rentcar.domain.User;
import rentcar.service.RentCarService;
import rentcar.utils.UnityUtils;
import java.text.ParseException;
import java.util.Date;
public class RentCarView {
private String[] carsName = {"别克商务","宝马550i","别克林荫大道","金杯","金龙"};
private int[] carsMoney = {600,500,300,800,1500};
char aChar; //接受用户的单个字符进行判断
private boolean flag;//默认值false
private RentCarService service = new RentCarService(10);//假设只能有10个用户 创建操作对象 通过这个对象调用数组 来进行添加
public void sysMenu() throws ParseException {
//标题 ╭───────────╮
//欢迎来到:│CarsRentSys┃
// ╰━━━━━━━━━━━╯
System.out.println("tt╭───────────╮");
System.out.print("欢迎来到:");
System.out.println("│CarsRentSys┃");
System.out.println("tt╰━━━━━━━━━━━╯");
do{
//选项说明
System.out.println(UnityUtils.TargetString(65, '='));
System.out.println("选项(A)");
System.out.println("tA.查询车辆");//输入 本系统所能租赁的车辆种类 金额 和个数
System.out.println("tB.注册账号");
System.out.println("tC.登录租赁");//登录账号之后进行操作 真正的操作
System.out.print("请输入:");
aChar = UnityUtils.readChar(); //输入单个字符 存给key
//通过switch 选择结构 进行选择 进入其他的界面
switch (aChar){
case 'A':
listCarsType();
break;
case 'B':
//注册对象的账号和密码
addNewUser();
break;
case 'C'://登录并且进行下一步操作
signIn();
break;
default:
System.out.println("输入不合法,");
break;
}
}while(!flag);
}
public void listCarsType(){
//列表
System.out.println("tttttt轿车ttttttt客车(金杯、金龙)");
System.out.println("车型tttt别克商务 宝马550i 别克林荫大道ttt<=16座 >16座");
System.out.println("日租费/天tt600 500 300tttt800 1500");
System.out.println("剩余tttt"+service.getBk()+"tt"+service.getBm()+"ttt"+service.getBkly()+
"tttt"+service.getJb()+"tt"+service.getJl()
);
System.out.println(UnityUtils.TargetString(65, '='));
}
//创建新用户的界面
public void addNewUser(){
System.out.print("注册账号(共6位,首位必须是大小写字符,末尾不能是0):");
String account,passWord,randomCode,inputCode;
do{
//先创建账号
account = UnityUtils.readLine("[A-z]\d{4}[1-9]",6);
//账号的输入是否合理 还需要查看 账号是否已经存在
if(service.accountReasonable(account)){
break;
}else{
System.out.println("账号已存在");
System.out.print("重新输入:");
}
}while (!flag);
System.out.print("创建密码(共10位,首位大写,末尾以@结尾):");
do{
passWord = UnityUtils.readLine("[A-Z]\d{8}@", 10);
System.out.print("再次输入密码:");
String againPassWord = UnityUtils.readLine();//readLine 方法的重载
//随机生成验证码
randomCode = UnityUtils.randomGet(5);
System.out.println("验证码:"+randomCode);
System.out.print("输入验证码:");
inputCode = UnityUtils.readLine();
//判断两次密码 验证码 是否相同
if(UnityUtils.judgeSame(passWord, againPassWord, randomCode, inputCode)){
break;
}
}while (!flag);
//一切完成 创建数组中的元素对象
User newUser = new User(account,passWord);
//将这个对象添加到数组中 作为数组的元素
if(service.addNewUser(newUser)){
System.out.println("创建成功");
}
}
//登录账号
public void signIn() throws ParseException {
int sum = 0;
String account;
String passWord;
String randomCode;
String temp;
int index = 0;
do{
System.out.print("输入账号:");
account = UnityUtils.readLine();
System.out.print("输入密码:");
passWord = UnityUtils.readLine();
System.out.print("输入验证码("+(temp = UnityUtils.randomGet(4))+"):");
randomCode = UnityUtils.readLine();
if(!UnityUtils.judgeSame(temp, randomCode)){
System.out.println("验证码错误");
continue;//验证码错误 直接进入下一次循环
}
//找到对象的位置
index = service.getIndex(account);
if(index == -1){
//返回系统
System.out.println("返回主菜单");
sysMenu();
}
if(!(UnityUtils.judgeSame((service.getUser())[index].getUsePassWord(), passWord))|| (service.accountReasonable(account))){
if(sum > 3){
System.out.println("输入超三次,重新进入");
}
sum ++;
System.out.println("账号或密码错误(三次机会),剩余"+(3-sum)+"次");
continue;//剩余次数还没用完 直接进行下一次循环
}else{
System.out.println("登陆成功");
break;
}
}while (!flag);
operateMenu(index);
}
//操作页面 这里需要有个有参的 来对对象操作
public void operateMenu(int index) throws ParseException {
int firstMoney,lastMoney;//预款 和 尾款
int duration;//租赁的时长
int carPrice;//租车总共的价钱
Date date;//日期
User user = service.getUser()[index];
String yourCarChoice;//自己选择的车辆类型
String input = "";//空字符串
System.out.println("tt╭───────────╮");
System.out.print("用户选项:");
System.out.println("│tA~Et┃");
System.out.println("tt╰━━━━━━━━━━━╯");
System.out.println("tA:查看车辆");
System.out.println("tB:租赁汽车");
System.out.println("tC:支付尾款");
System.out.println("tD:查询租赁");
System.out.println("tE:返回主菜单");
do{
//接受用户的单个输入
System.out.print("输入选项(A~E):");
System.out.print("请输入:");
aChar = UnityUtils.readChar();
switch (aChar){
case 'A':
System.out.print("是否查看车辆信息(Yes or No):");
do{
input = UnityUtils.readLine();
if(UnityUtils.judgeSame(input, "Yes")){
listCarsType();
break;
}else if(UnityUtils.judgeSame(input, "No")){
continue;
}else{
System.out.println("输入不合法,重新输入");
}
}while (true);
break;
//B
case 'B':
yourCarChoice = choiceCar();//车辆名字储存
//判断是否租赁
System.out.println("是否租赁");
System.out.print("输入(Yes[租赁] or No[不租赁]):");
do{
input = UnityUtils.readLine();
if(input.equals("Yes")){
Car car = user.setUserCar();//这个index用户对象的 车辆对象
car.setCarType(yourCarChoice);//将车对象名字赋值
//选择租赁的时间
System.out.print("输入租赁时间:");
duration = UnityUtils.readNumber();
user.setRentDuration(duration);
date = UnityUtils.getDate();
System.out.println("租赁起始时间:"+UnityUtils.getStringDate(date)+"t租赁时长:"+duration
+"t截止时间:"+UnityUtils.getStringDate(date, duration));
System.out.println("所选车辆:"+car.getCarType());
car.setCarPrice(UnityUtils.getCarsMoney(carsName, carsMoney, car.getCarType()));//将价格赋予
user.setFirstMoney(service.FirstMoney(car,duration));//用户对象存预款
firstMoney = user.getFirstMoney();
carPrice = car.getCarPrice()*duration;
System.out.println("总款数:"+carPrice);
System.out.println("预付款:"+firstMoney);//输出预款
user.setLastMoney(carPrice - firstMoney);//尾款存入
}else if(input.equals("No")){
break;
}else{
System.out.println("输入有误");
continue;
}
break;
}while (!flag);
break;
//支付尾款
case 'C':
int yourLastMoney;
System.out.println("您要支付的尾款"+service.getLastMoney(user));
if(user.getLastMoney() == 0){
System.out.println("未租赁汽车");
}else{
while(true){
System.out.println("输入尾款");
yourLastMoney = UnityUtils.readNumber();
if(service.clearLastMoney(service.getUser()[index],yourLastMoney )){
System.out.println("尾款支付完毕");
break;
}else if(!service.clearLastMoney(service.getUser()[index],yourLastMoney )){
System.out.println("请一次性输入尾款");
}else{
System.out.println("输入不合法");
}
}
}
break;
case 'D':
//查询用户的信息
service.userInfor(user);
break;
case 'E':
//返回主界面
sysMenu();
break;
default:
System.out.println("输入不合法,输入()");
}
}while(!flag);
}
// 选车界面
public String choiceCar(){
String carName;//车名
System.out.println("tttttt轿车ttttttt客车(金杯、金龙)");
System.out.println("车型tttt别克商务 宝马550i 别克林荫大道ttt<=16座 >16座");
do{
System.out.print("输入车型:");
carName = UnityUtils.readLine();
if(!UnityUtils.carNameReasonable(carsName, carName)){
System.out.println("车型不存在");
continue;
}
//如果没有继续 就是车型符合
break;
}while (!flag);
//需要输出车的价格
System.out.println(carName+"的日租费:"+UnityUtils.getCarsMoney(carsName, carsMoney, carName));
return carName;
}
}
RentCar_App类
package rentcar;
import rentcar.view.RentCarView;
import java.text.ParseException;
public class RentCar_App {
public static void main(String[] args) throws ParseException {
new RentCarView().sysMenu();
}
}
package rentcar;
import rentcar.view.RentCarView;
import java.text.ParseException;
public class RentCar_App {
public static void main(String[] args) throws ParseException {
new RentCarView().sysMenu();
}
}



