30天搞定Java--day11

每日一考和复习

每日一考

  1. 构造器的作用是什么?使用中有哪些注意点(>=3条)
// 作用
1.创建对象
2.属性初始化
// 注意点
1.Java语言中,每个类都至少有一个构造器
2.默认构造器的修饰符与所属类的修饰符一致
3.一旦显式定义了构造器,则系统不再提供默认构造器
4.一个类可以创建多个重载的构造器
5.父类的构造器不可被子类继承
  1. 关于类的属性的赋值,有几种赋值的方式。谈谈赋值的先后顺序
// 按以下先后顺序
1.默认初始化
2.显示初始化
3.构造器中初始化
4.通过“对象.属性”或“对象.方法”的方式赋值

顺序为1->2->3->4
  1. this关键字可以用来调用哪些结构,简单说明一下其使用。
方法 属性 构造器
方法和属性调用:this.function()  this.value,本类没有会找父类
构造器:this(其它构造器的参数列表),必须声明在首行
  1. Java中目前学习涉及到的四种权限修饰符都有什么?并说明各自的权限范围
修饰符 类内部 同一个包 不同包的子类 同一个工程
private Yes
缺省 Yes Yes
protected Yes Yes Yes
public Yes Yes Yes Yes
  1. 创建Circle类,提供私有的radius属性,提供相应的get和set方法,提供求圆面积的方法。
public class Circle {
	private double radius;
	
	public void setRadius(double radius) {
		this.radius = radius;
	}
	
	public double getRadius() {
		return this.radius;
	}
	
	public double getArea() {
		return Math.PI*(this.radius * this.radius);
	}
}

复习
day10的学习内容

项目二:客户信息管理软件

项目要求

以下为我自己实现的代码,也可以看参考答案

// 题目提供的CMUtility.java
package com.water.project02;

import java.util.*;

/**
 * CMUtility工具类: 将不同的功能封装为方法,就是可以直接通过调用方法使用它的功能,而无需考虑具体的功能实现细节。
 */
public class CMUtility {
	private static Scanner scanner = new Scanner(System.in);

	/**
	 * 用于界面菜单的选择。该方法读取键盘,如果用户键入’1’-’5’中的任意字符,则方法返回。返回值为用户键入字符。
	 */
	public static char readMenuSelection() {
		char c;
		for (;;) {
			String str = readKeyBoard(1, false);
			c = str.charAt(0);
			if (c != '1' && c != '2' && c != '3' && c != '4' && c != '5') {
				System.out.print("选择错误,请重新输入:");
			} else
				break;
		}
		return c;
	}

	/**
	 * 从键盘读取一个字符,并将其作为方法的返回值。
	 */
	public static char readChar() {
		String str = readKeyBoard(1, false);
		return str.charAt(0);
	}

	/**
	 * 从键盘读取一个字符,并将其作为方法的返回值。 如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
	 */
	public static char readChar(char defaultValue) {
		String str = readKeyBoard(1, true);
		return (str.length() == 0) ? defaultValue : str.charAt(0);
	}

	/**
	 * 从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。
	 */
	public static int readInt() {
		int n;
		for (;;) {
			String str = readKeyBoard(2, false);
			try {
				n = Integer.parseInt(str);
				break;
			} catch (NumberFormatException e) {
				System.out.print("数字输入错误,请重新输入:");
			}
		}
		return n;
	}

	/**
	 * 从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。 如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
	 */
	public static int readInt(int defaultValue) {
		int n;
		for (;;) {
			String str = readKeyBoard(2, true);
			if (str.equals("")) {
				return defaultValue;
			}

			try {
				n = Integer.parseInt(str);
				break;
			} catch (NumberFormatException e) {
				System.out.print("数字输入错误,请重新输入:");
			}
		}
		return n;
	}

	/**
	 * 从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值。
	 */
	public static String readString(int limit) {
		return readKeyBoard(limit, false);
	}

	/**
	 * 从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值。 如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
	 */
	public static String readString(int limit, String defaultValue) {
		String str = readKeyBoard(limit, true);
		return str.equals("") ? defaultValue : str;
	}

	/**
	 * 用于确认选择的输入。该方法从键盘读取‘Y’或’N’,并将其作为方法的返回值。
	 */
	public static char readConfirmSelection() {
		char c;
		for (;;) {
			String str = readKeyBoard(1, false).toUpperCase();
			c = str.charAt(0);
			if (c == 'Y' || c == 'N') {
				break;
			} else {
				System.out.print("选择错误,请重新输入:");
			}
		}
		return c;
	}

	private static String readKeyBoard(int limit, boolean blankReturn) {
		String line = "";

		while (scanner.hasNextLine()) {
			line = scanner.nextLine();
			if (line.length() == 0) {
				if (blankReturn)
					return line;
				else
					continue;
			}

			if (line.length() < 1 || line.length() > limit) {
				System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:");
				continue;
			}
			break;
		}
		return line;
	}
}
// Customer.java
package com.water.project02;

public class Customer {
	private String name;
	private char gender;
	private int age;
	private String phone;
	private String email;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public char getGender() {
		return gender;
	}
	public void setGender(char gender) {
		this.gender = gender;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	
	public Customer() {
		
	}	
}
// CustomerList.java
package com.water.project02;

public class CustomerList {
	
	private Customer[] customers;
	private int total;            // 现有客户数量
	
	public CustomerList(int totalCustomer) {
		this.customers = new Customer[totalCustomer];
	}
	
	public boolean addCustomer(Customer customer) {
		if (total >= this.customers.length) {
			return false;
		}
		this.customers[this.total] = customer;
		this.total += 1;
		return true;
	}
	
	public boolean replaceCustomer(int index,Customer customer) {
		if (index >= total || index < 0){  
			return false;
		}
		this.customers[index] = customer;
		return true;
	}
	
	public boolean daleteCustomer(int index) {
		if (index >= total || index < 0){
			return false;
		}
		for(;index < this.total;index++) {
			this.customers[index] = this.customers[index + 1];
		}
		this.total -= 1;
		return true;
	}
	
	public Customer[] getAllCustomers() {
		Customer[] customersTotal = new Customer[this.total];
		for(int i = 0;i<this.total;i++) {
			customersTotal[i] = this.customers[i];
		}
		return customersTotal;
	}
	
	public Customer getCustomer(int index) {
		if (index >= total || index < 0){
			return null;
		}
		return customers[index];
	}
	
	public int getTotal() {
		return this.total;
	}
}
// CustomerView.java
package com.water.project02;

public class CustomerView {
	public static void main(String[] args) {
		CustomerView custView = new CustomerView();

		custView.enterMainMenu();
	}

	private CustomerList customerList = new CustomerList(10);
	private CMUtility cmu = new CMUtility();

	public void enterMainMenu() {
		while (true) {
			printMenu();

			char operation = cmu.readMenuSelection();
			System.out.println();

			switch (operation) {
			case '1':
				this.addNewCustomer();
				break;
			case '2':
				this.modifyCustomer();
				break;
			case '3':
				this.deleteCustomer();
				break;
			case '4':
				this.listAllCustomers();
				break;
			case '5':
				return;
			}
		}
	}

	private void printMenu() {
		System.out.println("-----------------客户信息管理软件-----------------");
		System.out.println();
		System.out.println("                  1 添 加 客 户");
		System.out.println("                  2 修 改 客 户");
		System.out.println("                  3 删 除 客 户");
		System.out.println("                  4 客 户 列 表");
		System.out.println("                  5 退           出");
		System.out.println();
		System.out.print("\t                  请选择(1-5):");
	}

	private void addNewCustomer() {

		System.out.println("---------------------添加客户---------------------");

		Customer customer = new Customer();
		System.out.print("姓名:");
		customer.setName(cmu.readString(10));

		System.out.print("性别:");
		customer.setGender(cmu.readChar());

		System.out.print("年龄:");
		customer.setAge(cmu.readInt());

		System.out.print("电话:");
		customer.setPhone(cmu.readString(20));

		System.out.print("邮箱:");
		customer.setEmail(cmu.readString(30));

		System.out.println();

		if (customerList.addCustomer(customer)) {
			System.out.println("---------------------添加完成---------------------\n");
		} else {
			System.out.println("---------------------添加失败---------------------\n");
		}

	}

	private void modifyCustomer() {
		System.out.println("---------------------修改客户---------------------");
		System.out.print("请选择待修改客户编号(-1退出):");
		int index = cmu.readInt();
		if (index == -1) {
			return;
		}

		index -= 1; // 编号从1开始

		Customer customer = new Customer();
		Customer customerOld = customerList.getCustomer(index);

		System.out.print("姓名(" + customerOld.getName() + "):");
		customer.setName(cmu.readString(10, customerOld.getName()));

		System.out.print("性别(" + customerOld.getGender() + "):");
		customer.setGender(cmu.readChar(customerOld.getGender()));

		System.out.print("年龄(" + customerOld.getAge() + "):");
		customer.setAge(cmu.readInt(customerOld.getAge()));

		System.out.print("电话(" + customerOld.getPhone() + "):");
		customer.setPhone(cmu.readString(20, customerOld.getPhone()));

		System.out.print("邮箱(" + customerOld.getEmail() + "):");
		customer.setEmail(cmu.readString(30, customerOld.getEmail()));

		if (customerList.replaceCustomer(index, customer)) {
			System.out.println("---------------------修改完成---------------------\n");
		} else {
			System.out.println("---------------------修改失败---------------------\n");
		}
	}

	private void deleteCustomer() {
		System.out.println("---------------------删除客户---------------------");
		System.out.print("请选择待删除客户编号(-1退出):");
		int index = cmu.readInt();

		System.out.print("确认是否删除(Y/N):");
		char flag = cmu.readConfirmSelection();

		if (flag == 'N') {
			return;
		}

		index -= 1;

		if (customerList.daleteCustomer(index)) {
			System.out.println("---------------------删除成功---------------------\n");
		} else {
			System.out.println("---------------------删除失败---------------------\n");
		}
	}

	private void listAllCustomers() {
		System.out.println("---------------------客户列表---------------------\n");
		System.out.println("编号\t姓名\t性别\t年龄\t电话\t邮箱");

		int total = customerList.getTotal();
		Customer[] customers = new Customer[total];
		customers = customerList.getAllCustomers();

		for (int i = 1; i <= total; i++) {
			System.out.println(i + "\t" + customers[i - 1].getName() + "\t" + customers[i - 1].getGender() + "\t"
					+ customers[i - 1].getAge() + "\t" + customers[i - 1].getPhone() + "\t"
					+ customers[i - 1].getEmail() + "\n");
		}

		System.out.println("--------------------客户列表完成--------------------\n");
	}
}

测试结果

在命令行对的挺齐的,到这就有点错位

-----------------客户信息管理软件-----------------

                  1 添 加 客 户
                  2 修 改 客 户
                  3 删 除 客 户
                  4 客 户 列 表
                  5 退       出

	                  请选择(1-5):1

---------------------添加客户---------------------
姓名:qwer
性别:男
年龄:33
电话:12345
邮箱:qwer@

---------------------添加完成---------------------

-----------------客户信息管理软件-----------------

                  1 添 加 客 户
                  2 修 改 客 户
                  3 删 除 客 户
                  4 客 户 列 表
                  5 退       出

	                  请选择(1-5):1

---------------------添加客户---------------------
姓名:abcd
性别:女
年龄:55
电话:54321
邮箱:asd@rrr

---------------------添加完成---------------------

-----------------客户信息管理软件-----------------

                  1 添 加 客 户
                  2 修 改 客 户
                  3 删 除 客 户
                  4 客 户 列 表
                  5 退       出

	                  请选择(1-5):1

---------------------添加客户---------------------
姓名:tre
性别:男
年龄:66
电话:43532
邮箱:fds@gfd

---------------------添加完成---------------------

-----------------客户信息管理软件-----------------

                  1 添 加 客 户
                  2 修 改 客 户
                  3 删 除 客 户
                  4 客 户 列 表
                  5 退       出

	                  请选择(1-5):4

---------------------客户列表---------------------

编号	姓名	性别	年龄	电话	邮箱
1	qwer	男	33	12345	qwer@

2	abcd	女	55	54321	asd@rrr

3	tre	男	66	43532	fds@gfd

--------------------客户列表完成--------------------

-----------------客户信息管理软件-----------------

                  1 添 加 客 户
                  2 修 改 客 户
                  3 删 除 客 户
                  4 客 户 列 表
                  5 退       出

	                  请选择(1-5):2

---------------------修改客户---------------------
请选择待修改客户编号(-1退出):1
姓名(qwer):
性别():
年龄(33):22
电话(12345):33333
邮箱(qwer@):
---------------------修改完成---------------------

-----------------客户信息管理软件-----------------

                  1 添 加 客 户
                  2 修 改 客 户
                  3 删 除 客 户
                  4 客 户 列 表
                  5 退       出

	                  请选择(1-5):3

---------------------删除客户---------------------
请选择待删除客户编号(-1退出):2
确认是否删除(Y/N):n
-----------------客户信息管理软件-----------------

                  1 添 加 客 户
                  2 修 改 客 户
                  3 删 除 客 户
                  4 客 户 列 表
                  5 退       出

	                  请选择(1-5):1

---------------------添加客户---------------------
姓名:iouy
性别:男
年龄:3
电话:33367
邮箱:feds@sd

---------------------添加完成---------------------

-----------------客户信息管理软件-----------------

                  1 添 加 客 户
                  2 修 改 客 户
                  3 删 除 客 户
                  4 客 户 列 表
                  5 退       出

	                  请选择(1-5):4

---------------------客户列表---------------------

编号	姓名	性别	年龄	电话	邮箱
1	qwer	男	22	33333	qwer@

2	abcd	女	55	54321	asd@rrr

3	tre	男	66	43532	fds@gfd

4	iouy	男	3	33367	feds@sd

--------------------客户列表完成--------------------

-----------------客户信息管理软件-----------------

                  1 添 加 客 户
                  2 修 改 客 户
                  3 删 除 客 户
                  4 客 户 列 表
                  5 退       出

	                  请选择(1-5):3

---------------------删除客户---------------------
请选择待删除客户编号(-1退出):2
确认是否删除(Y/N):y
---------------------删除成功---------------------

-----------------客户信息管理软件-----------------

                  1 添 加 客 户
                  2 修 改 客 户
                  3 删 除 客 户
                  4 客 户 列 表
                  5 退       出

	                  请选择(1-5):4

---------------------客户列表---------------------

编号	姓名	性别	年龄	电话	邮箱
1	qwer	男	22	33333	qwer@

2	tre	男	66	43532	fds@gfd

3	iouy	男	3	33367	feds@sd

--------------------客户列表完成--------------------

-----------------客户信息管理软件-----------------

                  1 添 加 客 户
                  2 修 改 客 户
                  3 删 除 客 户
                  4 客 户 列 表
                  5 退       出

	                  请选择(1-5):5
发布了18 篇原创文章 · 获赞 28 · 访问量 4392

猜你喜欢

转载自blog.csdn.net/weixin_42224119/article/details/105203778